﻿// JScript 文件
function LoadGame() {
    try {
        AjaxMethod.GetGamelist(LoadGame_CallBack);
    }
    catch (e) {
    }
}
function LoadGame_CallBack(response) {
    try {
        if (response.error != null) {
            //alert(response.error);
            return;
        }
        var GameTable = response.value;
        var dic_Game = document.getElementById("dic_GameList");
        if (GameTable != null) {
            dic_Game.length = 0;
            if (GameTable != null && typeof (GameTable) == "object") {
                dic_Game.options.add(new Option("Choose Game", 0));
                for (var i = 0; i < parseInt(GameTable.Rows.length); i++) {
                    var GameName = GameTable.Rows[i].GameName_en;
                    var GameID = GameTable.Rows[i].GameId;
                    dic_Game.options.add(new Option(GameName, GameID));
                }
            }
            for (var i = 0; i < dic_Game.length; i++) {
                if (dic_Game.options[i].value == "1") {
                    dic_Game.selectedIndex = i;
                }
            }
        }
        gameChange();
        LoadServerList();
        return;
    }
    catch (e) {
    }
}

function gameChange() {
    try {
        var dic_Game = document.getElementById("dic_GameList");
        var GameID = dic_Game.options[dic_Game.selectedIndex].value;
        AjaxMethod.GetAmountList(GameID, gameChange_CallBack);
    }
    catch (e) {
    }
}

function gameChange_CallBack(response) {
    try {
        if (response.error != null) {
            //alert(response.error);
            return;
        }
        var dic_Game = document.getElementById("dic_GameList");
        var GameID = dic_Game.options[dic_Game.selectedIndex].value;
        var AmountTable = response.value;
        var dic_Amount = document.getElementById("dic_Amount");
        if (GameID != 0) {
            if (AmountTable != null && typeof (AmountTable) == "object") {
                dic_Amount.disabled = false;
                dic_Amount.length = 0;
                dic_Amount.options.add(new Option("Choose Amount", 0));
                //数量赠送金币百分比活动20100623--START----------------
                var Num; var NumText; var ID;
                if (GameID == 1) {
                    for (var i = 0; i < AmountTable.Rows.length; i++) {
                        Num = AmountTable.Rows[i].NUM;
                        if (MarketingQuick(GameID, Num) != "") {
                            NumText = Num + "+" + MarketingQuick(GameID, Num);
                        } else { NumText = Num; }
                        ID = AmountTable.Rows[i].NUM;
                        dic_Amount.options.add(new Option(NumText, ID));
                    }
                }
                else {
                    for (var i = 0; i < AmountTable.Rows.length; i++) {
                        Num = AmountTable.Rows[i].NUM;
                        ID = AmountTable.Rows[i].NUM;
                        dic_Amount.options.add(new Option(Num, ID));
                    }
                }
                //数量赠送金币百分比活动20100623---END----------------
            }
            //        for(var i = 0;i < dic_Amount.length;i++)
            //        {
            //            if(GameID == 1 && dic_Amount.options[i].value == "20000")
            //            {
            //                dic_Amount.selectedIndex = i;
            //                document.getElementById("Amount").value = "20000";//给文本框Amount赋值
            //            }
            //        }
        }
        else {
            dic_Amount.disabled = true;
        }
        LoadServerList();
    }
    catch (e) {
    }
}

function LoadServerList() {
    try {
        var dic_Game = document.getElementById("dic_GameList");
        AjaxMethod.GetServerList(dic_Game.options[dic_Game.selectedIndex].value, LoadServerList_CallBack);
    }
    catch (e) {
    }
}

function LoadServerList_CallBack(response) {
    try {
        var dic_Game = document.getElementById("dic_GameList");
        var GameID = dic_Game.options[dic_Game.selectedIndex].value;
        var ServerTable = response.value;
        var dic_ServerList = document.getElementById("dic_ServerList");
        if (GameID != 0) {
            dic_ServerList.disabled = false;
            dic_ServerList.length = 0;
            if (ServerTable != null && typeof (ServerTable) == "object") {
                dic_ServerList.options.add(new Option("Choose Server", 0));
                for (var i = 0; i < parseInt(ServerTable.Rows.length); i++) {
                    var name = ServerTable.Rows[i].fullservername;
                    var id = ServerTable.Rows[i].ServerID;
                    dic_ServerList.options.add(new Option(name, id));
                }
            }
        }
        else {
            dic_ServerList.disabled = true;
        }
        return
    }
    catch (e) {
    }
}


function amountChange() {
    try {
        var dic_Amount = document.getElementById("dic_Amount");
        var Amount = dic_Amount.options[dic_Amount.selectedIndex].value;
        document.getElementById("Amount").value = dic_Amount.options[dic_Amount.selectedIndex].value; //给文本框Amount赋值
        currencyChange();
    }
    catch (e) {
    }
}

function serverChange() {
    try {
        var dic_Currency = document.getElementById("dic_Currency");
        var dic_ServerList = document.getElementById("dic_ServerList");
        var ServerID = dic_ServerList.options[dic_ServerList.selectedIndex].value;
        document.getElementById("ServerID").value = dic_ServerList.options[dic_ServerList.selectedIndex].value; //给文本框ServerID赋值
        if (ServerID == 0) {
            dic_Currency.disabled = true;
        }
        else {
            dic_Currency.disabled = false;
        }
        currencyChange();
    }
    catch (e) {

    }
}
//换算价格
function currencyChange() {
    try {
        var dic_ServerList = document.getElementById("dic_ServerList");
        var ServerID = dic_ServerList.options[dic_ServerList.selectedIndex].value;
        var dic_Amount = document.getElementById("dic_Amount");
        var Amount = dic_Amount.options[dic_Amount.selectedIndex].value;
        var dic_Currency = document.getElementById("dic_Currency");
        var Currency = dic_Currency.options[dic_Currency.selectedIndex].value;
        document.getElementById("CurrencySign").value = Currency;
        dic_Currency.disabled = false;
        AjaxMethod.GetPrice(ServerID, Amount, Currency, currencyChange_CallBack);
    }
    catch (e) {

    }
}

function currencyChange_CallBack(response) {
    try {
        var btn_buy = document.getElementById("ImageButton2");
        var price = response.value;
        var lbb = document.getElementById("txt_Price");
        var dic_Currency = document.getElementById("dic_Currency");
        var Currency = dic_Currency.options[dic_Currency.selectedIndex].value;
        if (price != null && Currency != '0') {
            btn_buy.disabled = false;
            lbb.value = price;
            document.getElementById("Price").value = price;
            var game = document.getElementById("dic_GameList");
            var servername = document.getElementById("dic_ServerList");
            document.getElementById("ProductName").value = game.options[game.selectedIndex].innerHTML + "-" + servername.options[servername.selectedIndex].innerHTML;   //ProductName
        }
        else {
            btn_buy.disabled = true;
            lbb.value = '';
        }
        return;
    }
    catch (e) {
    }
}

function Left_CheckPostData() {
    var dic_ServerList = document.getElementById("dic_ServerList");
    var ServerID = dic_ServerList.options[dic_ServerList.selectedIndex].value;
    var dic_Amount = document.getElementById("dic_Amount");
    var Amount = dic_Amount.options[dic_Amount.selectedIndex].value;
    var dic_Currency = document.getElementById("dic_Currency");
    var Currency = dic_Currency.options[dic_Currency.selectedIndex].value;
    if (ServerID == 0) {
        alert("Choose Server");
        return false;
    }
    if (Amount == 0) {
        alert("Please Choose Amount");
        return false;
    }
    if (Currency == 0) {
        alert("Please Choose Currency");
        return false;
    }
}
function MarketingQuick(gameID, amount) {
    /// <summary>
    /// 数量赠送金币百分比活动20100623
    /// </summary>
    var marketDiscount = 0;
    if (10000 <= amount && amount <= 25000) {
        marketDiscount = 0.1;
        return (marketDiscount * 100) + "% free";
    }
    else if (30000 <= amount && amount <= 100000) {
        marketDiscount = 0.15;
        return (marketDiscount * 100) + "% free";
    }
    else {
        marketDiscount = 0;
        return "";
    }
}
