var formValidationErrorReturn;
var formValidationColoring;
var formSetFocus = true;

function form_validate(){
	/*
	
	Current supported types of Questions:
	
	text 		-> 	text Fields - safe characters: -a-zA-Z0-9'. (space)
	number		->  text Fields - only numbers
	dropDown 	->  a single select
	multiple 	-> 	a series of check boxs
	radio 		-> 	a series of radio buttons
	generalText ->  text fields - no character validation
	email 		-> checks for one @ and atleast one .
	
	Current supported types of error coloring:
	separateContainer	-> Lists all errors in a UL tag in an div with an id called 'errorContainer'
	entire				-> fade the entire p/div containing the form element - requires p/div to have a unique ID
	errorOnly			-> fade only the error message
	errorField			-> fade the error message and the form field
	fieldOnly			-> Put error message inside the field, and fade the field
	
	Example of how function is called:
	validateForm('form Name', 'Error Coloring','Error color','form item feedback', 'form item name', 'form item type');
	validateForm('completionForm','Question 1','Q1','radio','Question 2','Q2','dropDown','Question 3','Q3','definition','Question 4','Q4','multiple');
	
	*/
	
	formValidationErrorReturn = true;

	formName 				= arguments[0];
	formValidationColoring 	= arguments[1];
	
	for(var n=2; n < arguments.length; n = n + 3){
		var field =  eval("document."+formName+"."+arguments[n+1]);
		if (arguments[n +2] == 'generalText'){
			
			// Characters that should not be in a text field
			var userData = field.value;
			
			if(userData == ""){
				form_setError(field,field.parentNode,"text_"+formName+""+n, arguments[n]);
			}else{
				form_removeError(field.parentNode, arguments[n]);
			}
		}else if(arguments[n +2] == 'text'){
			// Characters that should not be in a text field
			var regEx = /[^-a-zA-Z0-9'. ]+/;
			var userData = field.value;
			
			regExResults = userData.search(regEx);
			
			if(regExResults > -1 || userData == ""){
				form_setError(field,field.parentNode,"text_"+formName+""+n, arguments[n]);
			}else{
				form_removeError(field.parentNode, arguments[n]);
			}
		}else if(arguments[n +2] == 'number'){
			// Characters that should not be in a text field
			var regEx = /[^0-9' ]+/;
			var userData = field.value;
			
			regExResults = userData.search(regEx);
			
			if(regExResults > -1 || userData == ""){
				form_setError(field,field.parentNode,"text_"+formName+""+n, arguments[n]);
			}else{
				form_removeError(field.parentNode, arguments[n]);
			}
		}else if (arguments[n +2] == 'email'){
			
			// Characters that should not be in aN EMAIL Field
			var emailFilter=/^.+@.+\..{2,3}$/;
			//  not allowing the following:  ( ) < > [ ] , ; : \ / "
			var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
			
			var userData = field.value;
			
			// Remove any white space in the front
			while (userData.substring(0,1) == ' '){
				userData = userData.substring(1);
			}
			
			// Remove any trailing white space
    		while (userData.substring(userData.length-1,userData.length) == ' ') {
	    		userData = userData.substring(0,userData.length-1);
    		}
			
    		field.value = userData;
    		
    		if (!(emailFilter.test(userData)) || userData.match(illegalChars) || userData == ""){
				// bad data.  Clear the field and tell them to fill it in
				form_setError(field,field.parentNode,"text_"+formName+""+n, arguments[n]);
			}else{
				form_removeError(field.parentNode, arguments[n]);
			}
		}else if(arguments[n +2] == 'radio' || arguments[n + 2] == 'multiple'){			
			userSelected = false;

			for(i=0; i < field.length; i++){
				if(field[i].checked){
					userSelected = true;
				}
			}

			if(!userSelected){
				if(field.length){
					form_setError(field,field[0].parentNode.parentNode,"text_"+formName+""+n, arguments[n]);
				}
			}else{
				form_removeError(field[0].parentNode.parentNode, arguments[n]);
			}
		}else if (arguments[n +2] == 'dropDown'){		
			// As long as the selected index is greater than 0, then it means they have 
			// selected something other than the default ' -- '
			if(field.selectedIndex == 0){
				form_setError(field,field.parentNode,"text_"+formName+""+n, arguments[n]);
			}else{
				form_removeError(field.parentNode, arguments[n]);
			}
		}
	}

	return formValidationErrorReturn;
}

/*
	Current supported types of error coloring:
	entire		-> fade the entire p/div containing the form element
	errorOnly	-> fade only the error message
	errorField	-> fade the error message and the form field
*/
function form_setError(field,paragraphEl,id,errorMsg){
	formValidationErrorReturn = false;

	if(formValidationColoring == "separateContainer"){
		if(list = getErrorList()){
			// cycle through child nodes to see if the error message is already there
			var errorExists = false;
			var i = 0;
			
			for(var i = 0; i < list.childNodes.length; i++){
				if(list.childNodes[i].firstChild.nodeValue == errorMsg){
					errorExists = true;
					id = "error_"+ i;
				}
			}
						
			if(!errorExists){
				// create error
				var li = document.createElement('li');
				id = "error_"+ i;
				li.setAttribute("id", id);
				var liErrorMsg = document.createTextNode(errorMsg);
				li.appendChild(liErrorMsg);
				li.className = "formError";
				
				list.appendChild(li);
			}
		}
	}else if(formValidationColoring == "fieldOnly"){
		field.value = errorMsg;
	}else{
		if(paragraphEl.firstChild.className != "formError"){
		
			var theNewParagraph = document.createElement('p');
			theNewParagraph.setAttribute("id", id);
			var theTextOfTheParagraph = document.createTextNode(errorMsg);
			theNewParagraph.appendChild(theTextOfTheParagraph);
			theNewParagraph.className = "formError";
	
			paragraphEl.insertBefore(theNewParagraph,paragraphEl.firstChild);
		}
	}
	
	switch(formValidationColoring){	
		case "errorOnly":
				if($(id)){
					var originalColor = $(id).getStyle('background-color');
					
					$(id).setStyle('background-color', '#FFFF33');
					var fx = new Fx.Tween(id, 'background-color', {duration: 2000, wait: false}).start(originalColor);
				}
			break
		case "errorField":
				if($(id) && $(field.id)){
					var originalColor = $(id).getStyle('background-color');
					
					$(id).setStyle('background-color', '#FFFF33');
					var fx = new Fx.Tween(id, 'background-color', {duration: 2000, wait: false}).start(originalColor);
					
					var originalColor = $(field.id).getStyle('background-color');
					
					$(field.id).setStyle('background-color', '#FFFF33');
					var fx = new Fx.Tween(field.id, 'background-color', {duration: 2000, wait: false}).start(originalColor);
				}
			break;	
		case "fieldOnly":
				if($(field.id)){
					var originalColor = $(field.id).getStyle('background-color');

					$(field.id).setStyle('background-color', '#FFFF33');
					var fx = new Fx.Tween(field.id, 'background-color', {duration: 2000, wait: false}).start(originalColor);
				}
			break;
		case "entire":
				if($(paragraphEl.id)){
					var originalColor = $(paragraphEl.id).getStyle('background-color');
					
					$(paragraphEl.id).setStyle('background-color', '#FFFF33');
					var fx = new Fx.Tween(paragraphEl.id, 'background-color', {duration: 2000, wait: false}).start(originalColor);
				}
			break;
		case "separateContainer":
				if($(id)){
					var originalColor = $(id).getStyle('background-color');
					
					if(originalColor == "transparent"){
						originalcolor = "#FFFFFF"; 
					}

					$(id).setStyle('background-color', '#FFFF33');
					var fx = new Fx.Tween(id, 'background-color', {duration: 2000, wait: false}).start(originalcolor);
				}
			break;
	}
	
	
	if(formSetFocus && field[0]){
		field[0].focus();
		formSetFocus = false;
	}else if(formSetFocus && field){
		field.focus();
		formSetFocus = false;
	}
	
}

function form_removeError(paragraphEl,errorMsg){

	if(formValidationColoring == "separateContainer"){
		if(list = getErrorList()){			
			for(var i = 0; i < list.childNodes.length; i++){
				if(list.childNodes[i].firstChild.nodeValue == errorMsg){
					list.removeChild(list.childNodes[i]);
				}
			}
			
			if(!list.childNodes.length){
				container = document.getElementById("errorContainer");
				container.removeChild(list);
			}
		}
	}else if(formValidationColoring == "fieldOnly"){
		// do nothing for now
	}else{
		if(paragraphEl.firstChild.className == "formError"){
			// remove error statement
			paragraphEl.removeChild(paragraphEl.firstChild);
		}
	}
}

function getErrorList(){
	if(document.getElementById("errorContainer")){
		container = document.getElementById("errorContainer");
				
		if(container.firstChild && container.firstChild.nodeName == "UL"){
			list = container.firstChild;
		}else{
			list = document.createElement('ul');
			container.appendChild(list);
		}
		
		return list;
	}else{
		return false;
	}
}