// checks to verify a field entry at the least
function isNotEmpty(elem, whichField) { 
	var str = elem.value;
	// alert ("In isNotEmpty");
	
	// check each required "text" field, to make sure there is an entry and not empty
	if (str == null || str.length == 0) {
		alert("You must enter a [" +  whichField + "] to submit this form.");
		return false;
	} else {
		return true;
	}
	
}


// checks email address for proper format
function isEMailAddr(elem) {
	var str = elem.value;
	// alert ("you're in isEmailAddr");
	
	str = str.toLowerCase();
	if (str.indexOf("@") > 1) {
		var addr = str.substring(0, str.indexOf("@"));
		var domain = str.substring(str.indexOf("@") + 1, str.length);
		// at least one top level domain is required
		if (domain.indexOf(".") == -1) {
			alert("Please verify the [domain] portion of your [email address]. The domain is everything after the @ sign.");
			return false;
		}
		//parse address protion first, character by character
		for (var i = 0; i < addr.length; i++) {
			oneChar = addr.charAt(i).charCodeAt(0);
			// dot or hyphen not allowed in the first position; dot in last
			if ((i == 0 && (oneChar == 45 || oneChar == 46)) || (i == addr.length - 1 && oneChar == 46)) {
				alert("Please verify the [username] portion of your [email address].  The username is everything before the @ sign.");
				return false;
			}
			// acceptable characters (- . _ 0-9 a-z)
			if (oneChar == 45 || oneChar == 46 || oneChar == 95 || (oneChar > 47 && oneChar < 58) || (oneChar > 96 && oneChar < 123)) {
				continue;
			} else {
				alert("Please verify the [username] portion of your [email address].  The username is everything before the @ sign.");
				return false;
			}
		}
		for (i = 0; i < domain.length; i++) {
			oneChar = domain.charAt(i).charCodeAt(0);
			if ((i == 0 && (oneChar == 45 || oneChar == 46)) || ((i == domain.length - 1 || i == domain.length - 2) && oneChar == 46)) {
				alert("Please verify the [domain] portion of your [email address]. The domain is everything after the @ sign.");
				return false;
			}
			if (oneChar == 45 || oneChar == 46 || oneChar == 95 || (oneChar > 47 && oneChar < 58) || (oneChar > 96 && oneChar < 123)) {
				continue;
			} else {
				alert("Please verify the [domain] portion of your [email address]. The domain is everything after the @ sign.");
				return false;
			}
		}
		return true;
	}
	alert("Please verify your [email address] is formatted correctly. e.g. username@domain.com");
	return false;
}


function validate(form) {
	if (isNotEmpty(document.registration.firstName, 'First Name')) {
		if (isNotEmpty(document.registration.lastName, 'Last Name')) {
			if (isNotEmpty(document.registration.email, 'Email Address')) {
				if (isEMailAddr(document.registration.email)) {
					return true;
				}
			}
		}
	} else {
		return false;
	}
}