var FormValidator = function()
{
	this._errors = new Array();
	this._controls = new Array();
	this._isEmpty = function(data)
	{
		return (data == null || data == '');
	};
	this._isEmail = function(data)
	{
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		
		return filter.test(data);
	};
	this._isNumber = function(data)
	{
		return (!isNaN(data));
	};
	this._isDigitOnly = function(data)
	{
		if(this._isNumber(data))
		{
			if(data.indexOf(".") != -1)
			{
				return false;
			}
			return true;
		}
		return false;
	};
	this._isEqual = function(a, b)
	{
		return (a == b);
	};
	this._addError = function(controlId, message)
	{
		this._errors[this._errors.length] = message;
		this._controls[this._controls.length] = controlId;
	};
	this._isChecked = function(controlName, message)
	{
		var controls = document.getElementsByName(controlName);
		var total = controls.length;
		for(var i = 0; i < total; i++)
		{
			var ctl = controls[i];
			if(ctl.type == 'radio' && ctl.checked)
			{
				return true;
			}
		}
		return false;
	};
	this.checkEmpty = function(arg)
	{
		for(var controlId in arg)
		{
			var data = $U.text(controlId);
			if(this._isEmpty(data))
			{
				this._addError(controlId, arg[controlId]);
			}
		}
	};
	this.checkEmail = function(arg)
	{
		for(var controlId in arg)
		{
			var data = $U.text(controlId);
			if(!this._isEmpty(data) && !this._isEmail(data))
			{
				this._addError(controlId, arg[controlId]);
			}
		}
	};
	this.checkEqual = function(ctlToVal, ctlToComp, msg)
	{
		if(this._isEmpty(ctlToVal)) return;
		if($U.text(ctlToVal) != $U.text(ctlToComp))
		{
			this._addError(ctlToComp, msg);
		}
	};
	this.checkDigit = function(arg)
	{
		for(var controlId in arg)
		{
			var data = $U.text(controlId);
			if(!this._isEmpty(data) && !this._isDigitOnly(data))
			{
				this._addError(controlId, arg[controlId]);
			}
		}
	};
	this.checkNumber = function(arg)
	{
		for(var controlId in arg)
		{
			var data = $U.text(controlId);
			if(!this._isEmpty(data) && !this._isNumber(data))
			{
				this._addError(controlId, arg[controlId]);
			}
		}
	};
	this.checkSelected = function(arg)
	{
		for(var controlName in arg)
		{
			if(!this._isChecked(controlName))
			{
				this._addError(controlName, arg[controlName]);
			}
		}
	};
	this.isValid = function()
	{
		return (this._errors.length == 0);
	};
	this.focus = function()
	{
		if(this._controls.length > 0)
		{
			$U.focus(this._controls[0]);
		}
	};
	this.html = function()
	{
		var total = this._errors.length;
		
		if(total == 0) return '';
		
		var html = '<b>Please check the following error(s):</b>';
		
		html += '<ul>';
		
		for(var i = 0; i < total; i++)
		{
			html += '<li>' + this._errors[i] + '</li>';
		}
		
		html += '</ul>';
		
		return html;
	};
	this.text = function()
	{
		var total = this._errors.length;
		
		if(total == 0) return '';
		
		var txt = 'Please check the following error(s):' + "\n";
		
		for(var i = 0; i < total; i++)
		{
			txt += '* ' + this._errors[i];
			
			if(i < (total -1))
			{
				txt += "\n";
			}
		}
		
		return txt;
	}
}