/* * This function is used to validate entries on the registration form. It works by determining *	what fields are required based on CSS classes. The currently-used classes are as follows: * *		eventField - This is used when there are multiple events to select from. If there *				are any fields with this class, at least one of them must be "checked". *				This is independent of the other field types. *		requiredField - This is the basic class for text fields. The script will search *				through all fields marked as required and raise an error if they are blank. *				In addition, it will run the appropriate test on any required field that *				also has any of the later classes. *		emailField - This ensures that an email address is of the proper name@host.tld format. *		phoneField - This ensures that a given phone/fax number is legal to the extent of *				containing either seven or ten digits. *		zipField - This ensures that a given ZIP code is legal to the extent of containing *				either five or nine digits *		stateField - This ensures that the given state code is in the list of 50 states plus *				district and territories. * * Note that all the "types" of required fields will not be validated if they are not also *	marked as required. * * If there are any "eventField" fields and none of them are marked, this function will display *	an alert to that effect and return false immediately. * If one or more of the "requiredField" fields fail their test(s), this function will display *	an alert listing each problem with explanatory text based on the field's title attribute *	and return false. * If everything passes, it will return true.     */function validateFields() {	// before the normal, or "plebian" fields, we'd better make sure there's at least one event selected	eventFields = getElementsByClassName("eventField", document);	if(eventFields.length > 0) {		anySelected = false;		for(index = 0; index < eventFields.length; index++) {			currentElement = eventFields[index];			if(currentElement.checked) {				anySelected = true;				break;			}		}		if(!anySelected) {			// this is hard-coded for multiple events; maybe later I'll look into changing the wording			//	based on if it's a radio button set or checkboxes			alert("Please select at least one event to attend.");			return false;		}	}	// fetch every field on the form marked as required (of class "requiredField") 	requiredElements = getElementsByClassName("requiredField", document);		emailClass = new RegExp("\\bemailField\\b");	phoneClass = new RegExp("\\bphoneField\\b");	zipClass = new RegExp("\\bzipField\\b");	stateClass = new RegExp("\\bstateField\\b");		alertText = "";	for(index = 0; index < requiredElements.length; index++) {		currentElement = requiredElements[index];				if(trimAll(currentElement.value) == "") {			alertText += currentElement.title + " field is empty\n";		} else if(emailClass.test(currentElement.className)) {			emailReturn = legalEmail(currentElement.value);			if(emailReturn != "")				alertText += currentElement.title + emailReturn;		} else if(phoneClass.test(currentElement.className)) {			phoneReturn = legalPhone(currentElement.value);			if(phoneReturn != "")				alertText += currentElement.title + phoneReturn;		} else if(zipClass.test(currentElement.className)) {			zipReturn = legalZIP(currentElement.value);			if(zipReturn != "")				alertText += currentElement.title + zipReturn;		} else if(stateClass.test(currentElement.className)) {			stateReturn = legalState(currentElement.value);			if(stateReturn != "")				alertText += currentElement.title + stateReturn;		}	}	if(alertText != "") {		alert(alertText);		return false;	}	selectBox = document.forms[0].WhenUpgrade;	if(selectBox.selectedIndex == 0) {		alert("Please choose an upgrade plan answer.");		return false;	}	return true;}function legalZIP(aZip) {		cleanZip = stripNonDigits(aZip);	if(cleanZip.length != 5 && cleanZip.length != 9)		return " field is not a valid ZIP code.\n";	return "";}function legalPhone(aNumber) {		cleanNumber = stripNonDigits(aNumber);	if(cleanNumber.length != 10 && cleanNumber.length != 11)		return " field is not a valid phone number.\n";	return "";}function legalEmail(anAddress) {		validationResult = isEmail(anAddress);	if(validationResult != "")		return " field is not a valid e-mail address: " + validationResult + "\n";	return "";}function legalState(aState) {	var STATES = "AL/AK/AZ/AR/CA/CO/CT/DE/DC/FL/GA/GU/HI/IA/ID/IL/IN/IA/KS/KY/LA/ME/MD/MA/MI/MN/MS/MO/MT/NE/NV/NH/NJ/NM/NY/NC/ND/OH/OK/OR/PA/PR/RI/SC/SD/TN/TX/UT/VI/VT/VA/WA/WV/WI/WY";	var newStr = aState.toUpperCase();	if(STATES.indexOf(newStr) == -1 || newStr.indexOf("/") != -1 || newStr.length != 2) {		return " field is not a valid state code.\n";	}	return "";}// *********************************************************************************** //// http://www.webxpertz.net/forums/showthread.php?t=30555//	...except the commentingfunction getElementsByClassName(className, container) {	var arr   = [];	var reg   = new RegExp("\\b"+className+"\\b");	container = document.getElementById(container)||container||document;	var all   = container.all||container.getElementsByTagName("*");    var elm, k=-1;    for(index = 0; index < all.length; index++) {		elm = all[index];        	if(reg.test(elm.className))            arr[arr.length]=elm;    }    return arr}// This is useful for string trimming. This isn't built into JavaScript?function trimAll(sString) {	while (sString.substring(0,1) == ' ') {		sString = sString.substring(1, sString.length);	}	while (sString.substring(sString.length-1, sString.length) == ' ') {		sString = sString.substring(0, sString.length-1);	}	return sString;}function isNumeric(field) {	var errCount = 0;	var numdecs = 0;              // number of decimal points	var nummins = 0;              // number of minus signs	for(j = 0; j < field.length; j++) {		c = field.charAt(j);        // short hand notation for character at position j		if((c >= 0 && c <= 9) || (c == "." || c == "-")) {			if(c == ".") numdecs++;     // count the number of decimal points			if(c == "-") nummins++;     // count the number of minus signs		} else {      		errCount++;               // if it's none of those, increment error counter      	}    	}  	// error if count is non-zero or there are more than one decimal point or minus sign  	if(errCount > 0 || numdecs > 1 || nummins > 1) {    		return false;    	}  	return true;}function stripNonDigits(str) {	newStr = "";	for(j = 0; j < str.length; j++) {	    c = str.charAt(j);	    if(c >= "0" && c <= "9") {			newStr += c;		} 	}	return newStr;}// this is not my code, although I did edit it somewhat for my purposesfunction isEmail(emailStr) {	var checkTLD = 1;	var knownDomsPat = /^(com|COM|net|NET|org|ORG|edu|EDU|int|mil|MIL|gov|GOV|arpa|biz|aero|name|coop|info|pro|museum|tv)$/;	var emailPat = /^(.+)@(.+)$/;	var specialChars = "\\(\\)><@,;:\\\\\\\"\\.\\[\\]";	var validChars = "\[^\\s" + specialChars + "\]";	var quotedUser = "(\"[^\"]*\")";	var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;	var atom = validChars + '+';	var word = "(" + atom + "|" + quotedUser + ")";	var userPat = new RegExp("^" + word + "(\\." + word + ")*$");	var domainPat = new RegExp("^" + atom + "(\\." + atom +")*$");	var matchArray=emailStr.match(emailPat);	if(matchArray == null)		return "check @ and .'s";	var user = matchArray[1];	var domain = matchArray[2];	for(i = 0; i < user.length; i++) {		if (user.charCodeAt(i) > 127)			return "invalid characters in the user name";	}	for(i = 0; i < domain.length; i++) {		if (domain.charCodeAt(i) > 127)			return "invalid characters in the domain name";	}	if(user.match(userPat) == null)		return "user name appears to be invalid";	var IPArray = domain.match(ipDomainPat);	if(IPArray != null) {		for (var i = 1; i <= 4; i++) {			if (IPArray[i] > 255)				return "IP address is invalid";		}	} 	var atomPat = new RegExp("^" + atom + "$");	var domArr = domain.split(".");	var len = domArr.length;	for(i = 0; i < len; i++) {		if (domArr[i].search(atomPat) == -1)			return "domain name appears to be invalid";	}	if(checkTLD && domArr[domArr.length-1].length != 2 && 	   domArr[domArr.length-1].search(knownDomsPat) == -1)		return "need a well-known domain or two letter country code";	if(len < 2)		return "missing host name";	return "";}// this is not my code, although I did edit it somewhat for my purposesfunction isEmail(emailStr) {	var checkTLD = 1;	var knownDomsPat = /^(com|COM|net|NET|org|ORG|edu|EDU|int|mil|MIL|gov|GOV|arpa|biz|aero|name|coop|info|pro|museum|tv)$/;	var emailPat = /^(.+)@(.+)$/;	var specialChars = "\\(\\)><@,;:\\\\\\\"\\.\\[\\]";	var validChars = "\[^\\s" + specialChars + "\]";	var quotedUser = "(\"[^\"]*\")";	var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;	var atom = validChars + '+';	var word = "(" + atom + "|" + quotedUser + ")";	var userPat = new RegExp("^" + word + "(\\." + word + ")*$");	var domainPat = new RegExp("^" + atom + "(\\." + atom +")*$");	var matchArray=emailStr.match(emailPat);	if(matchArray == null)		return "check @ and .'s";	var user = matchArray[1];	var domain = matchArray[2];	for(i = 0; i < user.length; i++) {		if (user.charCodeAt(i) > 127)			return "invalid characters in the user name";	}	for(i = 0; i < domain.length; i++) {		if (domain.charCodeAt(i) > 127)			return "invalid characters in the domain name";	}	if(user.match(userPat) == null)		return "user name appears to be invalid";	var IPArray = domain.match(ipDomainPat);	if(IPArray != null) {		for (var i = 1; i <= 4; i++) {			if (IPArray[i] > 255)				return "IP address is invalid";		}	} 	var atomPat = new RegExp("^" + atom + "$");	var domArr = domain.split(".");	var len = domArr.length;	for(i = 0; i < len; i++) {		if (domArr[i].search(atomPat) == -1)			return "domain name appears to be invalid";	}	if(checkTLD && domArr[domArr.length-1].length != 2 && 	   domArr[domArr.length-1].search(knownDomsPat) == -1)		return "need a well-known domain or two letter country code";	if(len < 2)		return "missing host name";	return "";}