$(document).ready(function() {
    // Hiding the category panel by default
    //  $('.categoryPanel').hide();

    // Add Button Clicked
    //$('#add').click(function(e) {
    //      e.preventDefault();
    //      Swal.fire({
    //           title: 'Add record',
    //          text: 'Add a record to the database'
    //     });
    // })

    // Edit Button Clicked
    // Cancel Button clicked
    $('#cancel').click(function(e) {
        e.preventDefault();
        Swal.fire({
            title: 'Cancel Form Submition?',
            text: "Are you sure, you want to cancel the form submission?",
            type: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: "Yes",
            cancelButtonText: "No"
        }).then(function(result) {
            if (result.value) {
                $("#showhide-zone").css("display", "none"),
                    checkit('num')
            }
        })

    })

    // Delete button clicked
    $('#deleteTransaction').click(function(e) {
        e.preventDefault();
        Swal.fire({
            title: 'Delete Transaction?',
            text: "Are you sure, you want to delete this transaction?",
            type: 'error',
            showCancelButton: true,
            confirmButtonColor: '#d33',
            cancelButtonColor: '#3085d6',
            confirmButtonText: "Yes",
            _cancelButtonText: "No",
            get cancelButtonText() {
                return this._cancelButtonText;
            },
            set cancelButtonText(value) {
                this._cancelButtonText = value;
            },
        }).then(function(result) {
            if (result.value) {
                // YOur real code goes here about deleting the record
                location.reload();
            }
        })
    })
    $('#delete').click(function(e) {
        e.preventDefault();
        Swal.fire({
            title: 'Delete option not availeble',
            text: "To delete transaction, you must use desktop Application",
            type: 'error',
            showCancelButton: false,
            confirmButtonColor: '#d33',
            cancelButtonColor: '#3085d6',
            confirmButtonText: "Ok",
            _cancelButtonText: "Cancel",
            get cancelButtonText() {
                return this._cancelButtonText;
            },
            set cancelButtonText(value) {
                this._cancelButtonText = value;
            },
        }).then(function(result) {
            if (result.value) {
                // YOur real code goes here about deleting the record
                //location.reload();
            }
        })
    })

});

function getHTTPObject() {
    var xmlhttp;
    /*@cc_on
    @if (@_jscript_version >= 5)
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (E) {
                xmlhttp = false;
            }
        }
    @else
        xmlhttp = false;
    @end @*/
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        try {
            xmlhttp = new XMLHttpRequest();
        } catch (e) {
            xmlhttp = false;
        }
    }
    return xmlhttp;
};

function addNewItem() {
    Swal.fire({
        title: 'New item entry',
        html: '<input type="text" id="theitemid" class="swal2-input" placeholder="Itemid"><input type="text" id="theitemname" class="swal2-input" placeholder="Item Name">',
        confirmButtonText: 'Add',
        focusConfirm: false,
        preConfirm: function() {
            const theitemid = Swal.getPopup().querySelector('#theitemid').value;
            const theitemname = Swal.getPopup().querySelector('#theitemname').value;
            if (!theitemid || !theitemname) {
                Swal.showValidationMessage('Please enter Itemid and Name');
            }
            return { theitemid: theitemid, theitemname: theitemname };
        }
    }).then(function(result) {
        //    Swal.fire(`
        //theitemid: ${result.value.theitemid}
        //theitemname: ${result.value.theitemname}
        //`.trim());
        txt = result.value.theitemname;
    })
};

function html2json(jtbl) {
    var json = '{';
    var otArr = [];
    var tbl2 = $('#' + jtbl + ' tr').each(function(i) {
        x = $(this).children();
        var itArr = [];
        x.each(function() {
            itArr.push('"' + $(this).text() + '"');
        });
        otArr.push('"' + i + '": [' + itArr.join(',') + ']');
    })
    json += otArr.join(",") + '}'
    return json;
};


$("input[type=text]").focus(function() {
    $(this).select();
});
$("input[type=number]").focus(function() {
    $(this).select();
});