
$(document).ready(function () {


    let docno = getUrlVars()["docno"];
    setTimeout(function () {       
        $("#MainContent_txt_docno").val(docno);      
        getData();
    }, 500);

    setTimeout(function () {     
        $("#MainContent_txt_docno").val(docno);       
        getDataTable();
    }, 1000);


    try {
        $("#MainContent_Panel1").css('height', $(window).height() - 20);
    } catch (err) { }




    controlrowtable(0);


    $("#MainContent_txt_SeqNo_0").val("1");

    try {
        $("#g2").css('height', $(window).height() - 320);
    } catch (err) { }



    let flow = " <div class=\"stepper-wrapper\"> " +
          " <div class=\"stepper-item completed\"> " +
          " <div class=\"step-counter\">1</div> " +
          " <div class=\"step-name\">Scrap issued</div> " +
          " </div> " +

            " <div class=\"stepper-item active\"> " +
          " <div class=\"step-counter\">2</div> " +
        " <div class=\"step-name\"><select id=\"ddlskill\" style=\"width:150px;\"/></div> " +
          " <div class=\"step-name\" style=\"margin-top:5px;\">Skill sign</div> " +
          "  </div>" +
  " <div class=\"stepper-item active\"> " +
  " <div class=\"step-counter\">3</div> " +
          " <div class=\"step-name\"><select id=\"ddleng\" style=\"width:150px;\"/></div> " +
          " <div class=\"step-name\" style=\"margin-top:5px;\">Eng. sign</div> " +

  "  </div> " +
    " <div class=\"stepper-item active\"> " +
  " <div class=\"step-counter\">4</div> " +
          " <div class=\"step-name\"><select id=\"ddlmgr\" style=\"width:150px;\"/></div> " +
          " <div class=\"step-name\" style=\"margin-top:5px;\">Mgr. sign</div> " +

  "  </div> " +
    " <div class=\"stepper-item active\"> " +
  " <div class=\"step-counter\">5</div> " +
          " <div class=\"step-name\"><select id=\"ddlmgrdm\" style=\"width:150px;\"/></div> " +
          " <div class=\"step-name\" style=\"margin-top:5px;\">Mgr.Dm.</div> " +

  "  </div> " +
    " <div class=\"stepper-item active\"> " +
  " <div class=\"step-counter\">6</div> " +
  " <div class=\"step-name\">Printed out</div> " +
  "  </div>";
    $("#flow").html(flow)

  
        $('#MainContent_ddl_section').on('change', function (event) {
            getapproveflow(1, $("#ddlskill"));
            getapproveflow(2, $("#ddleng"));
            getapproveflow(3, $("#ddlmgr"));
            getapproveflow(4, $("#ddlmgrdm"));

        });
  


});
function newrowsform_enter(ctl) {
    inputrow = 0;
    var resindex = ctl.id.split("_");
    var irow = resindex[3].toString();

    irow = irow + 1;
    if (irow >= inputrow) {

        addrow("Y");

    }

}
function newrows(ctl) {
    inputrow = 0;
    var resindex = ctl.id.split("_");
    var irow = resindex[2].toString();

    irow = irow + 1;
    if (irow >= inputrow) {

        addrow("Y");

    }
}

let inputrow = 0;
function addrow(news) {



    $('#tbldt tr').find('input').each(function () {


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

            var ctlname = $(this).attr('id');
            var arrctl = ctlname.split('_');

            inputrow = parseFloat(arrctl[3].toString());



        }
    });

    var aClone = $('#tbldt tbody>tr:last').clone(true)
    var regex = new RegExp("_" + inputrow.toString(), "g");
    var newrow = aClone.html().replace(regex, "_" + (inputrow + 1).toString());
    inputrow++;
    $("#tbldt > tbody:last").append("<tr id='rowd_" + inputrow.toString() + "'>" + newrow + "</tr>");

    controlpage();
    controlrowtable(inputrow);



    let iseq = 1;
    $('#tbldt tbody>tr input[type="text"]').each(function () {
        let ctl = $(this).attr('id');
        if (ctl.indexOf("Seq") != -1) {
            if ($(this).is(':hidden')) {

            } else {
                $(this).val(iseq.toString());
                iseq++;

            }
        }
    });
}


function getitemname(ctl, itemno) {
    $.ajax({
        type: "POST",
        url: "ListItemDetail.aspx/getitemname",
        data: "{itemno:'" + itemno + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            var xmlDoc = $.parseXML(response.d);
            var xml = $(xmlDoc);
            var customers = xml.find("Table");
            $.each(customers, function () {
                var customer = $(this);


                $(ctl).val($(this).find("ItemName").text());


            });

        },
        failure: function (response) {
            alert(response.d);
        },
        error: function (response) {
            alert(response.d);
        }
    });

}


function controlrowtable(ctlid) {
    $("#MainContent_ddl_Unit_" + ctlid.toString()).val("THB/UNIT");

    Sys.Application.add_init(function () { $create(Sys.Extended.UI.AutoCompleteBehavior, { "completionInterval": 100, "completionListCssClass": "completionList", "completionListItemCssClass": "listItem", "completionSetCount": 10, "contextKey": "ITEM_NO,PM.dbo.ITEM_MASTER,ITEM_NO,'',''", "delimiterCharacters": ";,", "highlightedItemCssClass": "itemHighlighted", "id": "ac_ModelCode_" + ctlid.toString(), "minimumPrefixLength": 1, "serviceMethod": "GetMasterCodeFilter", "servicePath": "Autokeyword.asmx", "useContextKey": true }, { "shown": SetZIndex }, null, $get("MainContent_txt_ModelCode_" + ctlid.toString())); });
    Sys.Application.add_init(function () { $create(Sys.Extended.UI.AutoCompleteBehavior, { "completionInterval": 100, "completionListCssClass": "completionList", "completionListItemCssClass": "listItem", "completionSetCount": 10, "contextKey": "ITEM_NO,PM.dbo.ITEM_MASTER,ITEM_NO,'',''", "delimiterCharacters": ";,", "highlightedItemCssClass": "itemHighlighted", "id": "ac_Drawing_" + ctlid.toString(), "minimumPrefixLength": 1, "serviceMethod": "GetMasterCodeFilter", "servicePath": "Autokeyword.asmx", "useContextKey": true }, { "shown": SetZIndex }, null, $get("MainContent_txt_Drawing_" + ctlid.toString())); });
    Sys.Application.add_init(function () { $create(Sys.Extended.UI.AutoCompleteBehavior, { "completionInterval": 100, "completionListCssClass": "completionList", "completionListItemCssClass": "listItem", "completionSetCount": 10, "contextKey": "ERR_CODE+':'+DFECT_ITEM,NSTDWH.dbo.NST_Defect_Master,ERR_CODE+':'+DFECT_ITEM,'',''", "delimiterCharacters": ";,", "highlightedItemCssClass": "itemHighlighted", "id": "ac_Defect_" + ctlid.toString(), "minimumPrefixLength": 1, "serviceMethod": "GetMasterCodeFilter", "servicePath": "Autokeyword.asmx", "useContextKey": true }, { "shown": SetZIndex }, null, $get("MainContent_txt_Defect_" + ctlid.toString())); });



    $('#MainContent_txt_Remark_' + ctlid.toString()).on('keydown', function (event) {
        if (event.keyCode == 13 || event.keyCode == 9) {


            newrowsform_enter(this);
            $('#MainContent_txt_ModelCode_' + (parseFloat(ctlid) + 1).toString()).focus();

        }


    });
    $('#MainContent_txt_Drawing_' + ctlid.toString()).on('keydown', function (event) {
        if (event.keyCode == 13 || event.keyCode == 9) {

            setTimeout(function () {
                getitemname($('#MainContent_txt_PartName_' + ctlid.toString()), $('#MainContent_txt_Drawing_' + ctlid.toString()).val());
            }, 300);
        }


    });
    $('#MainContent_txt_ModelCode_' + ctlid.toString()).on('keydown', function (event) {

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

            setTimeout(function () {
                getitemname($('#MainContent_txt_ModelName_' + ctlid.toString()), $('#MainContent_txt_ModelCode_' + ctlid.toString()).val());
            }, 300);
        }




    });
    $('#MainContent_txt_Drawing_' + ctlid.toString()).on('focus click', function (event) {

        setTimeout(function () {
            getitemname($('#MainContent_txt_PartName_' + ctlid.toString()), $('#MainContent_txt_Drawing_' + ctlid.toString()).val());
        }, 300);



    });
    $('#MainContent_txt_PartName_' + ctlid.toString()).on('focus click', function (event) {


        setTimeout(function () {
            getitemname($('#MainContent_txt_ModelName_' + ctlid.toString()), $('#MainContent_txt_ModelCode_' + ctlid.toString()).val());
        }, 300);

    });

    $('#MainContent_txt_Defect_' + ctlid.toString()).on('keydown', function (event) {
        if (event.keyCode == 13 || event.keyCode == 9) {
            setTimeout(function () {
                let defectarr = $('#MainContent_txt_Defect_' + ctlid.toString()).val();
                if (defectarr.indexOf(':') != -1) {
                    let arr = defectarr.split(':');

                    $('#MainContent_txt_DefectName_' + ctlid.toString()).val(arr[1]);
                    $('#MainContent_txt_Defect_' + ctlid.toString()).val(arr[0]);

                } else {

                    $.ajax({
                        type: "POST",
                        url: "RecordData.aspx/getDefact",
                        data: "{defactno:'" + defectarr + "'}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (response) {
                            var xmlDoc = $.parseXML(response.d);
                            var xml = $(xmlDoc);
                            var customers = xml.find("Table");
                            $.each(customers, function () {
                                var customer = $(this);


                                $('#MainContent_txt_DefectName_' + ctlid.toString()).val($(this).find("DFECT_ITEM").text());


                            });

                        },
                        failure: function (response) {
                            alert(response.d);
                        },
                        error: function (response) {
                            alert(response.d);
                        }
                    });


                }




                let itemno = $('#MainContent_txt_Drawing_' + ctlid.toString()).val();



                $.ajax({
                    type: "POST",
                    url: "RecordData.aspx/getPrice",
                    data: "{itemno:'" + itemno + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        var xmlDoc = $.parseXML(response.d);
                        var xml = $(xmlDoc);
                        var customers = xml.find("Table");
                        $.each(customers, function () {
                            var customer = $(this);


                            $("#MainContent_txt_PricePerUnit_" + ctlid.toString()).val($(this).find("STD_PRC").text());
                            $("#MainContent_txt_PricePerGroup_" + ctlid.toString()).val($(this).find("STD_PRC").text());


                        });

                    },
                    failure: function (response) {
                        alert(response.d);
                    },
                    error: function (response) {
                        alert(response.d);
                    }
                });



            }, 500);
        }


    });

    $('#MainContent_txt_DefectName_' + ctlid.toString()).on('focus click', function (event) {
        let defectarr = $('#MainContent_txt_Defect_' + ctlid.toString()).val();
        if (defectarr.indexOf(':') != -1) {
            let arr = defectarr.split(':');
            setTimeout(function () {
                $('#MainContent_txt_DefectName_' + ctlid.toString()).val(arr[1]);
                $('#MainContent_txt_Defect_' + ctlid.toString()).val(arr[0]);
            }, 500);

        }
        else {
            $.ajax({
                type: "POST",
                url: "RecordData.aspx/getDefact",
                data: "{defactno:'" + defectarr + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    var xmlDoc = $.parseXML(response.d);
                    var xml = $(xmlDoc);
                    var customers = xml.find("Table");
                    $.each(customers, function () {
                        var customer = $(this);


                        $('#MainContent_txt_DefectName_' + ctlid.toString()).val($(this).find("DFECT_ITEM").text());


                    });

                },
                failure: function (response) {
                    alert(response.d);
                },
                error: function (response) {
                    alert(response.d);
                }
            });


        }

    });


    $('#MainContent_txt_Drawing_' + ctlid.toString()).on('keydown', function (event) {
        if (event.keyCode == 13 || event.keyCode == 9) {
            setTimeout(function () {

                let itemno = $('#MainContent_txt_Drawing_' + ctlid.toString()).val();



                $.ajax({
                    type: "POST",
                    url: "RecordData.aspx/getPrice",
                    data: "{itemno:'" + itemno + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        var xmlDoc = $.parseXML(response.d);
                        var xml = $(xmlDoc);
                        var customers = xml.find("Table");
                        $.each(customers, function () {
                            var customer = $(this);


                            $("#MainContent_txt_PricePerUnit_" + ctlid.toString()).val($(this).find("STD_PRC").text());
                       


                        });

                    },
                    failure: function (response) {
                        alert(response.d);
                    },
                    error: function (response) {
                        alert(response.d);
                    }
                });


            }, 500);

        }


    });




    $('#MainContent_txt_Qty_' + ctlid.toString()).on('input', function (event) {
        if (event.target.value.length == 1) {

            var elmnt = document.getElementById("g2");
            elmnt.scrollLeft += 800;

            let defectarr = $('#MainContent_txt_Defect_' + ctlid.toString()).val();
            if (defectarr.indexOf(':') != -1) {
                let arr = defectarr.split(':');
                setTimeout(function () {
                    $('#MainContent_txt_DefectName_' + ctlid.toString()).val(arr[1]);
                    $('#MainContent_txt_Defect_' + ctlid.toString()).val(arr[0]);
                }, 500);

            }
        }


    });

 



}

function getpm(ctl)
{

    var resindex = ctl.id.split("_");
    var irow = resindex[3].toString();
    $("#MainContent_ddl_PM_" + irow.toString()).html("");
    $("#MainContent_ddl_PM_" + irow.toString()).append('<option value="">---Select----</option>');

    $.ajax({
        type: "POST",
        url: "ListItemDetail.aspx/getPMWSCD",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            var xmlDoc = $.parseXML(response.d);
            var xml = $(xmlDoc);
            var customers = xml.find("Table");
            $.each(customers, function () {
                var customer = $(this);


                $("#MainContent_ddl_PM_" + irow.toString()).append('<option value="' + $(this).find("WS_CD").text() + '">' + $(this).find("PMWSCDNo").text() + "-" + $(this).find("PMWSCDName").text() + '</option>');


            });

        },
        failure: function (response) {
            alert(response.d);
        },
        error: function (response) {
            alert(response.d);
        }
    });
}

function saverecord() {

  
    if ($("#MainContent_ddl_ngfrom").val().length == 0) {
        alert("Please Don't forget select the NG From.");
        $("#MainContent_ddl_ngfrom").focus();

    }
    else {
        $("#loader").show();
        let rowCount = $('#tbldt tr').length;
        rowCount = rowCount - 1;
        let next = "";
        let n = 0;
        let str = "";
        let strhd = "";
        let chkroop = 0;
        let stramt = "";
        let index = 1;
        let pubsaveirow = "";


        $('#tbldt tr').find('input').each(function () {


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

                let ctlname = $(this).attr('id');
                let arrctl = ctlname.split('_');

                let i = parseFloat(arrctl[3].toString());


                try {



                    let docno = $('#MainContent_txt_docno').val();
                    let sectionno = $('#MainContent_ddl_section').val();
                    let section = $('#MainContent_ddl_section option:selected').text();
                    let shop = $('#MainContent_txt_shop').val();
                    let scraptypeno = $('#MainContent_ddl_scraptype').val();
                    let scraptype = $('#MainContent_ddl_scraptype option:selected').text();
                    let ngfromno = $('#MainContent_ddl_ngfrom').val();
                    let ngfrom = $('#MainContent_ddl_ngfrom option:selected').text();

                    let d_rdate = $('#d_RDate').val();
                    let m_rdate = $('#m_RDate').val();
                    let y_rdate = $('#y_RDate').val();


                    let ddlskill = $('#ddlskill option:selected').text();
                    let ddleng = $('#ddleng option:selected').text();
                    let ddlmgr = $('#ddlmgr option:selected').text();
                    let ddlmgrdm = $('#ddlmgrdm option:selected').text();



                    let seqno = $('#MainContent_txt_SeqNo_' + i.toString()).val();
                    let partname = $('#MainContent_txt_PartName_' + i.toString()).val();
                    let modelcode = $('#MainContent_txt_ModelCode_' + i.toString()).val();
                    let modelname = $('#MainContent_txt_ModelName_' + i.toString()).val();
                    let drawing = $('#MainContent_txt_Drawing_' + i.toString()).val();
                    let boi = $('#MainContent_ddl_Boi_' + i.toString()).val();
                    let defect = $('#MainContent_txt_Defect_' + i.toString()).val();
                    let defectname = $('#MainContent_txt_DefectName_' + i.toString()).val();
                    let qty = $('#MainContent_txt_Qty_' + i.toString()).val().replace(/,/g, '');
                    let priceperunit = $('#MainContent_txt_PricePerUnit_' + i.toString()).val().replace(/,/g, '');
                    let unit = $('#MainContent_ddl_Unit_' + i.toString()).val();
                    let unittext = $('#MainContent_ddl_Unit_' + i.toString() + ' option:selected').text();
                    let line = $('#MainContent_txt_Line_' + i.toString()).val();
                    let machine = $('#MainContent_txt_Machine_' + i.toString()).val();
                    let shift = $('#MainContent_txt_Shift_' + i.toString()).val();
                    let pmno = $('#MainContent_ddl_PM_' + i.toString()).val();
                    let pmtext = $('#MainContent_ddl_PM_' + i.toString() + ' option:selected').text();
                    let remark = $('#MainContent_txt_Remark_' + i.toString()).val();
                    let pricepergroup = "0";
                    $("#status_" + i.toString()).css({ 'background-color': '#D3250A' });
                    let user = $("#lbluserdetail").html();
                    let statusid = i.toString();


                    strhd = docno + '|' + sectionno + '|' + section + '|' + shop + '|' + scraptypeno + '|' + scraptype + '|' + ngfromno + '|' + ngfrom + '|' + d_rdate + '/' + m_rdate + '/' + y_rdate + '|' + user + '|' + ddlskill + '|' + ddleng + '|' + ddlmgr + '|' + ddlmgrdm + '|';


                    if (str.length == 0) {
                        str = seqno + '|' + partname + '|' + modelcode + '|' + drawing + '|' + boi + '|' + defect + '|' + qty + '|' + priceperunit + '|' + unit + '|' + line + '|' + machine + '|' + shift + '|' + pmno + '|' + pmtext + '|' + remark + '|' + unittext + '|' + user + '|' + pricepergroup + '|' + defectname + '|' + modelname + '|' + statusid + '|';
                    } else {
                        str = str + '^' + seqno + '|' + partname + '|' + modelcode + '|' + drawing + '|' + boi + '|' + defect + '|' + qty + '|' + priceperunit + '|' + unit + '|' + line + '|' + machine + '|' + shift + '|' + pmno + '|' + pmtext + '|' + remark + '|' + unittext + '|' + user + '|' + pricepergroup + '|' + defectname + '|' + modelname + '|' + statusid + '|';
                    }

                    if (chkroop == 5) {


                        stramt = stramt + str + "<";
                        chkroop = 0;
                        str = "";

                    }
                    if (index == rowCount) {

                        stramt = stramt + str + "<";

                        chkroop = 0;
                        str = "";
                    }

                    chkroop++;


                    if (index == rowCount) {
                        stramt = stramt.replace(/<</g, '');
                        let arrstr = stramt.split('<');


                        let numinst = 0;
                        for (inum = 0; inum < arrstr.length; inum++) {
                            if (arrstr[inum].length > 0) {



                                let Base64 = { _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", encode: function (e) { var t = ""; var n, r, i, s, o, u, a; var f = 0; e = Base64._utf8_encode(e); while (f < e.length) { n = e.charCodeAt(f++); r = e.charCodeAt(f++); i = e.charCodeAt(f++); s = n >> 2; o = (n & 3) << 4 | r >> 4; u = (r & 15) << 2 | i >> 6; a = i & 63; if (isNaN(r)) { u = a = 64 } else if (isNaN(i)) { a = 64 } t = t + this._keyStr.charAt(s) + this._keyStr.charAt(o) + this._keyStr.charAt(u) + this._keyStr.charAt(a) } return t }, decode: function (e) { var t = ""; var n, r, i; var s, o, u, a; var f = 0; e = e.replace(/[^A-Za-z0-9+/=]/g, ""); while (f < e.length) { s = this._keyStr.indexOf(e.charAt(f++)); o = this._keyStr.indexOf(e.charAt(f++)); u = this._keyStr.indexOf(e.charAt(f++)); a = this._keyStr.indexOf(e.charAt(f++)); n = s << 2 | o >> 4; r = (o & 15) << 4 | u >> 2; i = (u & 3) << 6 | a; t = t + String.fromCharCode(n); if (u != 64) { t = t + String.fromCharCode(r) } if (a != 64) { t = t + String.fromCharCode(i) } } t = Base64._utf8_decode(t); return t }, _utf8_encode: function (e) { e = e.replace(/rn/g, "n"); var t = ""; for (var n = 0; n < e.length; n++) { var r = e.charCodeAt(n); if (r < 128) { t += String.fromCharCode(r) } else if (r > 127 && r < 2048) { t += String.fromCharCode(r >> 6 | 192); t += String.fromCharCode(r & 63 | 128) } else { t += String.fromCharCode(r >> 12 | 224); t += String.fromCharCode(r >> 6 & 63 | 128); t += String.fromCharCode(r & 63 | 128) } } return t }, _utf8_decode: function (e) { var t = ""; var n = 0; var r = c1 = c2 = 0; while (n < e.length) { r = e.charCodeAt(n); if (r < 128) { t += String.fromCharCode(r); n++ } else if (r > 191 && r < 224) { c2 = e.charCodeAt(n + 1); t += String.fromCharCode((r & 31) << 6 | c2 & 63); n += 2 } else { c2 = e.charCodeAt(n + 1); c3 = e.charCodeAt(n + 2); t += String.fromCharCode((r & 15) << 12 | (c2 & 63) << 6 | c3 & 63); n += 3 } } return t } }

                                let parahd = Base64.encode(strhd);
                                let paradt = Base64.encode(arrstr[inum]);

                                let sendemail = "";
                                if (inum == 0)
                                {
                                    sendemail = "Y";

                                }

                                $.ajax({
                                    type: "POST",
                                    url: "ListItemDetail.aspx/SaveScrapRow",
                                    data: '{parahd:"' + parahd + '",paradt:"' + paradt + '",sendemail:"'+sendemail+'"}',
                                    contentType: "application/json; charset=utf-8",
                                    dataType: "json",
                                    success: function (response) {
                                        res = response.d;
                                        pubsaveirow = pubsaveirow + response.d + "|";

                                        if (numinst == arrstr.length - 2) {

                                            if (pubsaveirow.indexOf('|') != -1) {
                                                let arrstatus = pubsaveirow.split('|');
                                                if (arrstatus.length > 1) {

                                                    let saveednum = arrstatus.length - 2;

                                                    if ($("#MainContent_txt_docno").val().length == 0) {
                                                        $("#MainContent_txt_docno").val(arrstatus[0]);

                                                    }
                                                    $.each(arrstatus, function (index, value) {

                                                        try {
                                                            $("#status_" + value.toString()).css({ 'background-color': '#0DD30A' });

                                                        }
                                                        catch (err) { }





                                                    });
                                                    setTimeout(function () {
                                                        $("#loader").hide();
                                                        alert("Successfully updated.");
                                                       
                                                    }, 1000);
                                                }

                                            }
                                            else {


                                            }




                                        }
                                        numinst++;
                                    },
                                    failure: function (response) {
                                        //alert(response.d);
                                    },
                                    error: function (response) {
                                        //alert(response.d);
                                    }
                                });
                            }

                        }
                    }
                } catch (err) { } finally {
                }

                index++;

            }


        });


    }

}

function gvdelrowid(ctl, msg) {

    let resindex = ctl.id.split("_");
    let irow = resindex[2].toString();
    let rowCount = $('#tbldt tr').length;
    rowCount = rowCount - 1;

    let result = confirm(msg);
    if (result) {

        let docno = $('#MainContent_txt_docno').val();
        let itemno = $('#MainContent_txt_SeqNo_' + irow.toString()).val();
        let drawing = $('#MainContent_txt_Drawing_' + irow.toString()).val();
        let modelcode = $('#MainContent_txt_ModelCode_' + irow.toString()).val();


        $.ajax({
            type: "POST",
            url: "ListItemDetail.aspx/DeleteRow",
            data: "{docno:'" + docno + "',itemno:'" + itemno + "',drawing:'" + drawing + "',modelcode:'" + modelcode + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {


            },
            failure: function (response) {
                alert(response.d);
            },
            error: function (response) {
                alert(response.d);
            }
        });


        if (rowCount > 1) {
            $(ctl).closest('tr').remove();

        }
        else {
            if (irow != "0") {

                $(ctl).closest('tr').remove();
            }
            else {
                $('#tbldt tbody>tr input[type="text"]').each(function () {
                    $(this).val("");
                });
                $('#tbldt tbody>tr select').each(function () {
                    $(this).val("");
                });

            }
        }




        var iseq = 1;
        $('#tbldt tbody>tr input[type="text"]').each(function () {
            var ctl = $(this).attr('id');
            if (ctl.indexOf("Seq") != -1) {
                $(this).val(iseq.toString());
                iseq++;
            }

        });

        return true;
    }
    else {

        return false;
    }



}





function getData() {
    let docno = $("#MainContent_txt_docno").val();
    

    $.ajax({
        type: "POST",
        url: "ListItemDetail.aspx/GetDataHeader",
        data: '{docno: "' + docno + '"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            var xmlDoc = $.parseXML(response.d);
            var xml = $(xmlDoc);
            var customers = xml.find("Table");
            if (customers.length == 0) {

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

            }
            $.each(customers, function () {
                var customer = $(this);





                $('#MainContent_txt_docno').val($(this).find("DocNo").text());
                $('#MainContent_ddl_section').val($(this).find("SectionNo").text());
                $('#MainContent_txt_shop').val($(this).find("Shop").text());
                $('#MainContent_ddl_scraptype').val($(this).find("ScrapTypeNo").text());
                $('#MainContent_ddl_ngfrom').val($(this).find("NGFromNo").text());
  
                var d = $(this).find("DocDate").text().slice(0, 10).split('-');
                var year = parseInt(d[0].toString());
                $('#d_RDate').val(d[2].toString());
                $('#m_RDate').val(d[1].toString());
                $('#y_RDate').val(year.toString());



                getapproveflow(1, $("#ddlskill"));
                getapproveflow(2, $("#ddleng"));
                getapproveflow(3, $("#ddlmgr"));
                getapproveflow(4, $("#ddlmgrdm"));


                //$("#tbldt :input").prop("disabled", true);
                //$("#MainContent_Panel1 :input").prop("disabled", true);

            });
        },
        failure: function (response) {
            alert(response.d);
        },
        error: function (response) {
            alert(response.d);
        }
    });



    //var ix = 0;
    //currentpagegv = 1;
    //getDataTable(ivno, ix);

   


}



function getDataTable() {

    let docno = $("#MainContent_txt_docno").val();
    let i = 0;
   
    $.ajax({
        type: "POST",
        url: "ListItemDetail.aspx/GetDataDetail",
        data: '{docno:"' + docno + '"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {

            var xmlDoc = $.parseXML(response.d);
            var xml = $(xmlDoc);
            var customers = xml.find("Table");
          
            if (response.d == "<NewDataSet />") {


                controlpage();
                var iseq = 1;

                setTimeout(function () {

                    $('#tbldt tbody>tr input[type="text"]').each(function () {
                        var ctl = $(this).attr('id');
                        if (ctl.indexOf("Seq") != -1) {

                            $(this).val(iseq.toString());
                            iseq++;

                        }
                        if (ctl.indexOf("Model") != -1) {
                            if ($(this).val() == "") {

                                $(this).closest('tr').remove();
                            }


                        }

                    });
                }, 500);



                response = "";
                xmlDoc = "";
                xml = "";
                customers = "";

            } else {



                $.each(customers, function () {
                    var customer = $(this);

                    try {
                        addrow("N");
             
                        $('#MainContent_txt_SeqNo_' + i.toString()).val($(this).find("ItemNo").text());          
                        $('#MainContent_txt_PartName_' + i.toString()).val($(this).find("PartName").text());
                        $('#MainContent_txt_ModelCode_' + i.toString()).val($(this).find("ModelCode").text());
                        $('#MainContent_txt_ModelName_' + i.toString()).val($(this).find("ModelName").text());
                        $('#MainContent_txt_Drawing_' + i.toString()).val($(this).find("Drawing").text());
                        $('#MainContent_ddl_Boi_' + i.toString()).val($(this).find("PartBoi").text());
                        $('#MainContent_txt_Defect_' + i.toString()).val($(this).find("PartDefect").text());
                        $('#MainContent_txt_DefectName_' + i.toString()).val($(this).find("PartDefectDetail").text());
                        


                        $('#MainContent_txt_Qty_' + i.toString()).val($(this).find("PartQty").text());
                        $('#MainContent_txt_PricePerUnit_' + i.toString()).val($(this).find("PartPircePerUnit").text());
                       
                        $('#MainContent_txt_PricePerGroup_' + i.toString()).val($(this).find("PartPriceScrap").text());


                        $('#MainContent_txt_Line_' + i.toString()).val($(this).find("PartLine").text());
                        $('#MainContent_txt_Machine_' + i.toString()).val($(this).find("PartMachine").text());
                        $('#MainContent_txt_Shift_' + i.toString()).val($(this).find("PartShift").text());
                  

                        $("#MainContent_ddl_Unit_" + i.toString()).html("");
                        $("#MainContent_ddl_Unit_" + i.toString()).append('<option value="' + $(this).find("PartPriceUnitNo").text() + '">' + $(this).find("PartPriceUnit").text() + '</option>');



                        $("#MainContent_ddl_PM_" + i.toString()).html("");
                        $("#MainContent_ddl_PM_" + i.toString()).append('<option value="' + $(this).find("PartPmWscdNo").text() + '">' + $(this).find("PartPmWscd").text() + '</option>');


                      
                        $('#MainContent_txt_Remark_' + i.toString()).val($(this).find("PartRemark").text());


                        i++;
                    
                    } catch (err) { } finally { };

                });

                response = "";
                xmlDoc = "";
                xml = "";
                customers = "";
                setTimeout(function () {
                    var iseq = 1;
                    $('#tbldt tbody>tr input[type="text"]').each(function () {
                        var ctl = $(this).attr('id');

                        if (ctl.indexOf("Seq") != -1) {

                            $(this).val(iseq.toString());
                            iseq++;

                        }
                        if (ctl.indexOf("Model") != -1) {
                            if ($(this).val() == "") {

                                $(this).closest('tr').remove();
                            }


                        }

                    });
                 

                }, 500);

            }
        },
        failure: function (response) {

        },
        error: function (response) {

        }
    });


}


function getapproveflow(level, ctl) {

    $(ctl).html("");

    let sectionname = $('#MainContent_ddl_section option:selected').text();
    let scrapttype = $('#MainContent_ddl_scraptype option:selected').text();

    
    $.ajax({
        type: "POST",
        url: "ApproveFlowData.aspx/getApproveFlow",
        data: '{section:"' + sectionname + '",level:"' + level + '",scrapttype:"' + scrapttype + '"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            var xmlDoc = $.parseXML(response.d);
            var xml = $(xmlDoc);
            var customers = xml.find("Table");
            $.each(customers, function () {
                var customer = $(this);


                $(ctl).append('<option value="' + $(this).find("ApproveID").text() + '">' + $(this).find("ApproveName").text() + '</option>');


            });
        },
        failure: function (response) {
            alert(response.d);
        },
        error: function (response) {
            alert(response.d);
        }
    });

}