$(document).ready(function () {
    $("body").off('click').on("click", '.remove', remove);
    $("body").on("focus blur change", ".vouchdt", getvoucher);
    $("body").on("blur change", ".price_dz, .roe,.discperc", calDetail);
    $("body").on("change blur", ".vouchdt", chkSaleDt);
    $("body").on("click", "#submit ,#cancel", hide_but);

    function chkSaleDt() {
        var vouchdt = $(".vouchdt").val();
        $.post('chkSaleLock.php', {vouchdt: vouchdt}, function (data) {
            if (data > 0) {
                $("#submit").hide();
                $('form').on('keyup keypress', function (e) {
                    var keyCode = e.keyCode || e.which;
                    if (keyCode === 13) {
                        e.preventDefault();
                        return false;
                    }
                });
            } else {
                $("#submit").show();

            }

        });
    }

    $("#scancbill").hide();
//    $("#hdelete").show();

    function hide_but() {
        $("#ssubmit").hide();
        $("#scancel").hide();
    }

    $("#cancbill").click(function () {
        $("#cancmodel").modal('show');
    });


    $('.hflag').hide();



    function getvoucher() {
        var date = new Date($(".vouchdt").val()),
                month = (date.getMonth() + 1).toString(),
                vouchno = $('.vouchno').val(),
                voucher = '0' + month + vouchno;
        $('.monno').val(month);
        $('.voucher').val(voucher);
    }

    function calDetail() {
        var tr = $(this).closest("tr"),
                roe = $(".roe").val(),
                price_dz = tr.find(".price_dz").val(),
                discperc = tr.find(".discperc").val(),
                discount = (parseFloat(discperc) * parseFloat(price_dz) / 100).toFixed(2),
                price = price_dz - discount,
                ratefe = price / roe;
        tr.find(".discount").val(discount);
        tr.find(".price").val(price);
        tr.find(".ratefe").val(ratefe.toFixed(2));
    }


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

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

    function AddRows() {
        var ctr = $('#items').val();
        var prodtype = $('.prodtype').val();
        var hide_check = "";
        if ($("#del_all").prop("checked")) {
            hide_check = 0;
        } else {
            hide_check = 1;
        }

        var focusFld = ".focusFld_" + ctr;
        $.post('feloc_add.php', {ctr: ctr, hide: hide_check, prodtype: prodtype}, function (data) {
            $(data).appendTo('.tbody');
            ctr++;
            $('#items').val(ctr);
            $(focusFld).focus();
        });
    }



});