﻿/// <reference path="common.js" />
/// <reference path="xml2json.js" />

$().ready(function() {
    $("#sel_currency").change(function() {
        Site.lastserver = $("#sel_server").val();
        Site.lastquantity = $("#sel_quantity").val();
        if ($("#sel_server").attr("value") != "") {
            $("#sel_server").change();
        }
    });
    $("#sel_server").change(function() {

        if ($("#sel_server").attr("value") == "") {

            $("#sel_quantity").empty().append(String.format("<option value=\"{0}\">{1}</option>", "", "Select your server first"));
            $("#lbl_total").html("");
            return;
        }
        mask($("#sel_quantity"));
        Site.getgoldlist($("#sel_game").val(), $("#sel_server>option:selected").text(), $("#sel_currency").val(), function(data) {
            mask($("#sel_quantity"));
            var list = $.xml2json(data).Table;
            if (list) {
                window.goldlist = list;
                bindgold(list);
            }
            $("#sel_quantity").change();
        });
    });
    $("#sel_game").change(function() {
        mask($("#sel_server"));
        Site.getservers($("#sel_game").val(), function(data) {
            mask($("#sel_server"));
            var arr = [];
            arr.push(String.format("<option value=\"{0}\">{1}</option>", "", "---Select Your Server Please---"));
            for (var index = 0, len = data.length; index < len; index++) {
                var obj = data[index];
                arr.push(String.format("<option value=\"{0}\" style=\"{2}\" {3}>{1}</option>", obj.twgoldsellid, obj.server, parseInt(obj.lowpriceflag) ? "color:#f00;" : "", Site.lastserver > 0 ? (Site.lastserver == obj.twgoldsellid ? 'selected' : '') : ''));
            }
            $("#sel_server").empty().append(arr.join(''));
            Site.lastserver = "";
            if ($("#sel_server").attr("value") != "") {
                $("#sel_server").change();
            }
        });
    });

    $("#sel_quantity").change(function() {
        $("#lbl_total").text(Number($("#sel_quantity>option:selected").attr("title")).toFixed(2));
        $("#lbl_currency").text($("#sel_currency>option:selected").text());
        if ($("#tb_code").length && $("#tb_code").val().trim().length) {
            checkCode();
        }
    });
    if ($("#hdn_game").length && $("#sel_game").length && $("#hdn_game").val().length) {
        $(String.format("#sel_game>option[value='{0}']", $("#hdn_game").val())).attr("selected", true);
    }
    if ($("#hdn_currency").length && $("#sel_currency").length && $("#hdn_currency").val().length) {
        $("#sel_currency>option").each(function(index, el) {
            if ($(el).text() == $("#hdn_currency").val()) {
                $(el).attr("selected", true);
            }
        });
        //$(String.format("#sel_currency>option[value='{0}']", $("#hdn_currency").val())).attr("selected", true);
    }
    init();
    $("#sel_game").change();
});

var init = function() {
    //初始化游戏和货币

    if ($("#hdn_server").length && $("#hdn_server").val().trim().length && $("#sel_server").length) {
        Site.lastserver = $("#hdn_server").val().trim();
        //$(String.format("#sel_server>option[value='{0}']", $("#hdn_server").val().trim())).attr("selected", true);
    }
    if ($("#hdn_quantity").length && $("#hdn_quantity").val().trim().length && $("#sel_quantity").length) {
        Site.lastquantity = $("#hdn_quantity").val().trim();
        //$(String.format("#sel_quantity>option[value='{0}']", $("#hdn_quantity").val().trim())).attr("selected", true);
    }
};

var quick = function() {
    if (validate("div_quickinof")) {
        var config = new Object();
        config["p_currency"] = $("#sel_currency>option:selected").text();
        config["p_server"] = $("#sel_server>option:selected").val();
        config["p_quantity"] = $("#sel_quantity>option:selected").val();
        createForm(config, $("#sel_game>option:selected").val() == "euaion" ? "wow_gold_eu.aspx" : "wow_gold_us.aspx");
    }
};

var bindgold = function(list, code) {
    var unitname = "M Kinah";
    if ($("#gold_unitname").length) unitname = $("#gold_unitname").val();
    var arr = [];
    for (var index = list.length - 1; index >= 0; index--) {
        var obj = list[index];
        if (obj.unit > 0) {
            var givenum = Math.round(obj.givenum);
            var total = 0;
            var temp = obj.baseprice / obj.baseunit;
            temp = temp * obj.unit;
            temp = temp * obj.pricerate;
            temp = Math.round(temp);
            temp = Number(temp);
            total = Number(temp) + Number(obj.pricerest);
            var app = 0;
            if (code) {
                var flag = true;
                if (code.goldlimited > 0) {
                    if (code.goldlimited > obj.unit) flag = false;
                }
                if (code.moneylimited > 0) {
                    if (code.moneylimited > total) flag = false;
                }
                if (flag) {
                    if (code.rate > 0) {
                        app = Math.round(obj.unit * code.rate * 0.01);
                    }
                    var strTemp = total;
                    if (code.discount < 100) {
                        strTemp += " - " + (total * (1 - code.discount * 0.01)).toFixed(2);
                    }
                    if (code.money > 0) {
                        strTemp += " - " + code.money;
                    }
                    if (strTemp != total) {
                        total = eval(strTemp) + "(" + strTemp + ")";
                    }
                }
            }
            arr.push(String.format("<option title=\"{1}\" value=\"{0}\" {5}>{0}{2}{3} {4}</option>", obj.unit, total, obj.givenum > 0 ? " + " + Math.round(obj.givenum) : "", app > 0 ? " + " + app : "", unitname, (Site.lastquantity > 0 ? (Site.lastquantity == obj.unit ? 'selected' : '') : '')));
        }
    }
    $("#sel_quantity").empty().append(arr.join(''));
    Site.lastquantity = 0;
};