// no conflicts with other frameworks
var $j = jQuery.noConflict();

//
// window.onload function to setup various javascripts
//


/*****************************************************************************************
 *
 *                           General helper functions
 *
 *****************************************************************************************/

/**
 * Extend the String object so that we can use the trim() function like; sVar.trim();
 *
 *     var first_name = "Chris ";
 *     first_name = first_name.trim();
 *
 * @return string
 */
String.prototype.trim = function() {
  var a = this.replace(/^\s+/, '');
  return a.replace(/\s+$/, '');
};

/**
 * Write an email address to the screen. Helps so that bots can't parse email addresses from the site.
 *
 *     writeEmail("info", "info", "churchmedia.cc");
 *
 * @param string
 * @param string
 * @param string
 */
function writeEmail(contact, email, emailHost) {
  document.write("<a href=" + "&#109a&#105l" + "&#116&#111:" + email + "@" + emailHost+ ">" + contact + "@" + emailHost+"</a>");
}

/**
 * Pop up window in a certain size and no scrollbar.
 */
function pop(url, name, props) {
	window.open(url, name, props);
}




/*****************************************************************************************
 *
 *                    Functions for forms - highlight error fields
 *
 *****************************************************************************************/
function removeErrorClass(control_id) {
  $j('#'+control_id).parent().parent().removeClass('error');
  return true;
}
function addError(control_id) {
  $j('#'+control_id).select().focus();
  $j('#'+control_id).parent().parent().addClass('error');
  return true;
}




/*****************************************************************************************
 *
 *                             Functions for form validation
 *
 *****************************************************************************************/

//
// 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 validate_contact_us_form() {
  //Form validation
  var bError = false;
  if ($j('#cont_first_name').val() == '') { addError('cont_first_name'); bError = true; } else { removeErrorClass('cont_first_name'); }
  if ($j('#cont_last_name').val() == '') { addError('cont_last_name'); bError = true; } else { removeErrorClass('cont_last_name'); }

  var sEmail = $j('#cont_email').val();
  if (sEmail == '' || !sEmail.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { addError('cont_email'); bError = true; } else { removeErrorClass('cont_email'); }

  if ($j('#cont_comments').val() == '') { addError('cont_comments'); bError = true; } else { removeErrorClass('cont_comments'); }
  if ($j('#cont_security_code').val() == '') { addError('cont_security_code'); bError = true; } else { removeErrorClass('cont_security_code'); }

  if (bError == true) {
    alert ("Please correct the highlighted fields.");
    return false;
  }
  else {
    return 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 validate_lead_a_group_form() {
  //Form validation
  var bError = false;
  if ($j('#first_name').val() == '') { addError('first_name'); bError = true; } else { removeErrorClass('first_name'); }
  if ($j('#last_name').val() == '') { addError('last_name'); bError = true; } else { removeErrorClass('last_name'); }
  if ($j('#address_1').val() == '') { addError('address_1'); bError = true; } else { removeErrorClass('address_1'); }
  if ($j('#city').val() == '') { addError('city'); bError = true; } else { removeErrorClass('city'); }
  if ($j('#state').val() == '') { addError('state'); bError = true; } else { removeErrorClass('state'); }
  if ($j('#zip').val() == '') { addError('zip'); bError = true; } else { removeErrorClass('zip'); }
  if ($j('#country').val() == '') { addError('country'); bError = true; } else { removeErrorClass('country'); }
  if ($j('#phone').val() == '') { addError('phone'); bError = true; } else { removeErrorClass('phone'); }
  if ($j('#group_type').val() == '') { addError('group_type'); bError = true; } else { removeErrorClass('group_type'); }

  var sEmail = $j('#email').val();
  if (sEmail == '' || !sEmail.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { addError('email'); bError = true; } else { removeErrorClass('email'); }

//  if ($j('#comments').val() == '') { addError('comments'); bError = true; } else { removeErrorClass('comments'); }
  if ($j('#lead_security_code').val() == '') { addError('lead_security_code'); bError = true; } else { removeErrorClass('lead_security_code'); }

  if (bError == true) {
    alert ("Please correct the highlighted fields.");
    return false;
  }
  else {
    return true;
  }
}

//
// This is the javascript validation for the Register for Group 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 validate_register_group_form() {
  //Form validation
  var bError = false;
  if ($j('#group_id').val() == '') { addError('group_id'); bError = true; } else { removeErrorClass('group_id'); }
  if ($j('#first_name').val() == '') { addError('first_name'); bError = true; } else { removeErrorClass('first_name'); }
  if ($j('#last_name').val() == '') { addError('last_name'); bError = true; } else { removeErrorClass('last_name'); }
  if ($j('#address_1').val() == '') { addError('address_1'); bError = true; } else { removeErrorClass('address_1'); }
  if ($j('#city').val() == '') { addError('city'); bError = true; } else { removeErrorClass('city'); }
  if ($j('#state').val() == '') { addError('state'); bError = true; } else { removeErrorClass('state'); }
  if ($j('#zip').val() == '') { addError('zip'); bError = true; } else { removeErrorClass('zip'); }
  if ($j('#country').val() == '') { addError('country'); bError = true; } else { removeErrorClass('country'); }
  if ($j('#phone').val() == '') { addError('phone'); bError = true; } else { removeErrorClass('phone'); }

  var sEmail = $j('#email').val();
  if (sEmail == '' || !sEmail.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { addError('email'); bError = true; } else { removeErrorClass('email'); }

//  if ($j('#comments').val() == '') { addError('comments'); bError = true; } else { removeErrorClass('comments'); }
  if ($j('#register_group_security_code').val() == '') { addError('register_group_security_code'); bError = true; } else { removeErrorClass('register_group_security_code'); }

  if (bError == true) {
    alert ("Please correct the highlighted fields.");
    return false;
  }
  else {
    return 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 validate_enewsletter_form() {
  //Form validation
  var bError = false;
  if ($j('#enews_first_name').val() == '') { addError('enews_first_name'); bError = true; } else { removeErrorClass('enews_first_name'); }
  if ($j('#enews_last_name').val() == '') { addError('enews_last_name'); bError = true; } else { removeErrorClass('enews_last_name'); }
  if ($j('#enews_zip').val() == '') { addError('enews_zip'); bError = true; } else { removeErrorClass('enews_zip'); }
  if ($j('#enews_country').val() == '') { addError('enews_country'); bError = true; } else { removeErrorClass('enews_country'); }

  var sEmail = $j('#enews_email').val();
  if (sEmail == '' || !sEmail.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { addError('enews_email'); bError = true; } else { removeErrorClass('enews_email'); }

  if ($j('#enews_security_code').val() == '') { addError('enews_security_code'); bError = true; } else { removeErrorClass('enews_security_code'); }

  if (bError == true) {
    alert ("Please correct the highlighted fields.");
    return false;
  }
  else {
    return 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 validate_enews_form() {
  //Form validation
  var bError = false;
  if ($j('#enews_name').val() == '') { addError('enews_name'); bError = true; } else { removeErrorClass('enews_name'); }
  var sEmail = $j('#enews_email').val();
  if (sEmail == '' || !sEmail.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { addError('enews_email'); bError = true; } else { removeErrorClass('enews_email'); }

  if (bError == true) {
    alert ("Please enter a Name and Valid Email Address.");
    return false;
  }
  else {
    return 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 validate_send_to_friend_form() {
  //Form validation
  var bError = false;
  if ($j('#stf_name').val() == '') { addError('stf_name'); bError = true; } else { removeErrorClass('stf_name'); }
  var sEmail = $j('#stf_email').val();
  if (sEmail == '' || !sEmail.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { addError('stf_email'); bError = true; } else { removeErrorClass('stf_email'); }
  if ($j('#stf_friend_name_1').val() == '') { addError('stf_friend_name_1'); bError = true; } else { removeErrorClass('stf_friend_name_1'); }
  sEmail = $j('#stf_friend_email_1').val();
  if (sEmail == '' || !sEmail.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { addError('stf_friend_email_1'); bError = true; } else { removeErrorClass('stf_friend_email_1'); }
  if ($j('#stf_friend_name_2').val() !== "") {
    sEmail = $j('#stf_friend_email_2').val();
    if (sEmail == '' || !sEmail.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) { addError('stf_friend_email_2'); bError = true; } else { removeErrorClass('stf_friend_email_2'); }
  }

  if ($j('#stf_security_code').val() == '') { addError('stf_security_code'); bError = true; } else { removeErrorClass('stf_security_code'); }

  if (bError == true) {
    alert ("Please correct the highlighted fields.");
    return false;
  }
  else {
    return true;
  }
}

