$(document).ready(function () {
  $("body").on("click", ".remove", remove);
  $("body").on("input", ".passport", Calrows);
  $("body").on("blur keyup change click", ".passport, .memcash", Calamt);
  $("body").on("keyup blur", ".passport", getPassdetail);
  $("body").on("blur", ".subject", AddRows);

  function Calamt() {
    var memadd = 0;
    $(".memcash").each(function () {
      if (!isNaN(this.value) && this.value.length != 0) {
        memadd += parseFloat(this.value);
      }
    });
    $(".totamt").val(memadd);
  }

  function getPassdetail() {
    var tr = $(this).closest("tr"),
      passport = tr.find(".passport").val();
    if (passport != "") {
      $.ajax({
        type: "POST",
        dataType: "json",
        url: "getSettdetail.php",
        data: { passport: passport },
        success: function (response) {
          if (response != "") {
            var paxname = response.paxname,
              agent = response.agent,
            memcash = response.memcash;

            tr.find(".paxname").val(paxname);
            tr.find(".agent").val(agent);
            tr.find(".memcash").val(memcash);
            tr.find(".subject").focus();
          }
        },
        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;
    }
    ctr++;
    var focusFld = ".focusFld_" + ctr;
    $.post("settent_add.php", { ctr: ctr, hide: hide_check }, function (data) {
      $(data).appendTo("#tabledata");
      $("#items").val(ctr);
      $(focusFld).focus();
    });
  }

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

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