﻿
function clearNextContorl(elm) {
    var quickGold = jQuery(".table_quickbuy [index]");
    if (jQuery(elm).attr("index")) {
        var index = parseInt(jQuery(elm).attr("index"));
        quickGold.each(function() {
            if (jQuery(this).attr("index") && parseInt(jQuery(this).attr("index")) > index) {
                if (jQuery(this).is("select")) {
                    jQuery(this).children("option:first").attr("selected", "selected");
                    if (jQuery(this).is("[id=ddlServerKey]") == false) {
                        jQuery(this).children("option:gt(0)").remove();
                        if (index == parseInt(jQuery(this).attr("index")) - 1) {
                            jQuery(this).children("option:first").text("--Loading--").attr('value', '-1');
                        }
                        else {
                            jQuery(this).children("option:first").text("--Choose--").attr('value', '-1');
                        }
                    }
                }
                if (jQuery(this).is("input:text")) {
                    jQuery(this).val("0.00");
                }
            }

        });
    }
}

function GetSelectedValue(elmId) {
    var val = jQuery("#" + elmId).children(":selected").val();
    if (val == undefined) {
        return null;
    }
    else {
        return val;
    }
}

function ddlGame_Changed() {
    clearNextContorl(this);
    initServerKey(GetSelectedValue("ddlGame"));
}

function ddlServerKey_Changed() {
    clearNextContorl(this);
    initddlServer(GetSelectedValue("ddlGame"), GetSelectedValue("ddlServerKey"));
}

function ddlServer_Changed() {
    clearNextContorl(this);
    initddlProduct(GetSelectedValue("ddlServer"));
}

function ddlProduct_Changed() {
    clearNextContorl(this);
    inittxtPrice();
}

function initServerKey(gameCode) {
    if (gameCode == '013' || gameCode == '019') {
        jQuery("#ddlServerKey").show();
        if (jQuery("#ddlServerKey").next().is("br") == false) {
            jQuery("#ddlServerKey").after("<br/>");
        }
    }
    else {
        jQuery("#ddlServerKey").hide();
        jQuery("#ddlServerKey+br").remove();
    }
    jQuery("#ddlServerKey option:first").attr("selected", "selected");
    initddlServer(gameCode, GetSelectedValue("ddlServerKey"));

}

function initddlServer(gameCode, key) {
    if (gameCode != null && gameCode != undefined && key != null && key != undefined && gameCode != '-1') {
        jQuery.ajax({
            url: 'Ajax.aspx',
            type: 'get',
            data: 'methodName=LoadServerItems&para=' + gameCode + '&key=' + key + '&date=' + new Date(),
            dataType: 'json',
            error: function() { alert("Error!"); },
            success: function(json) {
                if (json != null && json.length > 0) {
                    var name, code, option;
                    for (var i = 0; i < json.length; i++) {
                        name = json[i].ServerNameOther;
                        code = json[i].Code;
                        option = "<option value=" + code + ">" + name + "</option>";
                        jQuery("#ddlServer").append(option);
                    }
                    jQuery("#ddlServer option:first").text("--Choose--");
                }
            }
        });
    }
}

function initddlProduct(serverCode) {
    if (serverCode != null && serverCode != undefined && serverCode != '-1') {
        jQuery.ajax({
            url: 'Ajax.aspx',
            type: 'get',
            data: 'methodName=LoadProductItems&para=' + serverCode + '&mulit=false&date=' + new Date(),
            dataType: 'json',
            error: function() { alert("error"); },
            success: function(json) {
                if (json != null && json.length > 0) {
                    var name, code, option;
                    for (var i = 0; i < json.length; i++) {
                        if (i == 0) {
                            jQuery("#ddlProduct").children().remove();
                        }
                        name = json[i].ProductName;
                        code = json[i].SaleProductPrice;                      
                        option = "<option value=" + code + ">" + name +"<span  style=\"color:Red\"> $"+parseFloat(code).toFixed(2)+"</span></option>";
                        jQuery("#ddlProduct").append(option);
                    }
                    jQuery("#ddlProduct option:first").attr("selected", "selected");
                    inittxtPrice();
                }
            }
        });
    }
}

function inittxtPrice() {
    var product = jQuery("#ddlProduct");
    var price = GetSelectedValue("ddlProduct");
    if (price != undefined && price != null && price != "-1") {
        jQuery("#txtPrice").val(parseFloat(price).toFixed(2));
    }
    else {
        jQuery("#txtPrice").val("0.00");
    }

}


function QuickGold_Submit() {
    if (document.getElementById('txtPrice').value.indexOf('0.00') != -1) {
        alert('Please choose your product');

    }
    else {
        var price = parseFloat(GetSelectedValue("ddlProduct"));
        if (isNaN(price) &&  price <= 0.00) {
            alert('Please choose your product');
        }
        else {
            jQuery("#hfPrice").val(price.toFixed(2));
            jQuery("#hfMoneyType").val("USD");
            jQuery("#hfUSDPrice").val(price);
            jQuery("#hfGame").val(jQuery("#ddlGame option:selected:first").text());
            jQuery("#hfServer").val(jQuery("#ddlServer option:selected:first").text());
            jQuery("#hfServerCode").val(jQuery("#ddlServer option:selected:first").val());
            jQuery("#hfProduct").val(jQuery("#ddlProduct option:selected:first").text());
            jQuery("#hfPriceText").val('$ ' + price.toFixed(2));
            jQuery("#fm_QuickGold").submit();
        }
    }
}
