//
// 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 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 ajaxPostProjectRequestForm(frmId, sConfirmationDivID, sSubmitButtonID) {
  enableSubmitButton(sSubmitButtonID, false);
  var tmr = startTimeout(sSubmitButtonID);
  $(sConfirmationDivID).hide();

  //Form validation
  //Personal Info...
  if ($F('pr_organization') == '') { alert("Please input your organization name."); $('pr_organization').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('pr_first_name') == '') { alert("Please input your first name."); $('pr_first_name').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('pr_last_name') == '') { alert("Please input your last name."); $('pr_last_name').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('pr_phone') == '') { alert("Please input your phone number."); $('pr_phone').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  var sEmail = $F('pr_email');
  if (sEmail == '') { alert("Please input your email address."); $('pr_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."); $('pr_email').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('pr_zip') == '') { alert("Please input your zip code."); $('pr_zip').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('pr_date') == '') { alert("Please input the date of your event."); $('pr_date').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('pr_attendees') == '') { alert("Please input the expected attendance for your event."); $('pr_attendees').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('pr_budget') == '') { alert("Please input the budget for your event."); $('pr_budget').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('pr_project') == '') { alert("Please input the name of your project."); $('pr_project').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('pr_proj_description') == '') { alert("Please input some basic details about your event."); $('pr_proj_description').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }

  if ($F('pr_security_code') == '') { alert("Please input the Verification Code that's displayed on your screen."); $('pr_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(peProjReq) {
          $(frmId).reset();
          peProjReq.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 ajaxPostCaseStudyForm(frmId, sConfirmationDivID, sSubmitButtonID) {
  enableSubmitButton(sSubmitButtonID, false);
  var tmr = startTimeout(sSubmitButtonID);
  $(sConfirmationDivID).hide();

  //Form validation
  //Personal Info...
  if ($F('cs_organization') == '') { alert("Please input your organization name."); $('cs_organization').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('cs_first_name') == '') { alert("Please input your first name."); $('cs_first_name').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('cs_last_name') == '') { alert("Please input your last name."); $('cs_last_name').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('cs_date') == '') { alert("Please input the date of your event."); $('cs_date').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('cs_proj_solution') == '') { alert("Please input the solution that we provided."); $('cs_proj_solution').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('cs_proj_results') == '') { alert("Please describe the results of the project."); $('cs_proj_results').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('cs_proj_feedback') == '') { alert("Please input the feedback."); $('cs_proj_feedback').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }

  if ($F('cs_security_code') == '') { alert("Please input the Verification Code that's displayed on your screen."); $('cs_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(peCaseStudy) {
          $(frmId).reset();
          peCaseStudy.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(peStf) {
          $(frmId).reset();
          peStf.stop();
        }, 10);
      } else {
        alert(transport.responseText.trim()); //response contains error text - display it
      }
      enableSubmitButton(sSubmitButtonID, true);
    }
  });
}