﻿    var $j = jQuery.noConflict();
    
    var gameCode;
    var moneyType = 'USD';
    var serverList = '';
    
    var tblHead = '<h3 style="margin-top:15px; width:180px;">VOLUME DISCOUNT</h3>\
                    <table border="0" cellpadding="0" cellspacing="0" class="table_gold_list">\
                      <tr>\
                        <th>NAME</th>\
                        <th>TOTAL</th>\
                        <th>Operate</th>\
                      </tr>';
    var tblBody = '';
    var tblFoot = '</table>';
    
    var dataHtml = '';
    
    $j(document).ready(function(){
        changeGame();
    });
    
    function changeGame(){
        var gameCode = document.getElementById('sltGame').value;
        var keyWord = document.getElementById('sltServerType').value;
        resetPriceTable();
        if(gameCode == '013' || gameCode == '019'){
            document.getElementById('sltServerType').style.display = 'block';
            getGameServer(gameCode, keyWord);
        }else{
            document.getElementById('sltServerType').style.display = 'none';
            getGameServer(gameCode, null);
        }
    }
    
    function changeKeyWord(){
        resetPriceTable();
        var gameCode = document.getElementById('sltGame').value;
        var 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){
        $j('#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');
        ddlServer.length = 0;
        ddlServer.options.add(new Option('--Loading--', '-1'));
        
        $j.ajax({
            url : '../GoldProduct.aspx',
            type : 'post',
            data : 'operate=GetServerList&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));
                }
            }
        }
    }

    function getProduct(gameServerCode){
        var ddlProduct = document.getElementById('sltProducts');
        if(gameServerCode == '-1'){
            resetPriceTable();
            ddlProduct.length = 0;
            ddlProduct.options.add(new Option('--please choose product--', '-1'));
            return;
        }
        $j('#div_wait').show();
        
        ddlProduct.length = 0;
        ddlProduct.options.add(new Option('--Loading--', '-1'));
        
        $j.ajax({
            url : '../GoldDispose.aspx',
            type : 'post',
            data : 'operate=getProduct&gameServerCode=' + gameServerCode + '&rd=' + Math.random(),
            dataType : 'html',
            success : function(html){
                $j('#div_wait').hide();
                dataHtml = html;
                if(dataHtml == ''){
                    ddlProduct.length = 0;
                    ddlProduct.options.add(new Option('--No Product--', '-1'));
                    return;
                }
                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, true);
            }
        });
    }
    
    function initProductTable(html, isSlide){
        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];
            }
            tblBody += "<tr align=\"left\" onclick=\"clickGoldTable('"+i+"');\" style=\"cursor:pointer\" onmouseover=\"this.style.backgroundColor='#FCFEB7'\" onmouseout=\"this.style.backgroundColor=''\"><td>"+ name +"</td><td>"+ price +"</td><td><a href=\"javascript:clickGoldTable('"+i+"');\">Choose</a></td></tr>";
        }
        if(isSlide){
            $j('#div_goldTable').hide();
            document.getElementById('div_goldTable').innerHTML = tblHead + tblBody + tblFoot;
            $j('#div_goldTable').show(1000);
        }else{
            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);
    }