﻿var array = new Array();

$(document).ready(function(){
     InitTxt();   //文本框效果
     GetGame();   //初始化游戏名
    GetCountry();
     GetNews();
     $('#btnBuy').click(BuyGold);
    
     $('#gameName').change(function(){                   //服务器联动
        onGameChange();  
    });
 
   $('#gameServer').change(function(){                //金币（Product)联动
     onServerChange()
   });
   
   $('#gameProduct').change(function(){                 //价格联动
     onProductChange();
   });
 });
 
 function onGameChange()
 {
   var gameCode =  $('#gameName')[0].value;
      $('#gameServer').empty();
      $('#gameProduct').empty();
      array = new Array();
      $('#price').val("Price($0.00)"); 
      $('#gameProduct').append('<option value=\"-1\">--Select your product--</option>');
      if(gameCode ==null || gameCode==-1)
      {
        $('#gameServer').append('<option value=\"-1\">--Select your Server--</option>');
        return;
      }
      GetServer(gameCode);
 }
 
 function onServerChange()
 {
   var serverCode = $('#gameServer')[0].value;
     $('#gameProduct').empty();
     array = new Array();
     $('#price').val("Price($0.00)");
     if(serverCode == null || serverCode == -1)
     {
       $('#gameProduct').append('<option value=\"-1\">--Select your product--</option>');
       return;
     }
      GetProduct(serverCode,"USD");
 }
 
 function onProductChange()
 {
    var price = $('#gameProduct')[0].value;
      $('#price').empty();
      if(price == "-1") 
      {
         $('#price').val("Price:($0.00)");
         return;
      }
      $('#price').val("Price:(" + price + ")");
 }
 
 function InitTxt()
 {
  $('#txtFullName').focus(function(){
  var fName = $('#txtFullName')[0].value;
  if(fName=='Full Name')
  $('#txtFullName')[0].value = '';
 });
 
 $('#txtFullName').blur(function(){
 var fName = $('#txtFullName')[0].value;
  if($.trim(fName) == '')
   $('#txtFullName').val('Full Name');
   
   fullNameCheck(fName);
 });
 
$('#txtCharacter').focus(function(){
var chararter = $('#txtCharacter')[0].value;
if(chararter == 'Character Name')
 $('#txtCharacter')[0].value = '';
 
}); 

$('#txtCharacter').blur(function(){
 var chararter = $('#txtCharacter')[0].value;
  if($.trim(chararter) == '')
   $('#txtCharacter').val('Character Name');
   
   characterCheck(chararter);
 });

$('#txtEmail').focus(function(){
 var email = $('#txtEmail')[0].value;
 if(email == 'E-mail Address')
 $('#txtEmail')[0].value = '';
 });

$('#txtEmail').blur(function(){
 var email = $('#txtEmail')[0].value;
  if($.trim(email) == '')
   $('#txtEmail').val('E-mail Address');
  
   emailCheck(email);
 });

$('#txtPhon2').focus(function(){
 var phone = $('#txtPhon2')[0].value;
 if( phone == 'Phone')
 $('#txtPhon2')[0].value = '';
 });
 
 $('#txtPhon2').blur(function(){
 var phone = $('#txtPhon2')[0].value;
  if($.trim(phone) == '')
   $('#txtPhon2').val('Phone');
   
   PhoneCheck(phone);
 });
 
 $('#txtDiscount').focus(function(){
 var discount = $('#txtDiscount')[0].value;
 if(discount == 'Discount Coupon:')
 $('#txtDiscount')[0].value = '';
 });
 
 $('#txtDiscount').blur(function(){
 var discount = $('#txtDiscount')[0].value;
  if($.trim(discount) == '')
   $('#txtDiscount').val('Discount Coupon:');
   
   discountCheck(discount);
 });
 }
 
 
 function GetGame()
 {
 $('#gameName').append('<option>--------Loading--------</option>');
   $.ajax({
    url:'/GetSiteGame.ashx',
    type:'get',
    complete:function(){ $('#gameName>option:first').remove();},
    success:BindGame
   });
 }
 
 function BindGame(xml)
 {
   $('#gameName').append('<option value=\"-1\">--Select your game--</option>');
   $(xml).find('Game').each(function(){
     var gameName = $(this).find('GameName').text();
     var gameCode = $(this).find('GameCode').text();
     $('#gameName').append('<option value = ' + gameCode + '>' + gameName + '</option>');
   });
   $('#gameName').val(-1);
 }
 
 function GetServer(code)
 {
   
   $('#gameServer').append('<option>--------Loading--------</option>');
   $.ajax({
     url: '/GetGameServer.ashx',
     data:'gameCode=' + code,
     complete:function(){ $('#gameServer>option:first').remove(); },
     type:'get',
     success:BindServer  
   });
 }
 
 function BindServer(xml)
 {
   $('#gameServer').append('<option value=\"-1\">--Select your game server--</option>');
   $(xml).find('Server').each(function(){
      var serverName = $(this).find('ServerName').text();
      var serverCode = $(this).find('ServerCode').text();
      $('#gameServer').append('<option value = ' + serverCode + '>' + serverName + '</option>');
   });   
   $('#gameServer').val(-1);
 }
 
 function GetProduct(servercode,moneyType)
 {
   $('#gameProduct').empty();
   array = new Array();
   $('#gameProduct').append('<option>--------Loading--------</option>');
   
   $.ajax({
     url:'/GetGoldProduct.aspx',
     data:'serverCode=' + servercode + '&moneyType='+ moneyType ,
     type:'get',
     complete:function(){$('#gameProduct>option:first').remove();},
     success:BindProduct
   });
 }
 
 function BindProduct(xml)
 {
   $('#gameProduct').append('<option value=\"-1\">Select your product</option>');
   $(xml).find('Product').each(function(){
     var productName = $(this).find('ProductName').text();
     var productPrice = $(this).find('SaleProductPrice').text();
     var productGuid = $(this).find('Guid').text();
     array.push(productGuid);
      
     $('#gameProduct').append('<option value = "' + productPrice + '">' + productName + '</option>');
   });
   $('#gameProduct').val(-1);
 }
 
  function GetCountry()
 {
   $('#selCountry').append('<option value=-1>----Loading----</option>');
   $.ajax({
     url:'/GetCountry.ashx',
     type:'get',
     complete:function(){ $('#selCountry>option:first').remove();},
     success:BindCountry 
   });
 }
 
 function BindCountry(xml)
 {
  $('#selCountry').append('<option value=\"-1\">Select your country</option>');
   $(xml).find('Country').each(function(){
      var countryName = $(this).find('CountryName').text();
      $('#selCountry').append('<option value = ' + countryName + '>' + countryName + '</option>');
   });
   $('#selCountry').append('<option value =\"Other\">Other</option>');
   $('#selCountry').val(-1);
 }
 
 function GetNews()
 {
   $('#newsWelcome').append("<img src=\"/images/thsale.gif\" alt=\"loading\" />");   
   $('#newsGoldContent1').append("<img id=\"imgGoldLoading\" src=\"/images/thsale.gif\" alt=\"loading\" />");
   $('#newsWowContent1').append("<img src=\"/images/thsale.gif\" alt=\"loading\" />");
 $.ajax({
     url:'/GetSingleNews.aspx',
     type:'get',
     data:'typeCode=001',
     complete:function(){ $('#newsWelcome>img').remove(); },
     success:function(txt){$('#newsWelcome').append(txt);}
   });
 
 $.ajax({
     url:'/GetSingleNews.aspx',
     type:'get',
     data:'typeCode=002',
     complete:function(){ $('#imgGoldLoading').remove(); },
     success:function(txt){
     var news = txt.split('┥');
      $('#newsGoldTitle1').append("<h6>" + news[0] + "</h6>" );
      $('#newsGoldContent1').append(news[1]);
      $('#newsGoldTitle2').append("<h6>" + news[2] + "</h6>" );
      $('#newsGoldContent2').append(news[3]);
     }
   });
      
      $.ajax({
     url:'/GetSingleNews.aspx',
     type:'get',
     data:'typeCode=003',
     complete:function(){ $('#newsWowContent1>img').remove();},
     success:function(txt){
     var newsW = txt.split('┥');
   $('#newsWowTitle1').append("<h6>" + newsW[0] + "</h6>" );
   $('#newsWowTitle2').append("<h6>" + newsW[3] + "</h6>" );
   $('#newsDate').append(newsW[2]);
   $('#newsWowContent1').append(newsW[1]);
   $('#newsWowContent2').append(newsW[4]);
   $('#newsDate2').append(newsW[5]);
     }
   }); 
 }
 
 
 function BuyGold()                                    //Btn Buy Now
 {
   var gameCode = $('#gameName')[0].value;
   var gameName = $('#gameName>option:selected').text();
   var serverCode = $('#gameServer')[0].value;
   var serverName = $('#gameServer>option:selected').text();
   var productValue = $('#gameProduct').val();
      
   var txtFullName = $('#txtFullName')[0].value;
   var txtCharacter = $('#txtCharacter')[0].value;
   var txtEmail = $('#txtEmail')[0].value;
   var txtPhon2 = $('#txtPhon2')[0].value;
   txtPhon2 = (txtPhon2 == 'Phone'?'':txtPhon2);
   var txtDiscount = $('#txtDiscount')[0].value;
   var country = $('#selCountry')[0].value;
   var payMethod = $("input[name=radPay][checked]").val();
      
   if(!drpCheck(gameCode,serverCode,productValue))
   return false;
   
   if(!fullNameCheck(txtFullName))
   return false;
   
   if(!characterCheck(txtCharacter))
   return false;
   
    if(!emailCheck(txtEmail))
   return false;
   
   if(!PhoneCheck(txtPhon2))
   return false;
   
   if(!discountCheck(txtDiscount))
   return false;
   
   if(country =="-1")
  {
    $('#countryerror').show();
    return false;
  }
  else 
  {
    $('#countryerror').hide();
  }
   
   var index = $('#gameProduct')[0].selectedIndex;
   if(array.length <= 0 || index > array.length)
   return false;
   var productGuid = array[index-1];  
    
   SentDataToServer(gameCode,gameName,serverCode,serverName ,productGuid,payMethod,txtFullName,txtCharacter,txtEmail,country,txtPhon2);
   
   window.location.href = "/GoldOrderDetail.aspx";
 }

function drpCheck(gameCode,serverCode,productValue)
{
  if(gameCode == null || gameCode ==-1)
  {
    $('#gameerror').show();
    return false;
  }
  else 
  {
   $('#gameerror').hide();
  }
  
  if(serverCode == null || serverCode ==-1)
  {
    $('#servererror').show();
    return false;
  }
  else 
  {
   $('#servererror').hide();
  }
  
  if(productValue == null || productValue == -1)
  {
    $('#priceerror').show();
    return false;
  }
  else
  {
    $('#priceerror').hide();
  } 
  return true;
}

function fullNameCheck(fName)
{
  if(fName == 'Full Name' || fName == '' || fName.length > 30)
  {
   $('#fNameerror').show();
   return false;
  }
  else 
  {
    $('#fNameerror').hide();
    return true;
  }
}

function characterCheck(character)
{
  if(character == 'Character Name' || character == '' || character.length > 30 )
  {
   $('#charactererror').show();
   return false;
  }
  else 
  $('#charactererror').hide();
  return true;
}

function emailCheck (email) {
 var emailPat=/^(.+)@(.+)$/;
 var matchArray=email.match(emailPat);
 if (matchArray==null || email.length > 50) {
  $('#mailerror').show();
 return false;
}
$('#mailerror').hide();
 return true;
}

function PhoneCheck(str)
{ 
   var reg=/^([0-9]|[\-])+$/g ;
   var marchArray = str.match(reg);
   
   if(str == 'Phone' || str == '' || str.length > 15 || marchArray == null)
   {
    $('#phoneerror').show();
    return false;
   }
   else
     $('#phoneerror').hide();
     return true;
}

function discountCheck(discount)   //折扣验证
{
  if(discount == 'Discount Coupon:' || discount == null || discount=='')
  return true;
  else 
  {
    var gameCode = $('#gameName')[0].value;
    $('#hideDiscount').show();
    var response = $.ajax({
     url:'/GoldCouponCheck.aspx',
     data:'couponCode=' + discount + '&gameCode=' + gameCode,
     type:'get',
     cache:'false',
     async:'false',
     complete:function(){$('#hideDiscount').hide();}
    }).responseText;
    if(response == 'wrong')
    {
      alert('This COUPON is error or has expired!');
      return false;
    }
    else
    return true;
  }
}

function SentDataToServer(gameCode,gameName,serverCode,serverName,productGuid,payMehtod,fullName,characterName,email,country,nonUsPhone)
{
  $.ajax({
        url:'/GoldDataDispose.aspx',
        data:'gameCode=' + gameCode + '&gameName=' + gameName + '&serverCode=' + serverCode + '&serverName=' + serverName 
          + '&productGuid=' + productGuid + '&payMethod=' + payMehtod
          + '&fullName=' + fullName + '&characterName=' + characterName + '&email=' + email + '&country=' + encodeURI(country) + '&nonUsPhone=' + encodeURI(nonUsPhone) 
          + '&moneyType=USD',
        type:'get',
        async:'false'
        });
}