$(document).ready(function () {
    $("body").on("keyup change blur focus click", "#finishTab , .full, .full2, .fullno, .half, .half2, .halfno, .childwob, .childwob2", calTikt);
    $("body").on("keyup change blur focus click", ".childwobno, .inf, .inf2, .infno, .visachg, .visachgno, .airtax, .airtaxno,.disc, .disc2", calTikt);
    $("body").on("keyup change blur focus click", ".discno, .farediff, .farediff2, .farediffno, .cancelchg, .cancelchg2, .cancelchgno, .amount, .nettot, .cgstno, .sgstno, .tcs, .netamt", calTikt);
    // $("body").on("keyup change blur focus ", '#finishTab, .fullno, .halfno, .childwobno, .infno, .visachgno,.airtax, .airtaxno, .discno, .farediffno, .cancelchgno, .amount, .nettot, .cgstno, .sgstno, .tcs, .netamt', calTikt);
    $("#submit").hide();
    $("body").on("focus", '.manualno', addRows);
    $("body").on("blur", '.haji', copyName);
    $("body").on("blur", '.batchno', getDeptDate);
    //    $("body").on("change", '.tour', getregnno);
    $("body").on("change focus", ".tour,.vouchdt,.haji", getRegno);
    // $("body").on("blur keyup  change", '.makchkin,.makchkout,.madchkin,.madchkout', checkDate);



    $(".paxmemModal").modal("hide");
    $(".PaxDetails").click(function () {
        $(".paxmemModal").modal("show");
    });

   $('.bkinv').click(function () {
        $('.bkinvprint').toggle("");
    });

    function getDeptDate() {
        var batchno = $(".batchno").val(),
                depdate = $(".depdate").val(),
                data = {batchno: batchno};
        if (depdate == "0000-00-00" || depdate == "") {
            $.ajax({
                type: 'GET',
                dataType: 'json',
                url: "get_dept_date.php",
                data: data,
                success: function (response) {
                    var m = response;

                    $(".depdate").val(m.depdate);
                    $(".passlimit").val(m.passlimit);
                    $(".halfpay").val(m.halfpay);
                    $(".fullpay").val(m.fullpay);

                },
                error: function (request, textStatus, errorThrown) {

                }
            });

        }
    }


    function getRegno() {
        var tour = $(".tour").val(),
                addflg = $(".addflg").val();

        if (addflg == 1) {
            $.ajax({
                type: "POST",
                dataType: "json",
                url: "tourregGen.php",
                data: {tour: tour},
                success: function (response) {
                    if (response != "") {
                        var regvouch = response.regvouch;
                        $('.regnno').val(regvouch);
                    }
                },
                error: function (request, textStatus, errorThrown) {
                }
            });
        }
    }


    function copyName() {
        var haji = $(".haji").val(),
                // surname = $(".surname").val(),
                pax = haji;
        $(".fname").val(pax);
    }

    function addRows() {
        var addflg = $(".addflg").val(),
                ctr = $("#items").val(),
                fullno = $(".fullno").val(),
                halfno = $(".halfno").val(),
                childwobno = $(".childwobno").val(),
                infno = $(".infno").val(),
                detail = ".fetchpart_" + ctr,
                ticktamt = $(".paxbody tr:last").find(".ticktamt").val(),
                touramt = $(".paxbody tr:last").find(".touramt").val(),
                totpax = parseFloat(fullno) + parseFloat(halfno) + parseFloat(childwobno) + parseFloat(infno);
        if (addflg == 1) {
            if (parseFloat(totpax) > parseFloat(ctr)) {
                $.post('bookform_add.php', {ctr: ctr, ticktamt: ticktamt, touramt: touramt}, function (data) {
                    $(data).appendTo('.paxbody');
                    ctr++;
                    $('#items').val(ctr);
                    $(detail).select2();

                });
            }
        }
    }


    $("#finishTab").click(function () {

        var vouchno = $('.vouchno').val(),
                vouchdt = $('.vouchdt').val(),
                haji = $('.haji').val(),
                surname = $('.surname').val(),
                craft = $('.craft').val(),
                depdate = $('.depdate').val(),
                passlimit = $('.passlimit').val(),
                halfpay = $('.halfpay').val(),
                fullpay = $('.fullpay').val(),
                add1 = $('.add1').val(),
                city = $('.city').val(),
                mobile = $('.mobile').val(),
                batchno = $('.batchno').val(),
                addflg = $('.addflg').val(),
                valid = true,
                a = 1;


        if (vouchno == '') {
            var message = "Voucher Cannot Be Empty";
            alert(message);
            $("#finishTab").show();

            return false;
            valid = false;
        }
        if (vouchdt == '') {
            var message = "Voucher Date Cannot Be Empty";
            alert(message);
            $("#finishTab").show();

            return false;
            valid = false;
        }
        if (haji == '') {
            var message = "Haji Name Cannot Be Empty";
            alert(message);
            $("#finishTab").show();

            return false;
            valid = false;
        }
        if (surname == '') {
            var message = "Surname Name Cannot Be Empty";
            alert(message);
            $("#finishTab").show();

            return false;
            valid = false;
        }
        if (craft == '') {
            var message = "Airline Cannot Be Empty";
            alert(message);
            $("#finishTab").show();

            return false;
            valid = false;
        }
        if (depdate == '') {
            var message = "Dept. Date Cannot Be Empty";
            alert(message);
            $("#finishTab").show();

            return false;
            valid = false;
        }
        if (passlimit == '' || passlimit == '0000-00-00') {
            var message = "Passport Limit Cannot Be Empty";
            alert(message);
            $("#finishTab").show();

            return false;
            valid = false;
        }
        if (halfpay == '' || halfpay == '0000-00-00') {
            var message = "Half Payment Date Cannot Be Empty";
            alert(message);
            $("#finishTab").show();

            return false;
            valid = false;
        }
        if (fullpay == '' || fullpay == '0000-00-00') {
            var message = "Full Payment Date Cannot Be Empty";
            alert(message);
            $("#finishTab").show();

            return false;
            valid = false;
        }
        if (add1 == '') {
            var message = "Address 1 Cannot Be Empty";
            alert(message);
            $("#finishTab").show();

            return false;
            valid = false;
        }
        if (city == '') {
            var message = "City Cannot Be Empty";
            alert(message);
            $("#finishTab").show();

            return false;
            valid = false;
        }
        if (mobile == '') {
            var message = "Mobile Cannot Be Empty";
            alert(message);
            $("#finishTab").show();

            return false;
            valid = false;
        }
        if (batchno == '') {
            var message = "Batch No. Cannot Be Empty";
            alert(message);
            $("#finishTab").show();

            return false;
            valid = false;
        }

        if (addflg == 1) {
            var haji = $(".haji").val();
            var surname = $(".surname").val();
            var tikt = $(".full").val();
            var tour = $(".full2").val();
            var paxname = haji.trim();
            $(".suffix").val("MR").trigger("change");
            $(".paxname").val(paxname);
            $(".paxgend").val("Male").trigger("change");
            $(".tiktamt").val(tikt);
            $(".touramt").val(tour);
            $(".paxModal").modal("show");
            if (valid) {
                var isValid = true;
                if (isValid) {
                    $("#finishTab").hide();
                    $("#submit").show();
                    $("#cancel").hide();
                }
            }
        } else {
            if (valid) {
//                alert('this');
                var isValid = true;
                if (isValid) {
                    $("#finishTab").hide();
                    $("#submit").click();
                    $("#cancel").hide();
                }
            }
        }
    });

    function calTikt() {

        var full = $(".full").val(),
                full2 = $(".full2").val(),
                fullno = $(".fullno").val(),
                half = $(".half").val(),
                half2 = $(".half2").val(),
                halfno = $(".halfno").val(),
                childwob = $(".childwob").val(),
                childwob2 = $(".childwob2").val(),
                childwobno = $(".childwobno").val(),
                inf = $(".inf").val(),
                inf2 = $(".inf2").val(),
                infno = $(".infno").val(),
                visachg = $(".visachg").val(),
                visachgno = $(".visachgno").val(),
                airtax = $(".airtax").val(),
                airtaxno = $(".airtaxno").val(),
                disc = $(".disc").val(),
                disc2 = $(".disc2").val(),
                discno = $(".discno").val(),
                farediff = $(".farediff").val(),
                farediff2 = $(".farediff2").val(),
                farediffno = $(".farediffno").val(),
                cancelchg = $(".cancelchg").val(),
                cancelchg2 = $(".cancelchg2").val(),
                cancelchgno = $(".cancelchgno").val(),
                cgstno = $(".cgstno").val(),
                sgstno = $(".sgstno").val(),
                tcs = $(".tcs").val(),
                fulltot = parseFloat(full) + parseFloat(full2),
                fullamt = fulltot * parseFloat(fullno);
        $(".fullamt").val(fullamt);

        var halftot = parseFloat(half) + parseFloat(half2),
                halfamt = halftot * parseFloat(halfno);
        $(".halfamt").val(halfamt);

        var childwobtot = parseFloat(childwob) + parseFloat(childwob2),
                childwobamt = childwobtot * parseFloat(childwobno);
        $(".childwobamt").val(childwobamt);

        var inftot = parseFloat(inf) + parseFloat(inf2),
                infamt = inftot * parseFloat(infno);
        $(".infamt").val(infamt);

        var visachgamt = parseFloat(visachg) * parseFloat(visachgno);
        $(".visachgamt").val(visachgamt);

        var airtaxamt = parseFloat(airtax) * parseFloat(airtaxno);
        $(".airtaxamt").val(airtaxamt);

        var disctot = parseFloat(disc) + parseFloat(disc2),
                discamt = disctot * parseFloat(discno);
        $(".discamt").val(discamt);

        var faredifftot = parseFloat(farediff) + parseFloat(farediff2),
                farediffamt = parseFloat(faredifftot) * parseFloat(farediffno);
        $(".farediffamt").val(farediffamt);

        var cancelchgtot = parseFloat(cancelchg) + parseFloat(cancelchg2),
                cancelchgamt = cancelchgtot * parseFloat(cancelchgno);
        $(".cancelchgamt").val(cancelchgamt);


        var amt = fullamt + halfamt + childwobamt + infamt + visachgamt + airtaxamt - discamt;
        $(".amount").val(amt);

        var nettot = amt + farediffamt + cancelchgamt;
        $(".nettot").val(nettot.toFixed(2));

        var cgstamt = parseFloat(nettot) * parseFloat(cgstno) / 100;
        $(".cgstamt").val(cgstamt.toFixed(2));

        var sgstamt = parseFloat(nettot) * parseFloat(sgstno) / 100;
        $(".sgstamt").val(sgstamt.toFixed(2));

        var netamt1 = nettot + cgstamt + sgstamt;

        var tcsamt = parseFloat(netamt1) * parseFloat(tcs) / 100;
        $(".tcsamt").val(tcsamt.toFixed(2));

        var netamt = nettot + cgstamt + sgstamt + tcsamt;


        var netamount = Math.round(netamt);
        $(".netamt").val(netamount);
    }


});