window.onload = jsFnOnload;

function jsFnOnload()
{
	if (document.getElementById)
	{	
	  
		/* Associate logo's onclick event with home page */
		var logo = document.getElementById("logo");
 		if (logo != null) {
			logo.onclick = function() {
				window.location = 'index.php';
				return false;
			}
		}
		
		var menu = document.getElementById("topmenu");
 		if (menu != null) 
		{
			var lis = menu.getElementsByTagName("li");
			for(aliindex=0;aliindex<lis.length;aliindex++)
			{
				if(lis[aliindex].className=='level1')
				{
					lis[aliindex].onclick = JSFnMenuLiClick;
					
					lis[aliindex].onmouseover = JSFnMenuLiOver;
					lis[aliindex].onmouseout = JSFnMenuLiOut;
				}
			}
		}		
		
		var aContactForm = document.getElementById('contactform');
		if (aContactForm != null) aContactForm.onsubmit = JSFnValidateContactForm;

		var aUserLoginForm = document.getElementById('UserLoginForm');
		if (aUserLoginForm != null) aUserLoginForm.onsubmit = JSFnValidateUserLoginForm;

		var aUserRegistrationForm = document.getElementById('UserRegistrationForm');
		if (aUserRegistrationForm != null) aUserRegistrationForm.onsubmit = JSFnValidateUserRegistrationForm;
	}
	
}
/************************************************************************************************************************************/

/* Set Class on Menu Li - Over */
function JSFnMenuLiOver()
{
	this.className = 'level1 hover';
}
/************************************************************************************************************************************/

/* Set Class on Menu Li */
function JSFnMenuLiOut()
{
	this.className = 'level1';
}
/************************************************************************************************************************************/

/* On Click - Menu Li */
function JSFnMenuLiClick()
{
	window.location = this.childNodes[0].href;
	//alert(this.childNodes[0]);
}
/************************************************************************************************************************************/

/* Go Back Link */
function JSFnGoBack()
{
	history.go(-1);
}
/************************************************************************************************************************************/

/* Contact Form Required Fields */
var aContactRequiredFields = new Array ("firstname","Please enter your name to continue","surname","Please enter your surname to continue");

/* Contact Form Validation*/
function JSFnValidateContactForm()
{
	var aEmail = document.getElementById('email');
	var aTelephone = document.getElementById('telephone');
	var aMobile = document.getElementById('mobile');
	if ((aEmail != null) && (aTelephone != null) && (aMobile != null))
	{
		if ((aEmail.value == '') && (aTelephone.value == '') && (aMobile.value == ''))
		{
			alert('You must provide either your telephone/mobile number or email address to continue.');
			return false;
		}
	}

	return JSFnValidateForm(aContactRequiredFields);
}
/************************************************************************************************************************************/

/* User Registration Required Fields */
var aUserRegistrationRequiredFields = new Array ("FirstName","Please enter your first name to continue",
		 									 	 "Surname","Please enter a surname to continue",
		 									 	 "Username","Please enter a username to continue",
										 		 "Password","Please enter a password to continue",
										 		 "PasswordRepeat","Please repeat password to continue",
									 			 "EmailAddress","Please enter an email address to continue");
/* User Registration Form Validation */
function JSFnValidateUserRegistrationForm()
{
	if (this.Password.value.length < 6)
	{
		alert("Please enter at least 6 characters in the \"Password\" field.");
		this.Password.focus();
		return (false);
	}
	
	// check if both password fields are the same
	if (this.Password.value != this.PasswordRepeat.value)
	{
		alert("The two passwords are not the same.");
		this.PasswordRepeat.focus();
		return (false);
	}
	
	return JSFnValidateForm(aUserRegistrationRequiredFields);
}
/************************************************************************************************************************************/

/* User Login Required Fields */
var aUserLoginRequiredFields = new Array ("Username","Please enter a username to continue",
									 	  "Password","Please enter a password to continue");
/* User Login Form Validation */
function JSFnValidateUserLoginForm()
{
	return JSFnValidateForm(aUserLoginRequiredFields);
}
/************************************************************************************************************************************/

/* Validate Forms Function */
function JSFnValidateForm(aRequiredFields)
{
	for (aIndex = 0; aIndex < aRequiredFields.length; aIndex = aIndex + 2)
	{
		currElement = document.getElementById(aRequiredFields[aIndex]);
		if (currElement != null)
		{
			if  (   (   (currElement.type == 'text')
				     && (currElement.value == ''))
				 || (   (currElement.type == 'password')
				     && (currElement.value == ''))
				 || (   (currElement.type == 'checkbox')
				     && (currElement.checked == false))
				 || (   (currElement.type == 'file')
				     && (currElement.value == ''))
				 || (   (currElement.type == 'textarea')
				     && (currElement.value == ''))
				 || (   (currElement.type == 'select-one')
				     && (currElement.value == '')))
			{
				alert(aRequiredFields[aIndex + 1]);
				return false;
			}
			else if (currElement.type == 'radio')
			{
				aIndex = aIndex + 2;
				if (!currElement.checked)
				{
					currElement = document.getElementById(aRequiredFields[aIndex]);
					if ((currElement.type == 'radio') && (!currElement.checked))
					{
						alert(aRequiredFields[aIndex + 1]);
						return false;
					}
				}
			}
		}
	}
	return true;
}
/************************************************************************************************************************************/