/*
 WILLGOODWIN.Form
 
 Generalised form validation code, which scans for particular classes that define restrictions on
 fields, and flags problems to the user with messages and visual cues.
 
 Errors are marked only with additional classes; it is up to the site/application to define how
 those errors look.
 
 REQUIRES: WILLGOODWIN.dom
 */
try 
{
	var test = (WILLGOODWIN.name == 'WILLGOODWIN');
} 
catch (e) 
{
	WILLGOODWIN = {};
}

WILLGOODWIN.Form = {};

WILLGOODWIN.Form = function()
{
	var defaults = {
		restrictions: {
		
			REQUIRED: {
				definingClass: 'njh-field-required',
				errorClass: 'njh-field-error',
				errorMessage: 'Please complete all required fields.',
				test: function(value)
				{
					return value !== '';
				}
			},
			
			EMAIL: {
				definingClass: 'njh-field-email',
				errorClass: 'njh-field-error',
				errorMessage: 'Please check your email address.',
				test: function(value)
				{
					var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
					return filter.test(value);
				}
			}
		
		},
		reportMultipleErrors: false
	};
	
	
	function validateForm(form, options)
	{
		var settings, key, restriction, e, elen, i, index, value, passed, errors;
		
		/*
		 Extend a local options object with the defaults, and then the provided options
		 */
		settings = defaults;
		errors = [];
		
		// clear any errors from a previous run
		for (key in settings.restrictions) 
		{
			restriction = settings.restrictions[key];
			e = WILLGOODWIN.dom.getElementsByClassName(restriction.errorClass, form);
			elen = e.length;
			
			for (i = 0; i < elen; i++) 
			{
				WILLGOODWIN.dom.removeClass(e[i], restriction.errorClass);
			}
		}

		// check each restriction in turn
		for (key in settings.restrictions) 
		{
			restriction = settings.restrictions[key];
			e = WILLGOODWIN.dom.getElementsByClassName(restriction.definingClass, form);
			elen = e.length;
			
			for (i = 0; i < elen; i++) 
			{
				// different tags require different methods of acquiring value
				switch (e[i].tagName)
				{
					case 'select':
						index = e[i].selectedIndex;
						value = e[i][index].value;
						break;
					default:
						value = e[i].value;
						break;
				}
				
				// test the value against the current restriction
				passed = restriction.test(value);
				
				if (!passed) 
				{
					// add the error class to the element
					WILLGOODWIN.dom.addClass(e[i], restriction.errorClass);
					
					// alert immediately if not allowed multiple error reporting
					if (!settings.reportMultipleErrors) 
					{
						e[i].focus();
						alert(restriction.errorMessage);
						return false;
					}
					else 
					{
						// push the error and the element onto a list
						errors.push({
							element: e[i],
							message: restriction.errorMessage
						});
					}
				}
			}
		}
		
		// check for queued errors, and if none then return true
		if (errors.length > 0) 
		{
			// handle the error list
			
			return false;
		}
		else 
		{
			return true;
		}
	}
	
	return {
		name: 'WILLGOODWIN.Form',
		description: 'Form validation module.',
		defaults: defaults,
		validateForm: validateForm
	};
}
();



WILLGOODWIN.Form.forceNumbersOnly = function(input_field)
{
	//This function forces an input field to accept only numbers.
	c = input_field;
	c.onkeyup = function()
	{
		var re = new RegExp(/[^\d]/);
		this.value = this.value.replace(re, "");
	};
};
