$(document).ready(function () {
    $('body').on('change', '.karigar', getAcNo);
    $('body').on('change', '.karigar', getLogDate);
    $('body').on('blur', '.vouchdt', getLogDate);

    function getLogDate() {
        var party = $(".karigar").val(),
                vouchdt = $(".vouchdt").val();
        if (party != '' && vouchdt != '') {
            $.get('chk_LogDate.php', {party: party}, function (data) {
                if (data != '0000-00-00') {
                    $(".logdate").val(data);
                    if (vouchdt < data) {
                        alert('Voucher Date is Less than Log Date');
//                        $(".party").select2("val", "");

                        $(".karigar").select2("open");

                    }
                }
            });
        }

    }

    $("form").submit(function () {
        $("#ssubmit").hide();

        var party = $(".party").val(),
                karigar = $(".karigar").val(),
                drawbank = $(".drawbank").val(),
                amount = $(".amount").val(),
                vouchdt = $(".vouchdt").val(),
                valid = true;

        if (amount <= 0) {
            alert('Amount Cannot Be Zero');
            $("#ssubmit").show();
            return false;
            valid = false;

        }
        if (party == '') {
            alert('Party Cannot Be Empty');
            $("#ssubmit").show();
            return false;
            valid = false;

        }
        if (karigar == '') {
            alert('Karigar Cannot Be Empty');
            $("#ssubmit").show();
            return false;
            valid = false;

        }
        if (drawbank == '') {
            alert('Bank Cannot Be Empty');
            $("#ssubmit").show();
            return false;
            valid = false;

        }

        if (party != '' && vouchdt != '') {
            $.get('chk_LogDate.php', {party: party}, function (data) {
                if (data != '0000-00-00') {
                    $(".logdate").val(data);
                }
            });
        }
        var logdate = $(".logdate").val();
        if (vouchdt < logdate) {
            alert('VOUCHER DATE IS LESS THAN Log Date');
            return false;
            valid = false;
        }
    });

    function getAcNo() {
        var karigar = $(".karigar").val();
        if (karigar != '') {
            $.post('getAcNo.php', {karigar: karigar}, function (data) {
                $(".acnum").val(data);
            });
        }
    }


});