/*
	Class: FormValidator
	A form class that encapsulates common validation functions and an error stack.

	Example Usage:
	(start code)
	<script type='text/javascript' src='FormValidator.class.js'></script>
	<script type='text/javascript'>
	function validateForm()
	{
		//initialize the validator object
		var validator = new FormValidator();

		//run validation rules against the form field values
		validator.notEmpty(document.getElementById('name').value, 'Please enter your name.');

		//check wether any of the validation calls failed
		if(validator.isValid()) {
			return true;
		}

		//give feedback to the user
		validator.alertErrors();
		return false;
	}
	</script>
	(end)

*/
function FormValidator()
{
	this.is_valid = true;
	this.errors = Array();

	this.alpha_regex =  /^[a-zA-Z]+$/;
	this.alpha_numeric_regex = /^[a-zA-Z0-9]+$/;
	this.email_regex = /(\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b)/i;
}

/*
	Function: raiseError
		Add an error to the internal errors stack. Meant to
		provide a means of invalidating the object from
		the outside, and providing a standard interface for
		member methods.
	Parameters:
		msg - The error message to add to the error stack.
	Returns:
		void
*/
FormValidator.prototype.raiseError = function(msg)
{
	this.is_valid = false;
	this.errors[this.errors.length] = msg;
}

/*
	Function: isValid
		Returns the internal marker for weather or 
		not any of the called validation functions 
		have failed.
*/
FormValidator.prototype.isValid = function()
{
	return this.is_valid;
}

FormValidator.prototype.countErrors = function()
{
	return this.errors.length;
}

FormValidator.prototype.getErrors = function()
{
	return this.errors;
}

FormValidator.prototype.alertErrors = function()
{
	var msg = 'Please correct the following errors:\n';
	for(var i=0;i<this.errors.length;i++) {
		msg += this.errors[i] + '\n';	
	}
	alert(msg);
}

FormValidator.prototype.notEmpty = function(value, msg)
{
	if(value.length == 0) {
		this.raiseError(msg);
		return false;
	}

	return true;
}

FormValidator.prototype.notEquals = function(value, not_value, msg)
{
	if(value == not_value) {
		this.raiseError(msg);
		return false;
	}
	return true;
}

FormValidator.prototype.isNumeric = function(value, msg)
{
	if(isNaN(val)) {
		this.raiseError(msg);
		return false;
	}
	return true;
}

FormValidator.prototype.isWithinNumericRange = function(value, min, max, msg)
{
	if(val < min || val > max) {
		this.raiseError(msg);
		return false;
	}
	return true;
}

FormValidator.prototype.isAlpha = function(value, msg)
{
	return this.matchRegex(value, this.alpha_regex, msg);
}

FormValidator.prototype.isAlphaNumeric = function(value, msg)
{
	return this.matchRegex(value, this.alpha_numeric_regex, msg);
}

FormValidator.prototype.isEmail = function(value, msg)
{
	return this.matchRegex(value, this.email_regex, msg);
}

FormValidator.prototype.matchRegex = function(value, regex, msg)
{
	if(typeof regex != 'Object') {
		regex = new RegExp(regex);
	}
	
	if(value.search(regex) == -1) {
		this.raiseError(msg);
		return false;
	}
	return true;
}

FormValidator.prototype.isChecked = function(field, msg)
{
	for(var i=field.length-1;i>-1;i--) {
		if(field[i].checked) {
			return true;
		}
	}

	this.raiseError(msg);
	return false;
}
