
function focusElement(formName, elemName) {
	var elem = document.forms[formName].elements[elemName];
	elem.focus( );
	elem.select( );
}

function focusElementSelect(formName, elemName) {
	var elem = document.forms[formName].elements[elemName];
	elem.focus( );
}

// validates that the field value string has one or more characters in it
function isNotEmpty(elem) {
	var str = elem.value;
	if (str == null || str.length == 0) {
		alert("Please fill in the required field.");
		setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
		return false;
	} else {
		return true;
	}
}
   
// validates that the field value string has one or more characters in it
function isNotBad(elem, badvalue) {
	var str = elem.value;
	if (str == badvalue) {
		alert("Unacceptable value.");
		setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
		return false;
	} else {
		return true;
	}
}
   
// validates that the entry is a positive or negative number
function isNumber(elem) {
	var str = elem.value;
	var oneDecimal = false;
	var oneChar = 0;
	// make sure value hasn't cast to a number data type
	str = str.toString( );
	for (var i = 0; i < str.length; i++) {
		oneChar = str.charAt(i).charCodeAt(0);
		// OK for minus sign as first character
		if (oneChar == 45) {
			if (i == 0) {
				continue;
			} else {
				alert("Only the first character may be a minus sign.");
				setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
				return false;
			}
		}
		// OK for one decimal point
		if (oneChar == 46) {
			if (!oneDecimal) {
				oneDecimal = true;
				continue;
			} else {
				alert("Only one decimal is allowed in a number.");
				setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
				return false;
			}
		}
		// characters outside of 0 through 9 not OK
		if (oneChar < 48 || oneChar > 57) {
			alert("Enter only numbers into the field.");
			setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
			return false;
		}
	}
	return true;
}
   
// validates that the entry is a positive int number
function isNumberInt(elem) {
	var str = elem.value;
	var oneChar = 0;
	// make sure value hasn't cast to a number data type
	str = str.toString( );
	for (var i = 0; i < str.length; i++) {
		oneChar = str.charAt(i).charCodeAt(0);
		// characters outside of 0 through 9 not OK
		if (oneChar < 48 || oneChar > 57) {
			alert("Enter only numbers into the field.");
			setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
			return false;
		}
	}
	return true;
}
   
// validates that the entry is 16 characters long
function isLen16(elem) {
	var str = elem.value;
	if (str.length != 16) {
		alert("Entry does not contain the required 16 characters.");
		setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
		return false;
	} else {
		return true;
	}
}
   
// validates that the entry is formatted as an email address
function isEMailAddr(elem) {
	var str = elem.value;
	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 required
		if (domain.indexOf(".") == -1) {
			alert("Verify the domain portion of the email address.");
			setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
			return false;
		}
		// parse address portion first, character by character
		for (var i = 0; i < addr.length; i++) {
			oneChar = addr.charAt(i).charCodeAt(0);
			// dot or hyphen not allowed in first position; dot in last
			if ((i == 0 && (oneChar == 45 || oneChar == 46))  || 
				(i == addr.length - 1 && oneChar == 46)) {
				alert("Verify the user name portion of the email address.");
				setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
				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("Verify the user name portion of the email address.");
				setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
				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("Verify the domain portion of the email address.");
				setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
				return false;
			}
			if (oneChar == 45 || oneChar == 46 || oneChar == 95 || 
				(oneChar > 47 && oneChar < 58) || (oneChar > 96 && oneChar < 123)) {
				continue;
			} else {
				alert("Verify the domain portion of the email address.");
				setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
				return false;
			}
		}
		return true;
	}
	alert("The email address may not be formatted correctly. Please verify.");
	setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
	return false;
}

// validate that the user made a selection other than default
function isChosen(elem) {
	if (elem.selectedIndex == 0) {
		alert("Please make a choice from the list.");
		setTimeout("focusElementSelect('" + elem.form.name + "', '" + elem.name + "')", 0);
		return false;
	} else {
		return true;
	}
}
   
// validate that the user has checked one of the radio buttons
function isValidRadio(elem) {
	var valid = false;
	for (var i = 0; i < elem.length; i++) {
		if (elem[i].checked) {
			return true;
		}
	}
	alert("Make a choice from the radio buttons.");
	setTimeout("focusElementSelect('" + elem.form.name + "', '" + elem.name + "')", 0);
	return false;
}