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 aContactForm = document.getElementById('contactform');
		if (aContactForm != null) aContactForm.onsubmit = JSFnValidateContactForm;

		var aAlertServiceForm = document.getElementById('AlertServiceForm');
		if (aAlertServiceForm != null) aAlertServiceForm.onsubmit = JSFnValidateAlertServiceForm;

		var aEmailAlertForm = document.getElementById('EmailAlertForm');
		if (aEmailAlertForm != null) aEmailAlertForm.onsubmit = JSFnValidateEmailAlertForm;

		var aApplicationForm = document.getElementById('ApplicationForm');
		if (aApplicationForm != null) aApplicationForm.onsubmit = JSFnValidateApplicationForm;
	}
}
/******************************************************************************************************************************************/

/* Validate the Contact Form: Fields */
var aContactRequiredFields = new Array ("FullName","Please enter your full name to continue",
									 	"TelephoneNo","Please enter a telephone no. to continue",
									 	"EmailAddress","Please enter an email address to continue");
/* Validate the Contact Form: Function */
function JSFnValidateContactForm()
{
	return JSFnValidateForm(aContactRequiredFields);
}

	/********************************/

/* Validate the Upload CV Form: Fields */
var aUploadCVRequiredFields = new Array ("EmailAddress","Please enter an email address to continue",
									 	"TelephoneNo","Please enter a telephone no. to continue",
									 	"DocumentAttach","Please attach your CV to continue");
/* Validate the Upload CV Form: Function */
function JSFnValidateUploadCVForm()
{
	return JSFnValidateForm(aUploadCVRequiredFields);
}

	/********************************/

/* Validate the Application Form: Fields */
var aApplicationRequiredFields = new Array ("FullName","Please enter your name to continue",
										 	"EmailAddress","Please enter an email address to continue",
										 	"TelephoneNo","Please enter a telephone no to continue",
										 	"Terms","Please tick the terms to continue");
/* Validate the Application Form: Function */
function JSFnValidateApplicationForm()
{
	return JSFnValidateForm(aApplicationRequiredFields);
}

	/********************************/

/* Validate the Alert Service Form: Fields */
var aAlertServiceRequiredFields = new Array ("YourName","Please enter your name 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",
									 		 "JobTypeID","Please select a job type to continue");
/* Validate the Alert Service Form: Function */
function JSFnValidateAlertServiceForm()
{
	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(aAlertServiceRequiredFields);
}

	/********************************/

/* Validate the Email Alert Form: Fields */
var aEmailAlertRequiredFields = new Array ("Username","Please enter a username to continue",
									 	   "Password","Please enter a password to continue");
/* Validate the Email Alert Form: Function */
function JSFnValidateEmailAlertForm()
{
	return JSFnValidateForm(aEmailAlertRequiredFields);
}

	/********************************/

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-multiple')
				     && (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;
}
