$(document).ready(function () {




    $("#MainContent_Panel1  .form-row").each(function () {


        try {

            var sp = $(this).html().split('<div class="form-row"');
            $.each(sp, function (index, value) {

                var spsub0 = value.toString().replace(/,,/g, ',0,').trim().split('<div class="formrow"');
                $.each(spsub0, function (index0, value0) {


                    var spsub1 = value.toString().replace(/,,/g, ',0,').trim().split('<span>');
                    //alert((spsub1.length-1).toString());
                    var spsub0 = value.toString().replace(/,,/g, ',0,').trim().split('<div class="formrow"');

                    var numcount = 1;

                    $.each(spsub1, function (index1, value1) {

                        if (value1.indexOf("label") != -1) {

                            $("#MainContent_Panel1  .form-row").find('input[type="image"]').each(function () {

                                try {

                                    $(this).css('height', '25px');
                                } catch (err) { };

                            });
                            $("#MainContent_Panel1  .form-row").find('span').each(function () {
                                try {



                                  




                                    if (value1.indexOf($(this).attr('id').toString()) != -1) {



                                        if ($(this).html().length >= 15) {
                                            $(this).css('width', '180px');
                                        }

                                        else {
                                            $(this).css('width', '120px');
                                        }


                                        if ($(this).html() == "...") {
                                            $(this).css('width', '500px');
                                        }

                                        if ($(this).html().length >= 50) {
                                            $(this).css('width', '400px');
                                        }


                                    }




                                } catch (err) { };

                            });


                            //numcount++;




                            $("#MainContent_Panel1  .form-row").find('input[type="text"]').each(function () {
                                try {
                                    if ($(this).attr('id').indexOf("ID") != -1) {

                                        $(this).css('width', '150px');
                                    }
                                    if ($(this).attr('id').indexOf("Name") != -1) {
                                        $(this).css('width', '250px');
                                    }
                                } catch (err) { };

                            });



                            //alert(value1.toString());

                        }



                    });

                });


            });



            try {
                if ($(this).html().indexOf("frmrow") != -1) {




                    $(".frmrow").each(function () {




                        if ($(this).html().indexOf("label") != -1) {



                            numcount++;




                        }
                        if ($(this).html().indexOf("</div>") != -1) {


                            //alert(numcount.toString());
                            numcount = 0;




                        }




                    });




                }

            } catch (err) { }



            //try{
            //    if($(this).html().indexOf("label")!=-1)
            //    {
            //        alert($(this).html());
            //    }
            //}catch(err){}
        } catch (err) { }


    });



    try {
        //var modal = document.getElementById('myModalwait');
        //modal.style.display = "block";
        //$("form :input").prop("disabled", true);
        $('input, :input').attr('autocomplete', 'off');
    } catch (err) { }













    $("input").not($(":button")).each(function () {

        var ctlid = $(this).attr("id");
       
        try {
            if (ctlid != "undefined" && ctlid != "MainContent_lblrownum") {
                var ctlidname = $(this).attr("id");
                if (ctlidname.indexOf("d_") != -1 || ctlidname.indexOf("m_") != -1 || ctlidname.indexOf("y_") != -1) {

                    var title = $(this).attr("title");
                    if (title.toString().indexOf("*") != -1) {
                        $("#" + ctlid.replace("d_", "lbl_").replace("m_", "lbl_").replace("y_", "lbl_")).css({ 'width': '120px', 'background-color': '#CD5C5C', 'color': 'white', 'border-radius': '0.25rem' });
                    }


                }
                else {

                    var title = $(this).attr("title");
                    if (title.toString().indexOf("*") != -1) {


                        //$(this).css({'border-color':'red'});
                        $("#" + ctlid.replace("txt_", "lbl_")).css({ 'background-color': '#CD5C5C', 'color': 'white', 'border-radius': '0.25rem' });


                    }
                    else if (title.toString().indexOf("codegen") != -1) {
                       
                        $("#" + ctlid.replace("txt_", "lbl_")).css({ 'background-color': 'DodgerBlue', 'color': 'white', 'border-radius': '0.25rem' });



                    }
                    else {
                        $("#" + ctlid.replace("txt_", "lbl_")).css({ 'color': 'black' });

                    }

                }


            }

            //if(ctlid.indexOf("frm")!=-1)
            //{
            //    $("#"+ctlid).css({'font-size':'24px'});
            //}

        }
        catch (err) {

        }

    });

    $("select").not($(":button")).each(function () {

        var ctlid = $(this).attr("id");

        try {
            if (ctlid != "undefined" && ctlid != "MainContent_lblrownum") {
                var title = $(this).attr("title");
                if (title.toString().indexOf("*") != -1) {
                    //$(this).css({'border-color':'red'});
                    $("#" + ctlid.replace("ddl_", "lbl_")).css({ 'background-color': '#CD5C5C', 'color': 'white', 'border-radius': '0.25rem' });


                }
                else if (title.toString().indexOf("codegen") != -1) {
                    $("#" + ctlid.replace("ddl_", "lbl_")).css({ 'background-color': 'DodgerBlue', 'color': 'white', 'border-radius': '0.25rem' });



                }
                else {
                    $("#" + ctlid.replace("ddl_", "lbl_")).css({ 'color': 'black' });

                }


            }

            //if(ctlid.indexOf("frm")!=-1)
            //{
            //    $("#"+ctlid).css({'font-size':'24px'});
            //}

        }
        catch (err) {

        }

    });


    $("textarea").not($(":button")).each(function () {

        var ctlid = $(this).attr("id");

        try {
            if (ctlid != "undefined" && ctlid != "MainContent_lblrownum") {
                var title = $(this).attr("title");
                if (title.toString().indexOf("*") != -1) {
                    //$(this).css({'border-color':'red'});
                    $("#" + ctlid.replace("txt_", "lbl_")).css({ 'background-color': '#CD5C5C', 'color': 'white', 'border-radius': '0.25rem' });


                }
                else if (title.toString().indexOf("codegen") != -1) {
                    $("#" + ctlid.replace("txt_", "lbl_")).css({ 'background-color': 'DodgerBlue', 'color': 'white', 'border-radius': '0.25rem' });



                }
                else {
                    $("#" + ctlid.replace("txt_", "lbl_")).css({ 'color': 'black' });

                }


            }

            //if(ctlid.indexOf("frm")!=-1)
            //{
            //    $("#"+ctlid).css({'font-size':'24px'});
            //}

        }
        catch (err) {

        }

    });



    $("#MainContent_Panel1 input[type='text']").each(function () {
        $("#" + $(this).attr('id')).val("");
    });
    $("#MainContent_Panel1 select").each(function () {
        $("#" + $(this).attr('id')).val("");
    });
    $("#MainContent_Panel1 textarea").each(function () {
        $("#" + $(this).attr('id')).html("");
    });








    $("input").each(function () {

        var ctlid = $(this).attr("id");

        try {

            if (ctlid.indexOf("btnedit") != -1) {

                $(this).hide();
            }
            if (ctlid.indexOf("btndel") != -1) {

                $(this).hide();
            }

        }
        catch (err) {

        }

    });

    try {
        controlpage();
    } catch (err) {
    }
    finally {

    }
    try {
        //$("#MainContent_txt_cltsearch").focus();
    } catch (err) {
    }
    finally {

    }

   












});




function controlpage() {




    $("textarea").keydown(function (event) {
        if (event.keyCode == 13 || event.keyCode == 9) {
            var s = $(this).val();

        }
    });


   
    $("select").keydown(function (event) {


        if (event.keyCode == 13 || event.keyCode == 9) {

            var fields = $(this).parents('form:eq(0),body').find('button,input[type="text"],textarea,select:not([disabled])');
            var index = fields.index(this);
            if (index > -1 && (index + 1) < fields.length) {



                fields.eq(index + 1).focus();
                fields.eq(index + 1).select();




            }
            return false;


        }


    });

    $("input").not($(":button")).keydown(function (evt) {


        if (evt.keyCode == 13 || evt.keyCode == 9) {
            evt.preventDefault();


            iname = $(this).val();

            if (iname !== 'Submit') {
                //var fields = $(this).parents('form:eq(0),body').find('button,input[type="text"]:not([readonly]),textarea,select:not([disabled])');
                var fields = $(this).parents('form:eq(0),body').find('button,input[type="text"],textarea,select:not([disabled])');
                var index = fields.index(this);
                if (index > -1 && (index + 1) < fields.length) {


                    fields.eq(index + 1).focus();
                    fields.eq(index + 1).select();


                }
                return false;
            }


        }
        else if (evt.keyCode == 8) {

            //$(this).val("");             
            //$(this).focus();
            //$(this).select();

        }


    });

    $("input").not($(":button")).each(function () {

        var ctlid = $(this).attr("id");

        try {
            if (ctlid != "undefined" && ctlid != "MainContent_lblrownum") {
                var title = $(this).attr("title");
                //if(title.toString()=="int")
                //{
                $("#" + ctlid).on('change', function (event) {

                    if (title.toString() == "intf") {
                        let ctlvalue = $("#" + ctlid).val();
                        ctlvalue = parseFloat(ctlvalue.replace(",", ""));
                        $("#" + ctlid).val(addCommas(ctlvalue.toFixed(2)));
                        $("#" + ctlid).css({ 'text-align': 'right' });
                    }


                });


                $("#" + ctlid).on('keyup', function (event) {
                    if (title.indexOf("int") != -1) {
                        if (/[^0-9\.\,]/g.test(this.value)) this.value = this.value.replace(/[^0-9\.\,]/g, '');
                        $("#" + ctlid).css({ 'text-align': 'right' });

                    }

                    $("input").not($(":button")).each(function () {

                        var ctlidx = $(this).attr("id");

                        try {
                            if (ctlidx != "undefined" && ctlidx != "MainContent_lblrownum") {

                                if ($("#" + ctlidx).val() == "NaN") {
                                    $("#" + ctlidx).val("");
                                }


                            }


                        }

                        catch (err) { }

                    });


                });


                //}






            }

            //if(ctlid.indexOf("frm")!=-1)
            //{
            //    $("#"+ctlid).css({'font-size':'24px'});
            //}

        }
        catch (err) {

        }

    });


}



function SetZIndex(control, args) {
    // Set auto complete extender control's z-index to a high value
    // so it will appear on top of, not under, the ModalPopUp extended panel.
    control._completionListElement.style.zIndex = 9999999999999;
}



function checkreq() {
    var chk = "";
    $("input").not($(":button")).each(function () {

        var ctlid = $(this).attr("id");

        try {
            if (ctlid != "undefined" && ctlid != "MainContent_lblrownum") {
                var title = $(this).attr("title");
                if (title.toString().indexOf("*") != -1 && $(this).val() == "") {


                    msgwarning(this, 'กรุณาระบุข้อมูล ' + title.toString().replace("*", ""), ctlid);


                    $(this).focus();
                    chk = "N";
                    return false;



                }


            }



        }
        catch (err) {

        }

    });

    if (chk == "") {
        $("textarea").not($(":button")).each(function () {

            var ctlid = $(this).attr("id");

            try {
                if (ctlid != "undefined" && ctlid != "MainContent_lblrownum") {
                    var title = $(this).attr("title");
                    if (title.toString().indexOf("*") != -1 && $(this).val() == "") {
                        msgwarning(this, 'กรุณาระบุข้อมูล ' + title.toString().replace("*", ""), ctlid);
                        $(this).focus();
                        chk = "N";
                        return false;


                    }


                }


            }
            catch (err) {

            }

        });
    }


    if (chk == "") {
        $("select").not($(":button")).each(function () {

            var ctlid = $(this).attr("id");

            try {
                if (ctlid != "undefined" && ctlid != "MainContent_lblrownum") {
                    var title = $(this).attr("title");
                    if (title.toString().indexOf("*") != -1 && ($(this).val() == "" || $(this).val() == "0")) {
                        msgwarning(this, 'กรุณาระบุข้อมูล ' + title.toString().replace("*", ""), ctlid);
                        $(this).focus();
                        chk = "N";
                        return false;


                    }


                }


            }
            catch (err) {

            }

        });
    }



    if (chk == "") {
        chk = "N";
        var ctlchk = "";
        $('#tbldt input[type="text"]').each(function () {
            var ctlid = $(this).attr("id");
            if (ctlid.indexOf("ProductID") != -1) {
                if ($(this).val() != "") {
                    chk = "";
                    return false;

                } else {

                    ctlchk = ctlid;
                    return false;
                }
            }


        });

        if (ctlchk.length == 0) {
            chk = "";
        }

        if (chk == "N") {
            msgwarning(this, 'กรุณาระบุข้อมูล', '');
            $("#" + ctlchk).focus();


        }


    }





    return chk;

}





function CheckOne(obj) {




    var grid = obj.parentNode.parentNode.parentNode;
    var inputs = grid.getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].type == "checkbox") {
            if (obj.checked && inputs[i] != obj && inputs[i].checked) {
                inputs[i].checked = false;


            }
        }
    }
}

function HighlightRow(chkB) {
    var IsChecked = chkB.checked;
    if (IsChecked) {
        chkB.parentElement.parentElement.style.backgroundColor = '#DADCDA';
        chkB.parentElement.parentElement.style.color = 'white';
    } else {
        chkB.parentElement.parentElement.style.backgroundColor = 'white';
        chkB.parentElement.parentElement.style.color = 'black';
    }
}

function SelectAll(obj, childname) {
    var CheckValue = obj.checked;

    var objCheckBoxes = document.getElementsByTagName("input");
    if (!objCheckBoxes)
        return;
    var countCheckBoxes = objCheckBoxes.length;

    if (countCheckBoxes == 1) {

        if (objCheckBoxes.name.indexOf("gvEmployees") >= 0 && objCheckBoxes.name.indexOf(childname) >= 0)
            objCheckBoxes.checked = CheckValue;
    }
    else {
        // set the check value for all check boxes 
        for (var i = 0; i < countCheckBoxes; i++)
            if (objCheckBoxes[i].type == "checkbox")
                if (objCheckBoxes[i].name.indexOf("gvEmployees") >= 0 && objCheckBoxes[i].name.indexOf(childname) >= 0)
                    objCheckBoxes[i].checked = CheckValue;
    }
}


function SelectAll1(obj, childname) {
    var CheckValue = obj.checked;

    var objCheckBoxes = document.getElementsByTagName("input");
    if (!objCheckBoxes)
        return;
    var countCheckBoxes = objCheckBoxes.length;

    if (countCheckBoxes == 1) {

        if (objCheckBoxes.name.indexOf("gv") >= 0 && objCheckBoxes.name.indexOf(childname) >= 0)
            objCheckBoxes.checked = CheckValue;
    }
    else {
        // set the check value for all check boxes 
        for (var i = 0; i < countCheckBoxes; i++)
            if (objCheckBoxes[i].type == "checkbox")
                if (objCheckBoxes[i].name.indexOf("gv") >= 0 && objCheckBoxes[i].name.indexOf(childname) >= 0)
                    objCheckBoxes[i].checked = CheckValue;
    }
}

function addCommas(amount) {



    return amount.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1,');

    //nStr += '';
    //x = nStr.split('.');
    //x1 = x[0];
    //x2 = x.length > 1 ? '.' + x[1] : '';
    //var rgx = /(\d+)(\d{3})/;
    //while (rgx.test(x1)) {
    //    x1 = x1.replace(rgx, '$1' + ',' + '$2');
    //}
    //return x1 + x2;


}
function addCommas1(nStr) {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1;
}
function chkdec(ele) {
    var num = parseFloat(ele.value);
    ele.value = addCommas(num.toFixed(2));
    if (ele.value == 'NaN') {
        ele.value = '0';
    } else {

        ele.value = addCommas(num.toFixed(2));
    }
}
function chkint(ele) {
    var num = parseFloat(ele.value);
    ele.value = addCommas1(num);
    if (ele.value == 'NaN') {
        ele.value = '0';
    } else {
        ele.value = addCommas(num);

    }
}


function postBackHiddenField(hiddenFieldID) {
    var hiddenField = $get(hiddenFieldID);
    if (hiddenField) {
        __doPostBack(hiddenFieldID, '');
    }
}
function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];

    }



    return vars;
}