if (!validator)
{
	var validator = 
	{
		items: new Array(),

		//Check each element of given form against functions we have registered
		validate: function(form) 
		{
			var errors = Array();
			var foundErrors = {};
			for(var i = 0; i < this.items.length; i++)
			{
				var element = this.items[i].element;
				var func = this.items[i].func;
				var label = this.items[i].label;
				var id = this.items[i].id;

				if (element && element.form == form)
				{
					var error = func(element.value);
					var errorElem = document.getElementById(id + 'Errors');
					if (errorElem && !foundErrors[id]) errorElem.innerHTML = '';
						
					if (error)
					{
						errors.push('The field "' + label + '" is invalid: ' + error);
						if (errorElem) errorElem.innerHTML += label + ' ' +error;
						foundErrors[id] = 1;
						if (!element.style.border)
						{
							element.className = 'fieldError';
						}
					}					
					else if (!foundErrors[id])
					{
						element.className = '';
					}
				}
			}

			if (errors.length)
			{
				alert('There were errors with your submission. Please correct these and try again');
				return false;
			}
			return true;
		},

		//Register a function to check when form is submitted
		register: function(label, id, func) 
		{
			this.items.push({
					id: id, 
					label: label, 
					element: null,
					func: func});
		},

		//Push each item from the queue onto out list of real elements
		init: function()
		{
			for(var i = 0; i < this.items.length; i++)
			{
				this.items[i].element = document.getElementById(this.items[i].id);
			}
		},

		//Helper
		checkText: function(value, min, max)
		{
			var length = value.length;
			if (min != null && length < min) 
			{
				if (min <= 1)
					return "is required.";
				else 
					return "must contain at least " + min + " characters.";
			}
			if (max != null && length > max) 
			{
				return "must not contain more than " + max + " characters.";
			}
		},

		checkEmail: function(value)
		{
			if (!value.match(/.+?@.+?\..+/))
			{
				return 'is not a valid email address.';
			}
		}
	}
	window.onload = function(){validator.init();};
}