//
// This is used by all of the ajaxPostABC functions below.
// It re-enables the submit button and removes the spinner class and puts back the normal btn_submit class.
//
// NOTE: This function requires the Prototype.js library
//
function enableSubmitButton(button_id, bEnabled) {
  if (bEnabled == true) {
    $(button_id).removeClassName('btn_submit_spinner').addClassName('btn_submit').enable();
  } else {
    $(button_id).removeClassName('btn_submit').addClassName('btn_submit_spinner').disable();
  }
  return true;
}

//
// This function is used to create a timer for a form that was submitted.
// If the timer fires then we know we've exceeded the maximum allowed time for the
// server to process the request. IE, we never got a response back from the server
// so we need to re-enable the submit button and tell the user so they can submit
// the request again if they want to.
//
function startTimeout(sSubmitButtonID) {
  var timer = new PeriodicalExecuter(function(peObject) {
    enableSubmitButton(sSubmitButtonID, true);
    peObject.stop();
  }, 30);
  return timer;
}

//
// This is the javascript validation for the eNewsletter Subscription form (includes/panels/switch_box.php).
// If all of the validation passes then the form is posted using AJAX (not the normal form POST). The response
// is then processed and we either show a <DIV> that has the confirmation message or we display a javascript
// alert popup with the error message returned from the PHP script.
//
function ajaxPostEnewsForm(frmId, sConfirmationDivID, sSubmitButtonID) {
  enableSubmitButton(sSubmitButtonID, false);
  var tmr = startTimeout(sSubmitButtonID);
  $(sConfirmationDivID).hide();

  //Form validation
  if ($F('enews_first_name') == '') { alert("Please input your first name."); $('enews_first_name').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('enews_last_name') == '') { alert("Please input your last name."); $('enews_last_name').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  var sEmail = $F('enews_email');
  if (sEmail == '') { alert("Please input your email address."); $('enews_email').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if (!sEmail.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { alert("Invalid email address."); $('enews_email').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('enews_zip') == '') { alert("Please input your zip code."); $('enews_zip').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('enews_country') == '') { alert("Please input your country."); $('enews_country').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }

  if ($F('enews_security_code') == '') { alert("Please input the Verification Code that's displayed on your screen."); $('enews_security_code').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }

  //Submit the form via Prototype's form "request" ajax method
  $(frmId).request ({
    onSuccess: function(transport) {
      tmr.stop(); //Since we got a response we can stop the timeout timer
      if (transport.responseText.trim() == '1') {
        Effect.Appear(sConfirmationDivID, {duration:.5});
        new PeriodicalExecuter(function(peEnews) {
          $(frmId).reset();
          peEnews.stop();
        }, 10);
      } else {
        alert(transport.responseText.trim()); //response contains error text - display it
      }
      enableSubmitButton(sSubmitButtonID, true);
    }
  });
}

//
// This is the javascript validation for the Contact form (contact.php). If all of the
// validation passes then the form is posted using AJAX (not the normal form POST). The response
// is then processed and we either show a <DIV> that has the confirmation message or we display
// a javascript alert popup with the error message returned from the PHP script.
//
function ajaxPostContactForm(frmId, sConfirmationDivID, sSubmitButtonID) {
  enableSubmitButton(sSubmitButtonID, false);
  var tmr = startTimeout(sSubmitButtonID);
  $(sConfirmationDivID).hide();

  //Form validation
  if ($F('cont_first_name') == '') { alert("Please input your first name."); $('cont_first_name').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('cont_last_name') == '') { alert("Please input your last name."); $('cont_last_name').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('cont_address_1') == '') { alert("Please input your address."); $('cont_address_1').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('cont_city') == '') { alert("Please input your city."); $('cont_city').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('cont_state') == '') { alert("Please input your state."); $('cont_state').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('cont_zip') == '') { alert("Please input your zip code."); $('cont_zip').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('cont_country') == '') { alert("Please input your country."); $('cont_country').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('cont_phone') == '') { alert("Please input your phone number."); $('cont_phone').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  var sEmail = $F('cont_email');
  if (sEmail == '') { alert("Please input your email address."); $('cont_email').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if (!sEmail.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { alert("Invalid email address."); $('cont_email').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('cont_comments') == '') { alert("Please input your comments."); $('cont_comments').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }

  if ($F('cont_security_code') == '') { alert("Please input the Verification Code that's displayed on your screen."); $('cont_security_code').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }

  $(frmId).request( {
    onSuccess: function(transport) {
      tmr.stop(); //Since we got a response we can stop the timeout timer
      if (transport.responseText.trim() == '1') {
        Effect.Appear(sConfirmationDivID, {duration:.5});
        new PeriodicalExecuter(function(peContact) {
          $(frmId).reset();
          peContact.stop();
        }, 10);
      } else {
        alert(transport.responseText.trim()); //response contains error text - display it
      }
      enableSubmitButton(sSubmitButtonID, true);
    }
  });
}

//
// This is the javascript validation for the Send to Friend form. If all of the
// validation passes then the form is posted using AJAX (not the normal form POST). The response
// is then processed and we either show a <DIV> that has the confirmation message or we display
// a javascript alert popup with the error message returned from the PHP script.
//
function ajaxPostStfForm(frmId, sConfirmationDivID, sSubmitButtonID) {
  enableSubmitButton(sSubmitButtonID, false);
  var tmr = startTimeout(sSubmitButtonID);
  $(sConfirmationDivID).hide();

  //Form validation
  if ($F('stf_name') == '') { alert("Please input your name."); $('stf_name').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  var sEmail = $F('stf_email');
  if (sEmail == '') { alert("Please input your email address."); $('stf_email').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if (!sEmail.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { alert("Invalid email address."); $('stf_email').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }

  if ($F('stf_friend_name_1') == '') { alert("Please input at your friend's name."); $('stf_friend_name_1').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  var sFriendEmail1 = $F('stf_friend_email_1');
  if (sFriendEmail1 == '') { alert("Please input your friend's email address."); $('stf_friend_email_1').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if (!sFriendEmail1.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { alert("Invalid email address."); $('stf_friend_email_1').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }

  if ($F('stf_security_code') == '') { alert("Please input the Verification Code that's displayed on your screen."); $('stf_security_code').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }

  //Submit the form via Prototype's form "request" ajax method
  $(frmId).request ({
    onSuccess: function(transport) {
      tmr.stop(); //Since we got a response we can stop the timeout timer
      if (transport.responseText.trim() == '1') {
        Effect.Appear(sConfirmationDivID, {duration:.5});
        new PeriodicalExecuter(function(peEnews) {
          $(frmId).reset();
          peEnews.stop();
        }, 10);
      } else {
        alert(transport.responseText.trim()); //response contains error text - display it
      }
      enableSubmitButton(sSubmitButtonID, true);
    }
  });
}

//
// This is the javascript validation for the Donation form. If all of the validation passes
// then the form is posted using AJAX (not the normal form POST). The response is then processed
// and we either show a <DIV> that has the confirmation message or we display a javascript alert
// popup with the error message returned from the PHP script.
//
function ajaxPostDonateForm(frmId, sConfirmationDivID, sSubmitButtonID) {
  enableSubmitButton(sSubmitButtonID, false);
  var tmr = startTimeout(sSubmitButtonID);
  $(sConfirmationDivID).hide();

  //Form validation
  if ($F('don_first_name') == '') { alert("Please input your first name."); $('don_first_name').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('don_last_name') == '') { alert("Please input your last name."); $('don_last_name').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('don_address_1') == '') { alert("Please input your address."); $('don_address_1').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('don_city') == '') { alert("Please input your city."); $('don_city').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('don_state') == '') { alert("Please input your state."); $('don_state').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('don_zip') == '') { alert("Please input your zip code."); $('don_zip').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('don_country') == '') { alert("Please input your country."); $('don_country').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('don_phone') == '') { alert("Please input your phone number."); $('don_phone').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  var sEmail = $F('don_email');
  if (sEmail == '') { alert("Please input your email address."); $('don_email').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if (!sEmail.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { alert("Invalid email address."); $('don_email').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }

  //Amount and giving frequency
  if ($F('don_amount') == '') { alert("Please input the amount you'd like to donate."); $('don_amount').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  var amount = $F('don_amount');
  amount = parseInt(amount);
  if (amount <= 0) { alert("Please input an amount."); $('don_amount').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }

  //Credit card information
  if ($F('don_type_of_card') == '') { alert("Please input the type of credit card you'd like to use."); $('don_type_of_card').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('don_card_name') == '') { alert("Please input the name displayed on the credit card."); $('don_card_name').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('don_card_number') == '') { alert("Please input the credit card number you'd like to use."); $('don_card_number').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('don_exp_month') == '') { alert("Please input the credit card expiration month."); $('don_exp_month').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('don_exp_year') == '') { alert("Please input the credit card expiration year."); $('don_exp_year').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }

  if ($F('don_security_code') == '') { alert("Please input the Verification Code that's displayed on your screen."); $('don_security_code').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }

  $(frmId).request( {
    onSuccess: function(transport) {
      tmr.stop(); //Since we got a response we can stop the timeout timer
      if (transport.responseText.trim() == '1') {
        Effect.Appear(sConfirmationDivID, {duration:.5});
        new PeriodicalExecuter(function(peContact) {
//          $(frmId).reset();
          peContact.stop();
        }, 10);
      } else {
        alert(transport.responseText.trim()); //response contains error text - display it
      }
      enableSubmitButton(sSubmitButtonID, true);
    }
  });
}