//Copyright 2003-2005 Phierce Web, LLC. No part of this script may be used without express written permission.
function check_in_array(haystack,needle) {
	var foundIt = false;
	flatStack = "|:|" + haystack.join("|:|") + "|:|";
	if (flatStack.indexOf("|:|" + needle + "|:|") > -1) foundIt = true;
	return foundIt;
}

function formBtn(elt,btnAction,formName) {
	if (typeof(elt) == "object") {
		theButton = elt;
	} else if (typeof(elt) == "string") {
		theButton = fixElement(elt);	
	}
	if (typeof(theButton) == "object") {
		var classSplitter;
		if (elt.className.indexOf("Out") > -1) classSplitter = "Out";
		else if (elt.className.indexOf("Over") > -1) classSplitter = "Over";
		else if (elt.className.indexOf("Down") > -1) classSplitter = "Down";
		else if (elt.className.indexOf("Up") > -1) classSplitter = "Up";
		else if (elt.className.indexOf("Click") > -1) classSplitter = "Click";
		var classParts = elt.className.split(classSplitter);
		if (btnAction=="out" || btnAction=="over") {
			theButton.className = classParts[0] + btnAction.substring(0,1).toUpperCase() + btnAction.substring(1,btnAction.length).toLowerCase();
		}
	}
	if (btnAction=="click" && formName) {
		validateThenSubmit(formName);	
	}
}
function getParentForm(elt,formName) {
	if (elt.parentElement) {
		while (elt.parentElement) {
			elt = elt.parentElement;
			if (elt.tagName.toUpperCase() == "FORM") break;
		}
	} else if (elt.parentNode) {
		while (elt.nextSibling) {
			elt = elt.nextSibling;
			elt = elt.parentNode;
			if (elt.tagName.toUpperCase() == "FORM") break;
		}
	}
	return elt;
}
/*function validateThenSubmit(elt) {
	if (typeof(elt) == "object") {
		if (elt.tagName.toUpperCase() != "FORM") {
			theForm = getParentForm(elt);	
		} else {
			theForm = elt;
		}	
	} else if (typeof(elt) == "string") {
		theForm = fixElement(elt);
	}
	if (typeof(theForm) == "object") {
		if (validateForm(theForm)) theForm.submit();
	} else {
		alert("Cannot validate the form.");	
	}
}*/

function validateThenSubmit(elt) {
	theForm = fixElement(elt);
	if (typeof(theForm) == "object") {
		if (validateForm(theForm)) theForm.submit();
	} else {
		alert("Cannot validate the form.");	
	}
}

function validateFormOld(whichForm) {
	validContent = true;
	var errorMessage = "The following problems occured with the form:\n";
	var separator = "______________________________________________________________";
	errorMessage += separator + "\n\n";
	var i,j;
	for (i=0;i<theFields.length;i++) {
		var theCompareField = null;
		var ccTypes = null;
		var ccParts = null;
		if (window.debug && debug == 1) alert(theFields[i][0]);
		if (theFields[i][0].indexOf("[]") > -1) { //if the form name contains [], it is a multiple
			var theTypes = new Array();
			theForm = eval("document." + whichForm);
			for (j=0;j<theForm.elements.length;j++) {
				if (theForm.elements[j].name == theFields[i][0]) { //scroll through the entire form and look for elements with the same name as what we've specified
					if (theFields[i][4] == "multipleType") {
						if (check_in_array(theTypes,theForm.elements[j].value)) {
							validContent = false;
							errorMessage += "You may only save one " + theForm.elements[j].value + " " + theFields[i][1] + ".\n";
						} else {
							theTypes[theTypes.length] = theForm.elements[j].value;
						}
					} else {
						msg = validateElementOld(theForm.elements[j],theFields[i][1],theFields[i][4],theFields[i][2],theFields[i][3],theFields[i][5],theCompareField,ccTypes);
						if (msg.length > 0) {
							validContent = false;
							errorMessage += msg + "\n";
						}
					}
				}
			}
		} else {
			currField = eval("document." + whichForm + "." + theFields[i][0]);
			if  (theFields[i][4].indexOf("cc-") > -1) { //validate credit card
				ccParts = theFields[i][4].split("-");
				oldFieldName = theFields[i][4];
				theFields[i][4] = ccParts[0];
				ccTypes = ccParts[ccParts.length - 1].substring((ccParts[ccParts.length - 1].indexOf("(") + 1),(ccParts[ccParts.length - 1].indexOf(")")));
				theFields[i][4] = oldFieldName;
			} else if (theFields[i][4].indexOf("-") > -1) {
				compareFieldName = theFields[i][4].substring((theFields[i][4].indexOf("(") + 1),theFields[i][4].indexOf(")"));
				theCompareField = eval("document." + whichForm + "." + compareFieldName);																																		 
			} else {
				theCompareField = null;
			}
			
			msg = validateElementOld(currField,theFields[i][1],theFields[i][4],theFields[i][2],theFields[i][3],theFields[i][5],theCompareField,ccTypes);
			if (msg.length > 0) {
				validContent = false;
				errorMessage += msg + "\n";
			}
		}
	}
	errorMessage += separator;
	if (!validContent) alert(errorMessage);
	return validContent;
}

function validateElementOld(whichElement,displayName,whichType,minDigits,maxDigits,required,matchWith,ccTypes) {
	var errorMessage = "";
	
	var regExp = new Array();
	regExp['integer'] = /^-?\d+$/;
	regExp['decimal'] = /^[\d\.]+$/;
	regExp['money'] = /^\$?(\d|,)+(\.\d{2})?$/;
	regExp['string'] = /.*/;
	regExp['binary'] = /^[true,false,0,1]$/;
	regExp['hex'] = /^#[A-Fa-f0-9]{6}$/;
	regExp['email'] = /^([a-zA-Z0-9_\-])([a-zA-Z0-9_\-\.]*)@(\[((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}|((([a-zA-Z0-9\-]+)\.)+))([a-zA-Z]{2,}|(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\])$/;
	regExp['email-(email2)'] = /^([a-zA-Z0-9_\-])([a-zA-Z0-9_\-\.]*)@(\[((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}|((([a-zA-Z0-9\-]+)\.)+))([a-zA-Z]{2,}|(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\])$/;
	regExp['username'] = /^[a-zA-Z0-9_\-]+$/;
	regExp['password'] = /.*/;
	regExp['emailalias'] = /^([A-Za-z])([a-zA-Z0-9_\-]){2}([a-zA-Z0-9_\-]+)?$/;
	regExp['phone'] = /^1?\s*[-\.]?\s*(\d{3}|\(\s*\d{3}\s*\))\s*[-\.]?\s*\d{3}\s*[-\.]?\s*\d{4}$/;
	regExp['zip'] = /(^[A-Z]{1,2}[0-9]{1,2}[A-Z]? ?[0-9][ABDEFGHJLNPQRSTUWXYZ]{2}$)|(^postal code$)|(^postal$)|(^\d{5}(-\d{4})?$)|(^[A-Za-z]\d[A-Za-z][ -]?\d[A-Za-z]\d$)|(^\d{4}$)|(^[a-zA-Z]{2}[ ]*[a-zA-Z0-9]{2}$)/;
	regExp['date'] = /^\d{1,2}\/|-\d{1,2}\/|-\d{4}$/;
	regExp['mysqlDate'] = /^\d{4}-\d{2}|-\d{2}$/;
	regExp['file'] = /.*/;
	regExp['ssn'] = /^\d{3}[\-\. ]?\d{2}[\-\. ]?\d{4}$/;
	regExp['ein'] = /^\d{2}[\-\. ]?\d{7}$/;
	regExp['ssnein'] = /^(\d{3}[\-\. ]?\d{2}[\-\. ]?\d{4})|(\d{2}[\-\. ]?\d{7})$/;
	regExp['ip'] = /^\d\d?\d?\.\d\d?\d?\.\d\d?\d?\.\d\d?\d?$/;
	regExp['proPass'] = /^\d{6}$/;
	regExp['dropdown'] = /^..*$/;
	regExp['cc'] = /^(\*{12}\d{4})|(4\d{3}-?\d{4}-?\d{4}-?\d{4})|(5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4})|(6011-?\d{4}-?\d{4}-?\d{4})|(3[4,7]\d{13})|(3[4,7]\d{13})$/;
	regExp['ccMasked'] = /^\*{12}\d{4}$/;
	regExp['visa'] = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
	regExp['mc'] = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
	regExp['discover'] = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
	regExp['amex'] = /^3[4,7]\d{13}$/;
	regExp['diners'] = /^3[0,6,8]\d{12}$/;
	
	var noRegExp = new Array();
	noRegExp['integer'] = "\"" + displayName + "\" must contain only digits.";
	noRegExp['decimal'] = "\"" + displayName + "\" must contain only digits or a period.";
	noRegExp['money'] = "\"" + displayName + "\" must be a valid dollar amount ($123.45).";
	noRegExp['string'] = "\"" + displayName + "\" must be a valid string.";
	noRegExp['binary'] = "\"" + displayName + "\" must be either true or false.";
	noRegExp['hex'] = "\"" + displayName + "\" must be a hexidecimail color code (#AF614C).";
	noRegExp['email'] = "\"" + displayName + "\" must be a properly formatted email address (you@yourserver.com).";
	noRegExp['email-(email2)'] = "\"" + displayName + "\" must be a properly formatted email address (you@yourserver.com).";
	noRegExp['username'] = "\"" + displayName + "\" may only contain letters, numbers, underscores, or dashes.";
	noRegExp['password'] = "\"" + displayName + "\" must be a valid password.";
	noRegExp['emailalias'] = "\"" + displayName + "\" must start with a letter and should not contain any blank spaces or special characters including: . , ? ; : \" ' ! @ # $ % ^ & * ( ), etc.";
	noRegExp['phone'] = "\"" + displayName + "\" must be a properly formatted phone number (123) 456-7890.";
	noRegExp['zip'] = "\"" + displayName + "\" must be a properly formatted zip code (12345-6789) or (A1B 2C3).";
	noRegExp['date'] = "\"" + displayName + "\" must be in date format (MM/DD/YYYY).";
	noRegExp['mysqlDate'] = "\"" + displayName + "\" must be in date format (YYYY-MM-DD).";
	noRegExp['file'] = "\"" + displayName + "\" must contain the name of a file on your drive.";
	noRegExp['ssn'] = "\"" + displayName + "\" must be a valid social security number (123-45-6789).";
	noRegExp['ein'] = "\"" + displayName + "\" must be a valid federal employer identification number (12-3456789).";
	noRegExp['ssnein'] = "\"" + displayName + "\" must be a valid social security number (123-45-6789) or federal employer identification number (12-3456789).";
	noRegExp['ip'] = "\"" + displayName + "\" must be a valid IP address (123.456.789.012).";
	noRegExp['proPass'] = "\"" + displayName + "\" must be a valid ProPass number (123456).";
	noRegExp['dropdown'] = "You must select a value for \"" + displayName + "\".";
	noRegExp['cc'] = whichElement.value + " is not a valid card number.";
	
	var digits = new Array('integer','decimal','money');
	var alternateTypes = new Array('radio','checkbox');
	
	var units = "character";
	var i;
	for (i=0;i<digits.length;i++) {
		if (whichType == digits[i]) {
			units = "digit";
			break;
		}
	}
	
	if (typeof(regExp[whichType]) != "undefined" || (check_in_array(alternateTypes,whichType))) {
		if (whichType == "checkbox") {
			if (required && !whichElement.checked) errorMessage += "\"" + displayName + "\" must be checked.";
		} else if (whichType == "radio") {
			var i;
			var isChecked = false;
			for (i=0;i<whichElement.length;i++) {
				if (whichElement[i].checked) isChecked = true;
			}
			if (!isChecked) errorMessage += "Please select a " + displayName + ".";
		} else if (whichType == "cc") {
			var validCC = false;
			var types = ccTypes.split("|");
			re = regExp[whichType];
			for (i=0;i<ccTypes.length;i++) if (re.test(whichElement.value)) validCC = true;
			re = regExp['ccMasked'];
			if (validCC && !re.test(whichElement.value)) {
				// Checksum ("Mod 10")
				// Add even digits in even length strings or odd digits in odd length strings.
				var checksum = 0;
				var currCcNum = whichElement.value.replace(/\-/g,"");
				
				for (j=(2-(currCcNum.length % 2)); j<=currCcNum.length; j+=2) {
					checksum += parseInt(currCcNum.charAt(j-1));
				}
				// Analyze odd digits in even length strings or even digits in odd length strings.
				for (j=(currCcNum.length % 2) + 1; j<currCcNum.length; j+=2) {
					var digit = parseInt(currCcNum.charAt(j-1)) * 2;
					if (digit < 10) { 
						checksum += digit; 
					} else { 
						checksum += (digit-9);
					}
				}
				if ((checksum % 10) != 0) {
					validCC = false;
				}			
			}
			
			if (!validCC) errorMessage += noRegExp[whichType];
		} else {
			if (required || !required && whichElement.value) {
				re = regExp[whichType];
				if (!re.test(whichElement.value)) errorMessage += noRegExp[whichType];
				else if (matchWith && whichElement.value != matchWith.value) errorMessage += "\"" + displayName + "s\" do not match.";
				else if (whichElement.value.length < minDigits) errorMessage += "\"" + displayName + "\" must contain " + minDigits + " or more "+units+"(s).";
				else if (whichElement.value.length > maxDigits) errorMessage += "\"" + displayName + "\" must contain " + maxDigits + " or less "+units+"(s).";
			}
		}
	}
	
	return errorMessage;
}

//new form validator
function validateForm(whichForm) {
	var errorMessage = "The following problems occured with the form:\n";
	var separator = "______________________________________________________________";
	errorMessage += separator + "\n\n";
	var i,j;
	var validContent = 1;//innocent until proven guilty
	
	for (i=0;i<whichForm.elements.length;i++) {//validate each element in the form if property 'validate' is specified
		elt = whichForm.elements[i];		
		if ((elt.validate && elt.validate == 1) || elt.getAttribute("validate") == 1) {
			msg = validateElement(elt);
			if (msg.length > 0) {
				validContent = false;
				errorMessage += msg + "\n";
			}
		}
	}
	errorMessage += separator;
	if (!validContent) alert(errorMessage);
	return validContent;
}

function validateElement(elt) {
	var errorMessage = "";
	
	validateType = (elt.validatetype) ? elt.validatetype : elt.getAttribute("validatetype");
	required = (elt.required) ? elt.required : elt.getAttribute("required");
	displayName = (elt.displayname) ? elt.displayname : elt.getAttribute("displayname");
		if (!displayName) displayName = (elt.id) ? elt.id : elt.name;
	minLength = (elt.minlength) ? elt.minlength : elt.getAttribute("minlength");
	maxLength = (elt.maxlength) ? elt.maxlength : elt.getAttribute("maxlength");
	compareTo = (elt.compareto) ? elt.compareto : elt.getAttribute("compareto");
	compareElt = 0;
	if (compareTo && fixElement(compareTo)) compareElt = fixElement(compareTo);
	typeinfo = (elt.typeinfo) ? elt.typeinfo : elt.getAttribute("typeinfo");
	typeinfoElt = 0;
	if (typeinfo && fixElement(typeinfo)) typeinfoElt = fixElement(typeinfo);
	if (typeinfoElt) {
		validateType = typeinfoElt.value.toLowerCase();
	}
	
	var ccre = /cc\-\(.*\)/;
	if (ccre.test(validateType)) {
		ccTypes = validateType.substring(4,validateType.length-1);
		validateType = "cc";
	}
	
	var regExp = new Array();
	regExp['snumber'] = /^S\d{8}$/;
	regExp['integer'] = /^-?\d+$/;
	regExp['decimal'] = /^[\d\.]+$/;
	regExp['money'] = /^\$?(\d|,)+(\.\d{2})?$/;
	regExp['string'] = /.*/;
	regExp['binary'] = /^[true,false,0,1]$/;
	regExp['hex'] = /^#[A-Fa-f0-9]{6}$/;
	regExp['email'] = /^([a-zA-Z0-9_\-])([a-zA-Z0-9_\-\.]*)@(\[((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}|((([a-zA-Z0-9\-]+)\.)+))([a-zA-Z]{2,}|(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\])$/;
	regExp['username'] = /^[a-zA-Z0-9_\-]+$/;
	regExp['password'] = /.*/;
	regExp['emailalias'] = /^([A-Za-z])([a-zA-Z0-9_\-]){2}([a-zA-Z0-9_\-]+)?$/;
	regExp['phone'] = /^1?\s*[-\.]?\s*(\d{3}|\(\s*\d{3}\s*\))\s*[-\.]?\s*\d{3}\s*[-\.]?\s*\d{4}$/;
	regExp['areacode'] = /^\d+$/;
	//regExp['phonenumber'] = /^\d{3}\s*[-\.]?\s*\d{4}$/;
	regExp['phonenumber'] = /^[0-9 -\.]+$/;
	regExp['zip'] = /(^[A-Z]{1,2}[0-9]{1,2}[A-Z]? ?[0-9][ABDEFGHJLNPQRSTUWXYZ]{2}$)|(^postal$)|(^postal code$)|(^\d{5}(-\d{4})?$)|(^[A-Za-z]\d[A-Za-z][ -]?\d[A-Za-z]\d$)|(^\d{4}$)/;
	regExp['date'] = /^\d{1,2}\/|-\d{1,2}\/|-\d{4}$/;
	regExp['mysqlDate'] = /^\d{4}-\d{2}|-\d{2}$/;
	regExp['file'] = /.*/;
	regExp['ssn'] = /^\d{3}[\-\. ]?\d{2}[\-\. ]?\d{4}$/;
	regExp['ein'] = /^\d{2}[\-\. ]?\d{7}$/;
	regExp['ssnein'] = /^(\d{3}[\-\. ]?\d{2}[\-\. ]?\d{4})|(\d{2}[\-\. ]?\d{7})$/;
	regExp['ip'] = /^\d\d?\d?\.\d\d?\d?\.\d\d?\d?\.\d\d?\d?$/;
	regExp['proPass'] = /^\d{6}$/;
	regExp['dropdown'] = /^..*$/;
	regExp['cc'] = /^(\*{12}\d{4})|(4\d{3}-?\d{4}-?\d{4}-?\d{4})|(5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4})|(6011-?\d{4}-?\d{4}-?\d{4})|(3[4,7]\d{13})|(3[4,7]\d{13})$/;
	regExp['ccMasked'] = /^\*{12}\d{4}$/;
	regExp['visa'] = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
	regExp['mc'] = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
	regExp['discover'] = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
	regExp['amex'] = /^3[4,7]\d{13}$/;
	regExp['diners'] = /^3[0,6,8]\d{12}$/;
	
	var noRegExp = new Array();
	noRegExp['snumber'] = "\"" + displayName + "\" must be a properly formatted s-number (S12345678).";
	noRegExp['integer'] = "\"" + displayName + "\" must contain only digits.";
	noRegExp['decimal'] = "\"" + displayName + "\" must contain only digits or a period.";
	noRegExp['money'] = "\"" + displayName + "\" must be a valid dollar amount ($123.45).";
	noRegExp['string'] = "\"" + displayName + "\" must be a valid string.";
	noRegExp['binary'] = "\"" + displayName + "\" must be either true or false.";
	noRegExp['hex'] = "\"" + displayName + "\" must be a hexidecimail color code (#AF614C).";
	noRegExp['email'] = "\"" + displayName + "\" must be a properly formatted email address (you@yourserver.com).";
	noRegExp['username'] = "\"" + displayName + "\" may only contain letters, numbers, underscores, or dashes.";
	noRegExp['password'] = "\"" + displayName + "\" must be a valid password.";
	noRegExp['emailalias'] = "\"" + displayName + "\" must start with a letter and should not contain any blank spaces or special characters including: . , ? ; : \" ' ! @ # $ % ^ & * ( ), etc.";
	noRegExp['phone'] = "\"" + displayName + "\" must be a properly formatted phone number (123) 456-7890.";
	noRegExp['areacode'] = "\"" + displayName + "\" must be a properly formatted area code 123.";
	noRegExp['phonenumber'] = "\"" + displayName + "\" must be a properly formatted phone number 456-7890.";
	noRegExp['zip'] = "\"" + displayName + "\" must be a properly formatted zip code (12345-6789) or (A1B 2C3).";
	noRegExp['date'] = "\"" + displayName + "\" must be in date format (MM/DD/YYYY).";
	noRegExp['mysqlDate'] = "\"" + displayName + "\" must be in date format (YYYY-MM-DD).";
	noRegExp['file'] = "\"" + displayName + "\" must contain the name of a file on your drive.";
	noRegExp['ssn'] = "\"" + displayName + "\" must be a valid social security number (123-45-6789).";
	noRegExp['ein'] = "\"" + displayName + "\" must be a valid federal employer identification number (12-3456789).";
	noRegExp['ssnein'] = "\"" + displayName + "\" must be a valid social security number (123-45-6789) or federal employer identification number (12-3456789).";
	noRegExp['ip'] = "\"" + displayName + "\" must be a valid IP address (123.456.789.012).";
	noRegExp['proPass'] = "\"" + displayName + "\" must be a valid ProPass number (123456).";
	noRegExp['dropdown'] = "You must select a value for \"" + displayName + "\".";
	noRegExp['cc'] = "\"" + elt.value + "\" is not a valid card number.";
	noRegExp['visa'] =  "\"" + elt.value + "\" is not a valid Visa card number.";
	noRegExp['mc'] =  "\"" + elt.value + "\" is not a valid MasterCard number.";
	noRegExp['discover'] =  "\"" + elt.value + "\" is not a valid discover card number.";
	noRegExp['amex'] =  "\"" + elt.value + "\" is not a valid AmEx card number.";
	noRegExp['diners'] =  "\"" + elt.value + "\" is not a valid diners card number.";
	
	var digits = new Array('integer','decimal','money');
	var alternateTypes = new Array('radio','checkbox');
	
	var units = "character";
	var i;
	for (i=0;i<digits.length;i++) {
		if (validateType == digits[i]) {
			units = "digit";
			break;
		}
	}
	
	if (typeof(regExp[validateType]) != "undefined" || (check_in_array(alternateTypes,validateType))) {
		if (validateType == "checkbox") {
			if (required && !elt.checked) errorMessage += "\"" + displayName + "\" must be checked.";
		} else if (validateType == "radio") {
			var i;
			var isChecked = false;
			for (i=0;i<elt.length;i++) {
				alert(elt[i]);
				if (elt[i].checked) isChecked = true;
			}
			if (!isChecked) errorMessage += "Please select a/an " + displayName + ".";
		} else if (validateType == "cc") {
			var validCC = false;
			var types = ccTypes.split("|");
			re = regExp[validateType];
			for (i=0;i<ccTypes.length;i++) if (re.test(elt.value)) validCC = true;
			re = regExp['ccMasked'];
			if (validCC && !re.test(elt.value)) {
				// Checksum ("Mod 10")
				// Add even digits in even length strings or odd digits in odd length strings.
				var checksum = 0;
				var currCcNum = elt.value.replace(/\-/g,"");
				
				for (j=(2-(currCcNum.length % 2)); j<=currCcNum.length; j+=2) {
					checksum += parseInt(currCcNum.charAt(j-1));
				}
				// Analyze odd digits in even length strings or even digits in odd length strings.
				for (j=(currCcNum.length % 2) + 1; j<currCcNum.length; j+=2) {
					var digit = parseInt(currCcNum.charAt(j-1)) * 2;
					if (digit < 10) { 
						checksum += digit; 
					} else { 
						checksum += (digit-9);
					}
				}
				if ((checksum % 10) != 0) {
					validCC = false;
				}			
			}
			
			if (!validCC) errorMessage += noRegExp[validateType];
		} else {
			if (required == 1 || (!required || required == 0) && elt.value != "") {
				re = regExp[validateType];
				if (!re.test(elt.value)) errorMessage += noRegExp[validateType];
				if (compareElt && elt.value != compareElt.value) errorMessage += "\"" + displayName + "s\" do not match.";
				if (minLength && elt.value.length < minLength) errorMessage += "\"" + displayName + "\" must contain " + minLength + " or more "+units+"(s).";
				else if (maxLength && elt.value.length > maxLength) errorMessage += "\"" + displayName + "\" must contain " + maxLength + " or less "+units+"(s).";
			}
		}
	}
	
	return errorMessage;
}

function formBtnSwap(element) {
	classParts = element.className.split("Style");
	rootClass = classParts[0] + "Style";
	if (classParts[classParts.length - 1] == "Off") element.className = rootClass + "On";
	else element.className = rootClass + "Off";
}

function specialFormSubmit(whichForm,formAction,formTarget,validate) {
	theForm = fixElement(whichForm);
	theForm.action = (formAction) ? formAction : theForm.action;
	theForm.target = (formTarget) ? formTarget : "_self";
	if ((!validate) || (validate && validateForm(theForm))) {
		theForm.submit();
	}
}

function switchStates(whichCountryField) {

	// If global variable autoUpdateCountryDialingCode is set
	// then it will update the blank country dialing codes on the form
	if ( typeof autoUpdateCountryDialingCodes != 'undefined' ) {
		
		var countryDialingCode = '';
		for ( i = 0; i < countryDialingCodes.length; i++ ) {
			if ( countryDialingCodes[i][0] == whichCountryField.value ) {
				countryDialingCode = countryDialingCodes[i][1];
			}
		}
		
		if ( countryDialingCode ) {

			var fieldNames = new Array("dayCountryDialingCode1", "cellCountryDialingCode1");
			
			for ( i = 0; i < fieldNames.length; i++ ) {
				elemObj = fixElement( fieldNames[i] );

				if ( elemObj.value == '' ) {
					elemObj.value = countryDialingCode;
				}
				
				else if ( elemObj.value != countryDialingCode ) {
					var result = confirm( "Current value for "  + fieldNames[i] + " (" + elemObj.value + ") does not match your selected country's dialing code (" + countryDialingCode + "). Do you want to set the value of " + fieldNames[i] + " to " + countryDialingCode + "?" ); 
					
					if ( result ) {
						elemObj.value = countryDialingCode;
					}
				}
			}
		}
		
		else {
			alert( "We don't have a record for this Country's Dialing Code!" );
		}
	}
		
	//determine the name of the node id we're using
	var whichNodeId = "countryId";
	if (whichCountryField.id.indexOf(whichNodeId) == -1) whichNodeId = 'CountryId';
	whichNodeParts = whichCountryField.id.split(whichNodeId); 
	if (whichNodeParts.length > 1) { //get the name of the current node id
		prefix = whichNodeParts[0];
		nodeId = whichNodeParts[1];
	}
	//now determine the name of the state field
	if (prefix != '') whichStateField = fixElement(prefix + 'StateId' + nodeId);
	else whichStateField = fixElement('stateId' + nodeId);

	if (typeof(whichStateField) == "object") {
		whichStateField.options.selectedIndex = 0;
		if (!in_array(hasStates,whichCountryField.value)) {
			whichArray = states0;
		} else {
			whichArray = eval("states" + whichCountryField.value);
		}
		whichStateField.options.length = whichArray.length;
		for (i=0;i<whichArray.length;i++) {
			whichStateField.options[i].value = whichArray[i][0];
			whichStateField.options[i].text = whichArray[i][2];
		}
	}
}
	
var hasStates = new Array();
var states0 = new Array();
states0[0] = new Array('','No States/Provinces','No States/Provinces');