$(document).ready(function () {
    $("body").on("click", ".remove", remove);
    $("body").on("click", ".rmfitPax", rmFitPax);
    $("body").on("focus", ".sramt", autoAddRows);
    $("body").on("change blur", ".mehramno", automehram);
    $("body").on("change blur keyup", ".cancelamt,.discamt,.commission,.otherexp,.currencyamt,.moulimamt,.touramt,.ticketamt,.tcsamt", calAmt);
    $("body").on("click", ".chkmarried", checkMarried);
    $("body").on("click", ".passport_ready", checkPassport);
    $("body").on("blur", ".issuedate", fillExpiryDate);
    $("body").on("click", "#chq", chq);
    $("body").on("click", "#rtgs", rtgs);
    $("body").on("click", "#dd", dd);
    $("body").on("click", "#rec", rec);
    $("body").on("blur", ".cancdate", activaTab);
    $("body").on("blur change focus", ".suffix, .name, .midname, .surname", autoRecpName);
    $("body").on("blur", ".accomppax", getaccomppax);
    $("body").on("click", ".vermob", genMobOtp);
    $("body").on("click", ".veremail", genEmailOtp);
    $("body").on("click", '#subotp', otpsub);
    $("body").on("click", '#subemail', otpEmail);
    $("body").on("blur", '.expirydate', checkExpDate);
    // $("body").on("blur", '.mobile', checkMobile);
    $("body").on("blur change keyup focus", '.fcheckin, .fcheckout, .fnopax, .fnonights,.frate,.roe', calFood);
    $("body").on("blur change keyup focus", '.lcheckin, .lcheckout, .lnopax, .lnonights,.lrate,.roe', calLaund);
    $("body").on("blur change keyup focus", '.vrate, .vnopax', calVisa);
    $("body").on("blur change keyup focus", '.rrate, .rnoveh,.roe', calTrans);
    $("body").on("blur change keyup focus", '.tnopaxchild, .tnopaxinf, .tadult, .tchild, .tinfant, .tnopax, .tikthandchg,.tiktgstperc', calTikt);
    $("body").on("blur change keyup focus", '.vcountry', getVrate);


    $("body").on("blur keyup focus", '.checkin,.checkout,.nonights,.rate,.qty,.amount,.roe', getcalc);
    $("body").on("blur change", '.checkin, .checkout,.roe', calHotel);
    $("body").on("change", '.city', getHotel);
    $("body").on("change", '.scity', getHnm);
    $("body").on('click', '.sksearch', ModaLSearch);
    $("body").on('blur', '.mcheckout', checkinout);
    $("body").on('click', '.geninv', geninv);
    $("body").on('click', '.geninv2', geninv2);
    $("body").on("change", ".pstate", getStateCode);

    $("body").on("focus blur", ".mobile", getPaxDtl);
    $("body").on("blur change input", ".inrtotalamt, .discountinr", getINRAmts);
    $("body").on("change", "#nationality", getCallCode);

    function getCallCode() {
//        var nationality = $(this).val();

        var code = $(this).find(':selected').data('code') || '';
        $('.countcode').val(code);
    }

    function getINRAmts() {

        var tiktamt = parseFloat($(".tiktamt").val()) || 0,
                hotelinramt = parseFloat($(".hotelinramt").val()) || 0,
                foodinramt = parseFloat($(".foodinramt").val()) || 0,
                laundinramt = parseFloat($(".laundinramt").val()) || 0,
                transinramt = parseFloat($(".transinramt").val()) || 0,
                visaamt = parseFloat($(".visaamt").val()) || 0,
                discountinr = parseFloat($(".discountinr").val()) || 0,
                inrtotalamt = 0, netinramt = 0;
        inrtotalamt = tiktamt + hotelinramt + foodinramt + laundinramt + transinramt + visaamt;
        netinramt = inrtotalamt - discountinr;

        $(".inrtotalamt").val(inrtotalamt);
        $(".netinramt").val(netinramt);
    }



    function getPaxDtl() {
        var mobile = $(".mobile").val();

        $.ajax({
            type: "POST",
            dataType: "json",
            url: "getFitPax.php",
            data: {mobile: mobile},
            success: function (response) {
                if (response != "") {

                    var suffix = response.suffix,
                            surname = response.surname,
                            name = response.name,
                            midname = response.midname,
                            gender = response.gender,
                            aadhaarno = response.aadhaarno,
                            panno = response.panno,
                            address = response.address,
                            city = response.city,
                            pin = response.pin,
                            state = response.state,
                            statecode = response.statecode,
                            email = response.email,
                            gstno = response.gstno;

                    $('.suffix').val(suffix).trigger('change');
                    $('.surname').val(surname);
                    $('.name').val(name);
                    $('.midname').val(midname);
                    $('.gender').val(gender).trigger('change');
                    $('.aadhaarno').val(aadhaarno);
                    $('.panno').val(panno);
                    $('.address').val(address);
                    $('.city').val(city);
                    $('.pin').val(pin);
                    $('.pstate').val(state).trigger('change');
                    $('.pstatecode').val(statecode);
                    $('.pemail').val(email);
                    $('.gstno').val(gstno);

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

    function getStateCode() {
        var state = $(".pstate").val();
        $.get('get_stcode.php', {state: state}, function (data) {
            $(".pstatecode").val(data);
        });
    }


    var invno = $(".invno").val();
    if (invno > 0) {
        $(".invvis").show();
        $(".invprn").show();
        $(".invgen").hide();
    } else {
        $(".invvis").hide();
        $(".invprn").hide();
        $(".invgen").show();
    }

    $('.normalinv').hide("");

    $('.invprn').click(function () {
        $('.normalinv').toggle("slide");
    });


    $('.tiktpurch').click(function () {
        $('.ptikt').toggle("slide");
    });
    $('.visapurch').click(function () {
        $('.pvisa').toggle("slide");
    });
    $('.hotelpurch').click(function () {
        $('.photel').toggle("slide");
    });
    $('.foodpurch').click(function () {
        $('.pfood').toggle("slide");
    });
    $('.laundpurch').click(function () {
        $('.plaund').toggle("slide");
    });
    $('.transpurch').click(function () {
        $('.trans').toggle("slide");
    });


    $('.tiktvouch').click(function () {
        $('.tiktprint').toggle("");
    });
    $('.laundvouch').click(function () {
        $('.laundprint').toggle("");
    });
    $('.transvouch').click(function () {
        $('.transprint').toggle("");
    });
    $('.visavouch').click(function () {
        $('.visaprint').toggle("");
    });
    $('.foodvouch').click(function () {
        $('.foodprint').toggle("");
    });
    $('.hotelvouch').click(function () {
        $('.hotelprint').toggle("");
    });
    $('.combvouch').click(function () {
        $('.combprint').toggle("");
    });


    invhide();

    $(".invdthide2").hide();
    function invhide() {
        var invno = $(".invno").val();
        if (invno > 0) {
            $(".invdthide").show();
        } else {
            $(".invdthide").hide();
        }

    }

    function geninv() {
        $(".invdate").focus();
        $(".invdthide").show();
        $(".invdthide2").show();
        $(".invdate").removeAttr('disabled', 'disabled');

    }

    function geninv2() {
        var vouchno = $(".vouchno").val();
        var invdate = $(".invdate").val();
        if (invdate != "") {
            $.post("genInv.php", {vouchno: vouchno, invdate: invdate}, function (data) {
                if (data > 0) {
                    alert("Invoice Generated Successfully.");
                    $(".geninv").hide();
                    $(".invno").show();
                }
            });
        }
    }

    function checkinout() {

        var checkin = $(".mcheckin").val(),
                checkout = $('.mcheckout').val();

        if (checkout != '') {
            if (checkin > checkout) {
                alert("CheckOut Date Cannot Be Less Than CheckIn Date");
                $(".mcheckout").val("0000-00-00");
                $(".mcheckout").focus();
            }
        }

    }

    function ModaLSearch() {
        var city = $(".scity").val(),
                hotelnm = $(".shotelnm").val(),
                roomcatg = $(".roomcatg").val(),
                checkin = $(".mcheckin").val(),
                checkout = $(".mcheckout").val();

        if (city == '') {
            alert("Please Select City");
        }
        if (hotelnm == '') {
            alert("Please Select Hotel Name");
        }

        $.post('gethotelDetails.php', {
            city: city,
            hotelnm: hotelnm,
            roomcatg: roomcatg,
            checkin: checkin,
            checkout: checkout
        }, function (data) {
            $(".hoteldetail").html(data);
        });


        if (city != '' && hotelnm != '' && checkin != '' && checkout != '') {
            $('#modelWindow').modal('show');
        }
    }


    function getHnm() {
        var city = $(".scity").val();
        $.post('getHotel.php', {city: city}, function (data) {
            $(".shotelnm").html(data);
        });
    }

    function calTrans() {
        var tr = $(this).closest("tr"),
                nopax = tr.find(".rnoveh").val(),
                frate = tr.find(".rrate").val(),
                famount = nopax * frate;
        tr.find(".ramount").val(famount);
        var fpax = 0, fveh = 0, famt = 0;
        $(".rnopax").each(function () {
            if (!isNaN(this.value) && this.value.length != 0) {
                fpax += parseFloat(this.value);
            }
        });
        $(".rnoveh").each(function () {
            if (!isNaN(this.value) && this.value.length != 0) {
                fveh += parseFloat(this.value);
            }
        });
        $(".ramount").each(function () {
            if (!isNaN(this.value) && this.value.length != 0) {
                famt += parseFloat(this.value);
            }
        });

        var roe = $(".roe").val();
        var transinr = Math.round(famt * roe, 0);
        $(".transinramt").val(transinr);

        $(".transveh").val(fveh);
        $(".transpax").val(fpax);
        $(".transamt").val(famt);
        getINRAmts();
    }

    function getVrate() {
        var tr = $(this).closest("tr"),
                vouchdt = $(".vouchdt").val(),
                vcountry = tr.find(".vcountry").val(),
                data = {vcountry: vcountry, vouchdt: vouchdt};

        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "getViRate.php",
            data: data,
            success: function (response) {
                var m = response;
                var rate = m.rate;
                tr.find(".vrate").val(rate);

            }
        });
    }

    function calTikt() {
        var tr = $(this).closest("tr"),
                nopax = tr.find(".tnopax").val(),
                nopaxchild = tr.find(".tnopaxchild").val(),
                nopaxinf = tr.find(".tnopaxinf").val(),
                adult = tr.find(".tadult").val(),
                child = tr.find(".tchild").val(),
                infant = tr.find(".tinfant").val(),
                adultamt = nopax * adult,
                childamt = nopaxchild * child,
                infantamt = nopaxinf * infant,
                totalamt = parseFloat(adultamt) + parseFloat(childamt) + parseFloat(infantamt);
        var fpax = 0, fnights = 0, famt = 0, tottikt = 0, tiktgstamt = 0, totfinal = 0;
        tr.find(".tamount").val(totalamt);

        $(".tnopax").each(function () {
            if (!isNaN(this.value) && this.value.lenght != 0) {
                fpax += parseFloat(this.value);
            }
        });
        $(".tiktpax").val(fpax);

        $(".tamount").each(function () {
            if (!isNaN(this.value) && this.value.lenght != 0) {
                famt += parseFloat(this.value);
            }
        });
        $(".tiktsubtot").val(famt);

        var tikthandchg = $(".tikthandchg").val(),
                tiktgstperc = $(".tiktgstperc").val();

        var tiktgstamt = ((tikthandchg * tiktgstperc) / 100).toFixed(2);
        $('.tiktgstamt').val(tiktgstamt);

        var totfinal = Math.round(parseFloat(famt) + parseFloat(tikthandchg) + parseFloat(tiktgstamt));

        $(".tiktamt").val(totfinal);

        getINRAmts();
    }

    function calVisa() {
        var tr = $(this).closest("tr"),
                nopax = tr.find(".vnopax").val(),
                frate = tr.find(".vrate").val(),
                famount = nopax * frate;
        tr.find(".vamount").val(famount);
        var fpax = 0, fnights = 0, famt = 0;
        $(".vnopax").each(function () {
            if (!isNaN(this.value) && this.value.length != 0) {
                fpax += parseFloat(this.value);
            }
        });
        $(".vamount").each(function () {
            if (!isNaN(this.value) && this.value.length != 0) {
                famt += parseFloat(this.value);
            }
        });
        $(".visapax").val(fpax);
        $(".visaamt").val(famt);

        getINRAmts();
    }

    function daysdifference(firstDate, secondDate) {
        var startDay = new Date(firstDate);
        var endDay = new Date(secondDate);

// Determine the time difference between two dates     
        var millisBetween = startDay.getTime() - endDay.getTime();

// Determine the number of days between two dates  
        var days = millisBetween / (1000 * 3600 * 24);

// Show the final number of days between dates     
        return Math.round(Math.abs(days));
    }

    function calFood() {
        var tr = $(this).closest("tr"),
                fcheckin = tr.find(".fcheckin").val(),
                fcheckout = tr.find(".fcheckout").val();
        if (fcheckin != "0000-00-00" && fcheckout != "0000-00-00") {
            var days = daysdifference(fcheckin, fcheckout) || 0;
//        alert(days);
        }
        tr.find(".fnonights").val(days);
        var nopax = tr.find(".fnopax").val();
        var frate = tr.find(".frate").val();
        var famount = days * frate * nopax;
        tr.find(".famount").val(famount);
        var fpax = 0, fnights = 0, famt = 0;
        $(".fnonights").each(function () {
            if (!isNaN(this.value) && this.value.length != 0) {
                fnights += parseFloat(this.value);
            }
        });
        $(".fnopax").each(function () {
            if (!isNaN(this.value) && this.value.length != 0) {
                fpax += parseFloat(this.value);
            }
        });
        $(".famount").each(function () {
            if (!isNaN(this.value) && this.value.length != 0) {
                famt += parseFloat(this.value);
            }
        });
        var roe = $(".roe").val();
        var foodinr = Math.round(famt * roe, 0);
        $(".foodinramt").val(foodinr);
        $(".foodnights").val(fnights);
        $(".foodpax").val(fpax);
        $(".foodamt").val(famt);
        getINRAmts();
    }

    function calLaund() {
        var tr = $(this).closest("tr"),
                lcheckin = tr.find(".lcheckin").val(),
                lcheckout = tr.find(".lcheckout").val();

        if (lcheckin != "0000-00-00" && lcheckout != "0000-00-00") {
            var days = daysdifference(lcheckin, lcheckout) || 0;
        }

        tr.find(".lnonights").val(days);
        var nopax = tr.find(".lnopax").val();
        var frate = tr.find(".lrate").val();
        var famount = nopax * frate;
        tr.find(".lamount").val(famount);
        var fpax = 0, fnights = 0, famt = 0;
        $(".lnonights").each(function () {
            if (!isNaN(this.value) && this.value.length != 0) {
                fnights += parseFloat(this.value);
            }
        });
        $(".lnopax").each(function () {
            if (!isNaN(this.value) && this.value.length != 0) {
                fpax += parseFloat(this.value);
            }
        });
        $(".lamount").each(function () {
            if (!isNaN(this.value) && this.value.length != 0) {
                famt += parseFloat(this.value);
            }
        });
        var roe = $(".roe").val();
        var laundinr = Math.round(famt * roe, 0);
        $(".laundinramt").val(laundinr);

        $(".laundnights").val(fnights);
        $(".laundpax").val(fpax);
        $(".laundamt").val(famt);
        getINRAmts();
    }

    // function checkMobile() {
    //     var mobile = $(".mobile").val();
    //     if (mobile != '') {
    //         if (mobile.length > 10) {
    //             alert('Mobile No. Cannot Be Greater than 10');
    //             $(".mobile").val("");
    //             $(".mobile").focus();
    //         }
    //         if (mobile.length < 10) {
    //             alert('Mobile No. Cannot Be Less than 10');
    //             $(".mobile").val("");
    //             $(".mobile").focus();
    //         }
    //     }
    // }

    $("form").submit(function (event) {
        var expirydate = $(".expirydate").val(),
                plusdate = $(".plusdate").val();

        if (expirydate != '0000-00-00' && expirydate != "" && plusdate != '0000-00-00') {
            if (expirydate < plusdate) {
                alert('Expiry Date Cannot Be Less then 8 months');
                $(".issuedate").val("0000-00-00");
                $(".expirydate").val("0000-00-00");
                $(".issueplace").val("");
                $(".passportno").val("");
                $(".passportno").focus();
                event.preventDefault();
                $("#submit").show();
            }
        }
    });

    function checkExpDate() {
        var expirydate = $(".expirydate").val(),
                plusdate = $(".plusdate").val();

        if (expirydate != '0000-00-00' && expirydate != "" && plusdate != '0000-00-00') {
            if (expirydate < plusdate) {
                alert('Expiry Date Cannot Be Less then 8 months');
                $(".issuedate").val("0000-00-00");
                $(".expirydate").val("0000-00-00");
                $(".issueplace").val("");
                $(".passportno").val("");
                $(".passportno").focus();
            }
        }
    }


    var emailver = $(".emailver").val(),
            mobver = $(".mobver").val();
    if (mobver != '1') {
        $(".vermob").show();
        $(".verifiedmob").hide();
    } else {
        $(".vermob").hide();
        $(".verifiedmob").show();
    }
    if (emailver != '1') {
        $(".veremail").show();
        $(".verifiedemail").hide();
    } else {
        $(".veremail").hide();
        $(".verifiedemail").show();
    }

    $("#edit").click(function () {
        $(".addflag").val("0");
    });

    function genEmailOtp() {
        var addflg = $(".addflag").val(),
                email = $(".email").val(),
                data = {email: email};
        if (addflg != 2) {
            if (email != '') {
                $.ajax({
                    type: 'GET',
                    dataType: 'json',
                    url: "getGenratedOtp.php",
                    data: data,
                    success: function (response) {
                        var m = response;
                        $("#overlay_form").show();
                        $(".subemail").show();
                        $(".subotp").hide();
                        alert("OTP Send to Entered Email Id.");
                        $(".otprecv").val("");
                        $(".otprecv").focus();
                    }
                });
            }
        }
    }


    function genMobOtp() {
        var addflg = $(".addflag").val(),
                mobile = $(".mobile").val(),
                data = {mobile: mobile};
        if (addflg != 2) {

            if (mobile.length == 10) {
                $.ajax({
                    type: 'GET',
                    dataType: 'json',
                    url: "getGenratedOtp.php",
                    data: data,
                    success: function (response) {
                        var m = response;
//                    if (m.success == true) {
                        $("#overlay_form").show();
                        $(".subemail").hide();
                        $(".subotp").show();
//                        $("#browdata").val(m.msp);
                        alert("OTP Send to Entered Mobile No.");
                        $(".otprecv").val("");
                        $(".otprecv").focus();
//                    }

                    }
                });
            } else {
                alert("Enter a Valid Mobile No.");
            }
        }
    }

    $("#close_form").click(function () {
        $("#overlay_form").hide();

    });

    function otpsub() {
        var otprecv = $(".otprecv").val(),
                mobile = $(".mobile").val(),
                data = {otprecv: otprecv, mobile: mobile};
        if (otprecv != '') {
            $.ajax({
                type: 'GET',
                dataType: 'json',
                url: "checkOtp.php",
                data: data,
                success: function (response) {
                    var m = response;
                    if (m.success == true) {
                        $("#overlay_form").hide();
                        alert('Mobile No. Verified');
                        $(".vermob").hide();
                        $(".verifiedmob").show();
                        $(".mobotp").val(otprecv);
                        $(".mobver").val("1");
                    } else {
                        alert('OTP Does Not Match!');

                    }


                }
            });
        }
    }

    function otpEmail() {
        var otprecv = $(".otprecv").val(),
                email = $(".email").val(),
                data = {otprecv: otprecv, email: email};
        if (otprecv != '') {
            $.ajax({
                type: 'GET',
                dataType: 'json',
                url: "checkOtp.php",
                data: data,
                success: function (response) {
                    var m = response;
                    if (m.success == true) {
                        $("#overlay_form").hide();
                        alert('Email Verified');
                        $(".veremail").hide();
                        $(".verifiedemail").show();
                        $(".emailotp").val(otprecv);
                        $(".emailver").val("1");

                    } else {
                        alert('OTP Does Not Match!');

                    }


                }
            });
        }
    }

    var pax = $(".accomppax").val();

    $.post("getpaxname.php", {pax: pax}, function (data) {
        $(".accompperson").val(data.name);
    }, 'json');


    function getaccomppax() {
        var pax = $(".accomppax").val();

        $.post("getpaxname.php", {pax: pax}, function (data) {
            $(".accompperson").val(data.name);
        }, 'json');
    }

    function autoRecpName() {
        var suf = $(".suffix").val(),
                name = $(".name").val(),
                midname = $(".midname").val(),
                surname = $(".surname").val(),
                fullpname = suf + ' ' + name + ' ' + ' ' + midname + ' ' + surname;
        $(".pay_pax_name").val(fullpname);
    }

    function activaTab() {
        $('.nav-tabs a[href="#tab5default"]').tab('show');
        $("#cancelamt").focus();

    }


    $("#chq_mod").hide();
    $("#rtgs_mod").hide();
    $("#dd_mod").hide();

    function chq() {
        $("#rtgs_mod").hide();
        $("#dd_mod").hide();
        $("#chq_mod").slideToggle(200);
    }

    function rtgs() {
        $("#chq_mod").hide();
        $("#dd_mod").hide();
        $("#rtgs_mod").slideToggle(200);
    }

    function dd() {
        $("#chq_mod").hide();
        $("#rtgs_mod").hide();
        $("#dd_mod").slideToggle(200);
    }

    function rec() {
        $("#chq_mod").hide();
        $("#rtgs_mod").hide();
        $("#dd_mod").hide();

        $("#rec_mod").slideToggle(200);
    }

    function fillExpiryDate() {
        var tt = document.getElementById('issuedate').value;

        var date = new Date(tt);
        var newdate = new Date(date);

        newdate.setDate(newdate.getDate() - 1);

        var dd = newdate.getDate();
        var mm = newdate.getMonth() + 1;
        var y = newdate.getFullYear() + 10;
        if (mm > 9) {
            mm = mm;
        } else {
            mm = '0' + mm;
        }
        if (dd > 9) {
            dd = dd;
        } else {
            dd = '0' + dd;
        }

        var someFormattedDate = y + '-' + mm + '-' + dd;
        document.getElementById('expirydate').value = someFormattedDate;
    }



    function calAmt() {
        var cancelamt = $(".cancelamt").val(),
                discamt = $(".discamt").val(),
                commission = $(".commission").val(),
                otherexp = $(".otherexp").val(),
                currencyamt = $(".currencyamt").val(),
                moulimamt = $(".moulimamt").val(),
                touramt = $(".touramt").val(),
                ticketamt = $(".ticketamt").val(),
                tcsamt = $(".tcsamt").val();
        var subtot = parseFloat(touramt);

        var nettot = parseFloat(subtot) + parseFloat(tcsamt) - parseFloat(discamt) - parseFloat(cancelamt);
        $(".subtotal").val(subtot);
        $(".netamt").val(nettot);

    }
    checkMarried();
    function checkMarried() {

        var chk = $(".chkmarried").prop('checked');

        if (chk == true) {
            $(".marrcol").show();
            $(".not_married").hide();

        } else {
            $(".marrcol").hide();
            $(".not_married").show();
        }
    }

    checkPassport();
    function checkPassport() {

        var chk = $(".passport_ready").prop('checked');

        if (chk == true) {
            $(".pasntrd").show();
            $(".passread").hide();

        } else {
            $(".pasntrd").hide();
            $(".passread").show();
        }
    }


    function readIMG(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();
            reader.onload = function (e) {
                $('#preview').attr('src', e.target.result);
            }
            reader.readAsDataURL(input.files[0]);
        }
    }

    $("#file3").change(function () {
        readIMG(this);
    });
    function readIMG2(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();
            reader.onload = function (e) {
                $('#preview2').attr('src', e.target.result);
            }
            reader.readAsDataURL(input.files[0]);
        }
    }
    $("#file2").change(function () {
        readIMG2(this);
    });


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

    ////////////////////////////////////////////// ADD Files Part - Staart  //////////////////////////////////////////////

    function rmFitPax() {
        var id = $(this).attr("id");

        $(".pxrw_" + id).remove();
    }

    $('#addtrpax').click(function () {
        AddPaxFit();
    });

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

        var detail = ".fetchpart_" + ctr;
        $.post('fitpax_add.php', {ctr: ctr, hide: hide_check}, function (data) {
            $(data).appendTo('.fitpaxbody');
            ctr++;
            $('#paxctr').val(ctr);
            $(detail).select2();
        });
    }

    $('#addtransflt').click(function () {
        AddTransFlt();
    });

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

        var detail = ".fetchpart_" + ctr;
        $.post('transflt_add.php', {ctr: ctr, hide: hide_check}, function (data) {
            $(data).appendTo('.ftldetbody');
            ctr++;
            $('#tfcnt').val(ctr);
            $(detail).select2();
        });
    }


    $('#addtrans').click(function () {
        AddTrans();
    });

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

        var detail = ".fetchpart_" + ctr;
        $.post('trans_add.php', {ctr: ctr, hide: hide_check}, function (data) {
            $(data).appendTo('.transbody');
            ctr++;
            $('#transcnt').val(ctr);
            $(detail).select2();
        });
    }

    $('#addtikt').click(function () {
        AddTikt();
    });

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

        var detail = ".fetchpart_" + ctr;
        $.post('tikt_add.php', {ctr: ctr, hide: hide_check}, function (data) {
            $(data).appendTo('.tiktbody');
            ctr++;
            $('#tiktcnt').val(ctr);
            $(detail).select2();
        });
    }


    $('#addfood').click(function () {
        AddFood();
    });

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

        var detail = ".fetchpart_" + ctr;
        $.post('food_add.php', {ctr: ctr, hide: hide_check}, function (data) {
            $(data).appendTo('.foodbody');
            ctr++;
            $('#foodcnt').val(ctr);
            $(detail).select2();
        });
    }

///////////////////Purchase Part - Start//////////////////////////////////////////////


    $('#addpurch').click(function () {
        AddPurch();
    });

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

        var detail = ".fetchpart_" + ctr;
        $.post('tranpurch_add.php', {ctr: ctr, hide: hide_check}, function (data) {
            $(data).appendTo('.purchbody');
            ctr++;
            $('#purchcnt').val(ctr);
            $(detail).select2();
        });
    }

    function getHotel() {

        var tr = $(this).closest("tr"),
                city = tr.find(".city").val();

        $.post('getHotel.php', {city: city}, function (data) {

            tr.find(".hotelnm").html(data);
        });
    }



    function daysdifference(firstDate, secondDate) {
        var startDay = new Date(firstDate);
        var endDay = new Date(secondDate);

// Determine the time difference between two dates     
        var millisBetween = startDay.getTime() - endDay.getTime();

// Determine the number of days between two dates  
        var days = millisBetween / (1000 * 3600 * 24);

// Show the final number of days between dates     
        return Math.round(Math.abs(days));
    }


    function calHotel() {
        var tr = $(this).closest("tr"),
                fcheckin = tr.find(".checkin").val(),
                fcheckout = tr.find(".checkout").val();
        if (fcheckin != "0000-00-00" && fcheckout != "0000-00-00") {
            var days = daysdifference(fcheckin, fcheckout);
//        alert(days);
        }
        tr.find(".nonights").val(days);
    }


    function getcalc() {
        var tr = $(this).closest("tr"),
                nonights = tr.find(".nonights").val(),
                qty = tr.find(".qty").val(),
                rate = tr.find(".rate").val();

        var amount = (qty * rate * nonights).toFixed(2);

        tr.find(".hamount").val(amount);
        var famt = 0;
        $(".hamount").each(function () {
            if (!isNaN(this.value) && this.value.lenght != 0) {
                famt += parseFloat(this.value);
            }
        });
        $(".hotelamt").val(famt);

        var roe = $(".roe").val();
        var hotelinr = Math.round(famt * roe, 0);
        $(".hotelinramt").val(hotelinr);
        getINRAmts();
    }

///////////////////Purchase Part - End//////////////////////////////////////////////

    $('#addlaund').click(function () {
        Addlaund();
    });

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

        var detail = ".fetchpart_" + ctr;
        $.post('laund_add.php', {ctr: ctr, hide: hide_check}, function (data) {
            $(data).appendTo('.laundbody');
            ctr++;
            $('#laundcnt').val(ctr);
            $(detail).select2();
        });
//        $(".barcode").focus();
    }



    $('#addvisa').click(function () {
        AddVisa();
    });

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

        var detail = ".fetchpart_" + ctr;
        $.post('visaent_add.php', {ctr: ctr, hide: hide_check}, function (data) {
            $(data).appendTo('.visabody');
            ctr++;
            $('#visacnt').val(ctr);
            $(detail).select2();
        });
    }

    $('#addnew').click(function () {
        AddRows();
    });
    function remove() {
        $(this).closest("tr").remove();
        calTikt();
        getcalc();
        calFood();
        calLaund();
        calTrans();
        calVisa();
        getINRAmts();
    }

    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('paxent_add.php', {ctr: ctr, hide: hide_check}, function (data) {
            $(data).appendTo('.tbody');
            ctr++;
            $('#items').val(ctr);
            $('#capacity').val(ctr);
            $(detail).select2();
        });
//        $(".barcode").focus();
    }
    function automehram() {
        var paxid = $(".mehramno").val();

        $.post('get_mehram.php', {paxid: paxid}, function (data) {
            $(".mehrname").val(data.name);
            // $(".mehrelation").val(data.rela).trigger('change');
        }, 'json');
    }
});