// from http://developer.apple.com/internet/webcontent/validation.html
// with modifications

// returns an email list that may be separated by various delimiters (white space, comma, semicolon)
// separated by a single delimiter ($delim)
function normalize(strEmailList, delim ) {
	return strEmailList.replace(/[;\,\s]+/g, delim);
}

function checkEmailList (strEmailList ) {
var delim = ",";

	var errList = "";
	var err = "";
	// replace any delimiter with a delim
	strEmailList = normalize(strEmailList, delim);
	var astrEmail = strEmailList.split(delim);  // split on delim
	// loop thru email list, checking each one
	for(i = 0; i < astrEmail.length; i++){
		if (astrEmail[i] != "") {
			err = check1Email(astrEmail[i], 1, ""); 
			if ( err != "" ) errList += astrEmail[i] + delim;
		}
	}
	return errList;
}

// Checks two emails to see if they're valid and match each other
function checkEmails (email1, email2, whoseAddress ) {
	var err = check1Email( email1, 1, whoseAddress );
	err += check1Email( email2, 2, whoseAddress );
	if ( err=="" && ( email1.toLowerCase() != email2.toLowerCase()) ) {
		err = toUpperFirst(whoseAddress) + " email addresses do not match.\n";
	}
	return err;
}

// validates one email address
function check1Email (strng, number, whoseAddress) {
var error="";
var strEmailAddr = whoseAddress + " email address";
var invalidMsg = "Please correct " + strEmailAddr + (number==1 ? "" : " (re-typed)");

	var emailFilter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if ( strng == "" ) {
		error = "Please " + (number==1 ? "enter " : "re-type ") + strEmailAddr + ".\n";
	} else if (!(emailFilter.test(strng))) { 
		error =  invalidMsg + ".\n";
	} else {
		//test email for illegal characters
		var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
		if (strng.match(illegalChars)) {
			error =  invalidMsg + ".\n";
		}
	}
	return error;    
}

// phone number - strip out delimiters and check for 10 digits numeric, > 10 digits alphanumeric
function checkPhone (strng, country) {

var error = "";
	if ((country == "") || (country.toUpperCase()=="USA")) {
		var intMinLength = 10;
		var strMinLengthMsg = "Please enter 10 digit phone number.\n";
	} else {
		var intMinLength = 7;
		var strMinLengthMsg = "Please enter complete phone number.\n";
	}

	var illegalChars = /\W/; // allow letters, numbers, and underscores
	if ( strng=="" ) {
		error = "Please enter a phone number.\n";
	} else {
		var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
		if (stripped.length < intMinLength) {
			error = strMinLengthMsg;
		} else if (isNaN(stripped.substring(0,intMinLength))) {
			error = "Invalid characters in phone number.\n";
		} else if (illegalChars.test(stripped.substring(intMinLength))) {
			error = "Invalid characters in phone number.\n";
		} 
	}
	return error;
}

// Checks text string used for names
function checkText (strng, fieldName) {
//var illegalChars = /\W/; // allow letters, numbers, and underscores
var illegalChars = /[^a-zA-Z\.\- ,_']/g; // allow letters, space, period, comma,hypen,underscore,apostrophe

	var error = "";
	if (strng == "") {
		error = "Please enter " + fieldName + ".\n";
	} else if (illegalChars.test(strng)) {
		error = toUpperFirst(fieldName) + " contains illegal characters.\n";
	} 
	return error;
}

// capitalizes first letter of string only
function toUpperFirst( strng ) {
	return strng.substr(0,1).toUpperCase()+strng.substr(1);
}