var FormTools = new Object();
Object.extend(FormTools, {
	
	init: function()
	{
		this.Rules = new Array();
	},
	
	ApplyRule: function(type, obj, event)
	{
		this['Rules'+type].apply(obj);
		
		// If given an event, attach it
		if(event != undefined)
		{
			obj.Execute = FormTools.Execute;
			obj.errId = obj.id+'_error';
			//addEvent($('contact_form'), 'submit', obj.Validate);
			addEvent(obj, event, obj.Execute);
		}
	},
	
	Check: function(frm)
	{
		var theState = this.Validate();
		return theState;
	},
	
	ClearError: function(elErr)
	{
		if($(elErr))
		{
			var err = $(elErr);
			err._parent.className = err._parent.className.substring(0, err._parent.className.indexOf(' input_autoerror'));
			err._parent = null;
			err.parentNode.removeChild(err);
		}
	},
	
	DisplayError: function(obj, error)
	{
		// Create element
		if(!$(obj.id+'_error'))
		{
			// Error element
			var objErr = document.createElement('div');
			objErr.setAttribute('id', obj.id+"_error");
			objErr.setAttribute('class', 'form_autoerror');
			
			// Title element
			var title = document.createElement('h3');
			title.appendChild(document.createTextNode('Error'));
			objErr.appendChild(title);
			
			// Error message
			objErr.appendChild(document.createTextNode(error));
			
			// Insert message
			obj.parentNode.insertBefore(objErr, obj.nextSibling);
			objErr._parent = obj;
			
			// Highlight parent
			obj.className = obj.className + ' input_autoerror';
		}
	},
	
	Execute: function()
	{
		var thePass = true;
		
		if(this.value.length < 1)
		{
			FormTools.DisplayError(this, "This is a required field");
			thePass = false
		}
		else if(this.value.match(this.regex) == this.value)
		{
			FormTools.ClearError(this.errId);
		}
		else
		{
			//FormTools.DisplayError(this, "Value found: "+ this.value +" ... "+ this.msgError);
			FormTools.DisplayError(this, this.msgError);
			thePass = false;
		}
		
		return thePass;
	},
	
	RulesCheckbox: function()
	{
	},
	
	RulesEmail: function()
	{
		//this.regex = /[a-z0-9\@\.\_\-]+/ig;
		this.regex = /^[\w\._\+-]+@[\w\.\-]+$/ig;
		this.msgError = 'Please enter a real email address, i.e. myname@youremail.com';
	},
	
	RulesEmpty: function()
	{
		this.regex = /^.*?$/gi;
		this.msgError = 'This field is required'
	},
	
	RulesName: function()
	{
		this.regex = /[a-z\.\-\s]+/ig;
		this.msgError = 'Please enter only your name'
	},
	
	RulesPhone: function()
	{
		this.regex = /[\d -\.\(\)\+]{10,}/g;
		this.msgError = 'Please enter your phone number, i.e. (123) 456-7890 x123';
	},
	
	RulesTextarea: function()
	{
		this.regex = /[\w\d\s\!\@\#\$\%\^\&\*\(\)\_\+\=\-\:\;\{\}\]\[\,\.\/\?\\\'\"]+/igm;
		this.msgError = 'You have entered an invalid character. This is usually < or >';
	},
	
	SetValidation: function(type, obj, event)
	{
		this.Rules.push(obj);
		
		this.ApplyRule(type, obj, event);
	},
	
	Validate: function()
	{
		var thePass = 0;
		for(var i=0; (obj = this.Rules[i]); i++)
		{
			var theState = obj.Execute();
			if(!theState)
			{
				thePass++;
			}
		}
		
		if(thePass > 0)
		{
			return false;
		}
		
		return true;
	}
});
FormTools.init();


/*
function ApplyRules()
{
	// Apply rules to contact form
	var frm = $('contact_form');
	FormTools.SetValidation('Name', frm.frmName, 'blur');
	FormTools.SetValidation('Phone', frm.frmPhone, 'blur');
	FormTools.SetValidation('Email', frm.frmEmail, 'blur');
	FormTools.SetValidation('Textarea', frm.frmQuestion, 'blur');
	//document.getElementById('frmPhone').focus();
}
addEvent(window, "load", ApplyRules);
*/