<!--
/******************************************************************************

FUNCTION:		checkEmail

ARGUMENTS:		str - String

DESCRIPTION:	This function will evaluate the string being passed in and
				determine if it is a valid e-mail address.
				
			
SYNTAX:			var str = "name@domain.com";
				if (!(checkEmail(str))) {
					// show error message
				}

AUTHOR:			Jim Whyte

DATE:			10 January 2001

******************************************************************************/
function checkEmail (str) {
	var a = str.indexOf("@");
	var p = str.lastIndexOf(".");
	var s = str.indexOf(" ");
	var l = str.length - 1;
	
	if (a < 1) return false;		// @ cannot be in first position
	if (p <= a + 1) return false;	// must be atleast one valid character btwn @ and .
	if (p == l) return false;		// must be atleast one valid char after .
	if (s != -1) return false;		// no empty spaces permitted
	return true;
}

/******************************************************************************

FUNCTION:		isEmpty

ARGUMENTS:		str - String

DESCRIPTION:	This function will determine if the string being received is
				blank or contains only whitespace (spaces, tabs, newline).  If
				this is the case, the function returns true, otherwise, it
				returns false.

SYNTAX:			var strText = " ";
				if (isEmpty(strText)) {
					// execute statement
				}

AUTHOR:			Jim Whyte

DATE:			10 January 2001

******************************************************************************/
function isEmpty (str) {
	if ((str != null) && (str != "")) {
		for (var i = 0; i < str.length; i++) {
			var temp = str.charAt(i);
			if ((temp != " ") && (temp != "\t") && (temp != "\n")) return false;
		}
	}
	return true;
}

/******************************************************************************

FUNCTION:		showError

ARGUMENTS:		obj - Form element
				str - Error message to display

DESCRIPTION:	This function will display an error message to the user and
				highlight/select the element in error.

SYNTAX:			return showError(form1.firstName, "Error!");

AUTHOR:			Jim Whyte

DATE:			10 January 2001

******************************************************************************/
function showError (obj, str) {
	if (obj.label != null) {
		str = obj.label + " " + str;
	} else {
		str = "This field " + str;
	}
	alert(str);
	if (obj.type != "select-one"){
		obj.select();
		obj.focus();
	}
	return false;
}

// ******************************************************
// function check(input)
// This function will accept 0-9, dots and commas ie 9,000,000.00
// ******************************************************

function check(input) {
  var ok = true;

  for (var i = 0; i < input.length; i++) {
    var chr = input.charAt(i);
    var result = false;
	if ((chr >= '0' && chr <= '9') ||
		chr == ',' || chr == '.')
      result = true;
    
    if (!result) ok = false;
  }
 
  return ok;
}
/******************************************************************************

FUNCTION:		verify

ARGUMENTS:		f - Form object

DESCRIPTION:	Upon submit, this function will iterate through each element in
				the form.  Validation check functions are grouped and executed
				by element type and then by custom element properties.
				
				Custom element properties are declared before calling this form
				in the onSubmit event.  If no custom properties are set, they
				will inherit default values that will cause them to behave as
				regular form elements, or 'be ignored' by the validation
				script.
				
				Any form element value that does not comply with the custom
				properties set for that element will generate an error message.
				This message will be displayed to the user, and the form
				element in error will be highlighted and/or selected.  The user
				will then have to resubmit the form in order to continue.  This
				process will repeat until the entire form has been successfully
				evaluated.
				
				Below is an explanation of the custom properties currently
				available for each form element:
				
				Element		Property		Description
				-------		--------		--------------------------------
				button		N/A				N/A
				
				checkbox	N/A				N/A
				
				file		isRequired		Setting this property to 'true'
											will not allow the user to leave
											the field blank.
				
				password	isRequired		Setting this property to 'true'
											will not allow the user to leave
											the field blank.
				
							minLength		Setting this property to a number
											'n' will not allow the user to
											enter less than 'n' number of
											characters.
							
				radio		N/A				N/A
				
				reset		N/A				N/A
				
				select-one	N/A				N/A
				
				select-many	N/A				N/A
				
				submit		N/A				N/A
				
				text		isDate			Setting this property to 'true'
											will require the user to enter a
											valid date string into the field.
							
							isEmail			Setting this property to 'true'
											will require the user to enter a
											valid e-mail address into the
											field.
							
							isNumeric		Setting this property to 'true'
											will require the user to enter a
											number into the field.
							
							isRequired		Setting this property to 'true'
											will not allow the user to leave
											the field blank.
							
							maxValue*		Setting this property to a number
											'n' will not allow the user to
											enter a numeric value greater than
											'n'.
							
							minLength		Setting this property to a number
											'n' will not allow the user to
											enter less than 'n' number of
											characters.
							
							minValue*		Setting this property to a number
											'n' will not allow the user to
											enter a numeric value less than
											'n'.
											
							* minValue and maxValue will only work if isNumeric
							is set to 'true'.		
						
				NOTE: If you want all form elements to be required, simply set
				the form.all.isRequired property to true.  This way you won't
				have to set the isRequired attribute for all form elements
				individually.

AUTHOR:			Jim Whyte

DATE:			10 January 2001

******************************************************************************/
function verify(f) {
	for (var i = 0; i < f.length; i++) {
		// create a variable containing the form element object
		var e = f.elements[i]
		// check for element type and then perform the necessary checks for
		// that type
		// also, check to see if the form.all.isRequired property
		// has been set
		switch (e.type) {
			case "button" :
				// don't do anything
				break;
			case "checkbox" :
				// don't do anything
				break;
			case "file" :
				// check if the element is empty
				if (isEmpty(e.value)) {
					// if the field is required, show error message and select field
					if (e.isRequired) {
						return showError(e, "is a required field.");
					}
				}
				break;
			case "password" :
				// check if the element is empty
				if (isEmpty(e.value)) {
					// if the field is required, show error message and select field
					if (e.isRequired) {
						return showError(e, "is a required field.");
					}
				}
				
				// if the text box is required, or not empty, then check for
				// custom field values...
				if ((e.isRequired) || !(isEmpty(e.value))) {
					// check for a minimum field length property
					if (e.minLength != null) {
						if (e.value.length < e.minLength) {
							return showError(e, "contains less than " + e.minLength + " numeric values.");
						}
					}
				}
				break;
			case "radio" :
				// don't do anything
				break;
			case "reset" :
				// don't do anything
				break;
			case "select-one" :
				if (e.isRequired) {
					//Check the selected index to see if it is empty
					if ((e.options[e.selectedIndex].value == "") && (e.options[e.selectedIndex].text == ""))
						return showError(e, "is a required field.");
				}
				// don't do anything
				break;
			case "select-many" :
				// don't do anything
				break;
			case "submit" :
				// don't do anything
				break;
			case "text" :
				// check if the element is empty
				if (isEmpty(e.value)) {
					// if the field is required, show error message and select field
					if (e.isRequired) {
						return showError(e, "is a required field.");
					}
				}
				
				// if the text box is required, or not empty, then check for
				// custom field values...
				if ((e.isRequired) || !(isEmpty(e.value))) {
					// check for a minimum field length property
					if (e.minLength != null) {
						if (e.value.length < e.minLength) {
							return showError(e, "contains less than " + e.minLength + " numeric values.");
						}
					}
					
					// check if the field should be a date
					if (e.isDate) {
						if (!(checkDate(e.value))) {
							return showError(e, "contains an invalid date.");
						}
					}
					
					// check if the field should be an e-mail address
					if (e.isEmail) {
						if (!(checkEmail(e.value))) {
							return showError(e, "contains an invalid e-mail address.");
						}
					}
					
					// will accept only 0-9, dots and commas
					if (e.isNumber) {
						if (!(check(e.value))) {
						return showError(e, "contains invalid number.");
						}
					}	
					// check if the field should be numeric.  Not accept dots and commas
					if (e.isNumeric) {
						// check if the field value is not a number
						if (isNaN(e.value)) {
							return showError(e, "can only contain a numeric value.");
						}
						// check if the field has a mimum value limit
						if (e.minValue != null) {
							if (parseFloat(e.value) < parseFloat(e.minValue)) {
								return showError(e, "must be greater than " + e.minValue + ".");
							}
						}
						// check if the field has a maximum value limit
						if (e.maxValue != null) {
							if (parseFloat(e.value) > parseFloat(e.maxValue)) {
								return showError(e, "is greater than " + e.maxValue + ".");
							}
						}
					}
				}
				break;
			case "textarea" :
				// check if the element is empty
				if (isEmpty(e.value)) {
					// if the field is required, show error message and select field
					if (e.isRequired) {
						return showError(e, "is a required field.");
					}
				}
				
				// if the field is required or not empty...
				if ((e.isRequired) || !(isEmpty(e.value))) {
					// check for a minimum field length property
					if (e.minLength != null) {
						if (e.value.length < e.minLength) {
							return showError(e, "This field contains less than " + e.minLength + " numeric values.");
						}
					}
										
					// check for a maximum field length property
					if (e.maxLength != null) {
						// check to see if it is too long
						if (e.value.length > e.maxLength) {
							// the user should be given the option to edit the field
							// or have the extra characters removed
							eMsg = "contains more than " + e.maxLength + " characters.\n\n"
							eMsg += "Click OK to truncate or CANCEL to edit, then resubmit."
							// if there is a label for this field, display it in the message
							if (e.label != null) {
								eMsg = e.label + ": " + eMsg;
							}
							// if the user clicked OK, then truncate the string value
							if (confirm(eMsg)) {
								e.value = e.value.substr(0, e.maxLength);
							}
							e.select();
							e.focus();
							return false;
						}
					}
				}
				break;
		} // end switch
	}
} // end verify

/******************************************************************************

FUNCTION:		trim

ARGUMENTS:		str - String

DESCRIPTION:	This function will trim the leading and trailing spaces off of
				the string passed to it.

SYNTAX:			newstring = trim(oldstring);

AUTHOR:			Jim Whyte

DATE:			10 January 2001

******************************************************************************/
function trim(str)
{
	var newstr;
	var intlen;
	var s
	newstr	= "";
	intlen	= str.length;
	for (i = 0; i <= intlen; i++) 
	{      
		s = str.substr(i,1);
		if (s != " ") 
		{
			beginpos = i;
			break;
		}
	}
	for (i = intlen; i >= 0; i--) 
	{      
		s = str.substr((i-1),1);
		if (s != " ") 
		{
			endpos = i;
			break;
		}
	}
	newstr = str.substr(beginpos,endpos);
	return newstr;
}
//-->

