/**
 * Javascript form validation framework
 * @author Chris Keeley <chris@dnaadvertising.co.uk>
 * @version 0.1
 * @date 28 Feb 2008
 */
 

/**
 * This object is the interface to the directory listing and defines lookup and insert methods.
 */
var directory = {
	/**
	 * Check to see if the form exists in the directory
	 * @param {Object} formName
	 */
	formExists: function(formName) {
		if(formConfigList[formName]) {
			return true;
		}
		return false;
	},
	/**
	 * lookup a value in the directory list
	 */
	lookup: function (formName, elementName) {
		return formConfigList[formName][elementName];
	},
	/**
	 * insert a value into the driectory list
	 */
	insert: function (id, formConfigObject) {
		formConfigList[id] = formConfigObject;
	}
}

/**
 * This method loops through the form elements and looks each one up 
 * in the config directory listing.
 * Each element (if required is true) is processed and any functionality is executed.
 * @param string formId the id of the form to process
 */
function validate( formId ) {
	var errorCount = 0;
	var errorMessages = '\nThere has been a problem submitting your form!\n________________________________________________\n\n';
	if ( directory.formExists( formId ) ) {
		// get each element from the form.
		$A( Form.getElements( formId ) ).each( function( formElement ){
			// lookup the current element in the directory of registered element.
			// if its a checkbox then we need to use the name attribute
			var item = directory.lookup( formId, ( formElement.type == "checkbox" ) ? formElement.name : formElement.id );
			// if its present and marked as required then run the associated validation routine.
			if ( item && item.required == true ) {
				// pass in the value of the element to the validation routine.
				if ( item.execute( $F( formElement.id ) ) == false ) {
					errorMessages += item.name + ' - ' + item.error + '\n\n';
					errorCount++;
				}
			}
		} );
	}
	else {
		// the form hasn't been added to the directory so we can't process it.
		errorMessages = errorMessages + " Unable to process form as it doesn't seem to exist in the directory! ";
		errorCount++;
	}
	
	if ( errorCount != 0 ) {
		alert( errorMessages );
		return false;
	}
	else {
	return true;
	}
} 

/**
  * This is the directory listing that holds 
  * all the form config objects
  */
var formConfigList = []; // do not edit me.




