﻿var $j = jQuery.noConflict();
var serverList = '';
var gameCode;
var keyWord;




    $j(document).ready(function(){
        gameCode = document.getElementById('gameCode').value;
        getServer(gameCode);
    });


      function getServer(gameCode){
            if(gameCode == '-1'){
                return;
            }
           
           
            var ddlServer = document.getElementById('sltServers');
            ddlServer.length = 0;
            ddlServer.options.add(new Option('Loading', '-1'));
            $j.ajax({
                url : '/Ajax.aspx?rd=' + Math.random(),
                type : 'post',
                data : 'operate=GetGoldServer&gameCode=' + gameCode,
                dataType : 'json',
                success : function(json){
                    serverList = json;
                    if(json == null){
                        ddlServer.length = 0;
                        ddlServer.options.add(new Option('No Server', '-1'));
                        return;
                    }
                    if(gameCode != '013' && gameCode != '019'){
                        //initServerList();
                    }else{
                        initServerByKeyWord($j('#sltServerType').val());
                    }
                    //$j('#div_wait_gold').hide();
                }
            });
        }



 function initServerByKeyWord(keyWord){
            if(serverList == null){
                return;
            }
            var ddlServer = document.getElementById('sltServers');
            ddlServer.length = 0;
            ddlServer.options.add(new Option('--Choose--', '-1'));
            for(var i = 0; i < serverList.length; i++){
                if(keyWord == serverList[i].ServerNameOther.substring(0, 1)){
                    ddlServer.options.add(new Option(serverList[i].ServerNameOther, serverList[i].Code));
                }
            }
        }



function changeKeyWord(){
        document.getElementById("txtAmount").value="";
            document.getElementById("goldPrice").value="0";
        gameCode = document.getElementById('gameCode').value;
        keyWord = document.getElementById('sltServerType').value;
        getGameServer(gameCode, keyWord);
        GetLevelProduct();
        
        displayToolTip('101');
    }
    
    
function getGameServer(gameCode, keyWord){
        //$j('#div_wait').show();
        
        var ddlServer = document.getElementById('sltServers');
        ddlServer.length = 0;
        ddlServer.options.add(new Option('--Loading--', '-1'));
        
        $j.ajax({
            url : '/GoldBuy.aspx',
            type : 'post',
            data : 'operate=OutServerList&gameCode='+ gameCode + '&rd=' + Math.random(),
            dataType : 'json',
            success : function(json){
                getGameServerByJson(gameCode, keyWord, json);
                //$j('#div_wait').hide();
            }
        });
        
        var gameServerCode = document.getElementById('sltServers').value;
    }
    
function getGameServerByJson(gameCode, keyWord, json){
        serverList = json;
        var ddlServer = document.getElementById('sltServers');
        ddlServer.length = 0;
        ddlServer.options.add(new Option('--please choose server--', '-1'));
        if(keyWord == null){
            for(var i = 0; i < json.length; i++){
                ddlServer.options.add(new Option(json[i].ServerNameOther, json[i].Code));
            }
        }else{
            for(var i = 0; i < json.length; i++){
                if(json[i].ServerNameOther.substring(0, 1) == keyWord){
                    ddlServer.options.add(new Option(json[i].ServerNameOther, json[i].Code));
                }
            }
        }
    }
    
    var lastAmount=0;
    function GetCustomerGoldPrice(amount)
        {
            var gameCode=document.getElementById('gameCode').value;
            
            try
                {
                    if(gameCode=="013"||gameCode=="019")
                        {
                            if(parseFloat(amount)<500)
                                {
                                    document.getElementById('txtAmount').value='500';
                                    amount=500;
                                 }
                        }
                    if(gameCode=="083"||gameCode=="087")
                        {
                            if(parseFloat(amount)<0.1)
                                {
                                    document.getElementById('txtAmount').value='0.1';
                                    amount=0.1;
                                }
                         }
                  }
             catch(e)
                {
                    return
                }
             var gameServerCode=document.getElementById('sltServers').value;
             if(gameServerCode=='-1')
                {
                    alert('Please choose your server');
                    return
                }
             if(amount==lastAmount)
                {
                    return
                }
             lastAmount=amount;
             //$j('#div_wait_gold2').show();
             //document.getElementById('txtPrice2').value='0';
             $j.ajax(
                {
                    url:'/Ajax.aspx?rd='+Math.random(),
                    type:'get',
                    data:'operate=GetCustomerGoldPrice&amount='+amount+'&gameServerCode='+gameServerCode,
                    dataType:'html',
                    success:function(data)
                        {
                              var sltStart=document.getElementById('sltStart').value;
                              var sltEnd=document.getElementById('sltEnd').value;
                            
                              document.getElementById("goldPrice").value = parseFloat(data);
                              var goldPrice = document.getElementById("goldPrice").value;
                              var plPrice = document.getElementById("plPrice").value;
                              var plPrice2 = document.getElementById("plPrice2").value;
                              if(sltStart=='----' || sltEnd=='----')
                              {
                               document.getElementById('divPrice').innerHTML="$"+parseFloat(data);
                              }
                              else
                              {  
                              document.getElementById('divPrice').innerHTML="$" +(parseFloat(goldPrice)+ parseFloat(plPrice2) + parseFloat(plPrice)).toFixed(2);
                              }
               
                        }
                 }
                 )
       }
       
       var isIE = navigator.userAgent.indexOf("MSIE") > 0;
        function checkCustomerEnter(e){
            var keyNum = 0;
            if(isIE){
                keyNum = e.keyCode;
            }else{
                keyNum = e.which;
            }
            var amount = document.getElementById('txtAmount').value;
            for(var i = 0; i < amount.length; i++){
                var keyCode = amount.charCodeAt(i);
                if(keyCode < 48 || keyCode > 57){
                    var gameCode = document.getElementById('gameCode').value;
                    if(gameCode != '013' || gameCode != '019'){
                        if(amount.charAt(i) != '.'){
                            amount = amount.replace(amount.charAt(i), '');               
                        }
                    }else{
                        amount = amount.replace(amount.charAt(i), '');
                    }
                }
            }
            document.getElementById('txtAmount').value = amount;
        }