

var Validator = function ()
{
	var clear_error = '&nbsp;';
	var has_error = '<br />';
	var that = this;
	
	this.Validate = function (rules)
	{
		var valid = true;
		
		for(rule in rules)
		{
			var item = rules[rule];
			if(item.input != null)
			{
				var error_msg = has_error + item.error_msg;
				
				var elem = $(item.input);
				var errorbox = $(item.error_box);
				
				errorbox.update(clear_error);
				
				// Is this optional and blank?
				if(elem.value.length == 0 && item.optional)
				{
					valid = valid && true;
				}
				else
				{
					var func = item.rule;
					var args = {'elem':elem,'match':item.match};
					if(!func(args))
					{
						errorbox.update(error_msg);
						valid = false;
					}
				}
			}
		}
		
		return valid;
	};
	
	this.Email = function (args)
	{
		var email = args.elem.value;
		return email.search(/^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/) == -1 ? false : true;
	};
	
	this.Phone = function (args)
	{
		var phone = args.elem.value;
		return (phone.search(/\d{3}\-\d{3}\-\d{4}/) == -1 || phone.length != 12) ? false : true;
	};
	
	this.IPAddress = function (ip)
	{
		return true;
	};
	
	this.NonEmpty = function (args)
	{
		var value = args.elem.value
		return value.length > 0;
	};
	
	this.Numeric = function (args)
	{
		return args.elem.value.length != 0 && !isNaN(args.elem.value);
	};
	
	this.Password = function (args)
	{
		return args.elem.value == $(args.match).value;
	}
	
	this.CheckBox = function (args)
	{
		return args.elem.checked == args.match;
	};
	
	this.ISBN = function (args)
	{
		var value = args.elem.value;
		return value.length == 10 || value.length == 13;
	};
	
	this.CourseCode = function (args)
	{
		var value = args.elem.value;
		return value.length == 4 && !isNaN(value);
	};
};
