// JavaScript Document
/* Apply Text box CSS and Error Text Box CSS */
function resetTextBoxCSS(object){
	object.style.border='1px solid #7f9db9';
	object.style.background='#ffffff';
}

//-----------------------------------------------------------------------------

function applyErrorTextBoxCSS(object){
	object.style.border='1px solid red';
	object.style.background='#FFDDDD';
}

//-----------------------------------------------------------------------------

/* Returns the first input element with the specified class name */
function getElementByClassName(name)
{
	var inputElements = document.getElementsByTagName('input');
	
	for (i=0; i<inputElements.length; i++)
	{
		if (inputElements[i].className == name)
		{
			return inputElements[i];
		}
	}
}

function check_form(form_name,exceptions){
	//get the fields that will not be checked
	var except_name = exceptions.split(',');
	
	var check_form = true;	

	form = document.getElementById(form_name);

	//get all the fields of the form
	for (c=0;c<form.length;c++)
	{
		//check the type of fields
		if(form[c].type=='text' || form[c].type=='password' || form[c].type=='textarea' || form[c].type=='checkbox' || form[c].type=='radio' || form[c].type=='select-one' || form[c].type=='select-multiple')
		{
			//check if the field is an exception
			var exception_check = false;
			
			// used to ensure that if a field fails a check it will not be cleared by passing another
			var field_error = false;
			
			for(x=0;x<except_name.length;x++)
			{
				if(form[c].name==except_name[x])
				{
					 exception_check = true;
				}
			}
			
			// Make sure the required fields are filled
			if(exception_check==false)
			{
				if(form[c].value=='')
				{
					applyErrorTextBoxCSS(form[c]);
					check_form = false;
					field_error = true;
					error_type = 'required field';
				}
				else
				{
					resetTextBoxCSS(form[c]);
				}
			}
			
			if (field_error == false)
			{
				if (form[c].className == 'text email' || form[c].className == 'text emailconfirm')
				{
					if (form[c].value != '')
					{
						var regex = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
							
						if (form[c].value.match(regex) == null)
						{
							applyErrorTextBoxCSS(form[c]);
		
							check_form = false;
							field_error = true;
							error_type = 'email';
						}
						else
						{
							resetTextBoxCSS(form[c]);
						}
					}
				}
			}
			
			if (field_error == false)
			{
				if (form[c].className == 'text emailconfirm')
				{				
					var emailField = getElementByClassName('text email');
									
					if (emailField.value != form[c].value)
					{
						applyErrorTextBoxCSS(form[c]);
						applyErrorTextBoxCSS(emailField);
						check_form = false;
						field_error = true;
						error_type = 'email confirmation';
					}
					else
					{
						resetTextBoxCSS(form[c]);
						resetTextBoxCSS(emailField);
					}
				}
			}
		}
		
		if ((form[c].type == 'hidden') && (form[c].name=='phonefield'))
		{
			var tmpvalue = document.getElementById(form[c].value.replace('Q_',''));
			var thevalue = tmpvalue.value;
			
			if ((thevalue.length <8) || (thevalue.length > 12) || (isNaN(thevalue)))
			{
				applyErrorTextBoxCSS(form[c]);
				check_form=false;
				field_error = true;
				error_type='phone number';
			}
		}
	}
	
	if(check_form==false){
		if(error_type=='required field')
		{
			alert('Please complete the fields marked in red or indicated by a * correctly');
		}
		else if (error_type == 'email confirmation')
		{
			alert('Please ensure that you have indentical email addresses in both the email address field and the email confirmation field');
		}
		else if (error_type == 'email')
		{
			alert('Please ensure you have entered a valid email address');
		}
		else if (error_type == 'phone number')
		{
			alert('Please ensure the entered phone number contains numbers only, no spaces and is between 8 and 12 numbers in length.');	
		}
		return false;
	}
	else{
		return true;
	}
}

//-----------------------------------------------------------------------------