/*
 Form Validation 1.03
 Copyright (C) 2001  e.magination network, llc.  All Rights Reserved.
   Feel free to reuse or modify this code,
   provided this header remains in tact.
   http://www.emagination.com
   jibba jabba strikes again.

 For more information on regular expressions in JavaScript, see:
 http://developer.netscape.com/viewsource/angus_strings.html
 
 Netscape JavaScript Resources:
 http://home.netscape.com/eng/mozilla/3.0/handbook/javascript/
 http://developer.netscape.com/library/documentation/communicator/jsref/index.htm
 
 v1.03 03/16/2001
   function isInvalidEmail
   * invalidates multiple @ symbols in value
   * invalidates consecutive periods in account field

 v1.02 02/27/2001
   new function
   * isNotNumber

 v1.01 02/19/2001
   new function
   * isInvalidZipCode
  
 v1.00 02/19/2001
   charter functions
   * elementIsEmpty
   * isInvalidEmail
   * isNotInteger
   * isInvalidPhone
   * isInvalidDate 

*/

function FormValidation(bullet, header) {
	this.bullet = (bullet) ? bullet : '  >>  ';
	this.header = (header) ? header : 'Your form has not been submitted for the following reasons:';
	this.firstElement = null;
	this.errors = new Array();
	this.addError = addError;
	this.alertErrors = alertErrors;
	this.focus = focusOnFirstElement;
	this.isValid = isValid;
}//FormValidation

function elementIsEmpty(objField) {
	objCurrentFormElement = objField;//for adding errors
	switch (objField.type) {
		case "text":
		case "textarea":
			return textElementIsEmpty(objField);
			break;
		case "select-one":
		case "select-multiple":
			return selectElementIsEmpty(objField);
			break;
		case "radio":    //single radio button
		case "checkbox": //single checkbox button
			return buttonElementIsEmpty(objField);
			break;
		default: //check for multiple buttons
			if ( (objField[0].type == "radio") || 
			     (objField[0].type == "checkbox") ) {
				return buttonElementIsEmpty(objField);
			}//if
	}//switch
}//elementIsEmpty

//regular expression, want to replace spaces with nothing.
function textElementIsEmpty(objField) {
	return ( (objField.value.match(/^\s*$/)) ? true : false );
}//textElementIsEmpty

function selectElementIsEmpty(objField) {
	return ( ( objField.selectedIndex == -1 ) ||
	     ( objField.options[objField.selectedIndex].value.replace(/^\s*$/, "").length == 0 ) ) 
}//selectElementIsEmpty

function buttonElementIsEmpty(objField) {
	var intChecked = 0; //count for number of checks
	if (objField.length) {//multiple buttons, objField.length is defined
		for ( var i=0; i < objField.length; i++ ) {
			if (objField[i].checked) return false;
		}//for
	}//if
	else { //single button, objField.length is undefined
		if (objField.checked) return false;
	}//else
	return true;
}//buttonElementIsEmpty




//email check
/*
  leading/trailing whitespace allowed
  primary domain is two alphabetical letters or com/net/edu/mil/gov/org
  remaining domains consist of alphanumeric characters/hyphens/periods
  cannot have double periods ".."
  cannot have "@."
  cannot have multiple "@"
 */
function isInvalidEmail(objField) {
	objCurrentFormElement = objField;//for adding errors
	var IsValidEmail = (objField.value.match(/^\s*(\S+@)[A-Za-z0-9\-^\.]+((\.com)|(\.edu)|(\.gov)|(\.mil)|(\.net)|(\.org)|(\.[A-Za-z]{2}))\s*$/gi));
	//check for double periods
	if (IsValidEmail)
		IsValidEmail = (!(objField.value.match(/\.{2}/)));
	//check for "@."
	if (IsValidEmail)
		IsValidEmail = (objField.value.match(/@[^\.]/));
	//check for "@.*@" (multiple @ symbols)
	if (IsValidEmail)
		IsValidEmail = (!(objField.value.match(/@.*@/)));
	return (!IsValidEmail);
}//isInvalidEmail


//integer check
/*
  leading/trailing whitespace allowed
 */
function isNotInteger(objField) {
	objCurrentFormElement = objField;//for adding errors
	var IsInteger = (objField.value.match(/^\s*\d*\s*$/g));
	return (!IsInteger);
}//isNotInteger


//number check (allow for decimals)
/*
  leading/trailing whitespace allowed
 */
function isNotNumber(objField) {
	objCurrentFormElement = objField;//for adding errors
	var IsNumber = (isNaN(objField.value));
	return (!IsNumber);
}//isNotNumber


//phone check (10 digit, no extensions)
/*
  leading/trailing whitespace allowed
  parentheses allowed surrounding area code
  space/hyphen/slash/period allowed after area code and after next three digits
 */
function isInvalidPhone(objField) {
	objCurrentFormElement = objField;//for adding errors
	var IsPhone = ( (objField.value.match(/^\s*\d{3}([ \-\/\.])?\d{3}([ \-\/\.])?\d{4}\s*$/g)) ||
	                (objField.value.match(/^\s*\(\d{3}\)([ \-\/\.])?\d{3}([ \-\/\.])?\d{4}\s*$/g)) );
	return (!IsPhone);
}//isInvalidPhone


//date check
/*
  test to see if getDate returns NaN for the date from value.
  NaN returned means value is not a valid date according to JavaScript.
  illegal dates such as January 32 are considered legal and
  adjust accordingly. (In this case, it would be February 1.)
 */
function isInvalidDate(objField) {
	objCurrentFormElement = objField;//for adding errors
	var objDate = new Date(objField.value);
	return (isNaN(objDate.getDate()));
}//isInvalidPhone

//zip code check
/*
  leading/trailing whitespace allowed
  checks for 5- or 9- digit zip codes
  for 9 digit, only space/hyphen is allowed between the 5- and 4-digit numbers
 */
function isInvalidZipCode(objField) {
	objCurrentFormElement = objField;//for adding errors
	var IsZipCode = ( (objField.value.match(/^\s*\d{5}\s*$/g)) ||
	                  (objField.value.match(/^\s*\d{5}[ \-]\d{4}\s*$/g)) );
	return (!IsZipCode);
}//isInvalidZipCode




//methods for FormValidation object class

function focusOnFirstElement() {
	//move browser to first error field
	with (this) {
		//focus
		if (firstElement && firstElement.focus) firstElement.focus();
		//if multiple buttons, focus on first option
		if ( (firstElement[0]) && (firstElement[0].focus) ) {
			firstElement[0].focus();
		}
		//select
		if (firstElement && firstElement.select) firstElement.select();
		//move to absolute left
		window.scrollBy(-screen.availWidth, 0);
	}//with
}//focusOnFirstElement

function isValid() {
	return (this.errors.length == 0);
}//isValid

function alertErrors() {
	with (this) {
		if (!isValid()) {
			var mesg = header + '\n';
			for (var i=0; i<errors.length; i++) {
				mesg += bullet + errors[i] + '\n';
			}//for
			alert(mesg);
			focus();
		}//if
	}//with
}//alertErrors

function addError(errorMessage, objElementAlt) {
	with (this) {
		errors[errors.length] = (errorMessage) ? errorMessage : objCurrentFormElement.name;
		if (firstElement == null) 
			firstElement = (objElementAlt) ? objElementAlt : objCurrentFormElement;
	}//with
}//addError

