/* Form Validation Script */

/*
	Author:	John Hill - jowhill
	Requirements - /js/effect/prototype.js, /js/effect/scriptaculous.js
	
	Use:	For each input field that is required do the following:
			
				<li class="required">
					<label for="field_name">Field Text</label>
					<input type="text" size="some_size" name="field_name" maxlength="some_max_length" />
				</li>
			
			This script will use the label text as the error message to the user if the required field is blank.
			
	
	Future Features:	Add ability to check for regular expressions based upon class name of input element.
							ie. <input type="text" size="10" maxlength="15" name="phone" class="phone" />
								would verify that a 10 digit U.S. phone number was entered.
*/

function validateForm( formID )
{	
	// Set some variables
	var errorList = []; // an array of erros to use later
	var returnValue = false; // don't initiall submit form, but make sure requirements are meet first.
	var requiredFormElements = $$( 'li.required' ); // get all required fields
	var i = 0; // an array counter
	// get the label text (feildTitle) and the input name
	requiredFormElements.each( 
		function(input)
		{
			var label 		= input.immediateDescendants();
			var obj			= Object.inspect( label[1] );
			var objType 	= obj.split( " " );
			var objectType	= objType[0];
			
			if( objectType.substring( objectType.length - 1 ) != ">" )
				objectType = objectType + ">";

			// for all required input elements check for presence of a value
			if( objectType == "<input>" || objectType == "<select>" || objectType == "<textarea>" )
			{	
				if( !$( label[1] ).present() )
				{
					var fieldTitle = label[0].innerHTML; // set field title to label text
					var fieldTitleLength = fieldTitle.length; // get length of field title
					fieldTitle = fieldTitle.substring( 0, fieldTitleLength-1 ); // kill off the last character, which is usually " : "
					errorList[i] = fieldTitle; // label text
					i++;
				}
			}
			else if( objectType == "<ul>" ) // If we have sub elements listed in a unordered list
			{
				var subInputs 	= label[1].descendants(); // get the list of elements
				var present		= false;
				// Loop over each and if the element is an input check to see if it has been 'checked'
				subInputs.each(
					function(subInput)
					{
						var subobjectType 	= Object.inspect( subInput );
						if( (subobjectType == "<input>") && ( $(subInput).checked ) )
						{
							present = true;
						}
					}
				);
				
				if( !present ) // If no radio or checkbox was actually checked, then add the error message
				{
					var fieldTitle = label[0].innerHTML; // set field title to label text
					var fieldTitleLength = fieldTitle.length; // get length of field title
					fieldTitle = fieldTitle.substring( 0, fieldTitleLength-1 ); // kill off the last character, which is usually " : "
					errorList[i] = fieldTitle; // label text
					i++;
				}
			}

		}
	 );
	
	if( errorList.length > 0 )
	{
		var exist = $('error');
		
		if( exist ) // If we already have an error div, then remove it.
		{
			exist.remove(); // This can happen if user has error more than once.
		}
		// dynamically create the error div, and then the errorList
		new Insertion.Before( formID, "<div id='error' style=\"display:none;\"><h6>The following are required:</h6></div>" );
		new Insertion.Bottom( $('error'), "<ul id='errorList'> &nbsp; </ul>" );

		// Add a new list element with each new error.
		errorList.each( 
			function(errorLi)
			{
				new Insertion.Bottom( 'errorList', '<li>' + errorLi + '</li>' );
			}
		  );
		
		// Move user to top of browser so they can see the new error box
		var positionLeft = $( formID ).offsetLeft;
		var positionTop  = $( formID ).offsetTop;
		
		window.scrollTo( positionLeft, positionTop-60 );
		new Effect.Appear('error');  
		returnValue = false;
	}
	else
	{
		returnValue = true;
	}
	
	return returnValue;
	
} // End validateForm fucntion
			