﻿//初始化
var specialServer = null;
var showStr = false;
var Site = new Object();
Site.lastquantity = 0;
$().ready(function () {
    _gold = new goldclass();
    //
    $("#sel_alpha").find("option").each(function (i) {
        if (i == 0) {
            $(this).attr("selected", true);
        }
    });    
});

//
var _gold = null;
var AlphaList = new Array();
var _ServerList = new Array();
function CheckHasAlphaList(alpha) {
    for(var i=0;i<AlphaList.length;i++) {
        if (AlphaList[i] == alpha) {
            return true;
        }       
    }
}
//
var goldclass = function () {
    return {
        changeGame: function () {
            //隐藏特价区服务器选择
            if ($("#sel_game").val() == 1) {
                $("#fs_sepecial").show();
            }
            else {
                $("#fs_sepecial").hide();
            }
            //加载服务器索引
            AlphaList.length = 0;
            $.getJSON("ashx/server.ashx", { "action": "alpha", "zone": 0, "game": $("#sel_game").val(), "random": Math.random() }, function (data) {
                $("#sel_alpha").empty().append("<option value=\"\">*</option>");
                _ServerList = data;
                if (data.length) {
                    for (var index in data) {
                        var server = $.trim(data[index].ServerName);
                        var alpha = server.substring(0, 1);
                        if (!CheckHasAlphaList(alpha)) {
                            $("#sel_alpha").append(String.format("<option value=\"{0}\" {1}>{0}</option>", alpha, $("#hdnAlpha").val() == alpha ? "selected" : ""));
                            AlphaList.push(alpha);
                        }
                    }
                    //
                    //Site.lastquantity = Site.lastquantity = $("#sel_game option:selected").attr("dq");
                    //
                    $("#sel_alpha").change();
                    //
                    changeCUR("cur_" + $("#sel_game option:selected").attr("cid"));
                }
            });
        },
        changeAlpha: function () {
            //加载服务器
            $("#sel_server").empty();
            if ($("#sel_alpha").val() == "") {
                $("#sel_server").append("<option value=\"\">-Choose Server-</option>");
                $("#sel_quantity").empty().append("<option q=\"0\" p=\"0\" value=\"\">-Choose Product-</option>").change();
                $('#div_goldTable').html("");
                return;
            };
            if (_ServerList.length > 0) {
                if ($("#sel_alpha").val() != "") {
                    $("#sel_server").append("<option value=\"\">-Choose Server-</option>");
                    for (var index = 0; index < _ServerList.length; index++) {
                        if ($("#sel_alpha").val() == _ServerList[index].ServerName.substring(0, 1)) {
                            if (specialServer != null) {
                                if (specialServer == _ServerList[index].ServerName)
                                    $("#sel_server").append(String.format("<option value='{1}' {2}>{0}</option>", _ServerList[index].ServerName, _ServerList[index].ID, "selected"));
                                else
                                    $("#sel_server").append(String.format("<option value='{1}' >{0}</option>", _ServerList[index].ServerName, _ServerList[index].ID));
                            } else
                                $("#sel_server").append(String.format("<option value='{1}' {2}>{0}</option>", _ServerList[index].ServerName, _ServerList[index].ID, $("#hdnServerID").val() == _ServerList[index].ID ? "selected" : ""));

                        }
                    }
                    //清空
                    specialServer = null;
                    //
                    $("#sel_server").change();
                }
            }
        },
        changeServer: function () {
            //加载金币段
            if ($("#sel_server").val() == "") {
                $("#sel_quantity").empty().append("<option q=\"0\" p=\"0\" value=\"\">-Choose Product-</option>").change();
                $('#div_goldTable').html("");
                return;
            };
            //
            var CurrencyID = "$";
            if ($(".sel_currency").length) {
                CurrencyID = $(".sel_currency").attr("d");
            }
            if ($("div.quick_order").length) {
                CurrencyID = $("#sel_game option:selected").attr("cid")
            }
            //设置价格
            $.getJSON("ashx/gold.ashx", { "action": "list", "gid": $("#sel_game").val(), "server": $("#sel_server option:selected").val(), "cid": CurrencyID, "random": Math.random() }, function (_datasource) {
                _datasource = eval(_datasource);
                if (_datasource.length) {
                    var Unit = $("#sel_game option:selected").attr("unit");
                    var CurrencyName = "$";
                    if ($(".sel_currency").length) {
                        CurrencyName = $(".sel_currency").attr("s");
                    }
                    if ($("div.quick_order").length) {
                        CurrencyName = $("#sel_game option:selected").attr("cur")
                    }
                    //    
                    if (Site.lastquantity <= 0)
                        Site.lastquantity = $("#sel_game option:selected").attr("dq");
                    if ($("#hdnLastQuantity").length) {
                        if ($("#hdnLastQuantity").val() > 0) {
                            Site.lastquantity = $("#hdnLastQuantity").val();
                        }
                    }                    
                    //
                    var strlist = "";
                    //下拉金币段数据
                    var AppendCashRate = 0;
                    for (var index in _datasource) {
                        var row = _datasource[index];
                        if (row.GoldQuantity) {
                            //              
                            AppendCashRate = row.AppendCashRate;
                            var SalePrice = row.SalePrice - (row.SalePrice * row.AppendCashRate);
                            var BasePrice = row.BasePrice - (row.BasePrice * row.AppendCashRate);                            
                            //
                            strlist += String.format("<option value='{1}' p={2} b={6} q={0} m={7} {5}>{0} {3} {4}</option>", Number(row.GoldQuantity), row.ID, row.Moreantissa > 0 ? getDecimal(parseInt(getInt(SalePrice)) + parseFloat(row.Moreantissa), 2) : getDecimal(SalePrice, 2), Unit, Number(row.AppendQuantity) > 0 ? " + " + Number(row.AppendQuantity) + " " + Unit : "", (Site.lastquantity == row.GoldQuantity ? 'selected' : ''), BasePrice, row.Moreantissa);
                        }
                    }
                    //会员价格标识
                    if ($("#username").html() != "" && AppendCashRate > 0) {
                        $("td#pricename").html("<em>Member Price:</em>");
                    }
                    //列表金币段数据
                    if ($("#div_goldTable").length) {
                        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=\"left\"> Quantity </td><td bgcolor=\"#F4F4F4\" align=\"center\" colspan=\"2\">" + (($("#username").html() != "" && AppendCashRate > 0) ? "<em>Member Price</em>" : "Price") + "</td></tr>";
                        var tblBody = "";
                        var tblFoot = "</table>";
                        //
                        for (var index in _datasource) {
                            var row = _datasource[index];
                            if (row.GoldQuantity) {
                                var SalePrice = row.SalePrice - (row.SalePrice * row.AppendCashRate);
                                if ((index % 2) == 0) {
                                    tblBody += String.format("<tr align=\"left\" onclick=\"clickGoldTable('" + index + "');\" style=\"cursor:pointer\" onmouseover=\"this.style.backgroundColor='#cecece'\" onmouseout=\"this.style.backgroundColor=''\" title=\"Click to select product!\"><td><span id=\"tdQuantity" + index + "\" title=\"{0}\">{0} {3}</span></td><td><span class=\"delete\">{6} {7}</span></td><td><span id=\"tdPrice" + index + "\" class=\"red\">{6} {8}</span></td></tr>", Number(row.GoldQuantity), row.ID, SalePrice, Unit, Number(row.AppendQuantity) > 0 ? " + " + Number(row.AppendQuantity) + " " + Unit : "", "", CurrencyName, getDecimal(SalePrice * (130 / 100), 2), row.Moreantissa > 0 ? getDecimal(parseInt(getInt(SalePrice)) + parseFloat(row.Moreantissa), 2) : getDecimal(SalePrice, 2));
                                } else {
                                    tblBody += String.format("<tr align=\"left\" bgcolor='#F4F4F4' onclick=\"clickGoldTable('" + index + "');\" style=\"cursor:pointer\" onmouseover=\"this.style.backgroundColor='#cecece'\" onmouseout=\"this.style.backgroundColor=''\" title=\"Click to select product!\"><td><span id=\"tdQuantity" + index + "\" title=\"{0}\">{0} {3}</span></td><td><span class=\"delete\">{6} {7}</span></td><td><span id=\"tdPrice" + index + "\" class=\"red\">{6} {8}</span></td></tr>", Number(row.GoldQuantity), row.ID, SalePrice, Unit, Number(row.AppendQuantity) ? " + " + Number(row.AppendQuantity) + " " + Unit : "", "", CurrencyName, getDecimal(SalePrice * (130 / 100), 2), row.Moreantissa > 0 ? getDecimal(parseInt(getInt(SalePrice)) + parseFloat(row.Moreantissa), 2) : getDecimal(SalePrice, 2));
                                }
                            }
                        }
                        //
                        $('#div_goldTable').html(tblHead + tblBody + tblFoot);
                        $('#div_goldTable').show(1000);
                    }
                    //
                    $("#sel_quantity").empty().append(strlist).change();
                }
                else {
                    $("#sel_quantity").empty().append("<option q=\"0\" p=\"0\" value=\"\">-Choose Product-</option>").change();
                }
            });
        },
        changeQutity: function () {
            Site.lastquantity = $("#sel_quantity option:selected").attr("q");
            //货币类型处理
            var CurrencyName = "$";
            if ($(".sel_currency").length) {
                CurrencyName = $(".sel_currency").attr("s");
            }
            if ($("div.quick_order").length) {
                CurrencyName = $("#sel_game option:selected").attr("cur")
            }
            //折扣码处理
            if ($("#tb_discount").length) {
                if ($("#sel_quantity option:selected").val() != "") {
                    checkCode();
                }
                else {
                    if ($("#lbl_price").attr("title")) {
                        var price = $("#lbl_price").attr("title");
                        $("#lbl_price").html(CurrencyName + " " + price);
                    }
                    $("#li_discount").empty().html("");
                }
            }
            //
            if ($("#sel_quantity option:selected").val() != "") {
                var price = getDecimal($("#sel_quantity option:selected").attr("p"), 2);
                //市场价格处理
                if ($("#lbl_oldprice").length) {
                    $("#lbl_oldprice").html(CurrencyName + " " + getDecimal(price * (130 / 100), 2));
                }
                //价格处理
                $("#lbl_price").html(CurrencyName + " " + price);
                $("#lbl_price").attr("title", price);
            }
            else {
                if ($("#lbl_oldprice").length) {
                    $("#lbl_oldprice").html(CurrencyName + " 0");
                }
                $("#lbl_price").html(CurrencyName + " 0");
            }
        }
    };
};

//
var buyGold = function (refer) {
    //金币下单开始
    if ($("#sel_quantity option:selected").val() == "") {
        alert("Please choose product!");
        return;
    }
    if (!validate("gold_panel")) return false;
    //收集下单信息
    var product = new Object();
    product["ID"] = $("#sel_quantity option:selected").val();
    product["ProductType"] = 1;
    product["Discount"] = "";
    product["GameID"] = $("#sel_game option:selected").val();
    product["GameName"] = $("#sel_game option:selected").text();
    product["ServerName"] = escape($("#sel_server option:selected").text());
    product["ServerID"] = $("#sel_server option:selected").val();
    product["Currency"] = $(".sel_currency").attr("s");
    product["CurrencyID"] = $(".sel_currency").attr("d");
    product["Rate"] = $(".sel_currency").attr("r");
    product["Gold"] = $("#sel_quantity option:selected").attr("q");
    product["Name"] = $("#sel_quantity option:selected").attr("q") + " Gold";
    product["Quantity"] = 1;
    product["SalePrice"] = $("#sel_quantity option:selected").attr("p");
    if ($("#tb_discount").val() != "" && parseFloat($("#td_discount").attr("title")) > 0) {
        product["Moreantissa"] = 0;
    }
    else {
        product["Moreantissa"] = $("#sel_quantity option:selected").attr("m");
    }
    product["RelateID"] = 0;
    product["Subscribe"] = $("#chkSubscribe").attr("checked") ? true : false;
    CartInfo.Confirm(product, refer);
};

//折扣码处理
var checkCode = function () {
    var baseprice = getDecimal($("#sel_quantity option:selected").attr("p"), 2);
    //
    if ($("#tb_discount").val() != "" && $("#sel_quantity option:selected").val() == "") {
        alert("Please choose product!");
        $("#li_discount").empty().html("");
        $("#tb_discount").val("");
        return;
    }
    if ($("#tb_discount").val() == "" && $("#sel_quantity option:selected").val() == "") return;
    //
    var info = new Object();
    info.GameID = $("#sel_game option:selected").val();
    info.ProductType = 1;
    info.Gold = getInt($("#sel_quantity option:selected").attr("q"));
    info.SalePrice = baseprice;
    if (!$("#tb_discount").length) $("#li_discount").html("");
    applycode($("#tb_discount").val(), info, function (obj, info) {
        if (obj) {
            if (obj.Invalid == 0) {
                //
                var arr = [];
                arr.push(String.format("<table class=\"tab_discount\"><tr><td class=\"l2\">Bonus Gold:</td><td class=\"r2\">{0}</td><td class=\"l2\">Discount:</td><td id=\"td_discount\" class=\"r2\" title=\"{1}\">{2} {1}</td></tr>", obj.Gold, obj.Price, $(".sel_currency").attr("s")));
                arr.push(String.format("<tr><td class=\"l2\">Bonus Gift:</td><td class=\"r2\">{0}</td><td class=\"l2\">Loyalty Points:</td><td class=\"r2\">{1}</td></tr></table>", obj.Item || "none", obj.Point));
                $("#li_discount").empty().html(arr.join(''));
                //
                if (obj.Price > 0) {
                    var nowprice = info.SalePrice - parseFloat(obj.Price);
                    nowprice = getDecimal(nowprice, 2);
                    $("#lbl_price").html(info.SalePrice + "-" + obj.Price + " = " + $(".sel_currency").attr("s") + " " + nowprice);
                    $("#lbl_price").attr("nowprice", nowprice);
                }
            }
            else {
                if ($("#lbl_price").attr("title")) {
                    var price = getDecimal($("#sel_quantity option:selected").attr("p"), 2);
                    $("#lbl_price").html($(".sel_currency").attr("s") + " " + price);
                    $("#lbl_price").attr("nowprice", price);
                }
                $("#li_discount").empty().html("");
                $("#tb_discount").val("");
            }
        }
    });
};

//
var changeCUR = function (id) {
    var curid = 0;
    var currate = 0;
    var curname = "$";
    //
    if ($(".btn_currency").length) {
        $(".sel_currency").removeClass("sel_currency");
        $("#" + id).addClass("sel_currency");
    }
    if ($(".sel_currency").length) {
        currate = $(".sel_currency").attr("r");
        curid = $(".sel_currency").attr("d");
        curname = $(".sel_currency").attr("s");
    }
    //
    $.post("ashx/cart.ashx", { "action": "changecur", "cid": curid, cur: curname, 'random': Math.random() }, function (data) {
        if ($(".changeprice[oldprice]").length) {
            $(".changeprice[oldprice]").each(function (idx, obj) {
                var price = getDecimal($(obj).attr("oldprice") * currate, 2);
                $(obj).html(curname + " " + price);
            });
        }
        //                
        $("#sel_server").change();
    });
}
//
