function initForm(formID, fieldArray)
{

	var passFlag = true;
	
	// loop through fieldArray, testing input name (position[0]) against validation type (position[1])
	for(x=0; x<fieldArray.length; x++)
	{
		
		var elementCollection = document.getElementsByName(fieldArray[x][0]); // return a collection of elements with the current name.
		
		// if it's a radio or checkbox collection (multiple elements with the same name), we need to determine the selected one and its value ...
		if(elementCollection.length > 1) 
		{
			// ... loop through the collection ...
			for(count=0; count<elementCollection.length; count++)
			{
				// ... find the checked one ...
				if(elementCollection[count].checked == true)
				{
					// ... and use that value.
					distilledvalue = elementCollection[count].value;
				} else {
					distilledvalue = ""; // if none are checked 
				}
			}
			
		} else {
			// else read the 1st value.
			distilledvalue = elementCollection[0].value;
		}
	
		// dtermine regexp to use based on validation type
		switch(fieldArray[x][1])
		{
			case "bill":
								testExpression = new RegExp("^[-0-9()\s]{8,10}$");
								break;
			case "alpha":
								testExpression = new RegExp("^[-A-Za-z\s]{1,}$");
								break;
			case "numeric":
								testExpression = new RegExp("^[-0-9()\s]{1,}$");
								break;
			case "alphanumeric":
								testExpression = new RegExp("^[-A-Za-z0-9\\s]{1,}$");
								break;
			case "day":
								testExpression = new RegExp("^[-0-9]{2,2}$");
								break;
			case "month":
								testExpression = new RegExp("^[-0-9]{2,2}$");
								break;
			case "year":
								testExpression = new RegExp("^[-0-9]{4,4}$");
								break;					
			case "postcode":
								testExpression = new RegExp("^[-0-9]{4,}$");
								break;
			case "phone":
								testExpression = new RegExp("^[-0-9()\s]{1,}$");
								break;
			case "anyvalue":
								testExpression = new RegExp("^[^~]{1,}$");
								break;					
			default: 
								alert("No validation for this element.");							
		}
		
		
		// run the regexp against the value.
		if(!testExpression.test(distilledvalue))
		{
			passFlag = false; // if it fails, set the flag to false (this will prevent form submission)	
			for(w=0; w < elementCollection.length; w++)
			{
				// prevent writing " error" classname multiple times by checking for it first.
				if(elementCollection[w].className.indexOf(" error") == -1)
				{
					elementCollection[w].className = elementCollection[w].className + " error"; // write the "error" class, seperated by a space
					
					var currentTitle = elementCollection[w].getAttribute("title");
					
					if(currentTitle)
					{
						currentTitle = currentTitle.replace("REQUIRED : ","");
						elementCollection[w].setAttribute("title","ERROR : " + currentTitle ); // rewrite the title of the field to include "ERROR :"
					};
					
					
				}
			
			}
		} else {
			// if the regexp test is ok, remove any instance of " error".
			for(w=0; w < elementCollection.length; w++)
			{
				elementCollection[w].className = elementCollection[w].className.replace(" error","");
			
			}
		}
				
	}
		// if there was any invalid fields (ie: passFlag == false), alert the user.
		if(!passFlag)
		{
			alert("There is a problem with some of the information entered.\n\nPlease check the highlighted fields.");
		}
		return passFlag;				
}

