$(document).ready(function () {
  $("body").on("keyup blur", ".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);
    $("body").on("change", '.type', showdetail);
//   $("#hprint").hide();


  var type = $('.type').val();

        if (type !== "GIFT") {

            $('#otherdtl2').hide();

        }else {           

            $('#otherdtl').hide();

        }



   function showdetail() {

        var type = $('.type').val();
        if (type !== "GIFT") {
            $('#otherdtl').css({'display': 'block'});
            $('#otherdtl2').css({'display': 'none'});
        } else if (type == "GIFT" ){
            $('#otherdtl2').css({'display': 'block'});
            $('#otherdtl').css({'display': 'none'});
        }
        
    }





  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,
            mobile = response.mobile;

          $(".name").val(name);
          $(".add1").val(add1);
          $(".add2").val(add2);
          $(".add3").val(add3);
          $(".mobile").val(mobile);
        }
      },
      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);
  }
});
