$(document).ready(function () {


    setCookie("pizinfo", "", 5);
    getdataheader();
    $("#msg").hide();

    setTimeout(function () {
        $('#MainContent_txt_result').on('input', function (event) {

            let result = $("#MainContent_txt_result").val();
            result = parseFloat(result.replace(/,/g, ''));

            if (result < minval || result > maxval) {

                $("#msg").show();


            }
            else {
                $("#msg").hide();

            }



        });
    }, 500);

});


let pubequid = "";
let pubinspectionid = "";
let pubitemname = "";
let pubresult = "";
let publink = "";

let minval = 0;
let maxval = 0;
function getdataheader() {

    publink = getUrlVars()["link"];

    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 equid = arr_para[0];
        let inspectionid = arr_para[1];

        pubequid = equid;
        pubinspectionid = inspectionid;


        $.ajax({
            type: "POST",
            url: "XR_chart_input_check_number.aspx/getinspectiondetail",
            data: '{equid:"' + equid + '", inspectionid: "' + 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").html("<div style='padding-top:6px;font-size:16px;'>" + $(this).find("itemname").text() + "</div>");
                    $("#MainContent_txt_procedure").html("<div style='line-height:16px;font-size:16px;'>" + $(this).find("howcheck").text() + "</div>");
                    $("#MainContent_txt_criteria").html("<div style='padding-top:6px;font-size:16px;'>" + $(this).find("criteriamin").text() + "~" + $(this).find("criteriamax").text() + " [" + $(this).find("unit").text() + "]</div>");
                    $("#MainContent_txt_method").html("<div style='padding-top:6px;font-size:16px;'>" + $(this).find("input_method").text() + "</div>");

                    minval = parseFloat($(this).find("criteriamin").text());
                    maxval = parseFloat($(this).find("criteriamax").text());
                    pubitemname = $(this).find("itemname").text();
                });


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

        let strhd = equid + '|' + inspectionid + '|';

        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: "XR_chart_input_check_number.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 statusvalue = $(this).find("resultvalue").text();
                    pubresult = status;

                    $("#MainContent_txt_result").val(statusvalue);


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

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

                    }

                });


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




    }






}

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() {



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

    }
    else if ($('#msg').is(':visible')) {

        swal(
          'Warning',
          'You specified a number above the specified range.',
          'warning'
        );


    }
    else {

        let strhd = "";
        let equid = pubequid;
        let inspectid = pubinspectionid;
        let itemname = pubitemname;
        let type = "1";
        let result = $("#MainContent_txt_result").val();
        let notic = $("#MainContent_txt_notice").val();
        let user = "admin";
        let piz = getCookie("pizinfo");
        let inspectid_id = "";


        strhd = inspectid_id + '|' + equid + '|' + inspectid + '|' + itemname + '|' + 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: "XR_chart_input_check_number.aspx/save",
            data: '{parahd:"' + parahd + '"}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                res = response.d;

                if (res.length == 0) {

                    swal(
                    'Success',
                    'Data saved successfully',
                    'success'
                    );

                    setTimeout(function () {
                        window.location.href = "XR_check_item?para=" + publink;
                    }, 1000);


                }
                else {

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

                }


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


    }




}

var interval;
function takephoto() {

    let para = pubequid + "|" + pubinspectionid + "|2|";
    //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").attr('src', piz_data);


    }

}

