﻿var xmlHttp;
var Faction="Alliance";
window.onload = function(){
xmlHttp=createXMLHttpRequest();
onFactionChange();
}
function createXMLHttpRequest(){
	if(window.XMLHttpRequest) return new XMLHttpRequest();
	try{return new ActiveXObject('MSXML2.XMLHTTP.4.0');}
	catch(e){try{return new ActiveXObject('MSXML2.XMLHTTP.3.0');}
	catch(e){try{return new ActiveXObject('MSXML2.XMLHTTP.2.6');}
	catch(e){try{return new ActiveXObject('MSXML2.XMLHTTP');}
	catch(e){try{return new ActiveXObject('Microsoft.XMLHTTP');}
	catch(e){return null;}}}}}
}
function FactionClick(i,id)
{  
  Faction=i;
  document.getElementById('tdAlliance').className="None";
  document.getElementById('tdHorde').className="None";
  if(id=="tdAlliance")
   document.getElementById("tdAlliance").className="focus";
   else
   document.getElementById("tdHorde").className="focus";
  onFactionChange();
}
//*****************************读取游戏服务器列表
function onFactionChange()
{
 xmlHttp=createXMLHttpRequest()
  var GameCode=document.getElementById("hgamecode").value;
  var selectGameServer=document.getElementById("tdGameServer");
  trGameServerList=document.getElementById("trServer");
  
  selectGameServer.innerHTML="<select><option value='Loading...'>Loading...</select>";
  trGameServerList.innerHTML="Loading...";
  if(GameCode==null ||GameCode=="")
    alert("Error!");
  if(GameCode=="013" || GameCode =="019")
   {
      var url="GoldDataDispose.aspx?GameCode="+GameCode+"&Cmd=GetServer&Faction="+Faction+"&"+new Date().toLocaleTimeString();
      xmlHttp.onreadystatechange=GetGameServer;
      xmlHttp.open("GET",url,true);
      xmlHttp.send(null);
   }
   else
   {
     document.getElementById("tab2").style.display="none";
     var url="GoldDataDispose.aspx?GameCode="+GameCode+"&Cmd=GetServer&"+new Date().toLocaleTimeString();
      xmlHttp.onreadystatechange=GetGameServer;
      xmlHttp.open("GET",url,true);
      xmlHttp.send(null);
   }
}
function GetGameServer()
{
   var selectGameServer=document.getElementById("tdGameServer");
    var listGameServer=document.getElementById("trServer");
   if(xmlHttp.readyState!=4)
   {
     return;
   }
   var text=xmlHttp.responseText;
   var data=text.split('*');
   selectGameServer.innerHTML=data[0];
   listGameServer.innerHTML=data[1];
   onServerChange();
}

//****************************读取产品列表

function onServerChange()
{  xmlHttp=createXMLHttpRequest();
  var tbProduct=document.getElementById("list");
  tbProduct.innerHTML="Loading...";
  var ServerCode=document.getElementById('selectGameServer').value;
  if(ServerCode==null || ServerCode=="")
   alert("Error!");
   else
   {
     var url="GoldDataDispose.aspx?ServerCode="+ServerCode+"&Cmd=GetProduct&Faction="+Faction+"&"+new Date().toLocaleTimeString();
      xmlHttp.onreadystatechange=GetProduct;

      xmlHttp.open("GET",url,true);
      xmlHttp.send(null);
   }
}

function GetProduct()
{ 
   var tbProduct=document.getElementById("list");
   if(xmlHttp.readyState!=4)
   {
     return;
   }
   var text=xmlHttp.responseText;
   tbProduct.innerHTML=text;
   Refresh();
}

//************************************

//*******************添加产品到购物车
function BuyProduct(id)
{
xmlHttp=createXMLHttpRequest();
 document.getElementById("mytotal").innerHTML="Loading...";
 var ServerCode=document.getElementById('selectGameServer').value;
 var PGuid=id;
  var GameCode=document.getElementById("hgamecode").value;
  var ServerName;
 if(GameCode=="013")
  ServerName=document.getElementById('selectGameServer').options[document.getElementById('selectGameServer').selectedIndex].text+" US"+"-"+Faction;
  else if(GameCode=="019")
    ServerName=document.getElementById('selectGameServer').options[document.getElementById('selectGameServer').selectedIndex].text+" EU"+"-"+Faction;
    else 
    ServerName=document.getElementById('selectGameServer').options[document.getElementById('selectGameServer').selectedIndex].text
  if(ServerCode==null || PGuid==null || ServerName==null || GameCode==null)
   alert("Error!");
   else
   {
     var url="CartDispose.aspx?ServerCode="+ServerCode+"&Cmd=AddProduct&ServerName="+ServerName+"&GameCode="+GameCode+"&Guid="+PGuid+"&"+new Date().toLocaleTimeString();
      xmlHttp.onreadystatechange=AddProduct;
      xmlHttp.open("GET",url,true);
      xmlHttp.send(null);
   }
}

function AddProduct()
{
   var shoppingcart=document.getElementById("mytotal");
   if(xmlHttp.readyState!=4)
   {
     return;
   }
   var text=xmlHttp.responseText;
   shoppingcart.innerHTML=text;
}

//***********************切换货币类型
function ChangeMoney(id)
{
xmlHttp=createXMLHttpRequest();
 document.getElementById("USD").className="None";
 document.getElementById("EUR").className="None";
 document.getElementById("GBP").className="None";
 document.getElementById("cartUSD").className="None";
 document.getElementById("cartEUR").className="None";
 document.getElementById("cartGBP").className="None";
 if(id=="USD"){
  document.getElementById("USD").className="focus";
   document.getElementById("cartUSD").className="focus";}
 else if(id=="EUR")
  {document.getElementById("EUR").className="focus";
   document.getElementById("cartEUR").className="focus";}
 else if(id=="GBP")
   {document.getElementById("GBP").className="focus";
    document.getElementById("cartGBP").className="focus";}


    if(id==null || id=="")
     id="USD";
      var url="ChangeMoneyType.aspx?MoneyType="+id+"&"+new Date().toLocaleTimeString();
      xmlHttp.onreadystatechange=GetMoneyType;
      xmlHttp.open("GET",url,true);
      xmlHttp.send(null);
}
function GetMoneyType()
{
    if(xmlHttp.readyState!=4)
   {
     return;
   }
   var text=xmlHttp.responseText;
  onServerChange();
}
//***********************刷新购物车
function Refresh()
{
xmlHttp=createXMLHttpRequest();
document.getElementById("mytotal").innerHtml="Loading...";
 var url="CartDispose.aspx?Cmd=Refresh&"+new Date().toLocaleTimeString();
  xmlHttp.onreadystatechange=RefreshCart;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}

function RefreshCart()
{ 
  var shoppingcart=document.getElementById("mytotal");
   if(xmlHttp.readyState!=4)
   {
     return;
   }
   var text=xmlHttp.responseText;
   shoppingcart.innerHTML=text;
   GetStore();
}


//****************************移除产品
function removeProduct(id)
{
xmlHttp=createXMLHttpRequest();
  if(id==null || id=="")
   alert("Error!");
   else
   {
    var url="CartDispose.aspx?Cmd=Remove&Guid="+id+"&"+new Date().toLocaleTimeString();
    xmlHttp.onreadystatechange=DelProduct;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
   }
}
function DelProduct()
{
  var shoppingcart=document.getElementById("mytotal");
   if(xmlHttp.readyState!=4)
   {
     return;
   }
   var text=xmlHttp.responseText;
   shoppingcart.innerHTML=text;
}

//********************列表服务器单击
function ServerClick(id)
{
xmlHttp=createXMLHttpRequest();
  var tbProduct=document.getElementById("list");
  tbProduct.innerHTML="Loading...";
  var ServerCode=id;
  document.getElementById('selectGameServer').value=id;
  if(ServerCode==null || ServerCode=="")
   alert("Error!");
   else
   {
     var url="GoldDataDispose.aspx?ServerCode="+ServerCode+"&Cmd=GetProduct&Faction="+Faction+"&"+new Date().toLocaleTimeString();
      xmlHttp.onreadystatechange=GetProduct;
      xmlHttp.open("GET",url,true);
      xmlHttp.send(null);
   }
}



//**********************读取库存
function GetStore()
{
    xmlHttp=createXMLHttpRequest();
    var ServerCode=document.getElementById('selectGameServer').value;
    var url="StoreDispose.aspx?Cmd=GetStore&ServerCode="+ServerCode+"&"+new Date().toLocaleTimeString();
    xmlHttp.onreadystatechange=writeStore;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function writeStore()
{
 var mystock=document.getElementById("mystock");
  var tbstock=document.getElementById("tblstock");
   if(xmlHttp.readyState!=4)
   {
     return;
   }
   var text=xmlHttp.responseText;
   var data=text.split('*');
   mystock.innerHTML=data[0];
   tbstock.innerHTML=data[1];
}