$(document).ready(function () {
  $("body").on("blur click keyup change", ".memcode", getCustname);
  $("body").on("change", ".formem", getmem);
  $("body").on("blur keyup click change", ".formem", chkmem);
  $("body").on("blur keyup click change", ".amount, .advance", cal);
  $("body").on("blur keyup click change", ".amount, advance", seccal);
//   $("#hprint").hide();

  function getCustname() {
    var memcode = $(".memcode").val();

    $.ajax({
      type: "POST",
      dataType: "json",
      url: "getmemdetail.php",
      data: { memcode: memcode },
      success: function (response) {
        if (response != "") {
          var name = response.name,
            add1 = response.add1,
            add2 = response.add2,
            add3 = response.add3,
            sex = response.gender,
            mstatus = response.mstatus,
            phydisabled = response.phydisabled,
            mobile = response.mobile;

            if (sex == 'F'){
            var  gender = 'Female';
            } else if (sex == 'M'){
            var gender = 'Male';
          }
          
          if (mstatus == 'W' || mstatus == 'Widow'){
            var  married = 'Widow';
          } else if (mstatus == 'M' || sex == 'Married'){
            var married = 'Married';
          } else if (mstatus == 'S' || sex == 'Single'){
            var married = 'Single';
          }
          $(".name").val(name);
          $(".add1").val(add1);
          $(".add2").val(add2);
          $(".add3").val(add3);
          $(".gender").val(gender);
          $(".mobile").val(mobile);
          $(".mstatus").val(married);
          $(".phydis").val(phydisabled);
          $(".gender").val(gender);
        }
      },
      error: function (request, textStatus, errorThrown) {},
    });
  }
  
  function getmem() {
    var formem = $(".formem").val();
    var hall = $(".hall").val();

    $.ajax({
      type: "POST",
      dataType: "json",
      url: "getmemdetail.php",
      data: { formem: formem , hall: hall},
      success: function (response) {
        if (response != "") {
          var amount = response.amount;

        
          $(".amount").val(amount);
        }
      },
      error: function (request, textStatus, errorThrown) {},
    });
  }

  function chkmem() {
    var checkmem = $(".formem").val();
    if (checkmem == "Member") {
      $(".membercode").show(100);
    } else {
      $(".membercode").hide(100);
    }
  }

  function cal() {
    var amount = $(".amount").val();
    advance = $(".advance").val();
    bal = $(".balance").val();
    if (amount > 0) {
        (bal = amount - advance);
    }
    $(".balance").val(bal);
}

function seccal() {
    var amount = $(".amount").val();
    advance = $(".advance").val();
    if(amount > 0){
        (advance = Math.round((amount / 100) * 50))
    }
    $(".advance").val(advance);
  }
});
