$(document).ready(function () {
    $("body").on("click", ".remove", remove);
    $("body").on("blur change keyup focus", '.rrate, .rnoveh,.roe', calTrans);
    // $("body").on("blur keyup", ".visano, .paxname, .period, .type", newPax);
    $("tbody").on("change blur", ".doi", getDoe);

    $('.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 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.lenght != 0) {
                fpax += parseFloat(this.value);
            }
        });
        $(".rnoveh").each(function () {
            if (!isNaN(this.value) && this.value.lenght != 0) {
                fveh += parseFloat(this.value);
            }
        });
        $(".ramount").each(function () {
            if (!isNaN(this.value) && this.value.lenght != 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);
    }

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

    $('#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();
        });
    }


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

    }
});
