$(document).ready(function () {
    $("body").on("click", ".remove", remove);
    $("body").on("keyup blur change", ".rate", Calc);
    $("body").on("blur change", '.checkin, .checkout,.roe', calHotel);
    $("body").on("blur keyup focus", '.checkin,.checkout,.nonights,.rate,.qty,.amount,.roe,.extchg', getcalc);
    // $("body").on("blur keyup", ".visano, .paxname, .period, .type", newPax);
    $("tbody").on("change blur", ".doi", getDoe);
    $("body").on("change", '.city', getHotel);
    $('.visapurch').click(function () {
        $('.pvisa').toggle("slide");
    });
    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 getHotel() {

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

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

            tr.find(".hotelnm").html(data);
        });
    }
    
    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 chg = $('.extchg').val();
        var totamt = parseFloat(chg) + parseFloat(famt);
        $(".totamt").val(totamt);

        var roe = $(".roe").val();
        var hotelinr = Math.round(totamt * roe,0);
        $(".hotelinramt").val(hotelinr);
    }
    
    function Calc() {
        var nopax = $(".nopax").val(),
                rate = $(".rate").val(),
                amount = nopax * rate;
        $(".totamt").val(amount);
    }

    function getDoe() {
        var tr = $(this).closest("tr");
        var inputDate = new Date(tr.find(".doi").val());
        var resultDate = new Date(inputDate);
        resultDate.setFullYear(inputDate.getFullYear() + 10);
        resultDate.setDate(resultDate.getDate() - 1);
        var resultDateString = resultDate.toISOString().split("T")[0];
        tr.find(".doe").val(resultDateString);
    }

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

    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("hotelsl_add.php", {ctr: ctr, hide: hide_check}, function (data) {
            $(data).appendTo(".tbody");
            ctr++;
            $("#items").val(ctr);
            $(detail).select2();
        });
//    newPax();
    }

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

    }
});
