//*****************************************************************************
function getObject(theId) {
	var elm = null;
	if (document.getElementById) {
		// browser implements part of W3C DOM HTML Gecko, Internet Explorer 5+, Opera 5+
		elm = document.getElementById(theId);
	} else if (document.all) {
		// Internet Explorer 4 or Opera with IE user agent
		elm = document.all[theId];
	} else if (document.layers) {
		// Navigator 4
		elm = document.layers[theId];
	}

 return elm;
}


// A utility function that returns true if a string contains only whitespace characters.
function isblank(s) {
	for(var i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '\t')) 
		    return false;
	}
	return true;
}

// A utility function that removes spaces, commas and dollar signs from a string.
function clearspaces(s) {
	var newstring = "";
	var len = s.value.length;
	for (i = 0 ; i < len ; i++) {
		var c = s.value.charAt(i);
		if ((c != " ") && (c !=	",") && (c != "$")) {
			newstring = newstring + c;
		}
	}
	s.value = newstring;
	return s.value
}

// A utility function that checks and formats a string for a valid zip or zip+4
function formatzip(z) {
	var tempnumber = "";
	var len = z.value.length;
	for (i = 0 ; i < len ; i++) {
		var c = z.value.charAt(i);
		if ( c <= "9" && c >=	"0") {
			tempnumber = tempnumber + c;
		}
	}
	if ( tempnumber.length == 5 || tempnumber.length == 9) {
		zip = tempnumber.substring(0,5);
		plus4 = tempnumber.substring(5,9);
		if (tempnumber.length == 9) {
			z.value = zip + "-" + plus4;
		} else {
			z.value = zip;
		}
		return z.value;
	} else {
	return false;
	}
}

// A utility function that checks and formats a string for a valid phone number
function formatphone(p) {
	var tempnumber = "";
	var len = p.value.length;
	for (i = 0 ; i < len ; i++) {
		var c = p.value.charAt(i);
		if ( c <= "9" && c >=	"0") {
			tempnumber = tempnumber + c;
		}
	}
	if ( tempnumber.length == 10) {
		areacode = tempnumber.substring(0,3);
		prefix = tempnumber.substring(3,6);
		digits = tempnumber.substring(6,10);
		p.value = "(" + areacode + ") " + prefix + "-" + digits;
		return p.value;
	} else if ( tempnumber.length == 7) {
		areacode = '209';
		prefix = tempnumber.substring(0,3);
		digits = tempnumber.substring(3,7);
		p.value = "(" + areacode + ") " + prefix + "-" + digits;
		return p.value;
	} else {
		return false;
	}
}

// A utility function that checks and formats a string for a valid 
// Email address
function checkemail(e) {
	var at = 0;
	var dot = 0;
	var countat = 0;
	e.value = clearspaces(e);
	var len = e.value.length;
	for (var i = 0; i < len; i++ ) {
		var c = e.value.charAt(i);
	if (c == '@') at = i, countat ++;
	if (c == '.') dot = i;
	}
	if ((at == 0) || (dot == 0) || (at > dot) || (dot - at < 2) || ( len - dot < 3) || (countat > 1)){
		return (false);
	} else {
		return e.value;
	}
}


function verify(f)
{
	var msg;
	var empty_fields = "";
	var errors = "";

	// Loop through the elements of the form, looking for all 
	// text and textarea elements that don't have an "optional" property
	// defined. Then, check for fields that are empty and make a list of them.
	// Also, if any of these elements have a "min" or a "max" property defined,
	// then verify that they are numbers and that they are in the right range.
	// Put together error messages for fields that are wrong.
	for(var i = 0; i < f.elements.length; i++) {
		var e = f.elements[i];
		if ( ((e.type == "text") || (e.type == "textarea") || (e.type == "select-one") || (e.type == "checkbox") || (e.type == "password")) && (e.mandatory) ) {
			// first check if the field is empty
			if ((e.value == null) || (e.value == "") || isblank(e.value)) {
				empty_fields += "\n					" + e.title;
				 continue;
			}

			// Now check for fields that are supposed to be numeric.
			if (e.numeric || (e.min != null) || (e.max != null)) { 
				var v = parseFloat(e.value);
				if (isNaN(v) || 
				((e.min != null) && (v < e.min)) || 
				((e.max != null) && (v > e.max))) {
					errors += "- The field " + e.title + " must be a number";
					if (e.min != null) 
						errors += " that is greater than " + e.min;
					if (e.max != null && e.min != null) 
						errors += " and less than " + e.max;
					else if (e.max != null)
						errors += " that is less than " + e.max;
						errors += ".\n";
				}
			}

			// Check for valid zip codes.
			if (e.zip) {
				if (!formatzip(e)) {
					errors += "- " + e.title + ": must be a valid zip code. i.e. 95350 or 95350-1234\n";
				} else {
					e.value = formatzip(e);
				}
			}

			// Check for valid phone numbers.
			if (e.phone) {
				if (!formatphone(e)) {
					errors += "- " + e.title + ": must be a valid phone number. i.e. (209) 555-1212\n";
				} else {
					e.value = formatphone(e);
				}
			}

			// Check for valid email addresses.
			if (e.email) {
				if (!checkemail(e)) {
					errors += "- " + e.title + ": must be a valid Email address.\n";
				} else {
					e.value = checkemail(e);
				}
			}
		}
	}

	// Now, if there were any errors, display the messages, and
	// return false to prevent the form from being submitted. 
	// Otherwise return true.
	if (!empty_fields && !errors) return true;

	msg	= "______________________________________________________\n\n"
	msg += "The form was not submitted because of the following error(s).\n";
	msg += "Please correct these error(s) and re-submit.\n";
	msg += "______________________________________________________\n\n"

	if (empty_fields) {
		msg += "- The following required field(s) are empty:" 
		+ empty_fields + "\n";
		if (errors) msg += "\n";
	}
	msg += errors;
	alert(msg);
	return false;
}
