// =addLoadEvent. Function that loads up any called function so that it is active when needed.
function addLoadEvent(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
		window.onload = func;
	}
	else 
	{
		window.onload = function()
		{
			oldonload();
			func();
		}
	}
}

// =isFilled. Check to see if a input field has been filled at all.  || field.value == field.defaultValue
function isFilled(field)
{
	if(field.value.length < 1)
	{
		return false;
	}
	else
	{
		return true;
	}
}

function isMax08(field)
{
	var formcontent = field.value;
	formcontent = formcontent.split(" ");
	if(formcontent.length > 8)
	{
		return false;
	}
	else
	{
		return true;
	}
}

function isMax12(field)
{
	var formcontent = field.value;
	formcontent = formcontent.split(" ");
	if(formcontent.length > 12)
	{
		return false;
	}
	else
	{
		return true;
	}
}

function isMax30(field)
{
	var formcontent = field.value;
	formcontent = formcontent.split(" ");
	if(formcontent.length > 30)
	{
		return false;
	}
	else
	{
		return true;
	}
}

function isMax50(field)
{
	var formcontent = field.value;
	formcontent = formcontent.split(" ");
	if(formcontent.length > 50)
	{
		return false;
	}
	else
	{
		return true;
	}
}

function isMax100(field)
{
	var formcontent = field.value;
	formcontent = formcontent.split(" ");
	if(formcontent.length > 100)
	{
		return false;
	}
	else
	{
		return true;
	}
}

// =isEmail. Check to see if the email is in the correct format.
function isEmail(field)
{
	if(field.value.indexOf("@") == -1 || field.value.indexOf(".") == -1)
	{
		return false;
	}
	else
	{
		return true;
	}
}

// =isNCL. Check to see if the email is in the correct format.
function isNCL(field)
{
	if(field.value.indexOf("@ncl.ac.uk") > 0 || field.value.indexOf("@newcastle.ac.uk") > 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

// =isDefaultChoice. Check to see if the dropdown choices have been left on default.
function isDefaultChoice(field)
{
	if(field.options.selectedIndex == 0)
	{
		return false;
	}
	else
	{
		return true;
	}
}
// =isNoChoice. Check to see if at least one selection has been made.
function isNoChoice(field)
{
	if(field.options.selectedIndex < 0)
	{
		return false;
	}
	else
	{
		return true;
	}
}
//
function isChecked(field)
{
	if(field.checked)
	{
		return true;
	}
	else
	{
		return false;
	}
}

// =validateForm. Loop through the form elements looking for those that are "required".
function validateForm(whichform)
{
	for (var i=0; whichform.elements.length; i++)
	{
		var element = whichform.elements[i];
		
		if(element.className.indexOf("required") != -1)
		{
			if(!isFilled(element))
			{
				alert("Please fill in the \""+element.name+"\" field.");
				return false;
			}
		}
		if(element.className.indexOf("max08") != -1)
		{
			if(!isMax08(element))
			{
				alert("You have gone beyond the 8 words allowed within the \""+element.name+"\" field.");
				return false;
			}
		}
		if(element.className.indexOf("max12") != -1)
		{
			if(!isMax12(element))
			{
				alert("You have gone beyond the 12 words allowed within the \""+element.name+"\" field.");
				return false;
			}
		}
		if(element.className.indexOf("max30") != -1)
		{
			if(!isMax30(element))
			{
				alert("You have gone beyond the 30 words allowed within the \""+element.name+"\" field.");
				return false;
			}
		}
		if(element.className.indexOf("max50") != -1)
		{
			if(!isMax50(element))
			{
				alert("You have gone beyond the 50 words allowed within the \""+element.name+"\" field.");
				return false;
			}
		}
		if(element.className.indexOf("max100") != -1)
		{
			if(!isMax100(element))
			{
				alert("You have gone beyond the 100 words allowed within the \""+element.name+"\" field.");
				return false;
			}
		}
		if(element.className.indexOf("email") != -1)
		{
			if(!isEmail(element))
			{
				alert("The \""+element.name+"\" field must be a valid email address");
				return false;
			}
		}
		if(element.className.indexOf("isNCL") != -1)
		{
			if(!isNCL(element))
			{
				alert("The \""+element.name+"\" field must contain a valid Newcastle University email address (@ncl.ac.uk or @newcastle.ac.uk)");
				return false;
			}
		}
		if(element.className.indexOf("defaultSelection") != -1)
		{
			if(!isDefaultChoice(element))
			{
				alert("You have to make a selection on the \""+element.name+"\" dropdown");
				return false;
			}
		}
		if(element.className.indexOf("selectionRequired") != -1)
		{
			if(!isNoChoice(element))
			{
				alert("You have to make at least one selection on the multi-choice selection box");
				return false;
			}
		}
		if(element.className.indexOf("checkRequired") != -1)
		{
			if(!isChecked(element))
			{
				alert("Please tick the  \""+element.name+"\" checkbox to confirm.");
				return false;
			}
		}
	}
	return true;
}

// =prepareForms. Once the form is submitted, loop through the various form inputs.
function prepareForms()
{
	for (var i=0; i<document.forms.length; i++)
	{
		var thisform = document.forms[i];
		thisform.onsubmit = function()
		{
			return validateForm(this);
		}
	}
}

addLoadEvent(prepareForms);
