function FormValidator(form)
{
	this.form = form;
	this.params = new Array();
	
	this.addElement = function (name, type, msg, optional)
	{
		var obj = new Object();
			obj.name = name;
			obj.type = type;
			obj.msg = msg;
			obj.optional = (optional == true) ? true : false;
			
		this.params[this.params.length] = obj;
	}
	
	this.getElementValue = function (element)
	{
		switch (element.type)
		{
			case 'select-one':
				return element.options[element.selectedIndex].value;
				break;
				
			default:
				return element.value;
				break;
		}
	}
	
	this.stripWhitespace = function (text)
	{
		var regExp = new RegExp(' ', "g", "i");
		if (text) return text.replace(regExp, "");
	}
	
	this.replaceChar = function (element, searchChar, replaceChar)
	{
		if (element && searchChar && this.form[element])
		{
			if (!replaceChar) replaceChar = '';
			var text = this.form[element].value;
			var regExp = new RegExp(searchChar, "g", "i");
			
			this.form[element].value = text.replace(regExp, replaceChar);
		}
	}
	
	this.validateString = function(value)
	{
		if (!value || value == '') return false;
		var val = value.toString();
		return (val != '' && val.length > 0 && val != 'null' && val != 'undefined');
	}
	
	this.validateInt = function(value)
	{
		for (var i = 0; i < value.length; i++)
		{
			if (isNaN(value.charAt(i)) || value.charAt(i) == " ") return false;
		}
		return true;
	}
	
	this.validateEmail = function(value)
	{
		var email = this.stripWhitespace(value);
		
		if (!this.validateString(email)) return false;
		
		var at = email.indexOf("@");
		var dot = email.lastIndexOf(".");
		
		if (at < 1 || dot < 3 || dot == email.length -1 || (dot - at) < 2)
		{
			return false;
		}
		else
		{
			var invalidChars = '!#$%^*()+{}[]|/:;"\'\\>?<,';
			
			for (var i = 0; i < email.length; i++)
			{
				if (invalidChars.indexOf(email.charAt(i)) != -1)
				{
					return false;
				}
			}
		}
		return true;
	}
	
	this.validateMatch = function (name)
	{
		var arr = name.split(',');
		var n = arr[0];
		var val = this.getElementValue(this.form[n]);
		
		for (var i = 0; i < arr.length; i++)
		{
			var n = arr[i];
			var value = this.getElementValue(this.form[n]);
			
			if (value != val) return false;
		}
		return true;
	}
	
	this.validateLength = function (value, len)
	{
		return value.toString().length >= len;
	}
	
	this.getElement = function (name)
	{
		var e = name.split(',');
		var n = e[0];
		return this.form[n];
	}
	
	this.validateElement = function (name, type, optional)
	{
		var valid = true;
		var element = this.getElement(name);
		var value = this.getElementValue(element);
		var t = type.split(',');
		var type = this.stripWhitespace(t[0]);
		var len = Math.floor(this.stripWhitespace(t[1]));
		
		if (optional == true && !this.validateString(value)) return true;
		
		switch (type)
		{
			case 'string':
				valid = this.validateString(value);
				break;
				
			case 'int':
				valid = this.validateInt(value);
				break;
				
			case 'email':
				valid = this.validateEmail(value);
				break;
				
			case 'match':
				valid = this.validateMatch(name);
				break;
				
			default:
				valid = this.validateString(value);
				break;
		}
		
		if (!isNaN(len)) valid = this.validateLength(value, len);
				
		return valid;
	}
	
	this.validateForm = function ()
	{
		for (var i = 0; i < this.params.length; i++)
		{
			var obj = this.params[i];
			
			if (!this.validateElement(obj.name, obj.type, obj.optional))
			{
				var element = this.getElement(obj.name);
				element.focus();
				alert(obj.msg);
				return false;
			}
		}
		
		return true;
	}
}