$(document).ready(function () {
    $("body").off('click').on("click", '.remove', remove);
//    $("body").on("keyup focus blur click", '.nag, .rate, .weight', itemCal);
    $("body").on("keyup focus blur click", '.nag, .rate , .foot, .weight, .straight', tfeet);
    $("body").on("keyup blur", '.amount', CalcRate);
    $("body").on("focus", '.sizemm', sizemmfunc);
//    $("body").on("focus", '.party', FocusParty);
    $("body").on("focus", '.guagemm', guagemmfunc);
    $("body").on("focus", '.makemm', makemmfunc);
    $("body").on("change blur", '#weight', ratefunc);
    $("body").on("change blur", '.soval', sizemmCal);
    $("body").on("change blur", '.goval', guagemmCal);
    $("body").on("change blur", '.moval', makemmCal);
    $("body").on("keyup", '.delv', delvchk);
    $("body").on("keyup", '.mark', markchk);
    $("body").on("keyup", '.ap', apchk);
    $("body").on("keyup", '.weld', weldchk);
    $("body").on("keyup", '.cashcred', cashcredchk);
    $("body").on("keyup", '.straight', straightchk);
    $("body").on("blur", '.rate', autoAddRows);
    $('body').on('blur', '.barcode', showdtl);
    $('body').on('change click', '.gsttype, .oms', getGST);
    $('body').on('change click', '.company', getBillNo);
    $('body').on('blur change click', '.partyname', getOrderNo);
    $('body').on('blur change click', '.partyname', getPartyType);
    $('body').on('change click', '.outwardno', getOrderDtl);
    $("body").off('click').on("click", '.remove', remove);
    $("body").on('click').on("blur", '.delvdate', chkdate);
    $('body').on('change keyup focus blur click', '.oms, .freightamt, .gsttype, .cgstperc, .sgstperc,.igstperc, .remove', mainbCal);
    $("body").on("keyup blur", '.gstflag', ChkGstFlag);
//    $("body").on("change", '.party', LeaveParty);
//    $("body").on("keyup", '.partyname', ShowPartyModal);
//    $("body").on("change blur", '.partyname', GetPartyAccode);
    $(".ndata input").on("focus", SeleInput);
    $("body").on("change blur", '.querysizeval', sizecal);
    $("body").on("change blur", '.queryggval', guagecal);
    $("body").on("change blur", '.querymakeval', makeCal);
    $("body").on("click", ".copydata", GetPrevData);
    $("body").on("blur change", ".weight", GetDateFromPartynDate);
    $("body").on("blur", ".foot", chkDup);
    
    function chkDup(){
        var party = $(".party").val(),
                nag = $(".nag").val(),
                vouchno = $(".vouchno").val(),
                size = $(".sval").val(),
                sizemm = $(".sizemm").val(),
                guagemm = $(".guagemm").val(),
                makemm = $(".makemm").val(),
                dia = $(".dia").val(),
                ap = $(".ap").val(),
                straight = $(".straight").val(),
                foot = $(".foot").val(),
                vouchdt = $(".vouchdt").val(),
                delv = $(".delv").val();


        $.post('chkduplentry.php', {party: party, vouchdt: vouchdt, nag: nag, sizemm: sizemm, size: size, foot: foot,vouchno:vouchno}, function (data) {

            if (data == 1) {
                $(".foot").val("");
                $(".foot").focus();
                alert("Duplicate Entry, Already Exist");
            }

        });
    }


    $('.srchparty').on('keydown', function (e) {
        var keyCode = e.keyCode || e.which;
        if (keyCode == 40) {
            $(".PTbody tr:visible:first").find(".disblsrchparty").focus();
            e.preventDefault();
            return false;
        }
    });


    $(".PartySrchTbl").hide();
    $('.srchparty').on('keyup', function (e) {
        var value = $(".srchparty").val().toLowerCase();
        $(".PartySrchTbl").show();
        $(".PTbody tr").filter(function () {

            var tr = $(this).closest("tr");
            if (tr.find(".disblsrchparty").val().toLowerCase().substr(0, value.length) == value.substr(0, value.length)) {
                tr.show();
            } else {
                tr.hide();
            }

        });

    });

    $('.PartySrchTbl input').on('keydown', function (e) {

        var keyCode = e.keyCode || e.which;
        if (keyCode == 40) {
//            $(this).closest("tr").next("tr").find('.disblsrchparty').focus();
//            $(this).closest("tr").nextUntil("tr:visible").find('.disblsrchparty').focus();
            $(this).closest("tr").nextAll().filter(":visible:first").find('.disblsrchparty').focus();
            e.preventDefault();
            return false;
        }

        if (keyCode == 38) {
//            $(this).closest("tr").prev("tr").find('.disblsrchparty').focus();
            $(this).closest("tr").prevAll().filter(":visible:first").find('.disblsrchparty').focus();
            e.preventDefault();
            return false;
        }

        if (keyCode == 13) {
            var tr = $(this).closest("tr"),
                    partyname = tr.find(".disblsrchparty").val();
            $(".PartySrchTbl").hide();

            $(".party").val(partyname);
            $(".nag").focus();

            e.preventDefault();
            return false;
        }


        if (keyCode != 13 || keyCode != 18 || keyCode != 38) {
            e.preventDefault();
            return false;
        }
    });


    $(".disblsrchparty").click(function () {
        var tr = $(this).closest("tr"),
                partyname = tr.find(".disblsrchparty").val();
        $(".PartySrchTbl").hide();

        $(".party").val(partyname);
        $(".nag").focus();

//            e.preventDefault();
//            return false;
    });

    function sizecal() {
        var qsval = $('.qsval').val(),
                querysizeval = $('.querysizeval').val();
        $.post('get_chalDtl.php', {sval: qsval, soval: querysizeval}, function (data) {
            $(".qsoval").val(data);
            // $('#sizemmod').modal('hide');
            $(".qsoval").focus();
        });
    }

    function GetDateFromPartynDate() {
        var vouchdt = $('.vouchdt').val(),
                sizemm = $('.sizemm').val(),
                makemm = $('.makemm').val(),
                guagemm = $('.guagemm').val(),
                party = $('.party').val();

        if (vouchdt != '' && party != '') {
            $.post('get_prevdatenparty.php', {vouchdt: vouchdt, party: party, sizemm: sizemm, guagemm: guagemm, makemm: makemm}, function (data) {
                $(".prevrecbody").html(data);
            });
        }
    }

    function GetPrevData() {
        var tr = $(this).closest("tr"),
                pname = tr.find('.partynametd').text(),
                paccode = tr.find('.copyaccode').val(),
                pvouchdt = tr.find('.copyvouchdt').val();
//                pvouchdt = tr.find('.cvouchdt').text();
        AddnewData();
        var ondate = new Date(pvouchdt);
//        alert(pvouchdt);
//        alert(ondate);
        var day = ("0" + ondate.getDate()).slice(-2);
        var month = ("0" + (ondate.getMonth() + 1)).slice(-2);
        var today = ondate.getFullYear() + "-" + (month) + "-" + (day);
        $(".vouchdt").val(today);

//        $(".vouchdt").trigger('change');
//        $(".vouchdt").val(pvouchdt);
        $(".partyname").val(pname);
        $(".party").val(paccode);
    }


    function guagecal() {
        var qgval = $('.qgval').val(),
                qsval = $('.qsval').val(),
                queryggval = $('.queryggval').val();
        $.post('get_chalDtlG.php', {gval: qgval, goval: queryggval, qsval: qsval}, function (data) {
            $(".qgoval").val(data);
            // $('#guagemmod').modal('hide');
            $(".qgoval").focus();
        });
    }


    function makeCal() {
        var qmval = $('.qmval').val(),
                querymakeval = $('.querymakeval').val();
        $.post('get_chalDtl.php', {sval: qmval, soval: querymakeval}, function (data) {
            $(".qmoval").val(data);
//            $('#makemmod').modal('hide');
            $(".qmoval").focus();
        });
    }


    function SeleInput() {
        this.select();
    }

//    function GetPartyAccode() {
//        var partyname = $(".partyname").val();
//        if (partyname != '' && partyname != '?') {
//            $.ajax({
//                type: 'GET',
//                dataType: 'json',
//                url: "get_accode.php",
//                data: {partyname: partyname},
//                success: function (response) {
//                    var maccode = response.accode;
//                    if (response == false || response == '') {
//                        alert('Party Name Not Found');
//                        $(".partyname").val('');
//                        $(".party").val('');
//                    } else {
//                        $(".party").val(maccode);
//                    }
//
//
//                },
//                error: function (request, textStatus, errorThrown) {
//                    alert(errorThrown);
//                }
//            });
//        }
//    }

//    $('#sizemmod').on('hidden.bs.modal', function () {
//        $(".sizemm").focus();
//    });

//
//    $('#getpartydata').on('hidden.bs.modal', function () {
//        GetDateFromPartynDate();
//        $(".nag").focus();
//        GetPartyAccode();
//    });

//    $('#getpartydata').on('shown.bs.modal', function () {
//        $('.srchparty').focus();
//    });

//    $('#guagemmod').on('hidden.bs.modal', function () {
//        $(".guagemm").focus();
//    });
//
//    $('#makemmod').on('hidden.bs.modal', function () {
//        $(".makemm").focus();
//    });

//    function ShowPartyModal() {
//        var party = $(".partyname").val();
//        var srchparty = $(".srchparty").val();
//        var ctr = 1;
//        if (party == "?") {
//            $("#getpartydata").modal('show');
//            $(".srchparty").focus();
////            $.post('get_SrchParty.php', {srchparty: srchparty, ctr: ctr}, function (data) {
////                $(".PTbody").html(data);
////            });
//        }
//    }

    function ChkGstFlag() {
        var gstflag = $(".gstflag").val();
        if (gstflag != '') {
            if (gstflag.toUpperCase() == "N" || gstflag.toUpperCase() == "Y") {
            } else {
                alert("GST Flag Should Be Y or N");
                $(".gstflag").val('N');
                return false;
            }
            if (gstflag.toUpperCase() == "Y") {
                $(".tblhsncode").show();
                $(".tblgstperc").show();
                $(".tblbillamt").show();
            } else {
                $(".tblhsncode").hide();
                $(".tblgstperc").hide();
                $(".tblbillamt").hide();
            }
        }
    }

//    function FocusParty() {
//        $(".party").select2('open');
//    }
//
//    function LeaveParty() {
//        if ($(".party").val() != '') {
////            if (nagparam == '') {
//                $('input').on('keydown', function (e) {
//                    var keyCode = e.keyCode || e.which;
//                    if (keyCode === 13) {
//                        $(".nag").focus();
//                    }
//                });
//
//                $('.select2').on('keydown', function (e) {
//                    var keyCode = e.keyCode || e.which;
//                    if (keyCode === 37) {
//                        $(".delvdate").focus();
//                    }
//                });
//
//                $('.select2').on('keydown', function (e) {
//                    var keyCode = e.keyCode || e.which;
//                    if (keyCode === 39) {
//                        $(".nag").focus();
//                    }
//                });
////            } else {
////                
////            }
//        }
//
//    }

//    function chkduplentry() {
//        var gval = $('.gval').val(),
//                goval = $('.goval').val();
//        $.post('get_chalDtlG.php', {gval: gval, goval: goval}, function (data) {
//            $(".guagemm").val(data);
//            $('#guagemmod').modal('hide');
//            $(".guagemm").focus();
//        });
//    }

    $("form[name='chalform']").submit(function () {

        var party = $(".party").val(),
                nag = $(".nag").val(),
                size = $(".sval").val(),
                sizemm = $(".sizemm").val(),
                guagemm = $(".guagemm").val(),
                makemm = $(".makemm").val(),
                dia = $(".dia").val(),
                ap = $(".ap").val(),
                straight = $(".straight").val(),
                foot = $(".foot").val(),
                vouchdt = $(".vouchdt").val(),
                delv = $(".delv").val();


        $.post('chkduplentry.php', {party: party, vouchdt: vouchdt, nag: nag, sizemm: sizemm, size: size, foot: foot}, function (data) {

            if (data == 1) {
                alert("Duplicate Entry, Already Exist");
                event.preventDefault();
            }

        });



        if (nag == '') {
            alert('NAG Cannot Be Empty');
            event.preventDefault();
        }

        if (delv == "D") {


            if (party == '') {
                alert('Party Cannot Be Empty');
                $(".partyname").focus();
                event.preventDefault();
            }
            if (nag == '') {
                alert('Nag Name Cannot Be Empty');
                $(".nag").focus();
                event.preventDefault();
            }
            if (sizemm == '') {
                alert('Size(MM) Cannot Be Empty');
                $(".sizemm").focus();
                event.preventDefault();
            }
            if (guagemm == '') {
                alert('Guage(MM) Cannot Be Empty');
                $(".guagemm").focus();
                event.preventDefault();
            }
            if (makemm == '') {
                alert('Make(MM) Cannot Be Empty');
                $(".makemm").focus();
                event.preventDefault();
            }
            if (dia == '') {
                alert('Dia Cannot Be Empty');
                $(".diamm").focus();
                event.preventDefault();
            }
            if (ap == '') {
                alert('A/P Cannot Be Empty');
                $(".ap").focus();
                event.preventDefault();
            }
            if (straight == '') {
                alert('Straight Cannot Be Empty');
                $(".straight").focus();
                event.preventDefault();
            }
            if (foot == '') {
                alert('Foot Cannot Be Empty');
                $(".foot").focus();
                event.preventDefault();
            }
        }



    });
    function chkdate() {
        var vouchdt = $(".vouchdt").val(),
                delvdt = $(".delvdate").val();
        if (delvdt != "") {
            if (delvdt < vouchdt) {
                alert("Delivery Date Cannot Be Less Than Voucher Date");
                $(".delvdate").val("00-00-0000");
            } else {

            }
        }
    }

    $('#prate').on('shown.bs.modal', function (e) {
        $(window).keydown(function (event) {
            if (event.keyCode == 27) {
                $('#prate').modal('hide');
            }
        });
    })

    $('#prate').on('hidden.bs.modal', function () {
        $(".rate").focus();
    })

//    $(".sval").blur(function(){
//        $(".soval").select2("open")
//    });


//
//    $(".soval").focus(function () {
//        $(".soval").select2("open");
//    })





    function getSearchData() {


        var party = $(".party").val(),
                sizemm = $(".sizemm").val(),
                guagemm = $(".guagemm").val(),
                makemm = $(".makemm").val(),
                data5 = {party: party, sizemm: sizemm, guagemm: guagemm, makemm: makemm};
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "get_prate.php",
            data: data5,
            success: function (response) {
                var prod = '';
                var color = '';
                for (var i = 0; i < response.length; i++) {

                    color = '';
                    if (response[i].trparty == "CYSP") {
                        color = "color:black";
                    } else if (response[i].trparty == "CYDP") {
                        color = "color:blue";
                    } else if (response[i].trparty == "PYSP") {
                        color = "background-color:#CDCDCD";
                    } else if (response[i].trparty == "PYDP") {
                        color = "background-color: #ADD8E6";
                    }



                    prod += "<tr class='clickable-row' style='font-size: 11px;padding:0px;" + color + "' data-href='" + response[i].action + "'>" +
                            "<td align='left'>" + response[i].mark + "</td>" +
                            "<td align='left'>" + response[i].vouchdt + "</td>" +
                            "<td align='left'>" + response[i].delv + "</td>" +
                            "<td align='left'>" + response[i].delvdate + "</td>" +
                            "<td align='left'>" + response[i].party + "</td>" +
                            "<td align='left' class='text-right'>" + response[i].nag + "</td>" +
                            "<td align='left' class='text-right'>" + response[i].sizemm + "</td>" +
                            "<td align='left' class='text-right'>" + response[i].guagemm + "</td>" +
                            "<td align='left' class='text-right'>" + response[i].makemm + "</td>" +
                            "<td align='left'>" + response[i].dia + "</td>" +
                            "<td align='left'>" + response[i].ap + "</td>" +
                            "<td align='left'>" + response[i].weld + "</td>" +
                            "<td align='left'>" + response[i].straight + "</td>" +
                            "<td align='left' class='text-right'>" + response[i].foot + "</td>" +
                            "<td align='left' class='text-right'>" + response[i].totfeet + "</td>" +
                            "<td align='left' class='text-right'>" + response[i].weight + "</td>" +
                            "<td align='left' class='text-right'>" + response[i].rate + "</td>" +
                            "<td align='left' class='text-right'>" + response[i].amount + "</td>" +
                            "<td align='left'>" + response[i].descrip + "</td>" +
                            "<td align='left'>" + response[i].cashcred + "</td>" +
                            "</tr>";
                }
                $('.prTbody').html(prod);
            },
            error: function (request, textStatus, errorThrown) {
                //   alert(errorThrown);
            }
        });
    }

    function tfeet() {
        var tr = $(this).closest("tr");
        var nag = tr.find(".nag").val();
        var foot = tr.find(".foot").val();
        var straight = tr.find(".straight").val();
        var weight = tr.find(".weight").val();
        var rate = tr.find(".rate").val();
        var amount = 0;

        if (weight == 0) {
            if (straight == 'Y') {
                var tfoot = nag * foot;
                amount = rate * tfoot;
            } else {
                var tfoot = 0;
                amount = rate * nag;
            }
        } else {
            amount = rate * weight;
        }
        tr.find(".amount").val(amount);
        tr.find(".totfeet").val(tfoot);
    }

    function CalcRate() {
        var tr = $(this).closest("tr");
        var nag = tr.find(".nag").val();
        var foot = tr.find(".foot").val();
        var straight = tr.find(".straight").val();
        var weight = tr.find(".weight").val();
        var amount = tr.find(".amount").val();
        var rate = 0;

        if (weight == 0) {
            if (straight == 'Y') {
                var tfoot = nag * foot;
//                alert('1');

                if (tfoot == 0) {
                    rate = 0;
                } else {
//                    alert('2');
                    rate = amount / tfoot;
                }
            } else {
                var tfoot = 0;

                if (nag == 0) {
                    rate = 0;
                } else {
//                    alert('3');
                    rate = amount / nag;
                }
            }
        } else {
            if (weight == 0) {
                rate = 0;
            } else {
                rate = amount / weight;
            }
        }
//        tr.find(".amount").val(amount);
//        tr.find(".totfeet").val(tfoot);
        tr.find(".rate").val(rate);
    }



    function cashcredchk() {
        var cashcred = $('.cashcred').val();
        if (cashcred != '') {
            if (cashcred.toLowerCase() === 'c' || cashcred.toLowerCase() === 'r') {
            } else {
                alert('Please Enter "C", "R" Only');
                $('.cashcred').val('');
            }
        }
    }
    function straightchk() {
        var straight = $('.straight').val();
        if (straight != '') {
            if (straight.toLowerCase() === 'y' || straight.toLowerCase() === 'n') {
            } else {
                alert('Please Enter "Y", "N" Only');
                $('.straight').val('');
            }
        }
    }
    function weldchk() {
        var weld = $('.weld').val();
        if (weld != '') {
            if (weld.toLowerCase() === 'y' || weld.toLowerCase() === 'n') {
            } else {
                alert('Please Enter "Y", "N" Only');
                $('.weld').val('');
            }
        }
    }
    function delvchk() {
        var delv = $('.delv').val();
        if (delv != '') {
            var ndelv = delv.toUpperCase();
            $(".delv").val(ndelv);
            if (delv.toLowerCase() === 'p' || delv.toLowerCase() === 'r' || delv.toLowerCase() === 'd') {

            } else {
                alert('Please Enter "P", "R", "D" Only');
                $('.delv').val('');
            }
        }

        var party = $(".party").val(),
                nag = $(".nag").val(),
                size = $(".sval").val(),
                sizemm = $(".sizemm").val(),
                guagemm = $(".guagemm").val(),
                makemm = $(".makemm").val(),
                dia = $(".dia").val(),
                ap = $(".ap").val(),
                straight = $(".straight").val(),
                foot = $(".foot").val(),
                vouchdt = $(".vouchdt").val(),
                delv = $(".delv").val();
        var today = new Date();
        var dd = String(today.getDate()).padStart(2, '0');
        var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
        var yyyy = today.getFullYear();

        today = yyyy + '-' + mm + '-' + dd;
        if (delv.toLowerCase() === 'd') {



            if (party == '') {
                alert('Party Cannot be Empty');

            }
            if (nag == '') {
                alert('Nag Cannot be Empty');
            }
            if (guagemm == '') {
                alert('Guage Cannot be Empty');
            }
            if (makemm == '') {
                alert('Make Cannot be Empty');
            }
            if (dia == '') {
                alert('Dia Cannot be Empty');
            }
            if (ap == '') {
                alert('A/P Cannot be Empty');
            }
            if (straight == '') {
                alert('Straight Cannot be Empty');
            }
            if (foot == '') {
                alert('Foot Cannot be Empty');
            }
            if (party == '' || nag == '' || sizemm == '' || guagemm == '' || makemm == '' || dia == '' || ap == '' || straight == '' || foot == '') {
                alert('Please Fill All Details');
                $(".delv").val("");
                $(".delv").focus();
            } else {
                $(".delvdate").val(today);

            }
        } else {
            $(".delvdate").val("0000-00-00");
        }
    }


    function markchk() {
        var mark = $('.mark').val();
        var ndelv = mark.toUpperCase();

        if (mark != '') {

            if (mark.toLowerCase() === 'u' || mark.toLowerCase() === 'v' || mark.toLowerCase() === 's') {
                $(".mark").val(ndelv);
            } else {
                alert('Please Enter "U-Urgent", "V - Very Urgent", "S-Sample" Only');
                $('.mark').val('');
            }
        }

    }
    
    function apchk() {
        var ap = $('.ap').val();
        if (ap != '') {
            if (ap.toLowerCase() === 'a' || ap.toLowerCase() === 'p' || ap.toLowerCase() === 'ap' || ap.toLowerCase() === 'n') {
            } else {
                alert('Please Enter "A", "P", "AP", "N" Only');
                $('.ap').val('');
            }
        }

    }


    function sizemmfunc() {
        var sizemm = $('.sizemm').val();
        if (sizemm == '') {
            $('#sizemmod').modal('show');
            $("#sizemmod").on('shown.bs.modal', function () {
                $(".sval").focus();
            });
        }
    }
    function guagemmfunc() {
        var guagemm = $('.guagemm').val();
        if (guagemm == '') {
            $('#guagemmod').modal('show');
            $("#guagemmod").on('shown.bs.modal', function () {
                $(".gval").focus();
            });
        }
    }
    function makemmfunc() {
        var makemm = $('.makemm').val();
        if (makemm == '') {
            $('#makemmod').modal('show');
            $("#makemmod").on('shown.bs.modal', function () {
                $(".mval").focus();
            });
        }
    }
    function ratefunc() {
        var rate = $('.rate').val();
        if (rate == '') {
            $('#prate').modal('show');
            getSearchData();
        }
    }
    function sizemmCal() {
        var sval = $('.sval').val(),
                soval = $('.soval').val();
        $.post('get_chalDtl.php', {sval: sval, soval: soval}, function (data) {
            $(".sizemm").val(data);
            $('#sizemmod').modal('hide');
            $(".sizemm").focus();
        });
    }
    function makemmCal() {
        var mval = $('.mval').val(),
                moval = $('.moval').val();
        $.post('get_chalDtl.php', {sval: mval, soval: moval}, function (data) {
            $(".makemm").val(data);
            $('#makemmod').modal('hide');
            $(".makemm").focus();
        });
    }
    function guagemmCal() {
        var gval = $('.gval').val(),
                sizemm = $('.sizemm').val(),
                goval = $('.goval').val();
        $.post('get_chalDtlG.php', {gval: gval, goval: goval, sizemm: sizemm}, function (data) {
            $(".guagemm").val(data);
            $('#guagemmod').modal('hide');
            $(".guagemm").focus();
        });
    }
    function remove() {
        $(this).closest("tr").remove();
    }
    $("#hsearch").hide();
    var complete = $(".complete").val(),
            usertyp = $(".usertyp").val();
    if (usertyp > 1) {
        if (complete != '' && complete != '0000-00-00') {
            $("#hedit").hide();
        } else {
            $("#hedit").show();
        }
    }

//    var addflg = $(".addflg").val();
//    if (addflg == 1) {
//        getItem();
//        function getItem() {
//
//            $.ajax({
//                type: 'GET',
//                dataType: 'html',
//                url: "get_inv_item.php",
//                data: "itemtype=WH",
//                success: function (response) {
//                    var m = response;
//                    $(".databarcode").html(m);
//                },
//                error: function (request, textStatus, errorThrown) {
//
//                }
//            });
//        }
//    }

    $("#hprevious").hide();
    $("#hnext").hide();
//    $("#submit").hide();

    $(document).keydown(function (event) {
//19 for Mac Command+S
        if (!(String.fromCharCode(event.which).toLowerCase() == 's' && event.altKey) && !(event.which == 19))
            return true;
        //alert("Ctrl-s pressed");
        $("#finishTab").click();
        event.preventDefault();
        return false;
    });
    $("#finishTab").click(function () {
        var valid = true;
        $(".myTable").find('tr').each(function () {
            var row = $(this);
            var srno = row.find('.sno').val();
            var rate = row.find('.rate').val();
            var item = row.find('.item').val();
            if (item != '') {
                if (rate == 0) {
                    alert('Rate Cannot Be Zero For Item  ' + item + '  at Sr.  ' + srno);
//                return false;
                    valid = false;
                    row.find('.rate').focus();
                }
            }

        });
//        var conft = confirm('Want to Update Rate in Item Master?');
//        if (conft == true) {
//            $('.saveupd').val('1');
//        } else {
//            $('.saveupd').val();
//        }

        if (valid) {
            var isValid = true;
            if (isValid) {
                $("#submit").show();
                $("#submit").click();
            }
        }
    });
    function getPartyType() {
        var party = $(".party").val(),
                addflg = $(".addflg").val();
        if (addflg == "1") {
            $.post('get_partytype.php', {party: party}, function (data) {
                $(".voltype").val(data);
            });
        }
    }

    function getOrderNo() {
        var party = $(".party").val(),
                addflg = $(".addflg").val();
        if (addflg == "1") {
            $.post('get_out_no.php', {party: party}, function (data) {
                $(".outwardno").html(data);
            });
        }
    }

    function getBillNo() {
        var company = $(".company").val(),
                vouchno = $(".vouchno").val(),
                yearno = $(".yearno").val(),
                billno = company + '/' + yearno + '/WH-' + vouchno;
        $(".billno").val(billno);
    }

    function getOrderDtl() {
        var outwardno = $(".outwardno").val();
        var addflg = $(".addflg").val();
        if (addflg == "1") {
            $.post('get_outward_dtl_wh.php', {outwardno: outwardno}, function (data) {
                $('.tbody').html("");
                $(data).appendTo('.tbody');
            });
        }
    }


    function showdtl() {
        var $row = $(this).closest("tr");
        var code = $row.find('.barcode').val(),
                party = $('.party').val();
        if (code != '') {
            $.ajax({
                type: 'GET',
                dataType: 'json',
                url: "show_item_dtl_new.php",
                data: "code=" + code + "&party=" + party + "",
                success: function (response) {

                    $row.find('.item').val(response.name);
                    $row.find('.company').val(response.compcode);
                    $row.find('.article').val(response.articode);
                    $row.find('.itemqty').val(response.itemqty);
                    $row.find('.color').val(response.colrcode);
                    $row.find('.size').val(response.sizecode);
                    $row.find('.rate').val(response.rate);
                    $row.find('.qty').focus();
                    $row.find('.qty').select();
                },
                error: function (request, textStatus, errorThrown) {
                    alert(errorThrown);
                }
            });
        }
//        getStock();
    }



    function getGST() {
        var gsttype = $(".gsttype").val();
//        var oms = $(".oms").val();
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "get_Gstperc.php",
            data: {gsttype: gsttype},
            success: function (response) {
                var gstperc = response.per;
                var cgstperc = gstperc / 2;
                if ($(".oms").prop('checked')) {
                    $('.igstperc').val(gstperc);
                    $('.cgstperc').val(0);
                    $('.sgstperc').val(0);
                    $('.cgst').val(0);
                    $('.sgst').val(0);
                } else {
                    $('.igstperc').val(0);
                    $('.igst').val(0);
                    $('.cgstperc').val(cgstperc);
                    $('.sgstperc').val(cgstperc);
                }

            },
            error: function (request, textStatus, errorThrown) {
                alert(errorThrown);
            }
        });
        mainbCal();
    }




    $('#addnew').click(function () {
        AddnewData();
    });
    function AddnewData() {
        $(".entextlbl").show();
        $(".todaydata").show();
        $(".ndata").show();
        $("#submit").show();
        $(".mark").focus();
        $(".tohide").hide();
    }
    function autoAddRows() {
        var ctr = $('#items').val();
//        alert(ctr);
        var tr = $(this).closest("tr");
        var srno = tr.find(".sno").val();
//                alert(srno);

        if (srno == ctr) {
//            AddRows();

        }
    }

    function remove() {
        $(this).closest("tr").remove();
    }
//
//    function AddRows() {
//        var ctr = $('#items').val();
//        var hide_check = "";
//        if ($("#del_all").prop("checked")) {
//            hide_check = 0;
//        } else {
//            hide_check = 1;
//        }
//        var detail = ".fetchpart_" + ctr;
//        $.post('invoicewh_add.php', {ctr: ctr, hide: hide_check}, function (data) {
//            $(data).appendTo('.tbody');
//            ctr++;
//            $('#items').val(ctr);
////            $(detail).select2();
//            $('#barcode_' + ctr).focus();
////            alert('#barcode_' + ctr);
//        });
//    }
    //    
//    function itemCal() {
//        var tr = $(this).closest("tr");
//        var nag = tr.find(".nag").val();
//        var foot = tr.find(".foot").val();
//        avr weight = tr.find(".weight").val();
//        var strt = tr.find(".straight").val();
//        var rate = tr.find(".rate").val();
//        var amount = 0;
//        if (weight == '') {
//            if (strt == 'Y') {
//                var tfoot = nag * foot;
//                amount = rate * tfoot;
//            } else {
//                var tfoot = 0;
//                amount = rate * nag;
//            }
//        } else {
//            amount = rate * weight;
//        }
//        tr.find(".amount").val(amount);
//    }

    function mainbCal() {
        var freight = $(".freightamt").val(),
                totamt = $(".totamt").val(),
                cgstperc = $(".cgstperc").val(),
                igstperc = $(".igstperc").val(),
                taxable = parseFloat(totamt) + parseFloat(freight),
                cgstamt = taxable * (cgstperc / 100),
                igstamt = taxable * (igstperc / 100),
                netamt = parseFloat(taxable) + parseFloat(cgstamt) + parseFloat(cgstamt) + parseFloat(igstamt);
        $(".taxable").val(taxable);
        $(".cgst").val(cgstamt);
        $(".sgst").val(cgstamt);
        $(".igst").val(igstamt);
        $(".netamt").val(netamt);
    }


// VALIDATIONS - START//

    $(".number").keydown(function (e) {
// Allow: backspace, delete, tab, escape, enter and .
        if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110]) !== -1 ||
                // Allow: Ctrl+A, Command+A
                        (e.keyCode === 65 && (e.ctrlKey === true || e.metaKey === true)) ||
                        // Allow: home, end, left, right, down, up
                                (e.keyCode >= 35 && e.keyCode <= 40)) {
                    // let it happen, don't do anything
                    return;
                }
                // Ensure that it is a number and stop the keypress
                if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
                    e.preventDefault();
                }
            });
    $('.alpha').keypress(function (e) {
        var regex = new RegExp("^[a-zA-Z ]+$");
        var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
        if (regex.test(str)) {
            return true;
        } else {
            e.preventDefault();
//                        alert('Please Enter Alphabate');
            return false;
        }
    });
    $("input").blur(function () {
        if ($(this).hasClass('PAN')) {
            var PanNo = $(this).val();
            var PanValidation = /^([a-zA-Z]{5})(\d{4})([a-zA-Z]{1})$/; // PAN validtion for Personal only	

            PanNo = PanNo.toUpperCase();
            $(this).val(PanNo);
            if (!PanValidation.test(PanNo)) {
                alert('Invalid PAN');
                $(this).val("");
            } else if (PanNo.length < 10) {
                alert('PAN Length Invalid');
                $(this).val("");
            } else {


            }
        }


        if ($(this).hasClass('mobileNo')) {
            if ($(this).val() != '') {
                var value = $(this).val();
                if (value.length < 15) {
                    alert('Invalid Number');
                    $(this).val("");
                } else if (value.substr(0, 1) == 9 || value.substr(0, 1) == 8 || value.substr(0, 1) == 7) {

                } else {

                }
            }
        }
        if ($(this).hasClass('email')) {
            var a = $(this).val();
            var filter = /^[A-Za-z0-9!#$%&\'*+-/=?^_`{|}~]+@[A-Za-z0-9-]+(\.[AZa-z0-9-]+)+[A-Za-z]$/;
            if (filter.test(a)) {

            } else {
                alert('Invalid Email');
                $(this).val("");
            }
        }
    });
    $('.alphanumaric').keypress(function isNumberCommaDot(evt) {
        var theEvent = evt || window.event;
        var key = theEvent.keyCode || theEvent.which;
        if (key === 9) { //TAB was pressed
            return;
        }

        key = String.fromCharCode(key);
        if (key.length == 0)
            return;
        var regex = /^[0-9a-zA-Z\9\b]*\.?[0-9]*$/;
        if (!regex.test(key)) {
            theEvent.returnValue = false;
            if (theEvent.preventDefault)
                theEvent.preventDefault();
        }
    });
    // VALIDATIONS - END  //




});