/***********************************************
* jQuery functions execute
***********************************************/

$(document).ready(function() {
  // bind form using 'ajaxForm'
  $('.checkout-submit').click(function() {   
    /*run the server side script to add the item */
    $('form#checkoutForm').ajaxSubmit({success: ShowValidCheckoutData, url: '_inc-checkout-validation.asp', dataType: "json"});

    return false;
  });

  function ShowValidCheckoutData(responseText, statusText) {
    var aOutput				  = responseText
    var sErrorText			= aOutput[0].Errors
    var message

    //what to show
    if ( sErrorText.length > 0 ) {
      message = '<strong>There were errors while trying to submit your availability form</strong><br />'
      message = message + sErrorText
      $("div.form-error").removeClass("hide").html(message)
    } else {
      $("div.form-error").addClass("hide").html("")

      // now we are all good, submit our form
      $('form#checkoutForm').submit()
    } 
  };

  // bind form using 'ajaxForm'
  $('.frm-submit').click(function() {
    /*run the server side script to add the item */
    $("form#bookingForm").ajaxSubmit({success: ShowValidData, url: '_inc-booking-validation.asp', dataType: "json"});

    return false
  });

  $('.updateInfo').change(function() {
    /*run the server side script to add the item */
    $('form#bookingForm').ajaxSubmit({success: updateFormData, method: "post", url: '_inc-booking-validation.asp', dataType: "json"});

    return false
  });

  function updateInfo() {
    /*run the server side script to add the item */
    $('form#bookingForm').ajaxSubmit({success: updateFormData, method: "post", url: '_inc-booking-validation.asp', dataType: "json"});

    return false
  }

  function ShowValidData(responseText, statusText) {
    var aOutput				  = responseText
    var sErrorText			= aOutput[0].Errors
    var message

    //what to show
    if ( sErrorText.length > 0 ) {
      message = '<strong>There were errors while trying to submit your availability form</strong><br />'
      message = message + sErrorText
      $("div.form-error").removeClass("hide").html(message)
    } else {
      $("div.form-error").addClass("hide").html("")

      // now we are all good, submit our form
      $('form#bookingForm').submit()
    } 
  };

  function updateFormData(responseText, statusText) {
    var aOutput				  = responseText
    var CheckinDate			= aOutput[0].CheckinDate
    var CheckoutDate		= aOutput[0].CheckoutDate
    var TotalCost			  = aOutput[0].TotalCost

    $("input[name=Checkout]").val(CheckoutDate)
    $("input[name=Checkin]").val(CheckinDate)

    if ( TotalCost.length > 0 ) {
      TotalCost = parseInt(TotalCost)
      $("input[name=TotalCost]").val('$' + TotalCost.toFixed(2))
    }
  }
});
