$(document).ready(function () {

    
 
        try {

            getdataapprove();

        } catch (err) { }

        $("#loader").hide();
        let month = $("#MainContent_txt_checkmonth").val();
        let year = $("#MainContent_txt_checkyear").val();

        getdataheader();

        if ($("#MainContent_txt_linegroup").val().length == 0) {

            getdata_tbl_header(year, month);

        }
   

     


        $('#MainContent_txt_linegroup').on('focus', function (event) {
            $("#MainContent_txt_model").val("");
            $('#tbldt tbody>tr td').each(function () {
                var ctl = $(this).attr('id');
                if (ctl.indexOf("_0") == -1) {

                    if ($(this).html() == "") {

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

                }
                else {

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


            });

        });
        $('#MainContent_txt_model').on('focus', function (event) {

            if ($("#tbldt tr").length == 2) {
                pubcurrentpage_checkitem = 1;
                irow = 0;
                getmachine(1, 0, 0);
            }
            else if ($("#MainContent_txt_model").val() != "") {
              
                pubcurrentpage_checkitem = 1;
                irow = 0;
                getmachine(1, 0, 0);
            }
        });

        setTimeout(function () {
            if ($("#MainContent_txt_model").val() != "") {

                pubcurrentpage_checkitem = 1;
                irow = 0;
                getmachine(1, 0, 0);
            }
        }, 2000);


});




function getcalendar() {
    

    let month = $("#MainContent_txt_checkmonth").val();
    let year = $("#MainContent_txt_checkyear").val();
    inputrow = 0;
    getdata_tbl_header(year, month);


}

function getdataapprove()
{
    try {
        let str_para = getUrlVars()["user"];
        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 userid = arr_para[0];
            let username = arr_para[1];
            let position = arr_para[2];
            let linegroup = arr_para[3];
            let linename = arr_para[4];
            let partnumber = arr_para[5];
            let month = arr_para[6];
            let year = arr_para[7];

            month = parseFloat(month);

            $("#MainContent_txt_linegroup").val(linegroup);
            $("#MainContent_txt_linename").val(linename);
            $("#MainContent_txt_partnumber").val(partnumber);
            $("#MainContent_txt_checkmonth").val(month.toString());
            $("#MainContent_txt_checkyear").val(year);
            $("#lbluserid").html(userid);
            $("#lblfullname").html(username);
            $("#lblposition").html(position);

            setCookie("userid", userid, 5);
            setCookie("username",userid, 5);
            setCookie("fullname", username, 5);
            setCookie("position", position, 5);
          


            getdata_tbl_header(year, month);

        }
    } catch (err) { }


}

function getdataheader() {


    try {
        let str_para = getUrlVars()["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 linegroup = arr_para[0];
            let linename = arr_para[1];
            let partnumber = arr_para[2];
            let machinename = arr_para[3];
            let month = arr_para[4];
            let year = arr_para[5];
            let lamp = arr_para[8];
            let model = arr_para[9];



            $("#MainContent_txt_linegroup").val(linegroup);
            $("#MainContent_txt_linename").val(linename);
            $("#MainContent_txt_partnumber").val(partnumber);
            $("#MainContent_txt_checkmonth").val(month);
            $("#MainContent_txt_checkyear").val(year);
            $("#MainContent_txt_lamp").val(lamp);
            $("#MainContent_txt_model").val(model);


            getdata_tbl_header(year, month);
            
        }
    } catch (err) { }

}


function showdata()
{

    let linename = $("#MainContent_txt_linename").val();
    let model = $("#MainContent_txt_model").val();
    let lamp = $("#MainContent_txt_lamp").val();


    $.ajax({
        type: "POST",
        url: "Machine_inspection_result.aspx/Get_linggroupid",
        data: '{lamp: "' + lamp + '",model:"' + model + '",linename:"' + linename + '"}',
        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);

                $('#MainContent_txt_linegroup').val($(this).find("linegroupid").text());
                if ($("#tbldt tr").length == 2) {

                    pubcurrentpage_checkitem = 1;
                    irow = 0;
                    getmachine(1, 0, 0);
                }

            });





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


 


    setTimeout(function () {
        getinspection();
    }, 1000);
 


}




function newrows(ctl) {

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


        if ($(this).attr('id').indexOf('Seq') != -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 = 0;
function addrow(news) {


    $('#tbldt tbody>tr:last').find('td').each(function () {
        try {

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

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

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



            }
        } catch (err) { };
    });

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


    let iseq = 1;
    $('#tbldt tbody>tr td').each(function () {
        try {
            let ctl = $(this).attr('id');
            if (ctl.indexOf("seqno_") != -1) {
                if ($(this).is(':hidden')) {

                } else {

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

                }
            }
        } catch (err) { };
    });






}


let tbl0 = "";
let tbl1 = "";
let tbl2 = "";
function getdata_tbl_header(year, month) {

    $("#loader").show();
    tbl0 = "<table class='tbl' id='tbldt' style='width:1108px;'>";


    tbl1 = "<thead><tr><th style='width:3%;'><span style='font-size:12px;' id='seqno'>No.</span><br><input type='button' id='btnselectall' class='button' value='All'  style='max-width:30px;max-height:30px;color:white;background-color:#C8CDC7;padding:0.1em;display:none;' onclick=\"javascript:selectall();return false;\"  /></th></th><th style='width:15%;'><span style='font-size:12px;' id='machinename'>Machine Name</span></th>";
    tbl2 = "<tbody id='tbdy'><tr><td id='seqno_0' style='width:20px;cursor:pointer;' onclick='javascript:selectitems(this);return false;'></td><td id='machinename_0'></td>";



    $.ajax({
        type: "POST",
        url: "Machine_inspection_result.aspx/GetAllDay",
        data: '{year: "' + year + '",month:"' + month + '"}',
        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);
               
             
                tbl1 = tbl1 + "<th id='daylisthd_"+$(this).find("listnumday").text()+"' style='width:20px;cursor:pointer;' onclick='javascript:selectday(this);return false;'><span style='font-size:12px;' id='hdday_" + $(this).find("listnumday").text() + "'>" + $(this).find("listnumday").text() + "</span></th>";
                tbl2 = tbl2 + "<td id='daylist" + $(this).find("listnumday").text() + "_0' ></td>";


            });


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


    setTimeout(function () {
        tbl1 = tbl1 + "</tr></thead>";
        let strtbl = tbl0 + tbl1 + tbl2 + "</tr></tbody></table>";

        $("#g2").html(strtbl);

        $.ajax({
            type: "POST",
            url: "Machine_inspection_result.aspx/GetDayOff",
            data: '{year: "' + year + '",month:"' + month + '"}',
            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);

                    $('#daylisthd_' + $(this).find("listnumday").text()).css({ 'background-color': '#B0B0AE' });
                    $('#daylist' + $(this).find("listnumday").text() + '_0').css({ 'background-color': '#B0B0AE' });

                });


                //pubcurrentpage_checkitem = 1;
                //irow = 0;
                //getmachine(1,0,0);

                $("#loader").hide();




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



    }, 1500);



  

}

function getinspection()
{
   
    $('#tbldt tbody>tr td').each(function () {

        var ctl = $(this).attr('id');
        if (ctl.indexOf("daylist") != -1) {
            $(this).html("");

            if ($("#" + ctl).css('background-color') == "rgb(203, 254, 116)") {

                $("#" + ctl).css({ 'background-color': 'rgba(0, 0, 0, 0)' });

            }
            if ($("#" + ctl).css('background-color') == "rgb(175, 214, 251)") {

                $("#" + ctl).css({ 'background-color': 'rgba(0, 0, 0, 0)' });

            }

          
               
        }

    });
  
    setTimeout(function () {
        $('#tbldt tbody>tr span').each(function () {
            try {
                var ctl = $(this).attr('id');
                if (ctl.indexOf("mchid_") != -1) {
                    if ($(this).html() != "") {

                        let arr = ctl.split('_');


                        let linegroup = $("#MainContent_txt_linegroup").val();
                        let partnumber = $("#MainContent_txt_partnumber").val();
                        let month = $("#MainContent_txt_checkmonth").val();
                        let year = $("#MainContent_txt_checkyear").val();
                        let machineid = $(this).html();
                        let userid = $("#lbluserid").html();



                        let strsend = linegroup + '|' + partnumber + '|' + month + '|' + year + '|' + machineid + '|' + userid + '|';


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



                        $.ajax({
                            type: "POST",
                            url: "Machine_inspection_result.aspx/getinspection",
                            data: '{parahd: "' + parahd + '"}',
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            success: function (response) {
                                var xmlDocx = $.parseXML(response.d);
                                var xmlx = $(xmlDocx);
                                var dataset = xmlx.find("Table");
                                if (dataset.length == 0) {



                                }

                                $.each(dataset, function () {
                                    var dataset = $(this);
                                    let status = $(this).find("resultstatus").text();
                                    let dayvale = $(this).find("daytocheck").text();
                                    let resultvalue = "";

                                    if (status == "OK") {

                                        resultvalue = "<span style='weight:900;color:#07B214;'>&#10004;</span>";

                                    }
                                    else {

                                        resultvalue = "<span style='weight:900;color:#B91A0A;'>&#10005;</span>";

                                    }


                                    let confirm = $(this).find("cm_status").text();
                                    let approve = $(this).find("app_status").text();
                              
                                    if (confirm != "" && approve=="") {

                                        $("#daylist" + dayvale.toString() + "_" + arr[1].toString()).html(resultvalue);
                                        $("#daylist" + dayvale.toString() + "_" + arr[1].toString()).css({ 'background-color': '#cbfe74' });
                                    }
                                    else if (approve != "") {

                                        $("#daylist" + dayvale.toString() + "_" + arr[1].toString()).html(resultvalue);
                                        $("#daylist" + dayvale.toString() + "_" + arr[1].toString()).css({ 'background-color': '#afd6fb' });
                                    }
                                    else
                                    {
                                        $("#daylist" + dayvale.toString() + "_" + arr[1].toString()).html(resultvalue);
                                    }




                                });

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




                    }


                }
            } catch (err) { };

        });
    }, 500);


    


}


let pubirow = 0;
let pubiseqno = 1;
let pubcurrentpage_checkitem = 1;

function getnext_machineitem() {


    //var elmnt = document.getElementById("g2");
    //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)) {


    //    try {

    //        setTimeout(
    //          function () {
    //              let len = $("#tbldt tbody").find('tr').length;
    //              pubcurrentpage_checkitem =  Math.ceil((len - 1) / 10);
                
    //              pubcurrentpage_checkitem = pubcurrentpage_checkitem + 1;
    //              getmachine(pubcurrentpage_checkitem,pubirow, parseInt(y, 0));



    //          }, 500);



    //    } catch (err) { };



    //}

}

function getmachine(currentpage,irow, scposstock) {




    let linegroup = $("#MainContent_txt_linegroup").val();
    let partnumber = $("#MainContent_txt_partnumber").val();


    let iseqno = irow + 1;

    $.ajax({
        type: "POST",
        url: "Machine_inspection_result.aspx/GetMachineMaster",
        data: '{currentpage:"' + currentpage + '",linegroup:"'+linegroup+'",partnumber:"'+partnumber+'"}',
        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) {

                setTimeout(function () {

                    $('#tbldt tbody>tr td').each(function () {
                        var ctl = $(this).attr('id');
                        if (ctl.indexOf("machinename_") != -1 && ctl.indexOf("_0") == -1) {

                            if ($(this).html() == "") {

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

                        }


                    });
                }, 500);

            }

            $.each(customers, function () {
                var customer = $(this);
                
                addrow("Y");
         
                $('#machinename_' + irow.toString()).html("<a href='#' onclick=\"javascript:resultdetail('" + $(this).find("machinename").text() + "');return false;\" style='text-decoration:none;font-size:14px'>" + $(this).find("machinename").text() + "</a><span id='mchid_" + irow.toString() + "' style='font-size:0.1px;color:white;opacity:0.1;'>" + $(this).find("machineid").text() + "</span>");
                $('#seqno_' + irow.toString()).html(iseqno.toString());              



                irow++;
                iseqno++;

            });


          
            //$("#MainContent_btnselect").click();
            pubirow = irow;
            //if (currentpage > 1) {
            //    $("#tbdy").animate({ scrollTop: (scposstock + 100) }, "fast");
            //}

            if (customers.length > 0) {

                setTimeout(function () {

                    let len = $("#tbldt tbody").find('tr').length;
                    pubcurrentpage_checkitem = Math.ceil((len - 1) / 10);
                    pubcurrentpage_checkitem = pubcurrentpage_checkitem + 1;
                    getmachine(pubcurrentpage_checkitem, pubirow, 0);



                }, 500);


            }
            

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


}
function resultdetail(machinename)
{
   

    let linegroup = $("#MainContent_txt_linegroup").val();
    let linename = $("#MainContent_txt_linename").val();    
    let partnumber = $("#MainContent_txt_partnumber").val();
    let month = $("#MainContent_txt_checkmonth").val();
    let year = $("#MainContent_txt_checkyear").val();
    let userid = $("#lbluserid").html();

    let lamp = $("#MainContent_txt_lamp").val();  
    let model = $("#MainContent_txt_model").val();



    if (linegroup.length == 0 || partnumber.length == 0) {


                           swal(
                           'Warning',
                           'Please select Line group and Part number.',
                           'warning'
                           );

    }
    else {

        let para = linegroup + "|" + linename + "|" + partnumber + "|" + machinename + "|" + month + "|" + year + "|" + dayvalue + "|" + itemvalue + "|" + lamp + "|" + model + "|" + userid+ "|";
        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);

        window.location.href = "Machine_inspection_sub_result?para=" + str_para;
    }


}

function createnextmonth() {



    let linegroup = $("#MainContent_txt_linegroup").val();
    let linename = $("#MainContent_txt_linename").val();
    let partnumber = $("#MainContent_txt_partnumber").val();
    let userid = $("#lbluserid").html();


    $.ajax({
        type: "POST",
        url: "Machine_inspection_result.aspx/checkconfirmapprove",
        data: '{linegroup:"' + linegroup + '",partnumber:"'+partnumber+'",userid:"'+userid+'"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            //Real Y
     
            if (response.d == "Y") {

                let linegroup = $("#MainContent_txt_linegroup").val();
                let linename = $("#MainContent_txt_linename").val();
                let partnumber = $("#MainContent_txt_partnumber").val();
                let month = $("#MainContent_txt_checkmonth").val();
                let year = $("#MainContent_txt_checkyear").val();
                let machine = "";

                let lamp = $("#MainContent_txt_lamp").val();
                let model = $("#MainContent_txt_model").val();
                let userid = $("#lbluserid").html();

                let para = linegroup + "|" + linename + "|" + partnumber + "|" + machine + "|" + month + "|" + year + "|" + dayvalue + "|" + itemvalue + "|" + lamp + "|" + model + "|" + userid+ "|";

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

                window.location.href = "Machine_checklist_create?para=" + str_para;

            }
            else {
                swal(
                    'Warning',
                    'Please kindly approval of this inspection.',
                    'warning'
                    );


            }


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


}

function approve()
{


    let linegroup = $("#MainContent_txt_linegroup").val();
    let linename = $("#MainContent_txt_linename").val();
    let partnumber = $("#MainContent_txt_partnumber").val();
    let userid = $("#lbluserid").html();



    $.ajax({
        type: "POST",
        url: "Machine_inspection_result.aspx/checkconfirm",
        data: '{linegroup:"' + linegroup + '",partnumber:"'+partnumber+'",userid:"'+userid+'"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            
            if(response.d=="Y")
            {

                let linegroup = $("#MainContent_txt_linegroup").val();
                let linename = $("#MainContent_txt_linename").val();
                let partnumber = $("#MainContent_txt_partnumber").val();
                let machine = "";

                let month = $("#MainContent_txt_checkmonth").val();
                let year = $("#MainContent_txt_checkyear").val();
                let lamp = $("#MainContent_txt_lamp").val();
                let model = $("#MainContent_txt_model").val();

         



                let para = linegroup + "|" + linename + "|" + partnumber + "|" + machine + "|" + month + "|" + year + "|" + dayvalue + "|" + itemvalue + "|" + lamp + "|" + model + "|";
                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);

                window.location.href = "Machine_inspection_approval?para=" + str_para;



            }
            else
            {
                swal(
                    'Warning',
                    'Please kindly confirm receipt of this inspection.',
                    'warning'
                    );


            }


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


}


let old_hd_css = "";
let old_dt_css = "";
let hdclick = "";
let dtclick = "";


let dayvalue = "";
let itemvalue = "";


function selectday(ctl) {
    dtclick = "";
    itemvalue = "";
    if (hdclick == "") {
        $('#tbldt tbody>tr td').each(function () {
            try {
                let ctltdid = $(this).attr('id');
                if ($("#" + ctltdid).css('background-color') == "rgb(148, 238, 182)") {

                    $("#" + ctltdid).css({ 'background-color': 'rgba(0, 0, 0, 0)' });

                }

            } catch (err) { }

        });
        hdclick = "Y";
    }

    setTimeout(function () {
        let ctlhdid = $(ctl).attr('id');
        
        if ($("#" + ctlhdid).css('background-color') == "rgb(239, 239, 239)") {


            old_hd_css = $("#" + ctlhdid).css('background-color');
            $("#" + ctlhdid).css({ 'background-color': '#94EEB6' });

        }
        else if ($("#" + ctlhdid).css('background-color') != "rgb(176, 176, 174)") {

            $("#" + ctlhdid).css({ 'background-color': old_hd_css });
        }


        let arr = ctlhdid.split('_');
        let irow = arr[1];

        dayvalue = dayvalue + $("#hdday_" + irow).html() + "*";


        $('#tbldt tbody>tr td').each(function () {

            let ctltdid = $(this).attr('id');

            if (ctltdid.indexOf("daylist" + irow.toString() + "_") != -1) {

                if ($("#" + ctltdid).css('background-color') == "rgba(0, 0, 0, 0)" || $("#" + ctltdid).css('background-color') == "rgb(176, 176, 174)") {

                    old_dt_css = $("#" + ctltdid).css('background-color');
                    $("#" + ctltdid).css({ 'background-color': '#94EEB6' });

                }
                else
                {


              
                    if ($(ctl).css('background-color') == "rgb(176, 176, 174)")
                    {
                        let bckcolor = $(ctl).css('background-color');
                        $("#" + ctltdid).css({ 'background-color': bckcolor });
                    }
                    else
                    {
                        $("#" + ctltdid).css({ 'background-color':'rgba(0, 0, 0, 0)' });

                    }


                }



            }



        });
    }, 300);

}
function selectitems(ctl) {


    //rgb(239, 239, 239);
    //rgba(0, 0, 0, 0);


    //rgb(176, 176, 174)
    dayvalue = "";
    hdclick = "";
    $('#tbldt thead>tr th').each(function () {
        try {
            let ctlthid = $(this).attr('id');
            if (ctlthid.indexOf("daylisthd_") != -1) {


                if ($("#" + ctlthid).css('background-color') == "rgb(148, 238, 182)") {

                    $("#" + ctlthid).css({ 'background-color': 'rgb(239, 239, 239)' });

                    let arr = ctlthid.split('_');
                    let irow = arr[1];
                    $('#tbldt tbody>tr td').each(function () {
                        try {
                            let ctltdid = $(this).attr('id');
                            if (ctltdid.indexOf("daylist" + irow.toString() + "_") != -1) {


                                if ($("#" + ctltdid).css('background-color') == "rgb(148, 238, 182)") {

                                    $("#" + ctltdid).css({ 'background-color': 'rgba(0, 0, 0, 0)' });

                                }
                               

                            }
                        } catch (err) { }

                    });


                } else if ($("#" + ctlthid).css('background-color') == "rgb(176, 176, 174)") {

                    let arr = ctlthid.split('_');
                    let irow = arr[1];
                    $('#tbldt tbody>tr td').each(function () {
                        try {
                            let ctltdid = $(this).attr('id');
                            if (ctltdid.indexOf("daylist" + irow.toString() + "_") != -1) {


                              

                                    $("#" + ctltdid).css({ 'background-color': 'rgb(176, 176, 174)' });



                            }
                        } catch (err) { }

                    });


                }


            }
        } catch (err) { }

    });




    let ctlhdid = $(ctl).attr('id');
    if ($("#" + ctlhdid).css('background-color') == "rgba(0, 0, 0, 0)") {


        old_hd_css = $("#" + ctlhdid).css('background-color');
        $("#" + ctlhdid).css({ 'background-color': '#94EEB6' });

    }
    else {

        $("#" + ctlhdid).css({ 'background-color': old_hd_css });
    }


    let arr = ctlhdid.split('_');
    let irow = arr[1];
    itemvalue = itemvalue + $("#mchid_" + irow).html() + "*";



    $('#tbldt tbody>tr td').each(function () {

        let ctltdid = $(this).attr('id');

        if (ctltdid.indexOf("_" + irow.toString()) != -1 && ctltdid.indexOf("seqno_") == -1) {

            if ($("#" + ctltdid).css('background-color') == "rgba(0, 0, 0, 0)") {

                old_dt_css = $("#" + ctltdid).css('background-color');
                $("#" + ctltdid).css({ 'background-color': '#94EEB6' });


            }
            else if ($("#" + ctltdid).css('background-color') == "rgb(148, 238, 182)") {


                $("#" + ctltdid).css({ 'background-color': old_dt_css });


            }



        }



    });


}

let selectstatus = "";
function selectall()
{
  
   
   
    if (selectstatus == "") {
        selectstatus = "Y";

    }
    else if (selectstatus == "Y") {
        selectstatus = "N";
        itemvalue = "";

    }
    else if (selectstatus == "N") {
        selectstatus = "Y";

    }





        $('#tbldt tbody>tr td').each(function () {

            let ctltdid = $(this).attr('id');


            let arr = ctltdid.split('_');
            let irow = arr[1];
            if (ctltdid.indexOf('seqno_') != -1)
            {
                itemvalue = itemvalue + $("#mchid_" + irow).html() + "*";

            }


            if (selectstatus == "Y" && $("#" + ctltdid).css('background-color') != "rgb(176, 176, 174)") {


                $("#" + ctltdid).css({ 'background-color': '#94EEB6' });

            }
            else if (selectstatus == "N" && $("#" + ctltdid).css('background-color') != "rgb(176, 176, 174)") {


                $("#" + ctltdid).css({ 'background-color': 'white' });
            }


        });

    


    
}

function controltable_css() {



    $('#tbldt thead>tr td').each(function () {
        let ctlid = $(this).attr('id');
        let arr = ctlid.split('_');
        let irow = arr[1];

        if (ctlid.indexOf("daylist" + irow.toString()) != -1)
        {
            $("#" + ctlid).css({ 'background-color': '#08B21F' });

        }
       


    });



}

function confirmmessage() {



    let linegroup = $("#MainContent_txt_linegroup").val();
    let linename = $("#MainContent_txt_linename").val();
    let partnumber = $("#MainContent_txt_partnumber").val();
    let lamp = $("#MainContent_txt_lamp").val();
    let model = $("#MainContent_txt_model").val();

    let machine = "";

    let month = $("#MainContent_txt_checkmonth").val();
    let year = $("#MainContent_txt_checkyear").val();


    let para = linegroup + "|" + linename + "|" + partnumber + "|" + machine + "|" + month + "|" + year + "|" + dayvalue + "|" + itemvalue + "|" + lamp + "|" + model + "|";
    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);

    window.location.href = "Machine_inspection_machine_confirm?para=" + str_para;


}
