$(document).ready(function () {

    $("body").on("click", '.remove', remove);
    $("body").on("input", '.passport', Calrows);
    $("body").on("blur keyup change click", '.passport, .misc, .courier', Calamt);
    $("body").on("keyup blur", ".passport", getPassdetail);


    function Calamt() {
        var memadd = 0;
        var totamt = 0;
        $(".memcash").each(function () {
          if (!isNaN(this.value) && this.value.length != 0) {
            memadd += parseFloat(this.value);
          }
        });
        $(".subtot").val(memadd);
        var couramt = $('.courier').val(), 
         miscamt = $('.misc').val(),
         subtot = $('.subtot').val(),
         totamt = parseFloat(subtot) + parseFloat(miscamt) + parseFloat(couramt);
         $('.totamt').val(totamt);
      }


  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,
              memcash = response.memcash,
              stampamt = response.stampamt,
              imigamt = response.imigamt,
              mofamt = response.mofamt,
              misc = response.misc;

          tr.find(".paxname").val(paxname);
          tr.find(".stampamt").val(stampamt);
          tr.find(".imigamt").val(imigamt);
          tr.find(".mofamt").val(mofamt);
          tr.find(".miscamt").val(misc);
          tr.find(".memcash").val(memcash);
        }
      },
      error: function (request, textStatus, errorThrown) {},
    });
    Calamt();
  }
  





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

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



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

});