$(document).ready(function () {
    $("body").on("change", ".bankname, .party", ChkDebCred);
    $("body").on("blur", "#amount", chkBill);
    $('body').on('blur', '.dsetoff', setoffd);
    $('body').on('blur', '.csetoff', setoffc);


    function showDays(firstDate, secondDate) {
        var date1 = firstDate;
        var date2 = secondDate.split("-").reverse().join("-");
        var mfirstday = new Date(date1);
        var mlastday = new Date(date2);
        var distance
        if (mfirstday > mlastday) {
            distance = mfirstday.getTime() - mlastday.getTime();
            distance = Math.ceil(distance / 1000 / 60 / 60 / 24);
        } else {
            distance = 0;
        }
        return distance;
    }

    $(".settotaldeb").hide();

    function setoffd() {
        var val = 0;
        var $row = $(this).closest("tr");
        var msetoff = parseFloat($row.find('.dsetoff').val()),
                mbalamt = parseFloat($row.find('.dbalamt2').val()),
                mbilamt = parseFloat($row.find('.dbillamt').val()),
                amtadj = parseFloat($row.find(".damtadj").val()),
                mbilldate = $row.find('.dbilldate').val(),
                mvouchdt = $('#vouchdt').val();
        if (isNaN(msetoff)) {
            msetoff = 0;
        }

        var newval = (mbilamt - amtadj).toFixed(2);
//        alert(newval);
        if (msetoff > newval) {
            $row.find('.dsetoff').val(0);
//            $row.find('.setoff').focus();
            msetoff = 0;
            alert("SETOFF AMOUNT CANNOT BE GREATER THAN BALANCE AMOUNT");
            $("#save").attr("disabled", "disabled");
            parseFloat($row.find('.dbalamt').val(mbalamt.toFixed(2)));

        } else {

            var balance = (mbilamt - amtadj - msetoff).toFixed(2);
            $row.find(".dbalamt").val(balance);

//        var remain = $("#remain").val();



            var mamt = $('#amount').val();

            /*********Total Part************/

            $(".dsetoff").each(function () {
                if (!isNaN(this.value) && this.value.length != 0) {
                    val += parseFloat(this.value);
                }
            });

            if (val > mamt) {
                $row.find('.dsetoff').val(0);
                $row.find('.dsetoff').focus();
                alert("TOTAL SETOFF AMOUNT CANNOT BE GREATER THAN TOTAL AMOUNT");
                $("#save").attr("disabled", "disabled");
            } else {
                $("#totsetoffdeb").val(val.toFixed(2));
                $("#save").removeAttr("disabled");

                var totdays = showDays(mvouchdt, mbilldate);
//                            alert(totdays);

                var totremain = mamt - val;

                $row.find('.ddays').val(totdays);
                $("#remaindeb").val(totremain.toFixed(2));
            }
        }
    }

    $(".settotalcred").hide();

    function setoffc() {
        var val = 0;
        var $row = $(this).closest("tr");
        var msetoff = parseFloat($row.find('.csetoff').val()),
                mbalamt = parseFloat($row.find('.cbalamt2').val()),
                mbilamt = parseFloat($row.find('.cbillamt').val()),
                amtadj = parseFloat($row.find(".camtadj").val()),
                mbilldate = $row.find('.cbilldate').val(),
                mvouchdt = $('#vouchdt').val();
        if (isNaN(msetoff)) {
            msetoff = 0;
        }

        var newval = (mbilamt - amtadj).toFixed(2);
//        alert(newval);
        if (msetoff > newval) {
            $row.find('.csetoff').val(0);
//            $row.find('.setoff').focus();
            msetoff = 0;
            alert("SETOFF AMOUNT CANNOT BE GREATER THAN BALANCE AMOUNT");
            $("#save").attr("disabled", "disabled");
            parseFloat($row.find('.cbalamt').val(mbalamt.toFixed(2)));

        } else {

            var balance = (mbilamt - amtadj - msetoff).toFixed(2);
            $row.find(".cbalamt").val(balance);

//        var remain = $("#remain").val();



            var mamt = $('#amount').val();

            /*********Total Part************/

            $(".csetoff").each(function () {
                if (!isNaN(this.value) && this.value.length != 0) {
                    val += parseFloat(this.value);
                }
            });

            if (val > mamt) {
                $row.find('.csetoff').val(0);
                $row.find('.csetoff').focus();
                alert("TOTAL SETOFF AMOUNT CANNOT BE GREATER THAN TOTAL AMOUNT");
                $("#save").attr("disabled", "disabled");
            } else {
                $("#totsetoffcred").val(val.toFixed(2));
                $("#save").removeAttr("disabled");

                var totdays = showDays(mvouchdt, mbilldate);
//                            alert(totdays);

                var totremain = mamt - val;

                $row.find('.cdays').val(totdays);
                $("#remaincred").val(totremain.toFixed(2));
            }
        }
    }


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

    function ChkDebCred() {
        var bankname = $(".bankname").val();
        var party = $(".party").val();

        if (bankname == party) {
            alert("Debit A/c. And Credit A/c. Cannot Be Same");
            $(".party").val("").trigger("change");
        }

    }



    $("#settmodaldeb").hide();
    $("#settmodalcred").hide();

    function chkBill() {
        var debac = $(".debac").val(),
                credac = $(".credac").val(),
                entdate = $(".vouchdt").val(),
                amount = $(".amount").val(),
                vouchno = $(".vouchno").val(),
                source = $(".source").val(),
                pvar = "R";


        $('#datadeb').empty();
        $("#settmodaldeb").hide();
        $.get('billsettjv.php',
                {accode: debac,
                    vouchdt: entdate,
                    amount: amount,
                    vouchno: vouchno,
                    pvar: "P",
                    parnew: "D",

                    source: source
                }, function (data) {
            if (data != "") {
                $('#datadeb').html(data);
                $('.billflag').val(1);

                $(".settotaldeb").show();
                $("#settmodaldeb").show();
                $(".setoffdeb").first().focus();
                $(".setoffdeb").first().select();
            }
        });

        $('#datacred').empty();
        $("#settmodalcred").hide();
        $.get('billsettjv.php',
                {accode: credac,
                    vouchdt: entdate,
                    amount: amount,
                    parnew: "C",
                    vouchno: vouchno,

                    pvar: "R",
                    source: source
                }, function (data2) {
            if (data2 != "") {

                $('#datacred').html(data2);
                $('.billflag').val(1);

                $(".settotalcred").show();
                $("#settmodalcred").show();
                $(".setoffcred").first().focus();
                $(".setoffcred").first().select();
            }
        });
    }


    //////////////////////////////////////////////Search Part - End  //////////////////////////////////////////////

});