$(document).ready(function () {
    $("body").on("change", '.flatno', getName);

    function getName() {
        var flatno = $('.flatno').val()

        if (flatno != '') {
            $.post('get_flatno.php', {flatno: flatno}, function (data) {
                $("#party").val(data.accode);
                $("#party").trigger('change');

            }, 'json');
        }

    }

    $(".proceed").click(function () {

        var billcycle = $('.billcycle').val(),
                vouchdt = $('.vouchdt').val(),
                member = $('.member').val(),
                yearno = $('.yearno').val(),
                valid = true,
                billcode = $('.billcode').val();

        if (yearno != "") {


            $.post('chk_billgen.php', {billcycle: billcycle, billcode: billcode, vouchdt: vouchdt, member: member}, function (data) {
                if (data.billcheck == 1) {
                    alert('Bill Already Exist For  -  ' + data.descrip);
                    valid = false;
                }

                if (data.check == 1) {
                    alert('Bill Cannot Be Generated For  -  ' + data.descrip + '    Because Next Period Already Exist');
                    valid = false;
                }


                if (valid) {
                    var isValid = true;

                    if (isValid) {
                        $("#submit").click();
                    }
                }

            }, 'json');
        } else {
            alert("Year Cannot be Empty");
        }
    });

    $(".delete").click(function () {

        var billcycle = $('.billcycle').val(),
                vouchdt = $('.vouchdt').val(),
                member = $('.member').val(),
                valid = true,
                billcode = $('.billcode').val();


        $.post('chk_billgen.php', {billcycle: billcycle, billcode: billcode, vouchdt: vouchdt, member: member}, function (data) {

            if (data.check == 1) {
                alert('Bill Cannot Be Deleted For  -  ' + data.descrip + '    Because Next Period Exist');
                valid = false;
            }


            if (valid) {
                var isValid = true;

                if (isValid) {
                    $("#delete").click();
                }
            }

        }, 'json');
    });


});