var whitespace = " \t\n\r";

function trim(str) {
	while (str.charAt(str.length - 1) == ' ')
			str = str.substring(0, str.length - 1);
	while (str.charAt(0) == ' ')
			str = str.substring(1, str.length);
	return str;
}
function isEmpty(s) {
	return ((s == null) || (s.length == 0));
}
function isWhitespace(s) {
	if (isEmpty(s)) 
		return true;
	for (var i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1) 
			return false;
	}
	return true;
}
function isDigit(c) {		
	return ((c >= "0") && (c <= "9"));
}
function isInteger(s) {		    
	for (var i = 0; i < s.length; i++) {
		var c = s.charAt(i);		
		if (!isDigit(c)) 
			return false;
	}
	return true;
}
function isEmail(obj) {
	var s = obj.value;
	if (isEmpty(s)) {
		alert ("The Email field is blank.\nPlease enter your Email address.");
		obj.focus();
		return false;
	}
	if (isWhitespace(s)) {
		alert ("The Email field contains only spaces.\nPlease enter your Email address.");
		obj.select();
		obj.focus();
		return false;
	}
	var i = 1;
	var sLength = s.length;
	while ((i < sLength) && (s.charAt(i) != '@'))
		i++;    	
	if ((i >= sLength) || (s.charAt(i) != '@')) { 
		alert ("The Email address is invalid (there is no '@').");
		obj.select();
		obj.focus();
		return false;
	} else i += 2;
	while ((i < sLength) && (s.charAt(i) != '.'))
		i++;
	if ((i >= sLength - 1) || (s.charAt(i) != '.'))  { 
		alert ("The Email address is invalid (there is no '.').");
		obj.select();
		obj.focus();
		return false;
	} else return true;
}
function isName(obj) {
	var s = obj.value;
	if (isEmpty(s)) {
		alert ("The Name field is blank.\nPlease enter your name.")
		obj.focus();
		return false;
	}
	if (isWhitespace(s)) {
		alert ("The Name field contains only spaces.\nPlease enter your name.");
		obj.select();
		obj.focus();
		return false;
	}
	return true;
}		
function isZIPCode(obj) {
	var s = obj.value;
	if (!isEmpty(s)) {
		if (!isInteger(s)){
			alert("The zip code is not a number.\nPlease enter a 5-digit number.");
			obj.select();
			obj.focus();
			return false;
		}
		if (s.length != 5){
			alert("Invalid zip code.\nPlease enter a 5-digit number.");
			obj.focus();
			return false;
		}
		return true;
	} else {
		alert ("The Zip Code field is blank.\nPlease enter your zip code.")
		obj.focus();
		return false;
	}
	return true;
}
