$(document).ready(function () {
  $("body").on("blur", ".memcode", getCustname);
  $("body").on("blur", ".eventdt, .hall", getHallDate);
  $("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,
            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);
  }


  $(".eventdt").on("blur", function () {

    var date = new Date($(".vouchdt").val()),
    
     eventdt = new Date($('.eventdt').val());

    if (date != '') {
        if (eventdt < date) {
            alert("Date Cannot Be Less Than Today's Date");
            $(".eventdt").val("0000-00-00");
            $(".eventdt").focus();
        }
    }

});



function getHallDate() {
  var eventdt = $(".eventdt").val();
  var hall = $(".hall").val();
  var vouchno = $(".vouchno").val();
  

  $.ajax({
    type: "POST",
    dataType: "json",
    url: "gethalldate.php",
    data: { eventdt: eventdt, hall: hall},
    success: function (response) {
      if (response != "") {
        var eventdt = response.eventdt,
        vouchno = response.vouchno;
        if (eventdt != "") {
        alert("Hall Date: " + eventdt + " " + "Is Already Booked For Voucher No." + vouchno);
        $(".eventdt").val('');
        $(".eventdt").focus();

        }
      }
    },
    error: function (request, textStatus, errorThrown) {},
  });
}















});
