﻿var $j = jQuery.noConflict();

var serverlist;



//        
//        //游戏改变获取服务器
function GameChanger(gameCode) {
    var ddlserver = document.getElementById("ddlServer");
    showLeftComment(gameCode);
    initMenu(gameCode);
//    if(gameCode=="019")
//    {
//        $j("#wowgoldbackgrouand").attr('src','/images/gold_pro_list_bg.jpg');
//        $j("#hidden_gameCode").val(gameCode);
//    }
//    if(gameCode=="013")
//    {
//        $j("#wowgoldbackgrouand").attr('src','/images/selectserver.jpg');
//        $j("#hidden_gameCode").val(gameCode);
//    }
     //$j('#ddlGame').changeBackGround(gameCode);
    //游戏服务器改变
    $j.ajax({
        url: '/gold/GoldAjax.aspx?rd=' + Math.random(),
        type: 'get',
        data: 'operate=Server&gameCode=' + gameCode,
        dataType: 'html',
        success: function(data) {
            serverlist = data.split(';');
            
            if (gameCode == "013" || gameCode == "019")
                keywordChanger('a', ddlserver);
            else {
                window.location.href = "http:/mmosavor.com/" + data;
            }
        }
    });

}


//        //游戏改变获取服务器
function GameOtherChanger(gameCode) {
    if (gameCode == "013") {
        window.location.href = "/gold/WoWGoldProduct.aspx?gameCode=013";
    }
    else if (gameCode == "019") {
        window.location.href = "/gold/WoWGoldProduct.aspx?gameCode=019";
    }
    else if (gameCode == "010") {
        window.location.href = "/Runescape2-Gold/";
    }
    var ddlserver = document.getElementById("ddlServer");
    showLeftComment(gameCode);
    initMenu(gameCode);
    setCustomProduct(gameCode);
     $j('#ddlGame').changeBackGround(gameCode);
    //游戏服务器改变
     $j.ajax({
         url: '/gold/GoldAjax.aspx?rd=' + Math.random(),
         type: 'get',
         data: 'operate=Server&gameCode=' + gameCode,
         dataType: 'html',
         success: function(data) {
             serverlist = data.split(';');
             ddlserver.options.length = 0;

             ddlserver.options.add(new Option('--Choose--', '-1'));
             for (var i = 0; i < serverlist.length; i++) {
                 var server = serverlist[i].split('|');
                 if (server[1] != undefined)
                     {
                     ddlserver.options.add(new Option(server[1], server[0]));
                     }
       

             }

         }
     });
  
}
//        
//        
//根据首字母加载服务器
function keywordChanger(keyWord) {
    var ddlserver = document.getElementById("ddlServer");
    if (serverlist == null) {
        return;
    }
    ddlserver.options.length = 0;
    ddlserver.options.add(new Option('--Choose--', '-1'));
    for (var i = 0; i < serverlist.length; i++) {
        var server = serverlist[i].split('|');
        if (server.length > 1) {
            if (keyWord.toUpperCase() == server[1].substring(0, 1).toUpperCase()) {
                ddlserver.options.add(new Option(server[1], server[0]));
            }
        }
    }
}
//    

function GameServerChanger(servercode) {


    if ($j('#hidden_gameCode').val() == "013" && servercode != "-1") {
        $j('#tip').show();
    } else {
        $j('#tip').hide();
    }
    //GetGoldStorge(servercode);
    getServerStorage(servercode);
    var ddlProduct = document.getElementById("ddlProduct");
    //游戏服务器改变
    $j.ajax({
        url: '/gold/GoldAjax.aspx?rd=' + Math.random(),
        type: 'get',
        data: 'operate=Product&servercode=' + servercode,
        dataType: 'html',
        success: function(html) {
            ddlProduct.length = 0;
            ddlProduct.options.add(new Option('--Choose--', '-1'));
            //   $(setData.domWait).hide();
            productList.data = html;
            if (html == '') {
                return;
            }
            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];
                ddlProduct.options.add(new Option(name, priceList));
            }

            setMoneyType(moneyType.getData(), false);
            initProductTable(productList.getData(), true);
            lastAmount = 0;

            if (servercode.substr(0, 3) == "019")
                getMoneyType('EUR');
            if (servercode.substr(0, 3) == "013")
                getMoneyType('USD');
            if (servercode.substr(0, 3) == "130")
                getMoneyType('USD');
            if (servercode.substr(0, 3) == "118")
                getMoneyType('EUR');

            GetCustomerGoldPrice($j('#txtAmount').val());
        }



    });

}

function GetGoldStorge(servercode)
{
    $j.ajax({
     url: '/gold/GoldAjax.aspx?rd=' + Math.random(),
        type: 'get',
        data: 'operate=Storge&servercode=' + servercode,
        dataType: 'text',
        success: function(html) {
        if(html!="")
        {
            $j('#tipgold').show();
            $j('#tip').hide();
        }
        else
        {
             $j('#tipgold').hide();
             $j('#tip').show();
        }
        }
    });
}

function productList(data) {
    this.data = data;
    this.getDate = function() {
        return this.data;
    }
}
productList.getData = function() { return this.data; }

function moneyType(data) {
    this.data = data;
    this.getDate = function() {
        return this.data;
    }
}
moneyType.getData = function() {
    var d = this.data;
    if (d != undefined) {
        return d;
    }
    return 'USD';
}


//产品显示列表
function initProductTable(html, isSlide) {
    var ddlProduct = document.getElementById("ddlProduct");
    var li = '';
    if (html == null || html == "") return;
    var data = html.split('|');
    var moneyChar = '$';
    var mtp = moneyType.getData();
    mtp = moneyType.getData();
    if (mtp == 'USD') {
        moneyChar = '$';
    } else if (mtp == 'EUR') {
        moneyChar = '€';
    } else if (mtp == '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 proBonusValues=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);
        }
        
        li += '<li style="cursor: pointer;" onclick=\'clickGoldTable(' + (i + 1) + ');\'><div class="gold_box_G">' + name;
        if (isNoBlank(proBonusValues)) {
            var test = parseFloat(proBonusValues);
            if (test > 0.00) {
                li += '<div class="freegold_bg"><img src="/images/free_gold_mmo.gif" /><br/>' + proBonusValues + 'G</div></div>';
            }
            else {
                li += '<div class="freegold_bg"><img src="/images/free_gold_mmo.gif" /><br/>' + proBonusValues+'</div></div>';
            }
		   
		}
		else
		{
		    li +='</div><p class="heng gold_ljg">' + moneyChar + ' ' + oldPrice + '</p>';
		}			           
		li +='<p class="red">' + price + '</p>';
//        var img1='<img src="/images/f10000.gif" />';
//        var img2='<img src="/images/f5000.gif" />';
//        var img3='<img src="/images/f4000.gif" />';
//        var img4='<img src="/images/f3000.gif" />';
        var gamecode = $j("#ddlGame option:selected").val();
//        if(gamecode=="013")
//            {
//               
//              
//                    if(name=="100000Gold")
//                    {
//                        li +='<p>'+img1+'</p>';
//                    }
//                    if(name=="50000 Gold")
//                    {
//                        li +='<p>'+img2+'</p>';
//                    }
//                    if(name=="40000 Gold")
//                    {
//                        li +='<p>'+img3+'</p>';
//                    }
//                    if(name=="30000 Gold")
//                    {
//                        li +='<p>'+img4+'</p>';
//                    }
//               
//            }
        
        
		 li +='</li>';
    }

    $j('div[class="gold_box"]').show();
    if (isSlide) {
        $j('#ul_gold').hide();
        document.getElementById('ul_gold').innerHTML = li;
        $j('#ul_gold').show(150);
    } else {
        document.getElementById('ul_gold').innerHTML = li;
    }

    var productIndex = ddlProduct.selectedIndex;
    //   alert(productIndex+"51515");
    if (productIndex != 0) {
        $j('#ul_gold > li').each(function(i) {
            if (i == productIndex - 1) {
                $j(this).css('background', '#FFF386');
            } else {
                $j(this).css('background', '');
            }
        });
    }
}



//设置多货币
function setMoneyType(mType, isBind) {
    moneyType.data = mType;
    var sp_priceId = 'sp_' + mType;
    var a_moneyTypeId = 'a_' + mType;
    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('ddlProduct');
    var priceList = ddlProduct.value.split('^');
    if (priceList.length < 3) {
        document.getElementById('sp_USD').innerHTML = '0.00';
        document.getElementById('sp_EUR').innerHTML = '0.00';
        document.getElementById('sp_GBP').innerHTML = '0.00';
    }
    else {
        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.getData();
    document.getElementById('hidden_productName').value = ddlProduct.options[ddlProduct.selectedIndex].text;

    document.getElementById('sp_moneyType2').innerHTML = moneyType.getData();
    if (isBind) { initProductTable(productList.getData(), false); }
}


//选择产品
function clickGoldTable(index) {
    var ddlProduct = document.getElementById("ddlProduct");
    ddlProduct.selectedIndex = index;
    setMoneyType(moneyType.getData(), false);
    $j('#ul_gold > li').each(function(i) {
        if (i + 1 == index) {
            $j(this).css('background', '#FFF386');
        } else {
            $j(this).css('background', '');
        }
    });
}


//产品选择改变
function loadPrice() {
    setMoneyType(moneyType.getData());
    var productIndex = document.getElementById("ddlProduct").selectedIndex;
    if (productIndex != 0) {
        $j('#ul_gold > li').each(function(i) {
            if (i == productIndex - 1) {
                $j(this).css('background', '#FFF386');
            } else {
                $j(this).css('background', '');
            }
        });
    }
}


//
function getMoneyType(moneyType) {
    setMoneyType(moneyType, true);
}


//库存
function getServerStorage(gameServerCode) {
    if (gameServerCode == '-1') {
        return;
    }

    var gmCode = gameServerCode.substr(0, 3);
    if (gmCode != '013'&& gmCode!='019') {
        return;
    }
    $j('#tip').show();

    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=serverStorage&serverCode=' + gameServerCode,
        dataType: 'html',
        success: function(data) {
            //alert(data);
            var goldSell = parseInt(data.split('|')[1]);
            var storage = parseInt(data.split('|')[0]);
            document.getElementById('goldSell').innerHTML = goldSell;
            if (parseInt(storage) > 120000) {
                document.getElementById('storage').innerHTML = '120000+';
            } else {
                document.getElementById('storage').innerHTML = storage;
            }
            var count = Math.round((parseFloat(storage) / 11700));
            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);
                      //$j('#' + id).attr('style', 'width:'+count*100+'px;');
                   i++;
                } else {
                    window.clearInterval(interValId);
                }
            }, 100);
        }
    });
}





//格式化数据
function formatDecimal(src, pos) { return Math.round(src * Math.pow(10, pos)) / Math.pow(10, pos); }

function formatFloat(src, pos) { return Math.round(src * Math.pow(10, pos)) / Math.pow(10, pos); }


function showLeftComment(gamecode) {
    if (gamecode == "013" || gamecode == "019") {
        $j('#websm_aion').hide();
        $j('#websm_eve').hide();
        $j('#websm_ffxi').hide();
        $j('#websm_wow').show();
    }
    else if (gamecode == "083" || gamecode == "087") {
        $j('#websm_wow').hide();
        $j('#websm_eve').hide();
        $j('#websm_ffxi').hide();
        $j('#websm_aion').show();
    }
    else if (gamecode == "003") {
        $j('#websm_wow').hide();
        $j('#websm_aion').hide();
        $j('#websm_ffxi').hide();
        $j('#websm_eve').show();
    }
    else if (gamecode == "001") {
        $j('#websm_aion').hide();
        $j('#websm_eve').hide();
        $j('#websm_wow').hide();
        $j('#websm_ffxi').show();
    }
    else {
        $j('#websm_aion').hide();
        $j('#websm_eve').hide();
        $j('#websm_ffxi').hide();
        $j('#websm_wow').show();
    }
}

function DIVShow(i) {
    if (document.getElementById('zxCheckBox').checked) {
        document.getElementById(i).style.display = 'block';
    } else {
        document.getElementById(i).style.display = 'none';
    }
}

function GetCustomerGoldPrice(amount) {
    var gameCode = document.getElementById('ddlGame').value;
    
    if (!document.getElementById('zxCheckBox').checked) {
        return;
    }
    try {
        if (amount == '') {
            $j("#txtPrice2").val('');
            return;
        }
        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('ddlServer').value;
    var moneyType = document.getElementById('sp_moneyType2').innerHTML;
    if (gameServerCode == '-1') {
        alert('Please choose your server');
        return;
    }
    if (amount == lastAmount && moneyType == lastMoneyType) {
        return;
    }
    lastAmount = amount;
    lastMoneyType = moneyType;

    $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 + '&moneyType=' + moneyType,
        dataType: 'html',
        success: function(data) {
            $j('#div_wait_gold2').hide();
            document.getElementById('txtPrice2').value = data;
        }
    });
}

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('ddlGame').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;
}


function canSubmitForm() {
    if (!document.getElementById('zxCheckBox').checked) {
        document.getElementById('txtAmount').value = '0';
    }
    if ($j('#ddlGame').val() == '-1') {
        alert('Please choose your game');
        return false;
    }
    if ($j('#ddlServer').val() == '-1') {
        alert('Please choose your server');
        return false;
    }
    if (document.getElementById('zxCheckBox').checked) {
        if (isNaN($j('#txtAmount').val())) {
            $j('#txtAmount').val('0');
        }
        if (parseFloat($j('#txtAmount').val()) <= 0) {
            alert('Please choose your product');
            return false;
        }
    }
    if (!document.getElementById('zxCheckBox').checked && $j('#ddlProduct').val() == '-1') {
        alert('Please choose your product');
        return false;
    }
    return checkForm();
}

function setCustomProduct(gameCode) {
    $j("#zixuan,#zx").hide();
    $j("#zxCheckBox").attr('checked', false);
    $j("#txtAmount,#txtPrice2").val('');
    if (gameCode && (gameCode == '083' || gameCode == '087')) {
        $j("#zixuan").show();
    }
}

function isNoBlank(str) {
    var notBlank = true;
    if (str == undefined || str == null) {
        notBlank = false;
    }
    else {
        var reg = new RegExp(/\S+/);
        notBlank = reg.test(str);
    }

    return notBlank;
}
    
    
    
