// JavaScript Document

// create object
function formValidator() {
	// set up array to hold error messages
	this.errorList = new Array;
	// set up object methods
	this.isEmpty = isEmpty; 
	this.isNumber = isNumber; 
	this.isAlphabetic = isAlphabetic; 
	this.isAlphaNumeric = isAlphaNumeric; 
	this.isWithinRange = isWithinRange; 
	this.isEmailAddress = isEmailAddress; 
	this.isChecked = isChecked; 
	this.raiseError = raiseError; 
	this.numErrors = numErrors; 
	this.displayErrors = displayErrors; 
}

/*
    * isEmpty() - check whether the specified form variable is empty;
    * isNumber() - check whether the specified form variable is a number;
    * isAlphabetic() - check whether the specified form variable contains alphabetic data;
    * isAlphaNumeric() - check whether the specified form variable contains alphanumeric data;
    * isWithinRange() - check whether the specified form variable contains a value within the specified numeric range;
    * isEmailAddress() - check whether the specified form variable contains a valid email address;
    * raiseError() - add an error message to the error stack;
    * displayErrors() - display the list of error messages as alert boxes;
    * numErrors() - return the number of error messages generated so far.
		
*/		


// check to see if input is whitespace only or empty
function isEmpty(val) {
//	if (val.match(/\s+$/) || val == "") {
//	if (val.match(/^[ \t]+|[ \t]+$/) || val == "") {
	if (val.match(/\s[^ ]\b/) || val == "") {		
		return true;
	}
	else {
		return false;
	} 
}

// check to see if input is number
function isNumber(val) {
	if (isNaN(val)) {
		return false;
	}
	else {
		return true;
	} 
}

// check to see if value is within min and max
function isWithinRange(val, min, max) {
	if (val >= min && val <= max){
		return true;
	}
	else {
		return false;
	} 
}

// check to see if form value is checked
/* Note the difference between this method, and the ones that preceded it. Previous object methods have been written to accept a form value as argument; this one accepts an object, representing the checkbox element, as argument. */
function isChecked(obj) {
	if (obj.checked) {
		return true;
	}
	else {
		return false;
	}
}

// check to see if input is alphabetic
function isAlphabetic(val) {
	if (val.match(/^[a-zA-Z]+$/)) {
	return true;
	}
	else {
	return false;
	}
}

// check to see if input is alphanumeric
function isAlphaNumeric(val) {
	if (val.match(/^[a-zA-Z0-9]+$/)) {
		return true;
	}
	else {
		return false;
	} 
}

// check to see if input is a valid email address
function isEmailAddress(val) {
	if (val.match(/^([a-zA-Z0-9])+([.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-]+)+/)) {
		/*  \b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b  */
		return true;
	}
	else {
		return false;
	} 
}

// add an error to error list
function raiseError(msg) {
	this.errorList[this.errorList.length] = msg;
}

// return number of errors in error array
function numErrors() {
	return this.errorList.length;
}

// display all errors
// iterate through error array and print each item
function displayErrors() {
	for (x=0; x<this.errorList.length; x++) {
		alert("Error: " + this.errorList[x]);
	}
}



/* Usage
----------------------------------------------*/
/*
<html>
<head>
<basefont face="Arial">
<script language="JavaScript" src="formValidator.js">
</script>
<script language="JavaScript">
// check form values
function checkForm()
{

// instantiate object
fv = new formValidator();
// perform checks
// check for empty name field
if (fv.isEmpty(document.forms[0].elements[0].value))
{
fv.raiseError("Please enter a name");
}
// check for empty age field
if (fv.isEmpty(document.forms[0].elements[1].value))
{
fv.raiseError("Please enter an age");
}
// check for valid age range
if (!fv.isWithinRange(document.forms[0].elements[1].value, 
1, 99))
{
fv.raiseError("Please enter an age in the 
range 1-99");
}
// check for empty email address
if (fv.isEmpty(document.forms[0].elements[5].value))
{
fv.raiseError("Please enter an email address");
}
// check for valid email address format
if (!fv.isEmpty(document.forms[0].elements[5].value) &&
!fv.isEmailAddress(document.forms[0].elements[5].value))
{
fv.raiseError("Please enter a valid email address");
}
// check for checkbox
if (!fv.isChecked(document.forms[0].elements[6]))
{
fv.raiseError("Please indicate your agreement with 
this site's terms and conditions");
}
// all done
// if errors, display, else proceed
if (fv.numErrors() > 0)
{
fv.displayErrors();
return false;
}
else
{
return true;
}
}
</script>
</head>
<body>
<form id="form_id" action="action.cgi" method="POST" onSubmit="return checkForm()">
<b>Name:</b>
<br>
<input type="text" name="name" size="15">
<p>
<b>Age:</b>
<br>
<input type="text" name="age" size="2" maxlength="2">
<p>
<b>Sex:</b>
<br>
<input type="Radio" name="sex" value="m" checked>Male
<input type="Radio" name="sex" value="f">Female
<p>
<b>Favourite sandwich type:
<br>
<select name="stype">
<option value="1">Thin crust
<option value="2">Thick crust
<option value="3">Toasted
</select>
<p>
<b>Email address:
<br>
<input type="text" name="email" size="25">
<p>
<input type="Checkbox" name="agree">
I agree to the terms and conditions of this site
<p>
<input type="Submit" name="submit" value="Save">
</form>
</body>
</html>
*/
