window.onload = jsFnOnload;

function jsFnOnload()
{
	if (document.getElementById)
	{			
		aLinks = document.getElementsByTagName("a");		
		for (aIndex = 0; aIndex < aLinks.length; aIndex++)
		{
			if (aLinks[aIndex].className == 'popup') aLinks[aIndex].onclick = JSFnPopup;
			if (aLinks[aIndex].className == 'helplink') aLinks[aIndex].onclick = JSFnHelpPopup;
			if (aLinks[aIndex].className == 'bighelplink') aLinks[aIndex].onclick = JSFnBigHelpPopup;
		} 
		
		aRegContinue = document.getElementById("regcontinue");	
		aPreviousPage = document.getElementById("prevpage");	
		if ((aRegContinue != null) && (aPreviousPage != null))
		{
			var aBackButton = document.createElement("input");
			aBackButton.value = 'Back';
			aBackButton.type = 'Button';
			aBackButton.onclick = JsFnBackToPreviousPage;
			aRegContinue.parentNode.insertBefore(aBackButton,aRegContinue);
		}
			
		aQualification = document.getElementById("QualificationID");	
		if (aQualification != null)
		{
			aQualification.onclick = JSFnQualificationAutomatics;
			aQualificationDetails = document.getElementById("QualificationDetails");
			if (aQualificationDetails != null)
			{
				if (aQualification.checked) aQualificationDetails.style.display = 'block';
				else aQualificationDetails.style.display = 'none';
			}
			if (findVar('DirectLink') == '1') JSFnQualificationAutomatics();			
		}
		
		aNoSupps = document.getElementById("NoSuppIDs");	
		if (aNoSupps != null) 
		{
			aNoSupps.onclick = JSFnSuppItemsUntick;	
			
			aInputs = document.getElementsByTagName("input");		
			for (aIndex = 0; aIndex < aInputs.length; aIndex++)
			{
				if ((aInputs[aIndex].type == 'checkbox') && (aInputs[aIndex].id != 'NoSuppIDs')) aInputs[aIndex].onclick = JSFnNoSuppItemsUntick;
			} 
		}
		
		// Validate Registration Entries
		aForm = document.getElementById("registration1form");	
		if (aForm != null) 
		{
			aForm.onsubmit = JSFnValidateReg1;	
		}
		
		aForm = document.getElementById("registration2form");	
		if (aForm != null) 
		{
			aForm.onsubmit = JSFnValidateReg2;	
		}
		aLogInButton = document.getElementById("loginbutton");	
		if (aLogInButton != null) 
		{
			aLogInButton.onclick = JSFnDisableReg2Validation;	
		}
		
		aPasswordInput = document.getElementById("Password");	
		aConfPasswordInput = document.getElementById("ConfirmPassword");	
		aContinueInput = document.getElementById("regcontinue");
		if ((aPasswordInput != null) && (aConfPasswordInput != null) && (aContinueInput != null))
		{
			aContinueInput.onclick = JsFnCheckPwds;
		}
		
		var aAttachmentForm = document.getElementById('AttachmentForm');
		if (aAttachmentForm != null) aAttachmentForm.onsubmit = JSFnValidateAttachmentForm;		
	}
}

function JSFnPopup()
{
	aPopup = window.open(this.href, 'popup', 'width=630px, height=700px, scrollbars');	
	aPopup.focus();
	return false;
}

function JSFnHelpPopup()
{
	aPopup = window.open(this.href, 'helppopup', 'width=630px, height=200px, scrollbars');	
	aPopup.focus();
	return false;
}

function JSFnBigHelpPopup()
{
	aPopup = window.open(this.href, 'helppopup', 'width=630px, height=400px, scrollbars');	
	aPopup.focus();
	return false;
}

function JSFnSuppItemsUntick()
{
	if (this.checked)
	{
		aInputs = document.getElementsByTagName("input");		
		for (aIndex = 0; aIndex < aInputs.length; aIndex++)
		{
			if ((aInputs[aIndex].type == 'checkbox') && (aInputs[aIndex] != this)) aInputs[aIndex].checked = false;
		} 
	}
}

function JsFnCheckPwds()
{
	aPasswordInput = document.getElementById("Password");	
	aConfPasswordInput = document.getElementById("ConfirmPassword");	
	if ((aPasswordInput != null) && (aConfPasswordInput != null))
	{
	 	if (aPasswordInput.value != aConfPasswordInput.value) 
		{
			alert('The two passwords you have entered are different - please enter the same password to continue.');
			return false;
		}
	}
}

function JSFnNoSuppItemsUntick()
{
	if (this.checked)
	{
		aNoSupps = document.getElementById("NoSuppIDs");	
		if (aNoSupps != null) aNoSupps.checked = false;	
	}
}

function JsFnBackToPreviousPage()
{
	aPreviousPage = document.getElementById("prevpage");	
	if (aPreviousPage != null)
	{
		window.location = aPreviousPage.value + '?direction=back';
	}
}

function JSFnQualificationAutomatics()
{
	aQualification = document.getElementById("QualificationID");	
	if (aQualification != null)
	{
		aMandatory = document.getElementById("MandatoryCourses");		
		if (aMandatory != null)
		{
			aCourseIDs = aMandatory.value.split(',');
			for (aIndex = 0; aIndex < aCourseIDs.length; aIndex++)
			{
				aCourseCb = document.getElementById("CourseID" + aCourseIDs[aIndex]);	
				if (aQualification.checked) aCourseCb.checked = true;
				//else aCourseCb.checked = false;
			}
		}
		aQualificationDetails = document.getElementById("QualificationDetails");
		if (aQualificationDetails != null)
		{
			if (aQualification.checked) aQualificationDetails.style.display = 'block';
			else aQualificationDetails.style.display = 'none';
		}
	}	
}

// Function used to retrieve variable values from the url
function findVar(varName)
{
	temp = null;
	searchString = varName.toLowerCase() + "=";
	testString = window.location.search.toLowerCase();
	testStart = testString.indexOf(searchString)
	if (testStart != -1)
	{
		testEnd = testString.indexOf("&", testStart)
		if(testEnd != -1)
		{
			temp = testString.substring(testStart + varName.length + 1, testEnd);
		}
		else if (testEnd == -1)
		{
			temp = testString.substring(testStart + varName.length + 1, testString.length);
		}
	}
	return unescape(temp);
}

function JSFnDisableReg2Validation()
{
	aForm = document.getElementById("registration2form");	
	if (aForm != null) 
	{
		aForm.onsubmit = null;	
	}
}

function JSFnValidateReg2()
{
	aFirstName = document.getElementById("FirstName");
	aSurname = document.getElementById("Surname");
	aTelephone = document.getElementById("Telephone");
	aEmail = document.getElementById("Email");
		
	if ((aFirstName != null) && (aSurname != null) && (aTelephone != null) && (aEmail != null)) 
	{
		if ((aFirstName.value == '') || (aSurname.value == '') || (aTelephone.value == '') || (aEmail.value == '')) 
		{
			alert('You must enter your name, telephone number and email address to continue.');
			return false;
		}
		else return true;
	}
}

function JSFnValidateReg1()
{
	// Have they ticked to do a qualification?
	aQualification = document.getElementById("QualificationID");
	
	if ((aQualification != null) && (aQualification.checked))
	{
		aMandSelections = document.getElementById("MandatoryCourses");
		aNumOptions = document.getElementById("OptionalChoicesToBeMade");
		aOptionalSelections = document.getElementById("OptionalCourses");
		aEitherSelections = document.getElementById("EitherOrCourses");
		
		// Get into proper arrays
		if (aMandSelections != null) aMandArray = aMandSelections.value.split(",");
		if (aOptionalSelections != null) aOptionalArray = aOptionalSelections.value.split(",");
		if (aEitherSelections != null) aEitherArray = aEitherSelections.value.split(",");
		
		// Ensure all mandatory courses are ticked!
		if (aMandSelections != null)
		{
			aFailed = false;
			for (aIndex = 0; aIndex < aMandArray.length; aIndex++)
			{
				aMandCB = document.getElementById("CourseID" + aMandArray[aIndex]);
				if ((aMandCB == null) || (! aMandCB.checked)) aFailed = true;
			}
			if (aFailed)
			{
				alert('You must select the mandatory courses indicated in the yellow box');
				return false;
			}
		}
		
		// Ensure right number of optional courses have been chosen
		if (aOptionalSelections != null)
		{
			aCount = 0;
			for (aIndex = 0; aIndex < aOptionalArray.length; aIndex++)
			{
				aOptCB = document.getElementById("CourseID" + aOptionalArray[aIndex]);
				if ((aOptCB != null) && (aOptCB.checked)) aCount++;
			}
			if (aCount != aNumOptions.value)
			{
				alert('You must select ' + aNumOptions.value + ' of the optional courses indicated in the yellow box');
				return false;
			}
		}
		
		// Ensure either ors have been selected.
		if (aEitherSelections != null)
		{
			aCount = 0;
			for (aIndex = 0; aIndex < aEitherArray.length; aIndex++)
			{
				aEitherCB = document.getElementById("CourseID" + aEitherArray[aIndex]);
				if ((aEitherCB != null) && (aEitherCB.checked)) aCount++;
			}
			if (aCount != 1)
			{
				alert('You must only select 1 of the Either Or courses indicated in the yellow box');
				return false;
			}
		}
	}
	return true;
}

AttachmentRequiredFields = new Array ("PurchaseOrder","You must attach a purchase order in either Word or pdf format before proceeding.");

function JSFnValidateAttachmentForm()
{
	return JSFnValidateForm(AttachmentRequiredFields);
}

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

