// ------------------------------------------
// Form Validation
// Based on a script written by Stephen Poley
// ------------------------------------------
	var nbsp = 160;    // non-breaking space char
	emptyString = /^\s*$/
	var proceed = 2;  
	var alertmsg  = "The form was not submitted due to missing or incorrect information.\n\n";
      alertmsg += "Please correct the error listed in red and re-submit the form.";
	var alertmsg2  = "The form was not submitted due to missing or incorrect information.\n\n";
      alertmsg2 += "Please correct the errors listed in red and re-submit the form.";
	// var node_text = 3; // DOM text node-type used for old browser check
// trim  -------------------------------------
// Trim leading/trailing whitespace off string
// -------------------------------------------
	function trim(str)
	{
		return str.replace(/^\s+|\s+$/g, '')
	};
// msg  ------------------------------------------------
// Display error message
// commonCheck routine must have previously been called
// -----------------------------------------------------
  function msg(fld,     // id of element to display message in
							 msgtype, // class to give element ("validateerror" or "validatenoerror")
							 message) // string to display
	{
		var dispmessage;
		if (emptyString.test(message)) 
			dispmessage = String.fromCharCode(nbsp);    
		else  
			dispmessage = message;
		var elem = document.getElementById(fld);
		elem.firstChild.nodeValue = dispmessage;  
		elem.className = msgtype;
	};
// commonCheck  -----------------------------
// Common code for all validation routines to
// check if empty fields are required.
// Returns true (validation passed), 
//         false (validation failed) or 
//         proceed (don't know yet)
// ------------------------------------------
  function commonCheck(vfld, // element to be validated
                       ifld, // id of element to receive error msg
											 reqd, // true if required
											 ftype) // type of field
	{
	/* Old browser checks (submits form without validation
	  if (!document.getElementById) 
	    return true;  // not available on this browser - leave validation to the server
	  var elem = document.getElementById(ifld);
	  if (!elem.firstChild)
	    return true;  // not available on this browser 
	  if (elem.firstChild.nodeType != node_text)
	    return true;  // ifld is wrong type of node  */
		if (emptyString.test(vfld.value)) {
			if (reqd) {
				if (ftype == 'select') {
					msg (ifld, "validateerror", "Please select one of the options.");
				}
				else {
					msg (ifld, "validateerror", "Please enter information for this field.");
				}
				vfld.focus();
				return false;
			}
		}
		else {
			msg (ifld, "validatenoerror", "");
		}
		return proceed;
	}
// validatePresent  ---------------------
// Validate if something has been entered
// Returns true if so 
// --------------------------------------
	function validatePresent(vfld, // element to be validated
													 ifld, // id of element to receive error msg
													 ftype) // type of field (select or blank)
	{
		var stat = commonCheck (vfld, ifld, true, ftype);
		if (stat != proceed) return stat;
		return true;
	};
// validateState  ---------------------
// Validate if something has been entered
// Returns true if so 
// --------------------------------------
	function validateState(vfld, // element to be validated
												 ifld) // id of element to receive error msg
	{
		if (document.forms.stdform.country.value=="United States" || document.forms.stdform.country.value=="Canada") {
			if (!validatePresent(vfld, ifld, "select")) { return false } else { msg ("msg_state", "validatenoerror", "") };
		}
		else {
			msg ("msg_state", "validatenoerror", "");
		}
		return true;
	};
// validateEmail  --------------------------
// Validate if e-mail address
// Returns true if so (and also if could not
// be executed because of old browser)
// -----------------------------------------
	function validateEmail  (vfld, // element to be validated
													 ifld, // id of element to receive error msg
													 reqd) // true if required
	{
		var stat = commonCheck (vfld, ifld, reqd);
		if (stat != proceed) return stat;
		var tfld = trim(vfld.value);
		var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/
		if (!email.test(tfld)) {
			msg (ifld, "validateerror", "Please enter a valid email address.");
			vfld.focus();
			return false;
		}
		else {
			msg (ifld, "validatenoerror", "");
		}
		return true;
	};
// validatePhone  --------------------------------
// Validate telephone number
// Returns true if so (and also if could not be 
// executed because of old browser).
// Permits spaces, hyphens, brackets and leading +
// -----------------------------------------------
	function validatePhone  (vfld, // element to be validated
													 ifld, // id of element to receive error msg
													 reqd) // true if required
	{
		var stat = commonCheck (vfld, ifld, reqd);
		if (stat != proceed) return stat;
		var tfld = trim(vfld.value);
		var telnr = /^\+?[0-9 ()-]+[0-9]$/
		if (!telnr.test(tfld)) {
			msg (ifld, "validateerror", "Please enter a valid telephone number.");
			vfld.focus();
			return false;
		}
		else {
			msg (ifld, "validatenoerror", "");
		}
		return true;
	};
// validateNumber  --------------------------------
// Validate number
// Returns true if so (and also if could not be 
// executed because of old browser).
// Permits commas
// -----------------------------------------------
	function validateNumber  (vfld, // element to be validated
													 ifld, // id of element to receive error msg
													 reqd) // true if required
	{
		var stat = commonCheck (vfld, ifld, reqd);
		if (stat != proceed) return stat;
		var tfld = trim(vfld.value);
		var numbr = /^[0-9,]+$/
		if (!numbr.test(tfld)) {
			msg (ifld, "validateerror", "Please enter a numeric value.");
			vfld.focus();
			return false;
		}
		else {
			msg (ifld, "validatenoerror", "");
		}
		return true;
	};
// getRadioValue  -----------------------
// Cycle through radio group to determine
// if at least one is selected
// Returns true if so
// --------------------------------------
function getGroupValues(rad) {			
	for (var i = 0; i<rad.length; i++) {				
		if (rad[i].checked) { return true; }
	}
	return false;
};
// validateGroup  ---------------------------------
// Validate option group
// Returns true if at least one option is selected.
// ------------------------------------------------
	function validateGroup  (vfld, // element to be validated
													 ifld, // id of element to receive error msg
													 gtype) // type of option group (one or blank)
	{
		if (!getGroupValues(vfld)) {
			if (gtype == 'one') {
			  msg (ifld, "validateerror", "Please select one of the options.");
			}
			else {
			  msg (ifld, "validateerror", "Please select one or more of the options.");
			}
			return false;
		}
		else {
			msg (ifld, "validatenoerror", "");
		}
		return true;
	};
