/**
 * Form Handler Functions
 * -------------------------
 * Copyright 2008 Paul Lewis 
 * All Rights Reserved
 * -------------------------
 * 
 * @author Paul Lewis
 * @version 23/02/2008
 */

// Sample Function for what to do when there are problems
function invalidForm()
{
	alert("There were problems with your form - Please check all fields");
}

var FormValidator = new Class(
{
	activate: function(strErrClass)
	{
		// get all the input fields in the agform
		var arrInput = $$(".agform input", ".agform textarea");
		// go through each one
		for(var i = 0; i < arrInput.length; i++)
		{
			// assign the array val
			var objInput = $(arrInput[i]);
			
			// if it has an alt, use it
			if(objInput.getAttribute('alt') && objInput.getAttribute('alt') != "")
			{
				// set the error class
				objInput.strErrClass = strErrClass;
				
				// on change, remove the error class
				objInput.addEvent('focus', function()
				{
					// remove it
					this.removeClass(this.strErrClass);
					
					// clear the feedback div
					var divFeedback = $(this.getProperty('id')+"_feedback");
					
					// if we find it, empty it
					if(divFeedback)
						divFeedback.empty();
				});
				
				// when this field hasn't got focus
				// to the regular expression check
				objInput.addEvent('blur', function()
				{
					if($(this).getAttribute('alt') && $(this).getProperty('alt') != "")
					{
						var arrAlt 		= $(this).getProperty('alt').split("~");
						var strMessage 	= arrAlt[0];
						var strRegExp 	= arrAlt[1];
						
						// get the field's value
						var mxValue 		= this.getValue();
						if(this.getProperty('type') == 'file')
							mxValue = this.value;
						
						// create a regular expression and
						// check the input's value
						var regCheck 		= new RegExp(strRegExp);
						if(!regCheck.test(mxValue))
						{
							// add the error class
							this.addClass(this.strErrClass);
							
							// find the feedback div and output the error message
							var divFeedback = $(this.getProperty('id')+"_feedback");
							
							if(divFeedback)
								divFeedback.setHTML(strMessage);
						}
						else
							this.removeClass(this.strErrClass);
					}
				});
				
			}
		}
	},
	
	validate: function(domForm)
	{
		// assume everything is fine
		var bValid 	 		= true;
		
		// assign the form to a Mootooled one
		var formValidate 	= $(domForm);
		
		// if the form exists
		if(formValidate)
		{
			// get the form's ID
			var strFormID 	= formValidate.getProperty('id');
			
			// check that there's an ID
			if(strFormID)
			{
				// get all the input fields in the agform
				var arrInput = $$("#"+strFormID+" input");
				
				// go through each one
				for(var i = 0; i < arrInput.length; i++)
				{
					// assign the array val
					var objInput = arrInput[i];
					
					// if it has an alt, use it
					if(objInput.hasAttribute('alt') != "")
					{
						var arrAlt 		= objInput.getProperty('alt').split("~");
						var strMessage 	= arrAlt[0];
						var strRegExp 	= arrAlt[1];
						
						// create a regular expression and
						// use it against the value
						var regCheck 		= new RegExp(strRegExp);
						
						// get the field's value
						var mxValue 		= objInput.getValue();
						if(objInput.getProperty('type') == 'file')
							mxValue = objInput.value;
							
						if(mxValue == false || !regCheck.test(mxValue))
						{
							// it failed
							bValid = false;
							objInput.addClass(objInput.strErrClass);
							
							// find the feedback div and output the error message
							var divFeedback = $(objInput.getAttribute('id')+"_feedback");
							
							if(divFeedback)
								divFeedback.setHTML(strMessage);
						}
						else
							objInput.removeClass(objInput.strErrClass);
					}
				}
			}
		}
		else
			bValid = false;
		
		// if the form is invalid and
		// we have a callback for what to
		// do then call it
		if(!bValid && this._invalidForm)
			this._invalidForm();
		
		return bValid;
	}
});

var formValidator 	= new FormValidator();

FormValidator.implement({
	_invalidForm: invalidForm
});
	
// On DOM Ready, add functionality to the form
window.addEvent('domready', function()
{
	// activate the validator
	formValidator.activate("error");
	
	// get all the forms
	var arrForms		= $$('.agform');
	for(var i = 0; i < arrForms.length; i++)
	{
		// do it like this (not a addEvent) because we don't want multiple
		// handlers and, more importantly, this must stop the form submitting
		// which the addEvent won't do
		arrForms[i].setAttribute('onsubmit', 'return formValidator.validate(this);');
	}
});
