﻿    var $j = jQuery.noConflict();
    
    var gameCode;
    var moneyType = 'USD';
    var serverList = '';
    var tag=1;
    var tblHead = '<table border="0" cellpadding="0" cellspacing="0" class="table_gold_list">\
                    <tr>\
                        <th colspan="3">VOLUME DISCOUNT</th>\
                    </tr>\
                     <tr>\
                        <td bgcolor="#F4F4F4" align="center"> Name </td>\
                        <td bgcolor="#F4F4F4" align="center">O.Price</td>\
                        <td bgcolor="#F4F4F4" align="center">C.Price</td>\
                     </tr>';
     var tblHead2 = '<table border="0" cellpadding="0" cellspacing="0" class="table_gold_list">\
                    <tr>\
                              <th colspan="2">VOLUME DISCOUNT</th>\
                            </tr>\
                     <tr>\
                        <td bgcolor="#F4F4F4" align="center">NAME</td>\
                        <td bgcolor="#F4F4F4" align="center">TOTAL</td>\
                     </tr>';             
    var tblBody = '';
    var tblFoot = '</table></div>';
    
    var dataHtml = '';
    
    $j(document).ready(function(){
        changeGame();
    });
    
    function changeGame(){
        resetPriceTable();
        setDisabled('sltGame', true, true);
        var gameCode = document.getElementById('sltGame').value;
        if(gameCode == '-1'){
            return;
        }
        var wowhtml="You may also interest in our Powerleveling, Account,Item, CD-key special offer.";
        var otherhtml="You may also interest in our other special offers.";
        if(gameCode == "013" || gameCode == "019"||gameCode=="010"){
                document.getElementById('sp_sign').innerHTML = 'Gold';
                $j('.font_coinMark').html('gold');
                $j('#GoldLeft1_gamedescription').html(wowhtml);
                $j("#GoldLeft1_divchat4").css("display", "block");
                $j("#GoldLeft1_divchat3").css("display", "none");
                $j("#GoldLeft1_divemail").css("display","none");
            }else if(gameCode == "087" || gameCode == "083"){
                document.getElementById('sp_sign').innerHTML = 'Million';
                $j('.font_coinMark').html('million gold');
                $j('#GoldLeft1_gamedescription').html(otherhtml);
                $j("#GoldLeft1_divchat4").css("display", "none");
                $j("#GoldLeft1_divchat3").css("display", "block");
                 $j("#GoldLeft1_divemail").css("display","block");
//                document.GetElementById('<%=divchat4.ClientID%>').style.display="none";
//                document.GetElementById('<%=divchat3.ClientID%>').style.display="block";
//                document.GetElementById('<%=divemail.ClientID%>').style.display="block";
            }else if(gameCode=="001")
            {
                document.getElementById('sp_sign').innerHTML='Million';
                $j('.font_coinMark').html('million Gil');
                $j('#GoldLeft1_gamedescription').html(otherhtml);
                $j("#GoldLeft1_divchat4").css("display", "none");
                $j("#GoldLeft1_divchat3").css("display", "block");
                $j("#GoldLeft1_divemail").css("display","block");
//                document.GetElementById('<%=divchat4.ClientID%>').style.display="none";
//                document.GetElementById('<%=divchat3.ClientID%>').style.display="block";
//                document.GetElementById('<%=divemail.ClientID%>').style.display="block";
            }
            else
            {
                $j('#GoldLeft1_gamedescription').html(otherhtml);
                $j("#GoldLeft1_divchat4").css("display", "none");
                $j("#GoldLeft1_divchat3").css("display", "block");
                $j("#GoldLeft1_divemail").css("display","block");
//                document.GetElementById('<%=divchat4.ClientID%>').style.display="none";
//                document.GetElementById('<%=divchat3.ClientID%>').style.display="block";
//                document.GetElementById('<%=divemail.ClientID%>').style.display="block";
            }
        if(gameCode != '013' && gameCode != '083' && gameCode != '087' && gameCode!='019' && gameCode!='001'){
            document.getElementById('tip').style.display = 'none';
        }else{
            document.getElementById('tip').style.display = 'block';
        }
        
        var keyWord = document.getElementById('sltServerType').value;
        
        $j.ajax({
            url : '/GoldBuy.aspx?rd=' + Math.random(),
            type : 'get',
            data : 'operate=GetConfigPrice&gameCode=' + gameCode,
            dataType : 'html',
            success : function(configPrice){
                $j('#priceConfig').val(configPrice);
                $j.ajax({
                    url : '/Ajax.aspx?rd=' + Math.random(),
                    type : 'post',
                    data : 'operate=GetLeftOtherBusiness&gameCode=' + gameCode,
                    dataType : 'html',
                    success : function(data){
                            $j('div[class="left_otherpro"] > ul').html(data);
                        if(gameCode == '013' || gameCode == '019'||gameCode=='083'||gameCode=='087'||gameCode=='001'){
                            if(gameCode == '013' || gameCode == '019'){
                                document.getElementById('sltServerType').style.display = 'block';
                                getGameServer(gameCode, keyWord); 
                            }else{
                                document.getElementById('sltServerType').style.display = 'none'; 
                                getGameServer(gameCode, null); 
                            }
                            $j('div[class=zixuan]').show();
                            if(document.getElementById('zxCheckBox').checked){
                                $j('#zx').show();
                            }
                        }else{
                            $j('div[class=zixuan]').hide();
                            $j('#zx').hide();
                            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(){
    var gameCode=document.getElementById('sltGame').value;
        $j('#div_goldTable').hide('600', function(){
        if(gameCode=="083"||gameCode=="087"){
        document.getElementById('div_goldTable').innerHTML = tblHead + tblFoot;
        }else{
           document.getElementById('div_goldTable').innerHTML = tblHead2 + 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 : '/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));
                }
            }
        }
    }

    function getProduct(gameServerCode){
        setDisabled('sltServers', true, true);
        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 : '/GoldBuy.aspx',
            type : 'post',
            data : 'operate=GetProduct&gameServerCode=' + gameServerCode + '&rd=' + Math.random(),
            dataType : 'html',
            success : function(html){
                GetServerStorage(gameServerCode);
                getOrdersByServer(gameServerCode);
                $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));
                }
                if (gameServerCode.split(',')[0] == "019") { 
                    var ddlServer = $j('#sltServers').find("option:selected").text();
                    if(ddlServer.indexOf('GB') > 0)
                    {
                        getMoneyType("GBP", true);
                    }
                    else
                    {
                        getMoneyType("EUR", true);
                    }
                }else{
                getMoneyType(moneyType, true);
                }
                initProductTable(html, true);
            }
        });
    }
    
    function initProductTable(html, isSlide){
        var gameCode = document.getElementById('sltGame').value;
        
        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 proBonus=product[3]; 
            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];
            }
            var priceConfig = parseFloat($j('#priceConfig').val());
            var oldPrice = formatFloat(parseFloat(price.split(' ')[1]) * 1.3, 2);
            if(priceConfig > 0){
                oldPrice = formatFloat(oldPrice / priceConfig, 2);
            }
            var profree="";
            if(gameCode=="013"||gameCode=="019")
            {
                if(proBonus!=null&&proBonus!="")
                {
                profree="<br/>"+proBonus+"Free Gold";
                }
                
            }
            if((tag%2)==0){
                tblBody += "<tr align=\"center\" onclick=\"clickGoldTable('"+i+"');\" style=\"cursor:pointer\" onmouseover=\"this.style.backgroundColor='#F4F4F4'\" onmouseout=\"this.style.backgroundColor=''\"><td>"+ name +profree+"</td><td class=\"heng\">"+ oldPrice +"</td><td class=\"red\">"+price+"</td></tr>";
            }else{
                tblBody += "<tr align=\"center\" bgcolor='#F4F4F4' onclick=\"clickGoldTable('"+i+"');\" style=\"cursor:pointer\" onmouseover=\"this.style.backgroundColor=''\" onmouseout=\"this.style.backgroundColor='F4F4F4'\"><td>"+ name+profree +"</td><td class=\"heng\">"+ oldPrice +"</td><td class=\"red\">"+price+"</td></tr>";
            }
            tag++; 
        }
        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(false);
    }
    
    function productChange(isLoadProductTable){
        getMoneyType(moneyType, isLoadProductTable);
    }
    
    function getMoneyType(mType, isLoadProductTable){
        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];
        
        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;
        
        document.getElementById('sp_moneyType2').innerHTML = moneyType;
        
//        if(isLoadProductTable){
            initProductTable(dataHtml);
//        }
        GetCustomerGoldPrice(document.getElementById('txtAmount').value, false);
    }
    
    function setDisabled(domId, isDisabled, isUndisabled){
        var dom = document.getElementById(domId);
        if(dom == null){
            return;
        }else{
            if(isDisabled && isUndisabled){
                dom.disabled = true;
                dom.disabled = false;
            }else{
                dom.disabled = true;
            }
        }
    }
    
    function GetServerStorage(gameServerCode){
        if(gameServerCode == '-1'){
            return;
        }
        
        var gmCode = gameServerCode.substr(0, 3);
        if(gmCode != '013' && gmCode != '083' && gmCode != '087' && gmCode!='019' && gmCode!='001'){
            return;
        }
        
        for(var i = 1; i <= 10; i++){
            var id = 'colorTd' + i;
            var nocolor = $j('#' + id).attr('nocolor');
            $j('#' + id).attr('style', 'BACKGROUND:' + nocolor);
        }
        $j.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 = 1500000;
                if(gmCode == '083' || gmCode == '087' || gmCode=='001'){
                    storage = formatFloat(storage / 1000, 1);
                    fullStorage = formatFloat(300000 / 1000, 1);
                }
                document.getElementById('goldSell').innerHTML = goldSell;
                if(parseInt(storage) > fullStorage){
                    document.getElementById('storage').innerHTML = fullStorage + '+';
                }else{
                    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 = $j('#' + id).attr('old');
                        $j('#' + id).attr('style', 'BACKGROUND:' + old);
                        i++;
                    }else{
                        window.clearInterval(interValId);
                    }
                }, 100);
            }
        });
    }
    
    function getOrdersByServer(serverCode){
//        $j('.goumaijilu').hide();
//        if(serverCode == '-1'){
//            return;
//        }
//        var gameCode = $j('#sltGame').val();
//        if(gameCode != '013'){
//            return;
//        }
//        $j('.goumaijilu').before('<div style="position:absolute; z-index: 99; margin-left: 200px; margin-top: 30px;"><img src="/images/process.gif">Selecting Order history randomly,please wait...</div>');
//        $j('.goumaijilu').prev().show();
//        $j.ajax({
//            url : '/Ajax.aspx?rd=' + Math.random(),
//            type : 'get',
//            data : 'operate=GetOrdersByServer&serverCode=' + serverCode + '&byPage=true&pageIndex=1&pageSize=10',
//            dataType : 'json',
//            success : function(data){
//                $j('.goumaijilu').prev().hide();
//                if(data == null){
//                    return;
//                }
//                var ddlServer = document.getElementById('sltServers');
//                var serverName = ddlServer.options[ddlServer.selectedIndex].text;
//                $j('#font_server').html(serverName);
//                var trHead = '<tr>\
//                            <th>Order Code</th>\
//                            <th>Amount</th>\
//                            <th>User Name</th>\
//                            <th>Date</th>\
//                            <th>Delivery Time</th>\
//                          </tr>';
//                $j('.goumaijilu > table').html(trHead);
//                var coinMark = 'Gold';
//                var pageSum = data[0].pageSum;
//                if(pageSum > 1){
//                    $j('#link_purchase').show();
//                }else{
//                    $j('#link_purchase').hide();
//                }
//                for(var i = 0; i < data.length; i++){
//                    var html = '<tr>';
//                    html += '<td class="id_2"><a href="/TrackOrder.aspx?search='+ data[i].Code +'" target="_blank">'+ data[i].Code +'</a></td>';
//                    html += '<td>'+ data[i].SaleTotalValue +' '+ coinMark +'</td>';
//                    html += '<td class="mail">'+ data[i].UserName +'</td>'
//                    html += '<td class="mail">'+ data[i].CreateTime +'</td>';
//                    html += '<td class="mail">'+ data[i].outTime +'</td>';
//                    $j('.goumaijilu > table').append(html);
//                }
//                $j('#link_purchase').attr('href', '/Purchase.aspx?serverCode='+ serverCode + '&serverName=' + serverName);
//                $j('.goumaijilu').show();
//            }
//        });
    }

function formatFloat(src, pos){return Math.round(src*Math.pow(10, pos))/Math.pow(10, pos);}

Date.prototype.format = function(format){ 
  var o = {
    "M+" : this.getMonth()+1, //month 
    "d+" : this.getDate(),    //day 
    "h+" : this.getHours(),   //hour 
    "m+" : this.getMinutes(), //minute 
    "s+" : this.getSeconds(), //second 
    "q+" : Math.floor((this.getMonth()+3)/3),  //quarter 
    "S" : this.getMilliseconds() //millisecond 
  }
  if(/(y+)/.test(format)) format=format.replace(RegExp.$1, 
    (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
  for(var k in o)if(new RegExp("("+ k +")").test(format)) 
    format = format.replace(RegExp.$1, 
      RegExp.$1.length==1 ? o[k] : 
        ("00"+ o[k]).substr((""+ o[k]).length)); 
  return format; 
}
