﻿function dataIsNotBlank(date) {
    var notBlank = true;
    if (date == undefined || date == null) {
        notBlank = false;
    }
    else {
        var reg = new RegExp(/\S+/);
        notBlank = reg.test(date);
    }

    return notBlank;
}

function dataIsInteger(date) {
    var bok = dataIsNotBlank(date);
    if (bok) {
        var reg = new RegExp(/^[-]*\d+$/);
        bok = reg.test(date);
    }
    return bok;
}

function ShowTab(event) {
    var $elm = $(event.target);
    if ($elm.is("a[name=PLSeriesType]")) {
        $("#" + this.id + " a").removeClass("tabactive").addClass("tab1");
        $elm.removeClass("tab1").addClass("tabactive");
        var type = $elm.attr("index");
        var index = "#tabcontent" + (parseInt(type) + 1);
        $(index).siblings().hide();
        $(index).css("display", "block");
        if (dataIsInteger(type) && $("table").is("[sertype=" + type + "]") == false && type!='0') {
            var gameCode = $("#hfGameCode").val();
            if (dataIsNotBlank(gameCode)) {
                if ($(index).children().is("#loading") == false) {
                    $(index).append("<div align=\"center\" id='loading'>Loading...</div>");
                }
                $.ajax({
                    url: '/GetPLProduct.aspx?rd=' + Math.random(),
                    type: 'get',
                    data: 'GameCode=' + gameCode + '&index=' + type,
                    dataType: 'html',
                    error: function() { /*$(index).children().remove(); alert("Error!");*/ },
                    success: function(data) {
                        //
                        if (dataIsNotBlank(data)) {
                            $(index).children().remove();
                            $(index).append(data);
                        }
                    }

                });
            }
        }
        //$("#tabcontent" + parseInt(type) + 1).show(100);
    }
    else {
        event.preventDefault();
    }

    event.stopPropagation();
}
function selectChanged(elm) {
    if (elm == null || elm == undefined) {
        return false;
    }
    var data = queryDataIsReady(elm);
    if (data != undefined && data != null && data != false && dataIsNotBlank(data)) {
        var strArray = data.split('|');
        if (strArray != null && strArray.length == 5) {
            GetProductInfo(strArray[0], strArray[1], strArray[2], strArray[3], strArray[4]);
            var type = strArray[4];
            var pj = $(":input[sertype=" + type + "][name=pj]:first");
            var pjname = $(":input[sertype=" + type + "][name=pjname]:first");
            var start = $(":input[sertype=" + type + "][name=start]:first");
            var end = $(":input[sertype=" + type + "][name=end]:first");
            pj.val(strArray[0]);
            pjname.val(strArray[1]);
            start.val(strArray[2]);
            end.val(strArray[3]);
        }
    }
}
function GetSelectedValueByElm(elm) {
    var val = $(elm).children(":selected").val();
    if (dataIsNotBlank(val)) {
        return val;
    }
    else {
        return null;
    }
}
function GetElementAttrubite(elm, attrName) {
    var value = $(elm).attr(attrName);
    if (dataIsNotBlank(value)) {
        return value;
    }
    return null;
}
function clearData(type) {
    var time = $(":input[name=iptTime_" + type + "]:first");
    var price = $(":input[name=iptPrice_" + type + "]:first");
    var pj = $(":input[sertype=" + type + "][name=pj]:first");
    var pjname = $(":input[sertype=" + type + "][name=pjname]:first");
    var start = $(":input[sertype=" + type + "][name=start]:first");
    var end = $(":input[sertype=" + type + "][name=end]:first");
    time.val("");
    price.val("");
    pj.val("");
    pjname.val("");
    start.val("");
    end.val("");

}
function queryDataIsReady(elm) {

    var bOK = false;
    var type = GetElementAttrubite(elm, "sertype");
    if (dataIsNotBlank(type)) {
        clearData(type);
        var radio = $("input[name=rbd_" + type + "]:checked:first");
        var Lv1 = $("select[name=start_" + type + "]:first option:selected").val();
        var Lv2 = $("select[name=end_" + type + "]:first option:selected").val();
        var slctLv = $("select[name=start_" + type + "]:first");
        if (Lv1 != undefined && Lv1 != null) {
            var isSingle = GetElementAttrubite(slctLv, "single");
            if (isSingle == "true") {
                Lv2 = "-1";
            }
            if (!dataIsInteger(Lv2) || !dataIsInteger(Lv1)) {
                return false;
            }
            else {
                if (isSingle == "false") {
                    if (Lv1 == "-1" || parseInt(Lv1) >= parseInt(Lv2)) {
                        return false;
                    }

                }
            }
            if (radio != undefined && radio != null) {
                var pjindex = $(radio).attr("pjIndex");
                var serindex = $(radio).attr("serIndex");
                if (dataIsInteger(pjindex) && dataIsInteger(serindex)) {
                    var pjGuid = $("input[name=pjGuid_" + type + "_" + serindex + "_" + pjindex + "]:first");
                    if (pjGuid != undefined &&
							pjGuid != null &&
							dataIsNotBlank(pjGuid.val()) &&
							dataIsNotBlank(radio.val())) {
                        bOK = true;
                        return pjGuid.val() + '|' + radio.val() + '|' + Lv1 + '|' + Lv2 + '|' + type;
                    }
                }
            }
        }
    }

    return bOK;
}

function GetProductInfo(pguid, nguid, start, end, index) {
    var d = new Date();
    var gamecode = $("#hfGameCode").val();
    var code = d.toLocaleTimeString();
    $.ajax({
        url: '/PLDataDispose.aspx?rd=' + Math.random(),
        type: 'get',
        data: 'pguid=' + pguid + '&nguid=' + nguid + '&start=' + start + '&end=' + end + '&index=' + index + '&gamecode=' + gamecode,
        dataType: 'html',
        success: function(data) {
            ProductInfoBack(data);
        }
    });
}

function ProductInfoBack(text) {
   
    var data = text.split("\n");
    if (data.length > 0) {
        if (data[0] != null && data[0] != "")
            $("input[name=iptPrice_" + data[2] + "]").val(data[0]);
        if (data[1] != null && data[1] != "")
            $("input[name=iptTime_" + data[2] + "]").val(data[1]);
    }
}

function displayToolTip(elm, event) {
    var tip = document.getElementById("tipPL");
    var text = $(elm).children("input[name=comment]:hidden").val();

    if (dataIsNotBlank(text) && tip != null && tip != undefined) {
        var x = 0;
        var y = 0;
        if (window.event) {
            x = window.event.x;
            y = window.event.y;
        }
        else {
            x = event.pageX;
            y = event.pageY;
        }

        tip.style.top = y + "px";
        tip.style.left = x + "px";
        $("#tipPL span[name=comment]").html(text);
        $(tip).show();
    }

}
function hideToolTip(elm) {
    var tip = $("#tipPL");
    tip.hide();

}

function sumbitClick(elm) {
}
   
