// 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) {
		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.");
				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.");
				return false;
			}
		}
		// characters outside of 0 through 9 not OK
		if (oneChar < 48 || oneChar > 57) {
			alert("Enter only numbers into the field.");
			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.");
		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.");
			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.");
				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.");
				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.");
				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.");
				return false;
			}
		}
		return true;
	}
	//alert("The email address may not be formatted correctly. Please verify.");
	return false;
}

// ---------------
// same functions but using regex
// ---------------

// validates that the field value string has one or more characters in it
function isNotEmpty_rgx(elem) {
	var str = elem.value;
	var re = /.+/;
	if (!str.match(re)) {
		return false;
	} else {
		return true;
	}
}

// validates that the entry is a positive or negative number
function isNumber_rgx(elem) {
	var str = elem.value;
	var re = /^[-]?\d*\.?\d*$/;
	str = str.toString();
	if (!str.match(re)) {
		alert("Enter only numbers into the field.");
		return false;
	}
	return true;
}

// validates that the entry is 16 characters long when
// input field's maxlength attribute is set to 16
function isLen16_rgx(elem) {
	var str = elem.value;
	var re = /\b.{16}\b/;
	if (!str.match(re)) {
		alert("Entry does not contain the required 16 characters.");
		return false;
	} else {
		return true;
	}
}

// validates that the entry is formatted as an email address
function isEMailAddr_rgx(elem) {
	var str = elem.value;
	var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
	if (!str.match(re)) {
		alert("Verify the email address format.");
		return false;
	} else {
		return true;
	}
}
