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;		
		
	}
	
}

function JSFnGoBack()
{
	history.go(-1);
}


function JSFnResetBox(box, defaultvalue, direction)
{
	switch(direction)
	{
		case 1:
			if (box.value == defaultvalue)
			{
				box.value = "";
			}
		break;
		
		case 2:
			if(box.value == "")
			{
				box.value = defaultvalue;
			}
		break;
	}
}


function jsFnValidateApplicationRadios()
{
	var errorMessage = "";
	
	for(c=1; c <= 5; c++)
	{
		aCurrentSection = '5c';
		aCurrentIndex = c;
		
		console.log('section_' + aCurrentSection + '_subject_'+ aCurrentIndex);
		
		if(document.getElementById('section_' + aCurrentSection + '_subject_'+ aCurrentIndex).value != '')
		{
			if((document.getElementById('section_' + aCurrentSection + '_predact_' + aCurrentIndex + '_pred').checked == false)
			&& (document.getElementById('section_' + aCurrentSection + '_predact_' + aCurrentIndex + '_act').checked == false))
			{
				errorMessage += "You must select either 'Predicted' or 'Actual' for each grade you've entered";
			}
		}
	}
	
	for(c=1; c <= 5; c++)
	{
		aCurrentSection = '5d';
		aCurrentIndex = c;
		
		console.log('section_' + aCurrentSection + '_subject_'+ aCurrentIndex);
		
		if(document.getElementById('section_' + aCurrentSection + '_subject_'+ aCurrentIndex).value != '')
		{
			if((document.getElementById('section_' + aCurrentSection + '_predact_' + aCurrentIndex + '_pred').checked == false)
			&& (document.getElementById('section_' + aCurrentSection + '_predact_' + aCurrentIndex + '_act').checked == false))
			{
				errorMessage += "You must select either 'Predicted' or 'Actual' for each grade you've entered";
			}
		}
	}
	
	if((document.getElementById('section_1a').value == '')
	||(document.getElementById('section_1b').value == '')
	||(document.getElementById('section_1c').value == '')
	||(document.getElementById('section_1e').value == ''))
	{
		errorMessage += "There were errors with the personal details you've entered.";
	}
	
	if(errorMessage != '')
	{
		alert(errorMessage);
		return false;
	} else return true;
}

var aContactRequiredFields = new Array ("firstname","Please enter your name to continue","surname","Please enter your surname to continue");

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);
}

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;
}

function ValidateRegistrationForm(currentform)
{
	aFirstName = currentform.elements.FirstName.value;
	aLastName = currentform.elements.LastName.value;
	aEmailAddress = currentform.elements.EmailAddress.value;
	aContactNo = currentform.elements.ContactNumber.value;
	
	aPassword = currentform.elements.Password.value;
	aConfirmPassword = currentform.elements.ConfirmPassword.value;
	
	aValidForm = true;
	aErrorMessage = '<ul id="formerrors">';
	
	if(aFirstName == '')
	{
		aErrorMessage += '<li>First Name cannot be blank.</li>';
	}
	
	if(aLastName == '')
	{
		aErrorMessage += '<li>Last Name cannot be blank.</li>';
	}
	
	if(aContactNo == '')
	{
		aErrorMessage += '<li>Contact Number cannot be blank.</li>';
	}
	
	if(aEmailAddress == '')
	{
		aErrorMessage += '<li>Email Address cannot be blank.</li>';
	}
	
	if(aPassword == '')
	{
		aErrorMessage += '<li>Password cannot be blank.</li>';
	}
	
	if(aConfirmPassword != aPassword)
	{
		aErrorMessage += '<li>Password confirmation doesn\'t match the password you\'ve entered.</li>';
	}
	
	if(aConfirmPassword == '')
	{
		aErrorMessage += '<li>Password confirmation cannot be blank.</li>';
	}
	
	aErrorMessage += '</ul>';
	
	if(aErrorMessage == '<ul id="formerrors"></ul>') aValidForm = true;
	else aValidForm = false;
	
	if(aValidForm == false) document.getElementById('messages').innerHTML = aErrorMessage;
	
	return aValidForm;
}


function ValidateEmpRegistrationForm(currentform)
{
	aEmployerName = currentform.elements.EmployerName.value;
	aFirstName = currentform.elements.FirstName.value;
	aLastName = currentform.elements.LastName.value;
	aEmailAddress = currentform.elements.EmailAddress.value;
	aContactNo = currentform.elements.ContactNumber.value;
	
	aPassword = currentform.elements.Password.value;
	aConfirmPassword = currentform.elements.ConfirmPassword.value;
	
	aValidForm = true;
	aErrorMessage = '<ul id="formerrors">';
	
	if(aEmployerName == '') aErrorMessage += '<li>Company name cannot be blank.</li>';
	if(aFirstName == '') aErrorMessage += '<li>First name cannot be blank.</li>';
	if(aLastName == '') aErrorMessage += '<li>Last name cannot be blank.</li>';
	if(aContactNo == '') aErrorMessage += '<li>Contact Number cannot be blank.</li>';
	if(aEmailAddress == '') aErrorMessage += '<li>Email Address cannot be blank.</li>';
	if(aPassword == '') aErrorMessage += '<li>Password cannot be blank.</li>';
	if(aConfirmPassword != aPassword) aErrorMessage += '<li>Password confirmation doesn\'t match the password you\'ve entered.</li>';
	if(aConfirmPassword == '') aErrorMessage += '<li>Password confirmation cannot be blank.</li>';

	aErrorMessage += '</ul>';
	
	if(aErrorMessage == '<ul id="formerrors"></ul>') aValidForm = true;
	else aValidForm = false;
	
	if(aValidForm == false) document.getElementById('messages').innerHTML = aErrorMessage;
	
	return aValidForm;
}
