// trim functions come from http://www.somacon.com/p355.php and are compatible with JS 1.2+ //
String.prototype.trim  = function() { return this.replace(/^\s+|\s+$/g,""); }
String.prototype.ltrim = function() { return this.replace(/^\s+/,"");       }
String.prototype.rtrim = function() { return this.replace(/\s+$/,"");       }

// Source: http://developer.apple.com/internet/webcontent/validation.html
function checkEmail (strng, fieldname) {
  var error="";
  if (strng == "") {
    error = "- " + fieldname + " is a required field.\n";
  }
  var emailFilter=/^.+@.+\..{2,3}$/;
  if (!(emailFilter.test(strng))) { 
    error = "- Please enter a valid email address.\n";
  } else {
    //test email for illegal characters
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
    if (strng.match(illegalChars)) {
      error = "- The email address contains illegal characters.\n";
    }
  }
  return error;    
}

function checkPhone (strng, fieldname, isReq) {
  if (isReq == null) { isReq = true; }
  var error = "";
  if (strng == "" && isReq) {
    error = "- " + fieldname + " is a required field.\n";
  }
  var stripped = strng.replace(/[\(\)\.\+\/\-\ ]/g, ''); //strip out acceptable non-numeric characters
  if (isNaN(parseInt(stripped))) {
    error = "- The phone number contains illegal characters.\n";
  }
  if (!(stripped.length >= 10)) {
    error = "- Please make sure the phone number is at least 10 digits.\n";
  } 
  return error;
}

function isEmpty(strng, fieldname) {
  if (strng == null || strng == "") {
  	  strng = "";
  }
  if (fieldname=="") {
	  fieldname="Name"
  }
  var error = "";
  strng = strng.trim();
  if (strng.length == 0) {
    error = "- " + fieldname + " is a required field.\n";
  }
  return error;	  
}

// Modifed function, check whether the first option is selected and displays an error if so.
function isEmptyState(strng, fieldname) {
	var error = "";
	if ((strng.selectedIndex == 0) || strng.options[strng.selectedIndex].value == "ST") {
		error = "- " + fieldname + " is a required field. \n";
	}
	return error;
}

// Ziptype: 0 checks both us and canadian zips
// 			1 checks only us zips
// 			2 checks only ca zips
function checkZIP(strng, fieldname, ziptype){ 
	if (ziptype == null) {
		ziptype = 0;
	}
	var error = "";
	var stripped = strng.replace(/[\(\)\.\+\/\-\ a-z]/ig, ''); //strip out acceptable non-numeric characters

	var us_zip = /^[0-9]{5}(\-[0-9]{4})?$/i;
	var ca_zip = /^[a-z0-9]{3}[\ \-][a-z0-9]{3}$/i;

	if (strng == "") {
		error = "- " + fieldname + " is a required field.\n";
	} else {
		if (ziptest == 1 && !(us_zip.test(strng)) ) {
			error = "- " + fieldname + " is not a valid US Zip Code.\n";
		} else if (ziptest == 2 && !(ca_zip.test(strng)) ) {
			error = "- " + fieldname + " is not a valid Canadian Zip Code.\n";
		} else if (ziptest == 0 && !(us_zip.test(strng)) && !(ca_zip.test(strng))) {
			error = "- " + fieldname + " is not a valid US or Canadian Zip Code.\n";
		}
	}
	return error;
}

// This function will loop through aany number of arguments to determine if a checkbox is checked in the set.
// Each subsequent argument is the NAME of the checkbox to be checked.
function isNotChecked(theForm) {
	var isChecked = false;
	for(var i=1; i<arguments.length; i++) {
		if (theForm[arguments[i]].checked) {
			isChecked = true;
		}
	}
	return !isChecked;
}

// the which variable is added to that this function can be used to handle more than one form
function checkForm(theForm, which) {
    var why = "";
	switch (which) {
		case 'cont':
			which = "contact";
			why += (theForm.s_Source.options[theForm.s_Source.selectedIndex].value == "" ? "- How did you hear about Powerstation is a required field.\n" : "");
			why += (isNotChecked(theForm, 's_Service_1', 's_Service_2', 's_Service_3', 's_Service_4', 's_Service_5') ? "- Desired Services is a required field.\n" : "");
			why += (theForm.s_Event.options[theForm.s_Event.selectedIndex].value == "" ? "- Type of event is a required field.\n" : "");
			why += isEmpty(theForm.s_First_Name.value, "First Name");
			
			var phone = 0;
			if (isEmpty(theForm.s_Work.value, "") == "") { // if is not empty
				why += checkPhone(theForm.s_Work.value, "Work phone");
				phone++;
			}
			if (isEmpty(theForm.s_Home.value, "") == "") { // if is not empty
				why += checkPhone(theForm.s_Home.value, "Home phone");
				phone++;
			}
			if (isEmpty(theForm.s_Cell.value, "") == "") { // if is not empty
				why += checkPhone(theForm.s_Cell.value, "Cell phone");
				phone++;
			}
			if (phone == 0) {
				why += "- Phone is a required field.\n";
			}

			why += checkEmail(theForm.s_Email.value, "Email");
			why += isEmpty(theForm.security_code.value, "Security validation");
			break;
		case 'empl':
			which = "employment";
			why += isEmpty(theForm.s_First_Name.value, "First name");
			why += isEmpty(theForm.s_Last_Name.value, "Last name");
			
			var phone = 0;
			if (isEmpty(theForm.s_Day_Phone.value, "") == "") { // if is not empty
				why += checkPhone(theForm.s_Day_Phone.value, "Daytime phone");
				phone++;
			}
			if (isEmpty(theForm.s_Evening_Phone.value, "") == "") { // if is not empty
				why += checkPhone(theForm.s_Evening_Phone.value, "Evening phone");
				phone++;
			}
			if (phone == 0) {
				why += "- Phone is a required field.\n";
			}
			
			why += checkEmail(theForm.s_EMail.value, "Email");
			
			why += (isNotChecked(theForm, 's_Primary_Interest', 's_Primary_Interest2', 's_Primary_Interest3', 's_Primary_Interest4', 's_Primary_Interest5') ? "- Primary Interest is a Required field.\n" : "");
			
			why += isEmpty(theForm.security_code.value, "Security validation");
			break;
	}
    if (why != "") {  // If there's an error,
       alert("Your " + which + " form submission has the following errors:\n\n"+why);    // Tell the user whats wrong,
       return false;  // And halt submission.
    }
return true;          // Otherwise allow submission to proceed.
}