﻿    var gameCode;
    var moneyType = 'USD';
    var serverList = '';
    
    var tblHead = '<table cellspacing="0" class="tab_c"><tr class="trgray"><th>Name</th><th>Price</th></tr>';
    var tblBody = '';
    var tblFoot = '</table>';
    
    var dataHtml = '';
    
    $(document).ready(function(){
        changeGame();
    });
    
    function changeGame(){
        var keyWord="";
        var gameCode = document.getElementById('sltGame').value;
        if(gameCode == "013" || gameCode == "019"){
                
                $('.font_coinMark').html('gold');
             
            }else if(gameCode == "087" || gameCode == "083"){
                
                $('.font_coinMark').html('million gold');
              
            }else if(gameCode=="001"){
                
                $('.font_coinMark').html('million Gil');
            }
            
        if(gameCode!="013"&&gameCode!="019"){
            document.getElementById('sltServerType').style.display='none';
        }else{          
            document.getElementById('sltServerType').style.display='';  
            keyWord = document.getElementById('sltServerType').value;
        }
        if(gameCode != '013'&&gameCode!='019' && gameCode != '083' && gameCode != '087' && gameCode!='001'){
            document.getElementById('tip').style.display = 'none';
        }else{
            document.getElementById('tip').style.display = 'block';
             if(gameCode == '1324')
             {
             document.getElementById('divSell').style.display = 'none';
             document.getElementById('tableTip').style.display = 'none';
             }else
             {
             document.getElementById('divSell').style.display = 'block';
             document.getElementById('tableTip').style.display = 'block';
             }
        }
       
        resetPriceTable();
        
        getGameServer(gameCode, keyWord);
    }
    
    function changeKeyWord(){
        resetPriceTable();
        var gameCode = document.getElementById('sltGame').value;
        var keyWord="";
        if(gameCode!="013"&&gameCode!="019"){
            document.getElementById('sltServerType').style.display='none';
        }else{     
            document.getElementById('sltServerType').style.display='';         
            keyWord = document.getElementById('sltServerType').value;
        }
        var ddlProduct = document.getElementById('sltProducts');
        ddlProduct.length = 0;
        ddlProduct.options.add(new Option('--please choose product--', '-1'));
        getGameServerByJson(gameCode, keyWord, serverList);
    }
    
    function resetPriceTable(){
        document.getElementById('div_goldTable').innerHTML = tblHead + tblFoot;
        document.getElementById('sp_USD').innerHTML = '$ 0.00';
        document.getElementById('sp_EUR').innerHTML = '€ 0.00';
        document.getElementById('sp_GBP').innerHTML = '£ 0.00';
    }
    
    function getGameServer(gameCode, keyWord){
        $('#div_wait').show();
        var ddlProduct = document.getElementById('sltProducts');
        ddlProduct.length = 0;
        ddlProduct.options.add(new Option('--please choose product--', '-1'));
        
        var ddlServer = document.getElementById('sltServers');
        $.ajax({
            url : '/GoldSale.aspx',
            type : 'post',
            data : 'operate=GetServerList&gameCode='+ gameCode + '&rd=' + Math.random(),
            dataType : 'json',
            success : function(json){
                getGameServerByJson(gameCode, keyWord, json);
                $('#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(gameCode=="013"||gameCode=="019"){
            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));
                }
            }
        }else{
                for(var i = 0; i < json.length; i++){
                    ddlServer.options.add(new Option(json[i].ServerNameOther, json[i].Code));
            }}
    }

    function getProduct(gameServerCode){
        if(gameServerCode == '-1'){
            resetPriceTable();
            var ddlProduct = document.getElementById('sltProducts');
            ddlProduct.length = 0;
            ddlProduct.options.add(new Option('--please choose product--', '-1'));
            return;
        }
        $('#div_wait').show();
        $.ajax({
            url : '/GoldDispose.aspx',
            type : 'post',
            data : 'operate=getProduct&gameServerCode=' + gameServerCode + '&rd=' + Math.random(),
            dataType : 'html',
            success : function(html){
            GetServerStorage(gameServerCode);
                dataHtml = html;
                document.getElementById('sltProducts').length = 0;
                var data = html.split('|');
                for(var i = 0; i < data.length; i++){
                    var product = data[i].split('@');
                    var guid = product[0];
                    var name = product[1];
                    var priceList = product[2];
                    document.getElementById('sltProducts').options.add(new Option(name, priceList));
                }
                getMoneyType(moneyType);
                initProductTable(html);
                $('#div_wait').hide();
            }
        });
    }
    
    function initProductTable(html){
        if(html=="")return;
        tblBody = '';
        var data = html.split('|');
        var moneyChar = '$';
        if(moneyType == 'USD'){
            moneyChar = '$';
        }else if(moneyType == 'EUR'){
            moneyChar = '€';
        }else if(moneyType == 'GBP'){
            moneyChar = '￡';
        }
        
        for(var i = 0; i < data.length; i++){
            var product = data[i].split('@');
            var guid = product[0];
            var name = product[1];
            var priceList = product[2]; 
            var price;
            if(moneyChar == priceList.split('^')[0].substring(0,1)){
                price = priceList.split('^')[0];
            }else if(moneyChar == priceList.split('^')[1].substring(0,1)){
                price = priceList.split('^')[1];
            }else{
                price = priceList.split('^')[2];
            }
            
            if(i % 2 == 0)
                tblBody += "<tr align=\"left\" onclick=\"clickGoldTable('"+i+"');\"  style=\"cursor:pointer\" onmouseover=\"this.style.backgroundColor='#E9F0FF'\" onmouseout=\"this.style.backgroundColor=''\"><td>"+ name +"</td><td>"+ price +"</td></tr>";
            else
                tblBody += "<tr align=\"left\" onclick=\"clickGoldTable('"+i+"');\"  style=\"cursor:pointer\" onmouseover=\"this.style.backgroundColor='#E9F0FF'\" onmouseout=\"this.style.backgroundColor=''\"><td>"+ name +"</td><td>"+ price +"</td></tr>";
        }
        document.getElementById('div_goldTable').innerHTML = tblHead + tblBody + tblFoot;
    }
    
    function clickGoldTable(index){
        var ddlProduct = document.getElementById('sltProducts');
        ddlProduct.selectedIndex = index;
        productChange();
    }
    
    function productChange(){
        getMoneyType(moneyType);
    }
    
    function getMoneyType(mType){
        moneyType = mType;
        var sp_priceId = 'sp_' + mType;
        var a_moneyTypeId = 'a_' + mType;
        
        if(document.getElementById('sltProducts').value == '-1'){
            return;
        }
        
        document.getElementById('sp_USD').style.color = '';
        document.getElementById('a_USD').style.color = '';
        document.getElementById('sp_EUR').style.color = '';
        document.getElementById('a_EUR').style.color = '';
        document.getElementById('sp_GBP').style.color = '';
        document.getElementById('a_GBP').style.color = '';
        
        var ddlProduct = document.getElementById('sltProducts');
        var priceList = ddlProduct.value.split('^');
        
        document.getElementById('sp_USD').innerHTML = priceList[0];
        document.getElementById('sp_EUR').innerHTML = priceList[1];
        document.getElementById('sp_GBP').innerHTML = priceList[2];
        
        // 改变选中货币类型的颜色（red）
        document.getElementById(sp_priceId).style.color = 'red';
        document.getElementById(a_moneyTypeId).style.color = 'red';
        
        // 设置货币类型、价格、产品名称
        document.getElementById('hidden_price').value = document.getElementById(sp_priceId).innerHTML;
        document.getElementById('hidden_moneyType').value = moneyType;
        document.getElementById('hidden_productName').value = ddlProduct.options[ddlProduct.selectedIndex].text;
        
        initProductTable(dataHtml);
    }
    function GetServerStorage(gameServerCode){
        if(gameServerCode == '-1'){
            return;
        }
        
        var gmCode = gameServerCode.substr(0, 3);
        if(gmCode != '013'&& gmCode != '019' && gmCode != '083' && gmCode != '087'&& gmCode!='001'){
            return;
        }
        
        for(var i = 1; i <= 10; i++){
            var id = 'colorTd' + i;
            var nocolor = $('#' + id).attr('nocolor');
            $('#' + id).attr('style', 'background:' + nocolor);
        }
        $.ajax({
            url : '/Ajax.aspx?rd=' + Math.random(),
            type : 'get',
            data : 'operate=GetServerStorage&gameServerCode=' + gameServerCode,
            dataType : 'html',
            success : function(data){
                var goldSell = parseInt(data.split('|')[1]);
                var storage = parseInt(data.split('|')[0]);           
                var fullStorage = 80000;
                if(gmCode == '083' || gmCode == '087'){
                    storage = formatFloat(storage / 1000, 1);
                }else if(gmCode=='001')
                {
                    storage = formatFloat(storage / 1000, 1);
                }
                document.getElementById('goldSell').innerHTML = goldSell;
                document.getElementById('storage').innerHTML = storage+'+';
                var count = parseInt(parseFloat(storage * 10) / fullStorage);
                var i = 1;
                var interValId = window.setInterval(function(){
                    if(count >= 10){
                        count = 10;
                    }
                    if(i <= count){
                        var id = 'colorTd' + i;
                        var old = $('#' + id).attr('old');
                        $('#' + id).attr('style', 'background:' + old);
                        i++;
                    }else{
                        window.clearInterval(interValId);
                    }
                }, 100);
            }
        });
    }
function formatFloat(src, pos){return Math.round(src*Math.pow(10, pos))/Math.pow(10, pos);}