// create object
function formValidator() {
	// set up array to hold error messages
	this.errorList = new Array;
	// set up object methods
	this.isEmpty = isEmpty;
	this.isSame = isSame;
	this.isNumber = isNumber;
	this.isAlphabetic = isAlphabetic;
	this.isAlphaNumeric = isAlphaNumeric;
	this.isWithinRange = isWithinRange;
	this.isEmailAddress = isEmailAddress;
	this.isChecked = isChecked;
	this.isCheckedRadio = isCheckedRadio;
	this.isSelected = isSelected;
	this.raiseError = raiseError;
	this.numErrors = numErrors;
	this.displayErrors = displayErrors;
	this.findObj = findObj;
	this.isWebsiteAddress = isWebsiteAddress;
}

// check to see if input is whitespace only or empty
function isEmpty(obj) 
{
	var val;
	obj	= findObj(obj);
	val = obj.value;
	if (val.match(/^s+$/) || val == "") 
	{
		return true;
	} 
	else 
	{
		return false;
	}
}

function isSame(obj1, obj2)
{
	// define variables
	var val1;
	var val2;
	
	// get the value of the first object
	obj1 = findObj(obj1);
	val1 = obj1.value;
	
	// get the value of the second object
	obj2 = findObj(obj2);
	val2 = obj2.value;
	
	if (val1 == val2)
		return true;
	else
		return false;
}

// check to see if input is number
function isNumber(obj) {
	var val;
	obj	= findObj(obj);
	val = obj.value;
	if (isNaN(val)) {
		return false;
	} else {
		return true;
	}
}

// check to see if input is alphabetic
function isAlphabetic(obj) {
	var val;
	obj	= findObj(obj);
	val = obj.value;
	if (val.match(/^[a-zA-Z]+$/)) {
		return true;
	} else {
		return false;
	}
}

// check to see if input is alphanumeric
function isAlphaNumeric(obj) {
	var val;
	obj	= findObj(obj);
	val = obj.value;
	if (val.match(/^[a-zA-Z0-9]+$/)) {
		return true;
	} else {
		return false;
	}
}

// check to see if value is within range
function isWithinRange(obj, min, max) {
	var val;
	obj	= findObj(obj);
	val = obj.value;
	if (val >= min && val <= max) {
		return true;
	} else {
		return false;
	}
}

// check to see if input is a valid email address
function isEmailAddress(obj) {
	var val;
	obj	= findObj(obj);
	val = obj.value;
	if (val.match(/^([a-zA-Z0-9])+([.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-]+)+/)) {
		return true;
	} else {
		return false;
	}
}

function isWebsiteAddress(obj) 
{
	var val;
	obj	= findObj(obj);
	val = new String(obj.value);
	val = val.toUpperCase();
	valString = val.split(".");
	alert(valString[0]);
	if ((valString[0] == "HTTP://") || (valString[0] == "HTTP://WWW"))
	{
		return true;
	} 
	else 
	{
		return false;
	}
}


function isCheckedRadio(obj) {
	var radioChecked = false;
	obj = findObj(obj);
	for (i = 0; i < obj.length; i++) {
		if (obj[i].checked) {
			radioChecked = true;
		}
	}
	return radioChecked;
}

// check to see if form value is checked
function isChecked(obj) {
	obj = findObj(obj);
	if (obj.checked) {
		return true;
	} else {
		return false;
	}
}

// check to see if dropdown value is selected
function isSelected(obj) {
	obj = findObj(obj);
	if (obj.selectedIndex == 0) {
		return false;
	} else {
		return true;
	}
}


// display all errors
// iterate through error array and build the error list, print all errors in a single box
function displayErrors() {
	errors = '';
	for (x=0; x<this.errorList.length; x++) {
		errors = errors + ' - ' + this.errorList[x] + "\r\n";
	}
	alert("Please rectify the following:\r\n" + errors);
}

// 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;
}

// end object

function findObj(n, d) {
	var p,i,x;
	if (!d) d = document;
	if ((p = n.indexOf('?'))>0 && parent.frames.length) {
		d = parent.frames[n.substring(p + 1)].document;
		n = n.substring(0, p);
	}
	if (!(x=d[n]) && d.all) x = d.all[n];
	for (i=0;!x&&i<d.forms.length;i++) x = d.forms[i][n];
	for (i=0;!x&&d.layers&&i<d.layers.length;i++) x = findObj(n, d.layers[i].document);
	if (!x && d.getElementById) x = d.getElementById(n);
	return x;
}
