$(document).ready(function () {
    $("body").off('click').on("click", '.remove', remove);
    $("body").on("keyup change blur focus", ".qty, .rate, .amount,.discperc,.discamt,.vatperc,.pcscart,.cartqty, .marketfee, .commperc,.roundoff", calDetail);
    // $("body").on("keyup change blur focus", ".qty, .rate, .amount", calMarkFee);
    $("body").on("blur", ".invno", chkinvno);
//    $("body").on("change keyup blur", ".qty, .rate,.totbillamt,.discperc,.roundoff,.freightperc,.packing,.courier", caltran);
    $("body").on("blur ", ".cost", autoadd);
    $("body").on("blur", ".itemname", chNew);

    $(".itemgrp").hide();

    $(document).keydown(function (event) {
        //19 for Mac Command+S
        if (!(String.fromCharCode(event.which).toLowerCase() == 'v' && event.altKey) && !(event.which == 19))
            return true;
//        alert("Ctrl-s pressed");
//        $("#edit").click();
        var href = $('#prndir').attr('href');
//        window.open = href;
//        window.location.href = href;
        window.open(href);
//        location.reload();
    });

    function chNew() {
        var tr = $(this).closest("tr"),
                itemname = tr.find(".itemname").val(),
                barcode = tr.find(".barcode").val();
        if (itemname != '' && barcode == '') {
            tr.find(".barcode").val("New");
        }
    }

    $("form").submit(function () {

        var party = $('.cmbparty').val(),
                totbillamt = $('.totbillamt').val(),
                valid = true;


        if (party == '') {
            var message = "Party Cannot Be Empty";
            alert(message);
            return false;
            valid = false;
        }

        if (parseFloat(totbillamt) <= 0 || totbillamt == '') {
            var message = "Netamt Cannot zero";
            alert(message);
            return false;
            valid = false;
        }



        if (valid) {
            var isValid = true;

            if (isValid) {
                $("#submit").hide();
                $("#cancel").hide();

            }
        }
    });

    $(".party").change(function () {
        var invno = $(".invno").val(),
                file = $(".file").val(),
                party = $(".party").val();
        $.post('chk_invo.php', {invno: invno, party: party, file: file}, function (data) {
            if (data.s == 1) {
                alert('These Invoice Already Exist For Voucher  ' + data.vouchno);
                $(".invno").val('');
                $(".invno").focus();

            }
        }, 'json');
    });

    function autoadd() {
        if ($(this).closest('tr').is(":last-child")) {
            AddRows();
        }
    }

    $('.invno').blur(function () {
        $('.party').select2("open");
    });


    function calDetail() {
        var tr = $(this).closest("tr"),
                cartqty = tr.find(".cartqty").val(),
                pcscart = tr.find(".pcscart").val(),
                qty = 0;
        if (pcscart > 0) {
            qty = cartqty * pcscart;
            tr.find(".qty").val(qty);
        } else {
            qty = tr.find(".qty").val();
        }
        var rate = tr.find(".rate").val(),
                amount = ((rate * qty) / 10).toFixed(2),
                discperc = tr.find(".discperc").val(),
                discamt = 0,
                subtot = 0,
                totqty = 0,
                total = 0;

        if (discperc > 0) {
            discamt = ((discperc * amount) / 100).toFixed(2);
        } else {
            discamt = tr.find(".discamt").val();
        }
        subtot = amount - discamt;

        total = (subtot).toFixed(2);
        tr.find(".amount").val(amount);
        tr.find(".discamt").val(discamt);
        tr.find(".total").val(total);
        var totamt = 0,
                totdisc = 0,
                totvat = 0,
                totbag = 0,
                tottotal = 0;
        $(".amount").each(function () {
            if (!isNaN(this.value) && this.value.lenght != 0) {
                totamt += parseFloat(this.value);
            }
        });
        $(".qty").each(function () {
            if (!isNaN(this.value) && this.value.lenght != 0) {
                totqty += parseFloat(this.value);
            }
        });
        $(".cartqty").each(function () {
            if (!isNaN(this.value) && this.value.lenght != 0) {
                totbag += parseFloat(this.value);
            }
        });
        $(".discamt").each(function () {
            if (!isNaN(this.value) && this.value.lenght != 0) {
                totdisc += parseFloat(this.value);
            }
        });

        $(".total").each(function () {
            if (!isNaN(this.value) && this.value.lenght != 0) {
                tottotal += parseFloat(this.value);
            }
        });
        $(".totbag").val(totbag);
        $(".totqty").val(totqty);
        $(".totamt").val(totamt);
        $(".totdisc").val(totdisc);
        $(".totvat").val(totvat);
        $(".tottotal").val(tottotal);

        calMain();
    }


    function calMain() {
        var totamt = $(".totamt").val();
        var marketfee = $(".marketfee").val();
        var roundoff = $(".roundoff").val();
        if ($(".roundoff").val() == "" || $(".roundoff").val() == "undefined") {
           var roundoff = 0;
        } else {
        var roundoff = $(".roundoff").val();
    }
        var commperc = $(".commperc").val(),
                commamt = (commperc * totamt) / 100;

        var netamt = parseFloat(totamt) + parseFloat(marketfee) + parseFloat(commamt) + parseFloat(roundoff);
        $(".commamt").val(commamt);
        $(".tottotal").val(netamt);
    }

    $(".calcmarketfee").click(function() {
        calDetail();
        calMarkFee();
    });
    
    function calMarkFee() {
        var mamtfee = $(".amtfee").val(),
            mqtyfee = $(".qtyfee").val(),
            mbagfee = $(".bagfee").val(),
            totqty = $(".totqty").val(),
            totbag = $(".totbag").val(),
            totamt = $(".totamt").val(),
            mf1 = totbag * mbagfee,
            mf2 = totqty * (mqtyfee / 100),
            mf3 = totamt * (mamtfee / 100),
            mff = parseFloat(mf1) + parseFloat(mf2) + parseFloat(mf3),
            mf = Math.round(mff, 2);

        $(".marketfee").val(mf);
    }


    function chkinvno() {
        var invno = $('.invno').val();
        var party = $(".party").val();
        if (invno.length > 0) {
            $.post('check_inv_dtl.php', {invno: invno, party: party}, function (data) {
                if (data == 'success') {
                    alert('This Bill No. For This Party Already Exist');
                    $('.invno').val('');
                    $('.invno').focus();
                }
            });
        }
    }

    $('#addnew').click(function () {
        AddRows();
//        caltran();
    });
    function remove() {
        $(this).closest("tr").remove();
//        caltran();
    }


    function AddRows() {
        var ctr = $('#items').val();
        var prodtype = $('.prodtype').val();
        var rd = $('.rd').val();
        var frmname = $('.frmname').val();
        var hide_check = "";
        if ($("#del_all").prop("checked")) {
            hide_check = 0;
        } else {
            hide_check = 1;
        }
        var focusFld = ".focusFld_" + ctr;
        ctr++;
        $('#items').val(ctr);
        $.post('purchase_add.php', {ctr: ctr, hide: hide_check, prodtype: prodtype, rd: rd, frmname: frmname}, function (data) {
            $(data).appendTo('.tbody');
            $(focusFld).focus();
        });
    }




});