$(document).ready(function () {
    $("body").off('click').on("click", '.remove', remove);
    $("body").on("keyup focus blur", '.amount', amtCal);
    $("body").on("change blur", '.party', getGrp);
    $("body").on("focus", '.remark', autoAddRows);
    $("body").on("keyup blur", '.debit, .credit', disableInwNo);

//    $("#print").prop("disabled", "true");
//    $("#print").css("background-color", "#ffffff");

    function disableInwNo() {
        var $row = $(this).closest("tr");
        var deb = $row.find('.debit').val();
        var cred = $row.find('.credit').val();
        if (deb > 0) {
            $row.find(".credit").attr("readonly", true);
        }
        else {
            $row.find(".credit").removeAttr("readonly");
//            $row.find(".debit").attr("readonly", true);
        }
        if (cred > 0) {
            $row.find(".debit").attr("readonly", true);
        }
        else {
            $row.find(".debit").removeAttr("readonly");
//            $row.find(".debit").attr("readonly", true);
        }
    }
    
    function getGrp() {
        var $row = $(this).closest("tr");
        var acc = $row.find('.party').val();
        
        $.post('get_grp.php', {accode : acc}, function (data) {
            $row.find('.grpcode').val(data);
        });

    }

    $('#addnew').click(function () {
        AddRows();
    });

    function autoAddRows() {
        var ctr = $('#items').val();
        var tr = $(this).closest("tr");
        var srno = tr.find(".srno").val();
        if (srno == ctr) {
            AddRows();

        }
    }

    function remove() {
        $(this).closest("tr").remove();
        amtCal();
    }

    function AddRows() {
        var ctr = $('#items').val();
        var hide_check = "";
        if ($("#del_all").prop("checked")) {
            hide_check = 0;
        } else {
            hide_check = 1;
        }
        
        var detail = ".fetchpart_" + ctr;
        $.post('cashmultipay_add.php', {ctr: ctr, hide: hide_check}, function (data) {
            $(data).appendTo('.tbody');
            ctr++;
            $('#items').val(ctr);
            $(detail).select2();
        });
    }


    function amtCal() {
        var val1 = 0;
                
        $(".amount").each(function () {
            if (!isNaN(this.value) && this.value.length != 0) {
                val1 += parseFloat(this.value);
            }
        });
        var amount = parseFloat(val1);

        $(".totamt").val(amount);
    }

    $("#submit").click(function () {
        var val1 = 0,
                val2 = 0;

        $(".credit").each(function () {

            if (!isNaN(this.value) && this.value.length != 0) {
                val1 += parseFloat(this.value);
            }
        });
        var amount = parseFloat(val1);

        $(".debit").each(function () {
            if (!isNaN(this.value) && this.value.length != 0) {
                val2 += parseFloat(this.value);
            }
        });
        var amount2 = parseFloat(val2);
        if (amount != amount2) {
            alert('Debit & Credit Total Does Not Match. Please Check');
        } else {
            $("#submit").focus();
        }
    });


    $(document).keydown(function (event) {
        if (!(String.fromCharCode(event.which).toLowerCase() == 'd' && event.altKey) && !(event.which == 19))
            return true;
        var val1 = 0,
                val2 = 0;
        var $row = $(this).closest("tr");
        $(".credit").each(function () {

            if (!isNaN(this.value) && this.value.length != 0) {
                val1 += parseFloat(this.value);
            }
        });
        var amount = parseFloat(val1);

        $(".debit").each(function () {
            if (!isNaN(this.value) && this.value.length != 0) {
                val2 += parseFloat(this.value);
            }
        });
        
        var amount2 = parseFloat(val2);
        if (amount != amount2) {
            alert('Debit & Credit Total Does Not Match. Please Check');
        } else {
            $("#submit").focus();
        }
//        $(".total").focus();

        event.preventDefault();
        return false;

    });
});
