﻿var d = new Date().toLocaleDateString();
var productGuid = new Array();  //存储Product的Guid
var productValue = new Array();
var goldGameId = '#ddlGame',goldServerId = '#selectGameServer',goldProductId = '#selectProduct'; //预定义GoldGame,GoldServer,GoldProduct的Id
var displayPriceId = '#txtPrice';

$(document).ready(function(){
 GetGoldGame();
});

function GetGoldGame()
 {
 $(goldGameId).append('<option>--------Loading--------</option>');
   $.ajax({
    url:'GetSiteGame.ashx',
    data:'d=' + d,
    type:'get',
    success:StartBindGoldGame 
   });
 }
 
  function StartBindGoldGame(xml)
 {
  $(goldGameId + '>option:first').remove();
   $(xml).find('Game').each(function(){
     var gameName = $(this).find('GameName').text();
     var gameCode = $(this).find('GameCode').text();
     $(goldGameId).append('<option value = ' + gameCode + '>' + gameName + '</option>');
   });
   
   setTimeout(function(){
                $(goldGameId).get(0).value="013";
           },1);
//   $(goldGameId).get(0).value="013";
   StartGetGoldServer("013");
 }
 
 function StartGetGoldServer(gameGuid)
 {
 $(goldServerId).append('<option>--------Loading--------</option>');
 $.ajax({
 url:'GetGameServer.ashx',
 data:'gameCode='+gameGuid+'&d='+d,
 type:'get',
 success:StartBindGoldServer
 });
 }
 
 function StartBindGoldServer(xml)
 {
    $(goldServerId).empty();
    $(xml).find('Server').each(function(i){
    var serverName=$(this).find('ServerName').text();
    var serverCode=$(this).find('ServerCode').text();
    $(goldServerId).append('<option value='+serverCode+'>'+serverName+'</option>');
    });
    $(goldServerId)[0].selectedIndex =0;
    StartGetGoldProduct($(goldServerId).val(),'USD');
 }
 
 function StartGetGoldProduct(serverGuid,moneyType)
 {
    productGuid=new Array();
    productValue=new Array();
    $(goldProductId).append('<option>--------Loading--------</option>');
    $.ajax({
    url:'GetGoldProduct.aspx',
    data:'serverCode='+serverGuid+'&moneyType='+moneyType+'&d='+d,
    type:'get',
    success:StartBindGoldProduct
    })
 }
 
 function StartBindGoldProduct(xml)
 {
    $(goldProductId).empty();
    $(xml).find('Product').each(function(){
        var productName=$(this).find('ProductName').text();
        var productPrice=$(this).find('SaleProductPrice').text();
        var guid=$(this).find('Guid').text();
        var pValue=$(this).find('ProductValue').text();
        productGuid.push(guid);
        productValue.push(pValue);
        $(goldProductId).append('<option value="'+productPrice+'">'+productName+'</option>');
    });
    $(goldProductId)[0].selectedIndex = 0;
    
   if($(goldGameId).val() =='013')
   {
     var count = $(xml).find('Product').length;
     for(var i=0;i<count;i++)
     {
       if($(goldProductId)[0].options[i].text == '5000 Gold')
       {
           setTimeout(function(){
                $(goldProductId)[0].options[i].selected = true;
                OnProductChange();
           },1);
           break;
       }
     }
   }
   OnProductChange();
 }
 
 function OnGameChange()                             //Game改变
  {
    productGuid = new Array();
    productValue = new Array();
    $(goldServerId).empty();
    $(goldProductId).empty();
    
    var guid = $(goldGameId).val();
   
    StartGetGoldServer(guid);
  }
  
  function OnServerChange(moneyType)                    //GameServer改变
  {
     if(moneyType =='' || moneyType == null)
     moneyType = "USD";
     
     var serverGuid = $(goldServerId).val();
     $(goldProductId).empty();
     productGuid = new Array();
     productValue = new Array();
     
      StartGetGoldProduct(serverGuid,moneyType);
  }
  
  function OnProductChange()
  {
   var index = $(goldProductId)[0].selectedIndex;
   var price = $(goldProductId).val();
    $(displayPriceId).val(price);
  }
  
  function GetProductGuid(index)            //获取产品Guid
  {
    if(productGuid.length <= 0 || index > productGuid.length )
    return;
    
    return productGuid[index];
  }
  
  function GetProductValue(index)
  {
     if(productValue.length <= 0 || index > productValue.length)
      return;
      
      return productValue[index];
  }
  
  function Check()
  {
     var gameCode = $(goldGameId).val();
     var serverCode = $(goldServerId).val();
     var product = $(goldProductId).val();
     
     if(gameCode =="-1")
     {
       alert("Please select your game!");
       return false;
     }
     
     if(serverCode == "-1")
     {
       alert("Please select your game server!");
       return false;
     } 
     
     if(product == "-1")
     {
        alert("Please select your quantity!");
        return false;
     }
     InitHideData();
     
     return true;
  }
  
  function InitHideData()
  {  
     var gameName = $(goldGameId + ">option:selected").text();
     var serverName = $(goldServerId + ">option:selected").text();
     var productName = $(goldProductId + ">option:selected").text();
     
     var index = $(goldProductId)[0].selectedIndex;
     var pGuid = GetProductGuid(index);
     var pValue = GetProductValue(index);
     $('#hidgameName').val(gameName);
     $('#hidserverName').val(serverName);
     $('#hidproductGuid').val(pGuid);
     $('#hidproductValue').val(pValue);
     $('#hidproductName').val(productName);
  }
  