function ChangeServer(serverID)
{
window.location="?game="+$("#hidGameName").attr("value")+"&server="+serverID+"&class="+$("#hidClass").attr("value");

}
Array.prototype.indexOf=function(el,start){
var start=start||0;
for(var i=0;
i<this.length;
++i){
if(this[i]===el){
return i;

}
}
return-1;

};
function addToCart(itemId,quantity,promotion){
AH4PAjax.Items.AjaxCommon.SaveItemToShoppingCart(itemId,quantity,promotion,"cart",CallBack);
function CallBack(response)
{
if(response!=null&&response.value!=null&&response.value==true)
{
LoadShoppingCart("cart");

}
else
{
alert("Error!");

}
}
}
function removeItem(itemId){
if(confirm("Are you sure remove this item!"))
{
var result=AH4PAjax.Items.AjaxCommon.RemoveItemFromShoppingCart(itemId,"cart",CallBack);

}
function CallBack(response)
{
if(response!=null&&response.value!=null&&response.value==true)
{
LoadShoppingCart("cart");

}
else
{
alert("Error!");

}
}
}
function LoadShoppingCart(cartName)
{
if(document.getElementById("myCartContent"))
{
document.getElementById("myCartContent").innerHTML="<p class=\"shopStaNo\" style=\"padding:5px;\"><img src=\"/images/icon/loading.gif\" /></p>";

}
else if(document.getElementById("myCartContentOrder"))
{

	document.getElementById("myCartContentOrder").innerHTML="<p class=\"shopStaNo\" style=\"padding:5px;\"><img src=\"/images/icon/loading.gif\" /></p>";


}
var cartHtml="";
var itemHtmlTemplate="<tr style=\"background-color:$ItemColor$\"><td class=\"sc0\"><span class=\"icon-frame\"><a href=\"$ItemWowHead$\" target=\"_blank\"><img width=\"31\" height=\"28\" src=\"http://www.ah4p-items.com$ItemImage$\" /></a></span></td><td class=\"sc1\">$ItemName$</td>$ItemQuantity$</td><td class=\"sc2\">$ItemLevel$</td>$ItemQuantity$</td><td class=\"sc3\"><$ItemTime$Hours</td><td class=\"sc4\"><font size=\"2\">$MoneyHtmlCode$ $SumPrice$</td><td class=\"quantity\"><a onclick=\"DeleteUnit($ItemIndex$);saveCart($ItemIndex$,$ItemID$, $Promotion$);\" style=\"cursor: pointer;\"/><strong> - </strong></a><input type=\"text\" value=\"$Quantity$\" style=\"width:50px; border:0;\" onblur=\"this.value=this.value=='' ? 1 : this.value;saveCart($ItemIndex$,$ItemID$,$Promotion$);\" class=\"inp\" name=\"itemQuantity\" id=\"itemQuantity_$ItemIndex$\" onkeyup=\"this.value=this.value.replace(/[^0-9]/,'')\" /><a onclick=\"addUnit($ItemIndex$);saveCart($ItemIndex$,$ItemID$,$Promotion$);\" style=\"cursor: pointer;\"><strong> + </strong></a></td><td class=\"remove\"><a onclick=\"removeItem('$ItemID$');\" style=\"cursor: pointer;\" title=\"Remove\" class=\"btn\">Remove</a><input type=\"hidden\" value=\"\" id=\"singlePrice_$ItemIndex$\" name=\"singlePrice\"/><input type=\"hidden\" value=\"1\" id=\"singleQuantity_$ItemIndex$\" name=\"singleQuantity\"/></td></tr>";
var itemHtml="";
var sumPrice=0;
var sumQuantity=0;
var moneyLogo="";
var moneyCode = "";
 var usdPrice = 0;
var itemIndex=1;
AH4PAjax.Items.AjaxCommon.LoadShoppingCart(cartName,CallBack);

function CallBack(response)
{

	if(response!=null&&response.value!=null)
{

	var tbCart=response.value;
	for(var i=0;i<tbCart.Rows.length;i++)
	{
	
		var tempItemHtml=itemHtmlTemplate;
		tempItemHtml=tempItemHtml.replace(/\$CartName\$/g,cartName);
		tempItemHtml=tempItemHtml.replace(/\$ItemID\$/g,tbCart.Rows[i]["ItemID"]);
		tempItemHtml=tempItemHtml.replace(/\$ItemWowHead\$/g,tbCart.Rows[i]["OrtherID"]!="0"?"http://www.wowhead.com/?"+(tbCart.Rows[i]["Back_4"]=="3"?"skill":tbCart.Rows[i]["Back_4"]=="2"?"spell":"item")+"="+tbCart.Rows[i]["OrtherID"]:"");
		tempItemHtml=tempItemHtml.replace(/\$ItemColor\$/g,i%2==1?"#2e2e35":"");
		tempItemHtml=tempItemHtml.replace(/\$ItemName\$/g,tbCart.Rows[i]["NewItemName"]);
		tempItemHtml=tempItemHtml.replace(/\$ItemImage\$/g,tbCart.Rows[i]["ItemImage"]);
		tempItemHtml=tempItemHtml.replace(/\$ItemQuantity\$/g,tbCart.Rows[i]["ItemQuantity"]!=0?"× "+tbCart.Rows[i]["ItemQuantity"]:"");
		tempItemHtml=tempItemHtml.replace(/\$ItemTime\$/g,tbCart.Rows[i]["ItemDelivery"]);
		tempItemHtml=tempItemHtml.replace(/\$ItemPrice\$/g,tbCart.Rows[i]["ItemPrice"]);
		tempItemHtml=tempItemHtml.replace(/\$ItemLevel\$/g,tbCart.Rows[i]["ItemLevel"]);
		tempItemHtml=tempItemHtml.replace(/\$SumPrice\$/g,tbCart.Rows[i]["SumPrice"]);
		tempItemHtml=tempItemHtml.replace(/\$ItemLevel\$/g,tbCart.Rows[i]["MinLevel"]=="0"?"1":tbCart.Rows[i]["MinLevel"]);
		tempItemHtml=tempItemHtml.replace(/\$ItemPic\$/g,tbCart.Rows[i]["ItemImage"]);
		tempItemHtml=tempItemHtml.replace(/\$Quantity\$/g,tbCart.Rows[i]["Quantity"]);
		tempItemHtml=tempItemHtml.replace(/\$MoneyHtmlCode\$/g,tbCart.Rows[i]["MoneyLogo"]);
		tempItemHtml=tempItemHtml.replace(/\$ItemIndex\$/g,itemIndex);
		tempItemHtml=tempItemHtml.replace(/\$Promotion\$/g,tbCart.Rows[i]["Promotion"]);
		itemHtml+=tempItemHtml;
		sumQuantity+=parseInt(tbCart.Rows[i]["Quantity"]);
		sumPrice+=parseFloat(tbCart.Rows[i]["SumPrice"]);
		usdPrice += parseFloat(tbCart.Rows[i]["USDPrice"]);
		moneyLogo=tbCart.Rows[i]["MoneyLogo"];
		moneyCode=tbCart.Rows[i]["MoneyCode"];
		document.getElementById("hidGame").value=tbCart.Rows[i]["GameName"];
		$("#lbGame").html(tbCart.Rows[i]["GameName"]);
		itemIndex++;
	}
	
}
if(itemHtml!="")
{

	var promotionPrice=sumPrice;
	var vipPrice=AH4PAjax.Items.AjaxCommon.GetVipPrice(sumQuantity,sumPrice).value.toFixed(2);
	if(cartName=="PromotionCart")
	{
		promotionPrice=AH4PAjax.Items.AjaxCommon.GetPromotionPrice(sumQuantity,sumPrice).value.toFixed(2);
	}
sumPrice=parseFloat(parseFloat(sumPrice).toFixed(2));
usdPrice=parseFloat(parseFloat(usdPrice).toFixed(2));
promotionPrice=parseFloat(promotionPrice)+parseFloat(vipPrice)-parseFloat(sumPrice);
promotionPrice=parseFloat(promotionPrice).toFixed(2);
var foot="";
if(document.getElementById("myCartContent"))
{
	foot="<tfoot><tr"+(itemIndex%2==0?" style=\"background-color:#2e2e35;\"":"")+"><td colspan=\"7\"><span class=\"total fl\" style=\"font-size:16px;font-weight:bold;\">Total: <span id=\"cartTotalPrice\">"+ (sumPrice != usdPrice ? "$" + usdPrice + "=" : "") + tbCart.Rows[0]["MoneyLogo"]+(promotionPrice!=sumPrice&&promotionPrice!=0 ? "<del>"+sumPrice.toFixed(2)+"</del> &nbsp;"+promotionPrice:sumPrice.toFixed(2))+"</span><span style=\"font-size:12px;font-weight:normal;\">&nbsp;&nbsp;Discount Code <input type=\"text\" id=\"txtCode\" name=\"txtCode\" value=\"\" style=\"border:0;width:100px;\" onblur=\"GetCodeDiscount();\" />&nbsp;<a onclick=\"GetCodeDiscount();\"><img src=\"../images/icon/dscode.gif\" /></a></span></span><a style=\"cursor:pointer;\" onclick=\"window.location='checkitem.html?code=' + document.getElementById('txtCode').value;\" class=\"btn\">Buy Now</a><label style=\"margin-top:4px;color:#ffffff;float:left;margin-left:15px;\">Remark：The promotion goods dont share any discount.</label><input id=\"promotionPrice\" type=\"hidden\" value=\""+(promotionPrice!=0?promotionPrice:sumPrice)+"\" /><input id=\"hidMoneyLogo\" type=\"hidden\" value=\""+moneyLogo+"\" /><input id=\"hidMoneyCode\" type=\"hidden\" value=\""+moneyCode+"\" /></td></tr></tfoot>";
}
else
{
foot="<tfoot><tr"+(itemIndex%2==0?" style=\"background-color:#2e2e35;\"":"")+"><td colspan=\"7\"><span class=\"total fl\" style=\"font-size:16px;font-weight:bold;\">Total: <span id=\"cartTotalPrice\">"+ (sumPrice != usdPrice ? "$" + usdPrice + "=" : "")+tbCart.Rows[0]["MoneyLogo"]+(promotionPrice!=sumPrice&&promotionPrice!=0?"<del>"+sumPrice.toFixed(2)+"</del> &nbsp;"+promotionPrice:sumPrice.toFixed(2))+"</span><span style=\"font-size:12px;font-weight:normal;\">&nbsp;&nbsp;Discount Code <input type=\"text\" id=\"txtCode\" name=\"txtCode\" value=\"\" style=\"border:0;width:100px;\" onblur=\"GetCodeDiscount();\" />&nbsp;<a onclick=\"GetCodeDiscount();\"><img src=\"../images/icon/dscode.gif\" /></a><label style=\"margin-top:16px;color:#ffffff;margin-left:15px;\">Remark：The promotion goods dont share any discount.</label></span></span><input id=\"promotionPrice\" type=\"hidden\" value=\""+(promotionPrice!=0?promotionPrice:sumPrice)+"\" /><input id=\"hidMoneyLogo\" type=\"hidden\" value=\""+moneyLogo+"\" /><input id=\"hidMoneyCode\" type=\"hidden\" value=\""+moneyCode+"\" /></td></tr></tfoot>";

}
cartHtml="<table id=\"shoppinCartList\"><thead><tr><th class=\"name\" colspan=\"2\">Item Name</th><th>Item Level</th><th class=\"delivery\">Delivery</th><th class=\"price\">Price</th><th class=\"quantity\" style=\"width:120px;\">Quantily</th><th class=\"remove\"></th></tr></thead><tbody>"+itemHtml+"</tbody>"+foot+"</table>";
if(document.getElementById("myCartContent"))
{
$("#myCartContent").css("display","none");
document.getElementById("myCartContent").innerHTML=cartHtml;
$("#myCartContent").show(1000);

}
else if(document.getElementById("myCartContentOrder"))
{
$("#myCartContentOrder").css("display","none");
document.getElementById("myCartContentOrder").innerHTML=cartHtml;
$("#myCartContentOrder").show(1000);

}
if(code!="")
{
$("#txtCode")[0].value=code;
GetCodeDiscount();

}
LoadULServer("All");

}
else
{
if(document.getElementById("myCartContent"))
{
$("#myCartContent").html("<p class=\"shopStaNo\"><img src=\"/images/icon/shoppingcartno.gif\" width=\"405\" height=\"33\" title=\"THERE IS NOTHING IN YOUR SHOPPING TROLLEY ATM\" alt=\"THERE IS NOTHING IN YOUR SHOPPING TROLLEY ATM\" /></p>");

}
else if(document.getElementById("myCartContentOrder"))
{
$("#myCartContentOrder").html("<p class=\"shopStaNo\"><img src=\"/images/icon/shoppingcartno.gif\" width=\"405\" height=\"33\" title=\"THERE IS NOTHING IN YOUR SHOPPING TROLLEY ATM\" alt=\"THERE IS NOTHING IN YOUR SHOPPING TROLLEY ATM\" /></p>");
alert("Your shopping cart is empty!");
history.go(-1);

}
}
}
}
var code="";
function addUnit(id)
{
var quantity=document.getElementById('itemQuantity_'+id);
var targetValue=Number(quantity.value)+1;
quantity.value=targetValue;

}
function DeleteUnit(id){
var quantity=document.getElementById('itemQuantity_'+id);
var targetValue=Number(quantity.value);
if(targetValue>1){
quantity.value=targetValue-1;

}
}
function saveCart(id,itemId,promotion)
{
var quantity=document.getElementById('itemQuantity_'+id).value;
if(quantity==''||quantity==0)
{
quantity=1;
document.getElementById('itemQuantity_'+id).value=1;

}
var result=AH4PAjax.Items.AjaxCommon.SaveItemToShoppingCart(itemId,quantity,promotion,"cart",CallBack);
function CallBack(response)
{
if(response==null||response.value==null)
{
alert("Error!");

}
else
{
LoadShoppingCart("cart");

}
}
}
function GetCodeDiscount(){
var key=document.getElementById("txtCode").value;
if(key!='')
{
code=key;
AH4PAjax.Items.AjaxCommon.GetDiscountInfo("",key,"cart",CallBack);

}
function CallBack(response)
{
if(response!=null&&response.value!=null&&response.value!=0)
{
var discountValue=response.value;
var price=Number(document.getElementById('promotionPrice').value);
var moneyLogo=document.getElementById("hidMoneyLogo").value;
var moneyCode=document.getElementById("hidMoneyCode").value;
if(moneyCode != "USD")
{
	var usdPrice = AH4PAjax.Items.AjaxCommon.GetUSDPrice(price-discountValue, moneyCode).value;
	document.getElementById('cartTotalPrice').innerHTML="<del>"+moneyLogo+price+"</del>&nbsp;($"+ usdPrice.toFixed(2) + "=" + moneyLogo+(price-discountValue).toFixed(2) + ")";
}
else
{
	document.getElementById('cartTotalPrice').innerHTML="<del>"+moneyLogo+price+"</del>&nbsp;"+moneyLogo+(price-discountValue).toFixed(2);
}
}
else
{
code="";
document.getElementById('txtCode').value="";
alert('Discount key not exits.');

}
}
}
function LoadULServer(charZ)
{
if(!document.getElementById("drpServer"))
return;
$("#drpServer")[0].options.length=1;
var _arrayServer=arrayServer[document.getElementById("hidGame").value];
var tempArrayServer=new Array();
for(var i=0;
i<_arrayServer.length;
i++)
{
if(_arrayServer[i].indexOf(charZ)==0||charZ=='All')
{
$("#drpServer")[0].options.add(new Option(_arrayServer[i],_arrayServer[i]))
}
}
}
code=getQueryString("code");
LoadShoppingCart("cart");

