$(document).ready(function () {

    $("body").on("click", '.remove', remove);
    $("body").on("input", '.passport', Calrows);
    $("body").on("blur keyup click", '.paxno, .avlbl, .remove', Calmain);
    $("body").on("keyup blur", ".passport", getPassdetail);
    $("body").on("blur", ".visaiss", AddRows);


    function Calmain() {
        var totpax = 0;
        var totavlbl = 0;
        $(".paxno").each(function () {
          if (!isNaN(this.value) && this.value.length != 0) {
            totpax += parseFloat(this.value);
          }
        });
        $(".totpax").val(totpax);
        $(".avlbl").each(function () {
          if (!isNaN(this.value) && this.value.length != 0) {
            totavlbl += parseFloat(this.value);
          }
        });
        $(".totavlbl").val(totavlbl);
      }


  function getPassdetail() {

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

    $.ajax({
      type: "POST",
      dataType: "json",
      url: "getinvdetail.php",
      data: { passport: passport },
      success: function (response) {
        if (response != "") {
          var paxname = response.paxname,
              submission_date = response.submission_date,
              memcash = response.memcash,
              mofano = response.mofano,
              delvstatus = response.delvstatus;

          tr.find(".paxname").val(paxname);
          tr.find(".memcash").val(memcash);
          tr.find(".submission_date").val(submission_date);
          tr.find(".mofano").val(mofano);
          tr.find(".delvstatus").val(delvstatus);
        }
      },
      error: function (request, textStatus, errorThrown) {},
    });
  }

    $('#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 focusFld = ".focusFld_" + ctr;
        $.post('hr_ent_add.php', {ctr: ctr, hide: hide_check}, function (data) {
          ctr++;
            $(data).appendTo('#tabledata');
            $('#items').val(ctr);
            $(focusFld).focus();
        });
    }

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



    function Calrows() {
       var rowCount = $('#tabledata tr').length;
        $('.nofpass').val(rowCount);
    }

});