// 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
 *
 *****************************************************************************************/

// Validate Enewsletter Form
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'); }
  
  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;
    $j('#enews_confirmation').toggle();
  }
}


