window.onload = jsFnOnload;

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

		var aSubmitRoleTypeButton = document.getElementById('submitroletype');
		var aSubmitRoleTypeSelectInput = document.getElementById('RoleType');
		if (aSubmitRoleTypeButton != null)
		{
			aSubmitRoleTypeButton.style.display = 'none';
			aSubmitRoleTypeSelectInput.onchange = JSFnChangeFurtherInfo;
		}
		
		var aCurrentRoleTypeID = document.getElementById('CurrentRoleID');
		
		if(aCurrentRoleTypeID.value == '')
		{
			/* Hide all the super groups */
			var aAllSuperGroupsHiddenInput = document.getElementById('allsupergroups');
			var aAllSuperGroupsHiddenInputRoleTypeArray = aAllSuperGroupsHiddenInput.value.split('::'); // Remove RoleTypeID from string
			
			var aAllSuperGroupsHiddenInputArray = aAllSuperGroupsHiddenInputRoleTypeArray[0].split(','); // Create array of supergroup IDs
			// Loop through supergroups hiding them
			for(var aIndex=0, Length=aAllSuperGroupsHiddenInputArray.length; aIndex<Length; ++aIndex)
			{
				var aSuperGroupHeaderHide = document.getElementById('supergroupdiv' + aAllSuperGroupsHiddenInputArray[aIndex]);
				aSuperGroupHeaderHide.style.display = 'none';
			}
			/****************************************************************************************************************************/
		}
		
		var aApplicationForm = document.getElementById('applicationform');
		if (aApplicationForm != null) aApplicationForm.onsubmit = JSFnValidateApplicationForm;
	}
	
}
/************************************************************************************************************************************/

/* Change Role Types depending on Role Selected */
function JSFnChangeFurtherInfo()
{
	var aGroupsToShow = this.value; // Get Groups IDs to show
	var aGroupsToShowRoleTypeArray = aGroupsToShow.split('::'); // Remove RoleTypeID from string
	var aGroupsToShowArray = aGroupsToShowRoleTypeArray[0].split(',');
	
	var SuperGroupID = aGroupsToShowRoleTypeArray[1];
	
	var aTeacheronlyDiv = document.getElementById('teacheronly');
	var aNoBursorDiv = document.getElementById('nobursor');
	
	// Only show this input box for teachers
	if(SuperGroupID == 258) aTeacheronlyDiv.style.display = 'block';
	else aTeacheronlyDiv.style.display = 'none';

	if(SuperGroupID == 261) aNoBursorDiv.style.display = 'none';
	else aNoBursorDiv.style.display = 'block';
	
	// Hide warning message	
	var aWarningMsg = document.getElementById('warningmsg');
	if(aWarningMsg != null) aWarningMsg.style.display = 'none';
	
	var aAllSuperGroupsHiddenInput = document.getElementById('allsupergroups');
	var aAllSuperGroupsHiddenInputRoleTypeArray = aAllSuperGroupsHiddenInput.value.split('::'); // Remove RoleTypeID from string
	var aAllSuperGroupsHiddenInputArray = aAllSuperGroupsHiddenInputRoleTypeArray[0].split(','); // Create array of supergroup IDs
	//alert(aAllSuperGroupsHiddenInputArray.length);
	
	//var Length = aAllSuperGroupsHiddenInputArray.length;
	
	for(aIndex = 0; aIndex < aAllSuperGroupsHiddenInputArray.length; aIndex++)
	{
		var aSuperGroupHeaderHide = document.getElementById('supergroupdiv' + aAllSuperGroupsHiddenInputArray[aIndex]);
		
		if(aGroupsToShowRoleTypeArray[0].indexOf(',' + aAllSuperGroupsHiddenInputArray[aIndex] + ',') != -1 && aSuperGroupHeaderHide != null)
		{
			aSuperGroupHeaderHide.style.display = 'block';
			
			//alert(aGroupsToShow + ' :: '  + aAllSuperGroupsHiddenInputArray + ' - ' + aGroupsToShowArray.indexOf(aAllSuperGroupsHiddenInputArray[aIndex]));	
		}
		else if(aSuperGroupHeaderHide != null) aSuperGroupHeaderHide.style.display = 'none';
	}
}
/************************************************************************************************************************************/


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


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

/* Application Form Required Fields */
var aApplyRequiredFields = new Array ("Title","Please select a Title to continue",
									  "Surname","Please enter your Surname to continue",
									  "FirstName","Please enter your First Name to continue",
									  "TelephoneNo","Please enter Home Telephone Number to continue",
									  "EmailAddress","Please enter your Home Email Address to continue",
									  "Password","Please enter a Password to continue",
									  "PasswordRepeat","Please repeat password to continue",
									  "Qualifications","Please enter your qualifications",
									  "CurrentPosts","Please enter your current posts",
									  "RecentPosts","Please enter your recent posts",
									  "RelevantExperience","Please enter your relevant experience",
									  "Gender","Please enter your gender",
									  "Eligible","Please select whether you are eligible to work in the UK",
									  "PersonalStatement","Please enter your personal statement",
									  "Convict","Please select whether you been convicted by the courts of any criminal offence",
									  "CourtAction","Please select whether there is any relevant court action pending against you",
									  "PoliceAction","Please select whether you have ever receieved a caution, reprimand or final warning from the police",
									  "Declaration","Please click the checkbox to confirm that you agree to the declaration.",
									  "Source","Please enter where you heard about Anthony Millard"
									  );
									  
/* Application Form Required Fields when logged in */
var aApplyLoggedInRequiredFields = new Array ("Title","Please select a Title to continue",
									  "Surname","Please enter your Surname to continue",
									  "FirstName","Please enter your First Name to continue",
									  "TelephoneNo","Please enter Home Telephone Number to continue",
									  "EmailAddress","Please enter your Home Email Address to continue",
									  "Qualifications","Please enter your qualifications",
									  "CurrentPosts","Please enter your current posts",
									  "RecentPosts","Please enter your recent posts",
									  "RelevantExperience","Please enter your relevant experience",
									  "Gender","Please enter your gender",
									  "MaritalStatusID","Please enter your marital status",
									  "Eligible","Please select whether you are eligible to work in the UK",
									  "PersonalStatement","Please enter your personal statement",
									  "Convict","Please select whether you been convicted by the courts of any criminal offence",
									  "CourtAction","Please select whether there is any relevant court action pending against you",
									  "PoliceAction","Please select whether you have ever receieved a caution, reprimand or final warning from the police",
									  "Declaration","Please click the checkbox to confirm that you agree to the declaration.",
									  "Source","Please enter where you heard about Anthony Millard"							  							  
									  );
									  
/* Application Form Validation */
function JSFnValidateApplicationForm()
{
	var WordCount = JSFnWordCount(); // Count personal statement words
	
	var aPersonalStatement = document.getElementById('PersonalStatement');
	
	var aLoggedIn = document.getElementById('logged_in');

	var aEducationCommencedMonth = document.getElementById('CommencedMonth1');
	var aEducationCommencedYear = document.getElementById('CommencedYear1');
	var aEducationCompletedMonth = document.getElementById('CompletedMonth1');
	var aEducationCompletedYear = document.getElementById('CompletedYear1');
	var aEducationSchool = document.getElementById('School1');
	var aEducationQualification = document.getElementById('Qualification1');

	var aEmploymentFromMonth = document.getElementById('FromMonth1');
	var aEmploymentFromYear = document.getElementById('FromYear1');
	var aEmploymentToMonth = document.getElementById('ToMonth1');
	var aEmploymentToYear = document.getElementById('ToYear1');
	var aEmploymentAddress = document.getElementById('EmploymentAddress1');
	var aEmploymentJobTitle = document.getElementById('JobTitle1');
	var aEmploymentReasonForLeaving = document.getElementById('ReasonForLeaving1');
	var aEmploymentSalary = document.getElementById('Salary1');

	var aTrainingDay = document.getElementById('TrainingDay1');
	var aTrainingMonth = document.getElementById('TrainingMonth1');
	var aTrainingYear = document.getElementById('TrainingYear1');
	var aTraining = document.getElementById('Training1');

	var aAwardDay = document.getElementById('AwardDay1');
	var aAwardMonth = document.getElementById('AwardMonth1');
	var aAwardYear = document.getElementById('AwardYear1');
	var aAward = document.getElementById('Award1');
	
	if(WordCount > 100)
	{
		alert("Only 100 words are allowed for your Relevant Experience text, please reduce the number of words you have used.");
		aPersonalStatement.focus();
		return false;		
	}		
	else if(aEducationCommencedMonth.value == '' || aEducationCommencedYear.value == '' || aEducationCompletedMonth.value == '' || aEducationCompletedYear.value == '' || aEducationSchool.value == '' || aEducationQualification.value == '')
	{
		alert("Please complete your Education details. We cannot process your application unless the Education details are fully completed");
		aEducationCommencedMonth.focus();
		return false;		
	}
	else if(aEmploymentFromMonth.value == '' || aEmploymentFromYear.value == '' || aEmploymentToMonth.value == '' || aEmploymentToYear.value == '' || aEmploymentAddress.value == '' || aEmploymentJobTitle.value == '' || aEmploymentSalary.value == '' || aEmploymentReasonForLeaving.value == '')
	{
		alert("Please complete your Employment details. We cannot process your application unless the Employment details are fully completed");
		aEmploymentFromMonth.focus();
		return false;		
	}
	else if(aTrainingDay.value == '' || aTrainingMonth.value == '' || aTrainingYear.value == '' || aTraining.value == '')
	{
		alert("Please complete your Training/Professional Development details. We cannot process your application unless the Training/Professional Development details are fully completed");
		aTrainingDay.focus();
		return false;		
	}
	else if(aAwardDay.value == '' || aAwardMonth.value == '' || aAwardYear.value == '' || aAward.value == '')
	{
		alert("Please complete your Awards and Membership of Professional Bodies details. We cannot process your application unless the Awards and Membership of Professional Bodies details are fully completed");
		aAwardDay.focus();
		return false;		
	}
	else
	{
		if(aLoggedIn.value == 0)
		{
			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 Password and Repeat Password do not match.");
				this.PasswordRepeat.focus();
				return (false);
			}
				
			return JSFnValidateForm(aApplyRequiredFields);
		}
		else return JSFnValidateForm(aApplyLoggedInRequiredFields);
	}
}

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

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

function JSFnWordCount()
{
	var aPersonalStatement = document.getElementById('PersonalStatement');
	var WordCount = 0;
	
	if (aPersonalStatement != null)
	{		
		var aFormContent = aPersonalStatement.value;
	
		aFormContent = aFormContent.split(" ");
	
//		alert(aFormContent.length);
	
		WordCount = aFormContent.length;
		
//		document.applicationform.wordcount3.value = WordCount;
	}
	
	return WordCount;
}
