
let publink = "";
let publinegroup = "";
let publinename = "";
let pubpartnumber = "";
let pubmachineid = "";
let pubmachinename = "";
let pubinspectionid = "";
$(document).ready(function () {

    $("#MainContent_lbl_dayweek").hide();
    $("#MainContent_ddl_dayweek").hide();
    $("#MainContent_lbl_Date").hide();
    $("#MainContent_txt_Date").hide();


    let str_para = getUrlVars()["para"];
    publink = getUrlVars()["publink"];
    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('|');


        publinegroup = arr_para[0];
        publinename = arr_para[1];
        pubpartnumber = arr_para[2];
        pubmachineid = arr_para[3];
        pubmachinename = arr_para[4];
        pubinspectionid = arr_para[5];


    }

    let type = getUrlVars()["type"];
    if (type == "edit") {
        getdata(pubinspectionid);

    }

    $('#MainContent_txt_frequencyName').on('focus', function (event) {

        setTimeout(function () {
            let item = $('#MainContent_txt_frequencyName').val();
            if (item.indexOf('/W') != -1) {
                $("#MainContent_lbl_dayweek").show();
                $("#MainContent_ddl_dayweek").show();
                $("#MainContent_lbl_Date").hide();
                $("#MainContent_txt_Date").hide();

            }
            else if (item.indexOf('/M') != -1) {

                $("#MainContent_lbl_dayweek").hide();
                $("#MainContent_ddl_dayweek").hide();
                $("#MainContent_lbl_Date").show();
                $("#MainContent_txt_Date").show();

            }
            else {
                $("#MainContent_lbl_dayweek").hide();
                $("#MainContent_ddl_dayweek").hide();
                $("#MainContent_lbl_Date").hide();
                $("#MainContent_txt_Date").hide();

            }
        }, 300);



    });

});

function save() {

    if (checkreq().length == 0) {
        let checkitem = $("#MainContent_txt_checkitem").val();
        let whoid = $("#MainContent_txt_whoId").val();
        let whoname = $("#MainContent_txt_WhoName").val();
        let freqid = $("#MainContent_txt_frequencyId").val();
        let freqname = $("#MainContent_txt_frequencyName").val();
        let how = $("#MainContent_txt_how").val();
        let inputid = $("#MainContent_txt_inputmethodId").val();
        let inputname = $("#MainContent_txt_inputmethodName").val();
        let unitid = $("#MainContent_txt_unitId").val();
        let unitname = $("#MainContent_txt_unitName").val();
        let x = $("#MainContent_txt_x").val();
        let upper = $("#MainContent_txt_upperlimit").val();
        let lower = $("#MainContent_txt_lowerlimit").val();


        let userid = $("#lbluserid").html();

        $("#MainContent_lbl_dayweek").hide();
        let weeknum = $("#MainContent_ddl_dayweek").val();
        let datenum = $("#MainContent_txt_Date").val();
        let reporttype = $("#MainContent_ddl_type").val();

        let strsend = publinegroup + '|' + pubmachineid + '|' + pubpartnumber + '|' + checkitem + '|' + whoid + '|' + whoname + '|' + freqid + '|' + freqname + '|' + how + '|' + inputid + '|' + inputname + '|' + unitid + '|' + unitname + '|' + x + '|' + upper + '|' + lower + '|' + userid + '|' + pubinspectionid + '|' + weeknum + '|' + datenum + '|' + reporttype + '|';



        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: "XR_inspection_item_edit.aspx/save_xr_chart",
            data: '{parahd:"' + parahd + '"}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                let res = response.d;
                if (res.length == 0) {
                    swal(
                     'Success',
                     'Data saved successfully',
                     'success'
                     );

                    setTimeout(function () {
                        getdata(pubinspectionid);
                    }, 1000);


                }
                else {
                    swal(
                     'Error',
                      res,
                     'error'
                      );
                }


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

    }
}


function getdata(inspectionid) {


    $.ajax({
        type: "POST",
        url: "XR_inspection_item_edit.aspx/getdata_xr_chart",
        data: '{value:"' + inspectionid + '"}',
        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_checkitem").val($(this).find("itemname").text());
                $("#MainContent_txt_whoId").val($(this).find("whoid").text());
                $("#MainContent_txt_WhoName").val($(this).find("whochecklist").text());
                $("#MainContent_txt_frequencyId").val($(this).find("freqid").text());
                $("#MainContent_txt_frequencyName").val($(this).find("frequency").text());
                $("#MainContent_txt_how").val($(this).find("howcheck").text());
                $("#MainContent_txt_inputmethodId").val($(this).find("input_type").text());
                $("#MainContent_txt_inputmethodName").val($(this).find("input_method").text());
                $("#MainContent_txt_unitId").val($(this).find("unitid").text());
                $("#MainContent_txt_unitName").val($(this).find("unit").text());
                $("#MainContent_txt_x").val($(this).find("input_x").text());
                $("#MainContent_txt_upperlimit").val($(this).find("criteriamax").text());
                $("#MainContent_txt_lowerlimit").val($(this).find("criteriamin").text());

                $("#MainContent_ddl_dayweek").val($(this).find("dayweek").text());
                $("#MainContent_txt_Date").val($(this).find("datemonth").text());
                $("#MainContent_ddl_type").val($(this).find("report_type").text());



                setTimeout(function () {
                    let item = $('#MainContent_txt_frequencyName').val();
                    if (item.indexOf('/W') != -1) {
                        $("#MainContent_lbl_dayweek").show();
                        $("#MainContent_ddl_dayweek").show();
                        $("#MainContent_lbl_Date").hide();
                        $("#MainContent_txt_Date").hide();

                    }
                    else if (item.indexOf('/M') != -1) {

                        $("#MainContent_lbl_dayweek").hide();
                        $("#MainContent_ddl_dayweek").hide();
                        $("#MainContent_lbl_Date").show();
                        $("#MainContent_txt_Date").show();

                    }
                    else {
                        $("#MainContent_lbl_dayweek").hide();
                        $("#MainContent_ddl_dayweek").hide();
                        $("#MainContent_lbl_Date").hide();
                        $("#MainContent_txt_Date").hide();

                    }
                }, 300);






            });





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


}

function back() {



    window.location.href = "RegisterEquipment_inspection_item?para=" + publink;



}