// CONTAINS global JS functions for use in validating form fields

// validate an email address
function check_email(theEmail,fieldName,manditoryField) {
	var ckE = theEmail.value;
	var ck2 = ckE;
	var atPos  = ckE.indexOf("@");
	var dotPos = ckE.indexOf(".");
	var lastDotPos = ckE.lastIndexOf(".");
	var lastAtPos  = ckE.lastIndexOf("@");
		ckE = ckE.replace(/[^a-zA-Z0-9_@.\-]/g,"");
		ck2 = ck2.replace(/[a-zA-Z0-9_@.\-]/g,"");
		if (ck2 != "") {
			theEmail.focus();
			return "Your email address in the " + fieldName + " field contains invalid characters.\n";
		}
		if (manditoryField || ckE.length > 1) {
			if (ckE.length == 0) {
				theEmail.focus();
				return "You need to enter the email address in the " + fieldName + " field.\n";
			}
			if (atPos >= 1 && dotPos >= 1 && lastDotPos > atPos+2 && atPos == lastAtPos && ckE.length > lastDotPos+2) {
				// ok
			} else {
				theEmail.focus();
				return "1 -- You must enter a valid email address in the " + fieldName + " field.\n";
			}
			// check email addys like b.smith@cw.us
			if (dotPos != lastDotPos && dotPos > atPos-2 && dotPos < atPos) {
				theEmail.focus();
				return "2 == You must enter a valid email address in the " + fieldName + " field.\n";
			}
			// check email addys like bsmith@cw.gw.us
			if (dotPos != lastDotPos && dotPos > lastDotPos-3 && dotPos > atPos) {
				theEmail.focus();
				return "3 == You must enter a valid email address in the " + fieldName + " field.\n";
			}
			if (lastDotPos != ckE.length-4 && lastDotPos != ckE.length-3) {
				theEmail.focus();
				return "5 -- You must enter a valid email address in the " + fieldName + " field.\n";
			}
		}
	return "";
}

// validate whether or a not a string is blank
function check_string(theString,fieldName,setLength) {
	if (theString.value == "" && setLength != 0) {
		theString.focus();
		return "You need to enter the " + fieldName + ".\n";
	}
	if (setLength > theString.value.length) {
		theString.focus();
		return "The " + fieldName + " requires at least " + setLength + " characters but you only entered " + theString.value.length + ".\n";
	}
	return "";
}

// validate whether or not a value matches a pattern
function check_pattern(theString,fieldName,pattern,correctPattern,manditory) {
	var ckS = theString.value;
		if (pattern == "zip") {
			pattern = /\d{5}(\-| )?\d{4}/;
			correctPattern = "xxxxx-xxxx";
		} else if (pattern == "phone") {
			pattern = /(\()*\d{3}(\)|\-| )*\d{3}(\)|\-| )*\d{4}/;
			correctPattern = "xxx-xxx-xxxx";
		} else if (pattern == "date") {
			pattern = /\d{2}(\/|-){1}\d{2}(\/|-){1}\d{4}/;
			correctPattern = "01/02/2004";
		} // else pattern = pattern;
	var ckStf = pattern.test(ckS);
		if (manditory || ckS.replace(/[x\-]/g,"").length != 0) {
			if (!ckStf) {
				theString.focus();
				return "The " + fieldName + " does not match the pattern: " + correctPattern + "\n";
			}
			if (ckS.replace(pattern,"").length > 0) {
				theString.focus();
				return "The " + fieldName + " is too long. The pattern should be: " + correctPattern + "\n";
			}
		}
	return "";
}

// for clearing a form field onclick
function clear_field(theField,defaultValue,newValue) {
	if (theField.value == defaultValue) {
		theField.value = newValue;
	}
}

