$(document).ready(function(){
if($("#hidGameName")[0].value.toLowerCase().indexOf("eu") == 0)
{
	game.ajax.common.SetCurrency("EUR");
}
else
{
	game.ajax.common.SetCurrency("USD");
}						   
if($("#drpServer")[0])
{
	$("#gameServer").html($("#drpServer")[0].value);
	ChangeServer($("#drpServer")[0].value);
}
CheckLogin();
});
function ChangeServer(server)
{
	$("#itemList").html("<div style=\"width:100%;text-align:center; color:#000; border-bottom:#519ED2 solid 1px; padding:15px 0 15px 0;\">Loading....<img src=\"../image/loading.gif\" /></div>");
	$("#gameServer").html(server);
	game.buy.common.CreateItemList($("#hidGameName").attr("value"), server, "0", MakeItemList);
	function MakeItemList(result)
	{
		var itemTemplate = "<div style=\"width:634px; border-bottom:#519ED2 solid 1px; height:39px; position:relative; color: #1862A3; font-weight:bold;\" ><div style=\"position:absolute; top:13px; left:65px; width:208px; line-height:12px;\" align=\"left\">$Unit$$InStock$</div><div style=\"position:absolute; top:13px; left:255px; width:50px; line-height:12px;\" align=\"left\">$Price$</div><div style=\"position:absolute; top:13px; left:370px; width:70px; line-height:12px;\" align=\"left\">$Quantity$</div><div style=\"position:absolute; top:8px; left:502px; width:115px; line-height:12px;\" align=\"left\"><input type=\"hidden\" id=\"$SaveInfoID$\" value=\"$SaveInfo$\" /><input type=\"hidden\" id=\"$SaveDefaultID$\" value=\"$SaveDefault$\" /><a style=\"cursor:pointer;\" onclick=\"SubmitConfirm($Index$);\"><img src=\"../image/BUY.gif\" /></a></div></div>";
		var resultItemList = "";
		if(result == null || result.value == null)
		{
			$("#itemList").html("<div style=\"width:100%;text-align:center;\">No Date!</div>");
		}
		var itemArray = result.value;
		for(var i=0; i<itemArray.Rows.length; i++)
		{
			var tempItemTemplate = itemTemplate;
			tempItemTemplate = tempItemTemplate.replace("$Unit$", itemArray.Rows[i].Unit);
			tempItemTemplate = tempItemTemplate.replace("$InStock$", itemArray.Rows[i].InStock);
			tempItemTemplate = tempItemTemplate.replace("$Price$", itemArray.Rows[i].Price);
			tempItemTemplate = tempItemTemplate.replace("$Index$", i);
			tempItemTemplate = tempItemTemplate.replace("$SaveInfoID$", "hidSaveInfo" + i);
			tempItemTemplate = tempItemTemplate.replace("$SaveInfo$", itemArray.Rows[i].Unit1+"|"+itemArray.Rows[i].Unit2+"|"+itemArray.Rows[i].Price1+"|"+itemArray.Rows[i].Price2+"|0");
			tempItemTemplate = tempItemTemplate.replace("$SaveDefaultID$", "hidDefault" + i);
			tempItemTemplate = tempItemTemplate.replace("$SaveDefault$", itemArray.Rows[i].Unit1+"|"+itemArray.Rows[i].Unit2+"|"+itemArray.Rows[i].Price1+"|"+itemArray.Rows[i].Price2+"|"+itemArray.Rows[i].MoneyLogo+"|"+itemArray.Rows[i].UnitName+"|0");
			tempItemTemplate = tempItemTemplate.replace("$Quantity$", "<table align=\"center\" cellSpacing=\"0\" cellPadding=\"0\" style=\"width:40px;height:15px;line-height:15px;\" background=\"../image/quantity.gif\"><tr><td style=\"cursor:pointer;width:16px\" onclick=\"ChangeQuantity(0,"+i+")\"></td><td id=\"tdQuantity"+i+"\" style=\"color:#FB9915\">1</td><td  style=\"cursor:pointer;width:16px\" onclick=\"ChangeQuantity(1,"+i+")\"></td></tr></table>");		
			resultItemList += tempItemTemplate;
		}
		$("#itemList").html(resultItemList);
		switch(itemArray.Rows[0].MoneyLogo)
		{
			case "$":
				$("#hidMoneyCode").attr("value","USD");break;
			case "&euro;":
				$("#hidMoneyCode").attr("value","EUR");break;
			case "&pound;":
				$("#hidMoneyCode").attr("value","GBP");break;
		}
	}
}

function ChangeQuantity(para,i)
{
	var tdQuantity=$("#tdQuantity"+i)[0];
	var Quantity=Number(tdQuantity.innerHTML);
	if(para==0)
	{
		if(Quantity>1)
			Quantity--;
	}
	else
	{
		if(Quantity<9)
			Quantity++;
	}
	tdQuantity.innerHTML=Quantity
	var hidDefault=$("#hidDefault"+i)[0];
	var hidSaveInfo=$("#hidSaveInfo"+i)[0];								
	var tdUnit=$("#tdUnit"+i)[0];
	var tdPrice=$("#tdPrice"+i)[0];
	var Info =hidDefault.value.split("|");
	var Unit1=Info[0];
	var Unit2=Info[1];
	var Price1=Info[2];
	var Price2=Info[3];
	var MoneyLogo=Info[4];
	var UnitName=Info[5];
	Unit1=Unit1*Quantity;
	Unit2=Unit2*Quantity;
	Price1=Price1*Quantity;
	Price2=Price2*Quantity;
	Price1=Price1.toFixed(2);
	Price2=Price2.toFixed(2);
	hidSaveInfo.value=Unit1+"|"+Unit2+"|"+Price1+"|"+Price2+"|"+0;
}

function SubmitConfirm(i)
{
	var tdQuantity = $("#tdQuantity" + i)[0];
	var Quantity=tdQuantity.innerHTML;
	var MoneyCode=$("#hidMoneyCode")[0].value
	var hidSaveInfo=$("#hidSaveInfo"+i)[0];
	var unit1,unit2,price1,price2,DeliveryTime;
	var Info =hidSaveInfo.value.split("|");
	unit1=Info[0];
	unit2=Info[1];
	price1=Info[2];
	price2=Info[3];
	var result =game.buy.common.SaveForm($("#hidGameName").attr("value"), $("#drpServer")[0].value, unit1, unit2, price1, price2, MoneyCode, Quantity).value;
	if(result=="0")
	{
		alert("Url overtime,Please refresh webpage.");
		window.location.reload();
	}
	else
	{
		Main("CheckOut");
		window.location="buy-gold-step1.html";
	}
}
