let pubpartnumber = "";
let pubmachineid = "";
let pubpara = "";
let pubresult = "";
let pubserialnumber = "";
let pubstausng = "";
$(document).ready(function () {


    
    getdataheader();
    
    $("#ctl1_0").show();
    $("#ctl2_0").hide();
    $("#ctl3_0").hide();
    $('#MainContent_txt_toolname').on('focus', function (event) {

        getdata_checkitem();
    
        var iseq = 1;
        $('#tbldt tbody>tr span').each(function () {
            var ctl = $(this).attr('id');


            if (ctl.indexOf("itemname_") != -1 && ctl.indexOf("_0")==-1) {
                    

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


            }
            else
            {
                $(this).html("");

            }

        });
                   

        setTimeout(function () {
           
            $("#MainContent_txt_notice").focus();

        }, 300);



        //if ($("#tbldt tr").length == 2) {
        //getdata_checkitem();
        //}
       



    });


});

function getdataheader() {


    const today = new Date();
    let day = String(today.getDate()).padStart(2, '0');
    let month = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
    let year = today.getFullYear();
    

    let currentDate = year + '/' + month + '/' + day;
    
    let str_para = getUrlVars()["para"];
    pubpara = str_para;
    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 de_para = Base64.decode(str_para);
    if(de_para.indexOf("|")!=-1)
    {
        let arr_para = de_para.split('|');
        let ddate = arr_para[0];
        let partnumber = arr_para[1];
        let empid = arr_para[2];
        let empname = arr_para[3];
        let machineid = arr_para[4];
        let machinename = arr_para[5];
        let model = arr_para[6];
     

        pubpartnumber = partnumber;



        $("#MainContent_txt_partnumber").html(partnumber);
        $("#MainContent_txt_model").html(model);




        pubcurrentpage_checkitem = 1;
        pubirow = 0;
        pubfilter = " and b.partnumber='" + partnumber + "'";
        getdata_checkitem(1, 0, pubfilter, 0);



    }




}


function getdata(partnumber,toolname,serialnumber,irow) {
  

    let strhd = partnumber + '|' + serialnumber + '|' + toolname + '|';

    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);
  

    $.ajax({
        type: "POST",
        url: "Jig_input_check.aspx/getdata",
        data: '{parahd:"' + parahd + '"}',
        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) {



            }

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

                let status = $(this).find("resultstatus").text();
                  


                pubresult = status;
       

                if (status == "OK") {
                    $("#MainContent_btnok_"+irow.toString()).css({ 'background-color': '#77A9F9' });

                }
                else if (status == "NG") {
                    $("#MainContent_btnng_"+irow.toString()).css({ 'background-color': '#F8183D' });

                }





                $("#MainContent_txt_notice").val($(this).find("notice").text());
                let piz = $(this).find("insp_piz").text();
                  

                if (piz.length > 0) {
                    $("#piz_"+irow.toString()).attr('src', piz);

                }

            });


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

    



}



function newrows(ctl) {

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


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

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

            let irow = parseFloat(arrctl[3].toString());
            irow = irow + 1;
            addrow("Y");
            return false;



        }
    });

}

let inputrow_checkitem = 0;
function addrow(news) {

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


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

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

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

        }
    });

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

    


}
function getnext_checkitem() {


    var elmnt = document.getElementById("gtbl");

    var y = elmnt.scrollTop;
    var x = elmnt.scrollHeight;
    var n = elmnt.clientHeight;
    //$("#MainContent_txt_linegroup").val(parseInt(y, 0).toString() + "," + (x - n).toString());
    if (parseInt(y, 0) == (x - n) - 1 || parseInt(y, 0) == (x - n)) {

        pubcurrentpage_checkitem = pubcurrentpage_checkitem + 1;
        try {

            setTimeout(
              function () {
                  getdata_checkitem(pubcurrentpage_checkitem, pubirow_checkitem, pubfilter_checkitem, parseInt(y, 0));



              }, 500);



        } catch (err) { };



    }

}


let pubirow_checkitem = 0;
let pubcurrentpage_checkitem = 1;
let pubfilter_checkitem = "";


function getdata_checkitem(currentpage, i, filterstring, scposstock) {
    $("#loader").show();
    pubfilter_checkitem = filterstring;
    $.ajax({
        type: "POST",
        url: "Jig_input_check.aspx/getItem",
        data: '{currentpage:"' + currentpage + '",filterstring:"' + filterstring + '"}',
        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 />") {

                $("#loader").hide();
                if (currentpage == '1') {
                    //alert("Data not found.");
                    //setTimeout(function () {
                    //location.reload();
                    //}, 1000);
                }

                //currentpagegv = 1;

                controlpage();
                var iseq = 1;

                setTimeout(function () {

                    $('#tbldt tbody>tr span').each(function () {
                        var ctl = $(this).attr('id');

                        if (ctl.indexOf("itemname_") != -1) {
                            var arr = ctl.split('_');
                            if (arr[3] != "0" && $(this).html() == "") {

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


                            }


                        }

                    });
                }, 500);



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

            } else {



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

                    try {
                        addrow("N");
                  

                        $("#MainContent_txt_toolname_" + i.toString()).html($(this).find("toolname").text());
                        $("#MainContent_txt_serialnumber_" + i.toString()).html($(this).find("serial").text());
                        $("#MainContent_txt_expiredate_" + i.toString()).html($(this).find("expiredate").text());

                        let partnumber = $("#MainContent_txt_partnumber").html();
                        let serialnumber = $("#MainContent_txt_serialnumber_" + i.toString()).html();
                        let toolname = $("#MainContent_txt_toolname_" + i.toString()).html();


                        getdata(partnumber, toolname, serialnumber,i.toString());



                        i++;
                        pubirow_checkitem = i;
                    } catch (err) { } finally { };
                    $("#loader").hide();
                });



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


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

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


                        }

                    });
                    if (currentpage > 1) {
                        $("#tbdy").animate({ scrollTop: (scposstock + 100) }, "fast");
                    }

                }, 500);

            }
        },
        failure: function (response) {

        },
        error: function (response) {

        }
    });






}
function checklen(ctl) {
    let notic = $("#MainContent_txt_notice").val();
    let remain = 100 - notic.length;
    $("#strnotice").html("Input have remaining " + remain.toString() + " character.");
    if (remain <= 0) {
        $("#MainContent_txt_notice").val(notic.substring(0, 99));

    }


}

function save(ctl) {

    let ctlid = $(ctl).attr('id');
    let arr = ctlid.split('_');
    let irow = arr[2];


    if (ctlid.indexOf("btnok") != -1) {
        if ($("#" + ctlid).css('background-color') == "rgb(227, 188, 8)") {

            $("#MainContent_btnok_" + irow.toString()).css('background-color', '#77A9F9');
            $("#MainContent_btnng_" + irow.toString()).css('background-color', '#E3BC08');
            pubresult = "OK";
            pubstausng = "";
        }
        else {

            $("#MainContent_btnok_" + irow.toString()).css('background-color', '#E3BC08');
            $("#MainContent_btnng_" + irow.toString()).css('background-color', '#E3BC08');
            pubresult = "";

        }

    }
    else {
        if ($("#" + ctlid).css('background-color') == "rgb(227, 188, 8)") {
            $("#MainContent_btnng_" + irow.toString()).css('background-color', '#F8183D');
            $("#MainContent_btnok_" + irow.toString()).css('background-color', '#E3BC08');
            pubresult = "NG";
            pubstausng = "Y";
        }
        else {

            $("#MainContent_btnok_" + irow.toString()).css('background-color', '#E3BC08');
            $("#MainContent_btnng_" + irow.toString()).css('background-color', '#E3BC08');
            pubresult = "";

        }

    }


    let notic = $("#MainContent_txt_notice").val();
    if (pubresult == "") {
        swal(
         'Warning',
         'Please select status.',
         'warning'
       );

    }
        //else if ($("#MainContent_btnng_" + irow.toString()).css('background-color') == "rgb(248, 24, 61)" && notic.length == 0) {

        //    swal(
        //      'Warning',
        //      'Please input notice.',
        //      'warning'
        //    );
        //    $("#MainContent_txt_notice").focus();
        //    $("#MainContent_txt_notice").select();

        //}
    else {

        let strhd = "";
        let partnumber = $("#MainContent_txt_partnumber").html();
        let serialnumber = $("#MainContent_txt_serialnumber_" + irow.toString()).html();
        let toolname = $("#MainContent_txt_toolname_" + irow.toString()).html();
        let type = "1";
        let result = pubresult;
        let notic = $("#MainContent_txt_notice").val();
        let user = "admin";
        let piz = $('#piz_'+irow.toString()).attr('src');
        let inspectid_id = "";


        strhd = inspectid_id + '|' + partnumber + '|' + serialnumber + '|' + toolname + '|' + type + '|' + result + '|' + notic + '|' + user + '|' + piz + '|';

        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);



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

                if (res.length == 0) {

                    swal({
                        title: "Success",
                        type: 'success',
                        text: "Data saved successfully",
                        timer: 1000,
                        showConfirmButton: false,
                        timerProgressBar: true
                    })

   .then(() => {
       swal({ 
           title: 'Finished!',
           type: 'info',
           timer: 1000,
           showConfirmButton: false
       })
   })
                   


                }
                else {

                    swal(
                     'Error',
                      res,
                     'error'
                     );

                }


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


    }



}

var interval;
var pubirow = "";
function takephoto(ctl) {

    let ctlid = $(ctl).attr('id');
    let arr = ctlid.split('_');
    let irow = arr[3];
    pubirow = irow;

    let serialnumber = $("#MainContent_txt_serialnumber_" + irow.toString()).html();


    let para = pubpartnumber + "|" + serialnumber + "|3|";
    //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 str_para = Base64.encode(para);
    setCookie("pizinfo", "", 5);

    let str_para = para;
    interval = setInterval(checkscan, 1000);


    window.open(
"Opencamera_takephoto?para=" + str_para,
'_blank' // <- This is what makes it open in a new window.
);




}
function checkscan() {
    let piz_data = getCookie("pizinfo");
    if (piz_data.length > 0) {

        //setCookie("pizinfo", "", 5);
        clearInterval(interval);
        $("#piz_"+pubirow.toString()).attr('src', piz_data);


    }

}

function saveall()
{
    let notic = $("#MainContent_txt_notice").val();
    if (pubstausng=="Y" && notic.length == 0) {

        swal(
          'Warning',
          'Please input notice.',
          'warning'
        );
        $("#MainContent_txt_notice").focus();
        $("#MainContent_txt_notice").select();

    }
    else
    {

        swal({
            title: 'Are you sure?',
            text: "Do you want request apporve!",
            type: 'question',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Yes, request it!'
        }).then(function (result) {
            if (result.value) {

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


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

                        var ctlname = $(this).attr('id');
                        var arrctl = ctlname.split('_');
                        var irow = arrctl[3];
                        if ($("#" + ctlname).html().length > 0) {
                           

                            let strhd = "";
                            let partnumber = $("#MainContent_txt_partnumber").html();
                            let model = $("#MainContent_txt_model").html();
                            let toolname = $("#MainContent_txt_toolname").val();
                            let toolno = $("#MainContent_txt_toolno").val();
                            let notic = $("#MainContent_txt_notice").val();
                            let user = "admin";
                            let piz = $('#piz_' + irow.toString()).attr('src');
                        

                            strhd =  partnumber + '|' + model + '|'+ toolno + '|'   + toolname + '|'+ notic +'|' + piz + '|';

                            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);



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

                                    if(res=="")
                                    {
                                        setTimeout(function () {
                                            window.location.href = "Tool_input_check?para="+pubpara;
                                        }, 1000);
                                    }


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



                        }



                    }

                });



                $.ajax({
                    type: "POST",
                    url: "Jig_input_check.aspx/sendmail",
                    data: '{partnumber: "' + pubpartnumber + '"}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        res = response.d;

                    },
                    failure: function (response) {
                        //alert(response.d);
                    },
                    error: function (response) {
                        //alert(response.d);
                    }
                });
             
                swal(
                'Success',
                'Data saved successfully',
                'success'
                );

                //setTimeout(function () {
                //    window.location.href = "Jig_check";
                //}, 1000);


            } else {
                return false;
            }
        })


       


    }

}


function getdata_checkitem() {
    let i=0;
    $("#loader").show();
    let toolno = $("#MainContent_txt_toolno").val();

    $.ajax({
        type: "POST",
        url: "Tool_input_check.aspx/getsubdata",
        data: '{id:"' + toolno + '"}',
        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 />") {

                $("#loader").hide();
                if (currentpage == '1') {
                    //alert("Data not found.");
                    //setTimeout(function () {
                    //    location.reload();
                    //}, 1000);
                }

                //currentpagegv = 1;

               
                var iseq = 1;

                setTimeout(function () {

                    $('#tbldt tbody>tr span').each(function () {
                        var ctl = $(this).attr('id');

                        if (ctl.indexOf("itemname_") != -1) {
                            var arr = ctl.split('_');
                            if (arr[3] != "0" && $(this).html() == "") {

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


                            }


                        }

                    });
                }, 500);



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

            } else {



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

                    try {
                        addrow("N");


                        $("#MainContent_txt_seqno_" + i.toString()).html($(this).find("itemno").text());
                        $("#MainContent_txt_itemname_" + i.toString()).html($(this).find("itemname").text());
                        let howcheck = $(this).find("howcheck").text();
                        if(howcheck=="1")
                        {
                            $("#ctl1_"+i.toString()).show();
                            $("#ctl2_"+i.toString()).hide();
                            $("#ctl3_"+i.toString()).hide();

                        }
                        else if(howcheck=="2")
                        {
                            $("#ctl2_"+i.toString()).show();
                            $("#ctl1_"+i.toString()).hide();
                            $("#ctl3_"+i.toString()).hide();

                        }
                        else if(howcheck=="3")
                        {
                            $("#ctl3_"+i.toString()).show();
                            $("#ctl2_"+i.toString()).hide();
                            $("#ctl1_"+i.toString()).hide();

                        }




                        i++;
              
                    } catch (err) { } finally { };
                    $("#loader").hide();
                });



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


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

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


                        }

                    });
                   

                }, 500);

            }
        },
        failure: function (response) {

        },
        error: function (response) {

        }
    });






}

function savedetail(ctl,result)
{
    let toolno = $("#MainContent_txt_toolno").val();
    let toolname = $("#MainContent_txt_toolname").val();
    let partnumber = $("#MainContent_txt_partnumber").html();
    let model = $("#MainContent_txt_model").html();

    let ctlid = $(ctl).attr('id');
    let arrctl = ctlid.split('_');
    let irow = arrctl[2].toString();
    let piz = $('#piz_'+irow.toString()).attr('src');

    let itemno = $("#MainContent_txt_seqno_"+arrctl[2].toString()).html();
    let itemname = $("#MainContent_txt_itemname_"+arrctl[2].toString()).html();


    if(result=="2")
    {
        result = $('#MainContent_txt_serialno_'+arrctl[2].toString()).val();

    }
    else if(result=="3")
    {
        result = $('#MainContent_txt_duedate_'+arrctl[2].toString()).val();

    }



    let user = "admin";
    let inspectid_id = "";


    strhd = inspectid_id + '|' + partnumber + '|' + toolno + '|' + toolname + '|' + model + '|' + itemno + '|' + itemname + '|' + result + '|'  + user + '|' + piz + '|';

    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);

          

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

            if (res.length == 0) {


                if (ctlid.indexOf("btnok") != -1) {
                    if ($("#" + ctlid).css('background-color') == "rgb(227, 188, 8)") {

                        $("#MainContent_btnok_" + irow.toString()).css('background-color', '#77A9F9');
                        $("#MainContent_btnng_" + irow.toString()).css('background-color', '#E3BC08');
                        pubresult = "OK";
                        pubstausng = "";
                    }
                    else {

                        $("#MainContent_btnok_" + irow.toString()).css('background-color', '#E3BC08');
                        $("#MainContent_btnng_" + irow.toString()).css('background-color', '#E3BC08');
                        pubresult = "";

                    }

                }
                else {
                    if ($("#" + ctlid).css('background-color') == "rgb(227, 188, 8)") {
                        $("#MainContent_btnng_" + irow.toString()).css('background-color', '#F8183D');
                        $("#MainContent_btnok_" + irow.toString()).css('background-color', '#E3BC08');
                        pubresult = "NG";
                        pubstausng = "Y";
                    }
                    else {

                        $("#MainContent_btnok_" + irow.toString()).css('background-color', '#E3BC08');
                        $("#MainContent_btnng_" + irow.toString()).css('background-color', '#E3BC08');
                        pubresult = "";

                    }

                }

                swal({
                    title: "Success",
                    type: 'success',
                    text: "Data saved successfully",
                    timer: 1000,
                    showConfirmButton: false,
                    timerProgressBar: true
                })


.then(() => {
    swal({ 
        title: 'Finished!',
        type: 'info',
        timer: 1000,
        showConfirmButton: false
    })
})


            }
            else {

                swal(
                 'Error',
                  res,
                 'error'
                 );

            }


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







}
