//
// 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 Become a Partner form (become_a_partner.tpl)
// If all of the validation passes then the form is posted using AJAX (not the normal form POST).
// The reponse 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 ajaxPostBecomePartnerForm(frmId, sConfirmationDivID, sSubmitButtonID) {
  enableSubmitButton(sSubmitButtonID, false);
  var tmr = startTimeout(sSubmitButtonID);
  $(sConfirmationDivID).hide();

  //Form validation
  if ($F('bap_fname') == '') { alert("Please input your first name."); $('bap_fname').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('bap_lname') == '') { alert("Please input your last name."); $('bap_lname').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('bap_addr1') == '') { alert("Please input your street address."); $('bap_addr1').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($('bap_country').selectedIndex == 0) { alert("Please input your country."); $('bap_country').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('bap_city') == '') { alert("Please input your city."); $('bap_city').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('bap_state') == '') { alert("Please input your state."); $('bap_state').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('bap_zip') == '') { alert("Please input your zip code."); $('bap_zip').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('bap_phone') == '') { alert("Please input your phone number."); $('bap_phone').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  var sEmail = $F('bap_email');
  if (sEmail == '') { alert("Please input your email address."); $('bap_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."); $('bap_email').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('bap_security_code') == '') { alert("Please input the Verification Code that's displayed on your screen."); $('bap_security_code').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }

  new Ajax.Request('index.php', {
    parameters: $(frmId).serialize(true),
    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(pePartner) {
          $(frmId).reset();
//          Effect.Fade(sConfirmationDivID, {duration:.5});
//          pePartner.stop();
//        }, 10);
      } else {
        alert(transport.responseText.trim()); //response contains error text - display it
      }
      enableSubmitButton(sSubmitButtonID, true);
    }
  });
}

//
// This is the javascript validation for the Tech Support form (technical_support.tpl)
// If all of the validation passes then the form is posted using AJAX (not the normal form POST).
// The reponse 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 ajaxPostTechSupportForm(frmId, sConfirmationDivID, sSubmitButtonID) {
  enableSubmitButton(sSubmitButtonID, false);
  var tmr = startTimeout(sSubmitButtonID);
  $(sConfirmationDivID).hide();

  //Form validation
  if ($F('ts_fname') == '') { alert("Please input your first name."); $('ts_fname').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('ts_lname') == '') { alert("Please input your last name."); $('ts_lname').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  var sEmail = $F('ts_email');
  if (sEmail == '') { alert("Please input your email address."); $('ts_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."); $('ts_email').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('ts_email2') == '') { alert("Please confirm your email address."); $('ts_email2').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('ts_email') != $F('ts_email2')) { alert("The email addresses didn't match."); $('ts_email').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($('ts_country').selectedIndex == 0) { alert("Please select your country."); $('ts_country').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('ts_zip') == '') { alert("Please input your zip code."); $('ts_zip').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($('ts_media_player').selectedIndex == 0) { alert("Please select your media player."); $('ts_media_player').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('ts_isp') == '') { alert("Please input your internet service provider."); $('ts_isp').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($('ts_connection').selectedIndex == 0) { alert("Please select your connection type."); $('ts_connection').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('ts_time') == '') { alert("Please input the time you attempted to watch the broadcast."); $('ts_time').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($('ts_videomotion').selectedIndex == 0) { alert("Please select the quality of the video."); $('ts_videomotion').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($('ts_videopicture').selectedIndex == 0) { alert("Please select the quality of the picture."); $('ts_videopicture').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($('ts_audiocontinuity').selectedIndex == 0) { alert("Please select the quality of the audio stream."); $('ts_audiocontinuity').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($('ts_audiofidelity').selectedIndex == 0) { alert("Please select the quality of the audio."); $('ts_audiofidelity').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('ts_url') == '') { alert("Please input the web page address where you are experiencing the problem."); $('ts_url').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('ts_description') == '') { alert("Please input a description of the problem you're experiencing."); $('ts_description').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('ts_security_code') == '') { alert("Please input the Verification Code that's displayed on your screen."); $('ts_security_code').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }

  new Ajax.Request('index.php', {
    parameters: $(frmId).serialize(true),
    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(peTech) {
          $(frmId).reset(); //TODO - test: 'frmTechSupport'
//          Effect.Fade(sConfirmationDivID, {duration:.5}); //'divConfirmationTechSupport'
//          peTech.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 (send_to_friend.tpl)
// If all of the validation passes then the form is posted using AJAX (not the normal form POST).
// The reponse 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 ajaxPostSendToFriendForm(frmId, sConfirmationDivID, sSubmitButtonID) {
  enableSubmitButton(sSubmitButtonID, false);
  var tmr = startTimeout(sSubmitButtonID);
  $(sConfirmationDivID).hide();

  //Form validation
  if ($F('stf_my_name') == '') { alert("Please input your name."); $('stf_my_name').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  var sEmail = $F('stf_my_email');
  if (sEmail == '') { alert("Please input your email address."); $('stf_my_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_my_email').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('stf_friend1') == '') { alert("Please input at your friend's name."); $('stf_friend1').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  var sFriendEmail1 = $F('stf_email1');
  if (sFriendEmail1 == '') { alert("Please input your friend's email address."); $('stf_email1').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_email1').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; }

  new Ajax.Request('index.php', {
    parameters: $(frmId).serialize(true),
    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(pe) {
          $(frmId).reset();
//          $(sConfirmationDivID).hide();
//          Effect.Fade('divSendToFriend', {duration:.5});
//          pe.stop();
//        }, 10);
      } else {
        alert(transport.responseText.trim()); //response contains error text - display it
      }
      enableSubmitButton(sSubmitButtonID, true);
    }
  });
}

//
// This is the javascript validation for the Partnership Information form (partnership_introduction.tpl)
// If all of the validation passes then the form is posted using AJAX (not the normal form POST).
// The reponse 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 ajaxPostPartnerInfoForm(frmId, sConfirmationDivID, sSubmitButtonID) {
  enableSubmitButton(sSubmitButtonID, false);
  var tmr = startTimeout(sSubmitButtonID);
  $(sConfirmationDivID).hide();

  //Form validation
  if ($F('pir_fname') == '') { alert("Please input your first name."); $('pir_fname').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('pir_lname') == '') { alert("Please input your last name."); $('pir_lname').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('pir_addr1') == '') { alert("Please input your street address."); $('pir_addr1').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($('pir_country').selectedIndex == 0) { alert("Please input your country."); $('pir_country').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('pir_city') == '') { alert("Please input your city."); $('pir_city').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('pir_state') == '') { alert("Please input your state."); $('pir_state').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('pir_zip') == '') { alert("Please input your zip code."); $('pir_zip').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('pir_phone') == '') { alert("Please input your phone number."); $('pir_phone').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  var sEmail = $F('pir_email');
  if (sEmail == '') { alert("Please input your email address."); $('pir_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."); $('pir_email').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('pir_security_code') == '') { alert("Please input the Verification Code that's displayed on your screen."); $('pir_security_code').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }

  new Ajax.Request('index.php', {
    parameters: $(frmId).serialize(true),
    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(pePartnerInfo) {
          $(frmId).reset();
//          Effect.Fade(sConfirmationDivID, {duration:.5});
//          pePartnerInfo.stop();
//        }, 10);
      } else {
        alert(transport.responseText.trim()); //response contains error text - display it
      }
      enableSubmitButton(sSubmitButtonID, true);
    }
  });
}

//
// This is the javascript validation for the Webmaster Contact form (webmaster_form.tpl)
// If all of the validation passes then the form is posted using AJAX (not the normal form POST).
// The reponse 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 ajaxPostWebmasterForm(frmId, sConfirmationDivID, sSubmitButtonID) {
  enableSubmitButton(sSubmitButtonID, false);
  var tmr = startTimeout(sSubmitButtonID);
  $(sConfirmationDivID).hide();

  //Form validation
  if ($F('wm_url') == '') { alert("Please input the problem URL."); $('wm_url').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($('wm_support').selectedIndex == 0) { alert("Please select a support area."); $('wm_support').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($('wm_browser').selectedIndex == 0) { alert("Please select which broswer you're using."); $('wm_browser').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($('wm_os').selectedIndex == 0) { alert("Please select which operating system you're using."); $('wm_os').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($('wm_subject').selectedIndex == 0) { alert("Please select the type of problem that you're experiencing."); $('wm_subject').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('wm_description') == '') { alert("Please describe the problem you're experiencing."); $('wm_description').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }
  if ($F('wm_security_code') == '') { alert("Please input the Verification Code that's displayed on your screen."); $('wm_security_code').activate(); enableSubmitButton(sSubmitButtonID, true); tmr.stop(); return false; }

  new Ajax.Request('index.php', {
    parameters: $(frmId).serialize(true),
    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(peWeb) {
          $(frmId).reset();
//          Effect.Fade(sConfirmationDivID, {duration:.5});
//          peWeb.stop();
//        }, 10);
      } else {
        alert(transport.responseText.trim()); //response contains error text - display it
      }
      enableSubmitButton(sSubmitButtonID, true);
    }
  });
}

//
// This is the javascript validation for the Prayer Request form (prayer_request.tpl)
// If all of the validation passes then the form is posted using AJAX (not the normal form POST).
// The reponse 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 ajaxPostPrayerRequestForm(frmId, sConfirmationDivID, sSubmitButtonID) {
  enableSubmitButton(sSubmitButtonID, false);
  var tmr = startTimeout(sSubmitButtonID);
  $(sConfirmationDivID).hide();

  //Form validation
  if ($F('prayer_details') == '') { alert("Please input your prayer request."); $('prayer_details').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; }

  new Ajax.Request('index.php', {
    parameters: $(frmId).serialize(true),
    onSuccess: function(transport) {
      tmr.stop(); //Since we got a response we can stop the timeout timer
      var sResponse = transport.responseText.trim();
      if (sResponse === "1") {
        Effect.Appear(sConfirmationDivID, {duration:.5});
//        new PeriodicalExecuter(function(pePrayer) {
          $(frmId).reset();
//          $(sConfirmationDivID).hide();
//          Effect.Fade('divPrayerRequest', {duration:.5});
//          pePrayer.stop();
//        }, 10);
      } else {
//        $('divPrayerFormConfirmation').show();
//        $('divPrayerFormConfirmation').innerHTML = "<p>" + sResponse + "</p>";
        alert(sResponse); //response contains error text - display it
      }
      enableSubmitButton(sSubmitButtonID, true);
    }
  });
}

function ajaxEventRegSearch(frmId, sConfirmationDivID, sSubmitButtonID){
  enableSubmitButton(sSubmitButtonID, false);
  var tmr = startTimeout(sSubmitButtonID);
  $(sConfirmationDivID).hide();		
  new Ajax.Request('index.php', {
    parameters: $(frmId).serialize(true),
    onSuccess: function(transport) {
      tmr.stop(); //Since we got a response we can stop the timeout timer
      var sResponse = transport.responseText.evalJSON();
	  if(sResponse.status == 1){
		Effect.Appear(sConfirmationDivID, {duration:.5});
		$(frmId).reset();
		document.getElementById("search").innerHTML = sResponse.records;
	  }
	  else {
	      Effect.Appear(sConfirmationDivID, {duration:.5});
		  document.getElementById(sConfirmationDivID).innerHTML = sResponse.errmsg;
	  }
	  enableSubmitButton(sSubmitButtonID, true);
	}
  });
}

function ajaxEventAttendance(frmId, sConfirmationDivID, sSubmitButtonID){
  enableSubmitButton(sSubmitButtonID, false);
  var tmr = startTimeout(sSubmitButtonID);
  $(sConfirmationDivID).hide();	
  new Ajax.Request('index.php', {
    parameters: $(frmId).serialize(true),
    onSuccess: function(transport) {
      tmr.stop(); //Since we got a response we can stop the timeout timer
      var sResponse = transport.responseText.evalJSON();
      if (sResponse.status == 1) {
		document.getElementById(sConfirmationDivID).innerHTML = sResponse.message;
        Effect.Appear(sConfirmationDivID, {duration:.5});
//        new PeriodicalExecuter(function(pePrayer) {
          $(frmId).reset();
//          $(sConfirmationDivID).hide();
//          Effect.Fade('divPrayerRequest', {duration:.5});
//          pePrayer.stop();
//        }, 10);
      } else {
//        $('divPrayerFormConfirmation').show();
//        $('divPrayerFormConfirmation').innerHTML = "<p>" + sResponse + "</p>";
        alert(sResponse.message); //response contains error text - display it
      }
      enableSubmitButton(sSubmitButtonID, true);
    }
  });
}

function ajaxEventSetAttendance(frmId, sConfirmationDivID, sSubmitButtonID){
  enableSubmitButton(sSubmitButtonID, false);
  var tmr = startTimeout(sSubmitButtonID);
  $(sConfirmationDivID).hide();	
  new Ajax.Request('index.php', {
    parameters: $(frmId).serialize(true),
    onSuccess: function(transport) {
      tmr.stop(); //Since we got a response we can stop the timeout timer
      var sResponse = transport.responseText;
      if (sResponse == 1) {
        Effect.Appear(sConfirmationDivID, {duration:.5});
//        new PeriodicalExecuter(function(pePrayer) {
          $(frmId).reset();
//          $(sConfirmationDivID).hide();
//          Effect.Fade('divPrayerRequest', {duration:.5});
//          pePrayer.stop();
//        }, 10);
      } else {
//        $('divPrayerFormConfirmation').show();
//        $('divPrayerFormConfirmation').innerHTML = "<p>" + sResponse + "</p>";
        alert("Test"); //response contains error text - display it
      }
      enableSubmitButton(sSubmitButtonID, true);
    }
  });
}

function ajaxPreregScan(frmId, sConfirmationDivID, sSubmitButtonID){
  enableSubmitButton(sSubmitButtonID, false);
  var tmr = startTimeout(sSubmitButtonID);
  $(sConfirmationDivID).hide();	
  new Ajax.Request('index.php', {
    parameters: $(frmId).serialize(true),
    onSuccess: function(transport) {
      tmr.stop(); //Since we got a response we can stop the timeout timer
	  var sResponse = transport.responseText;
      if (sResponse == 1) {
        Effect.Appear(sConfirmationDivID, {duration:.5});
//        new PeriodicalExecuter(function(pePrayer) {
          $(frmId).reset();
//          $(sConfirmationDivID).hide();
//          Effect.Fade('divPrayerRequest', {duration:.5});
//          pePrayer.stop();
//        }, 10);
      } else {
//        $('divPrayerFormConfirmation').show();
//        $('divPrayerFormConfirmation').innerHTML = "<p>" + sResponse + "</p>";
        alert(sResponse); //response contains error text - display it
      }
      enableSubmitButton(sSubmitButtonID, true);
    }
  });
}