// 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) {
	if (which == null) {
		which = "cont";
	}
    var why = "";
	switch (which) {
		case 'cont':
			which = "contact";
			why += isEmpty(theForm.name.value, "Name");
			why += isEmpty(theForm.company.value, "Company");
			why += checkPhone(theForm.phone.value, "Phone");
			why += checkEmail(theForm.email.value, "Email");
			why += (theForm.services.selectedIndex == -1 ? "- You must select at least one service." : "");
			//hearabout not needed because there is no "select" option.
			//why += (theForm.hearabout.options[theForm.hearabout.selectedIndex].value == "other" && theForm.name.value.trim == "" ? "- Please specify how you heard about ARC." : "");
			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.
}