function initApplicationFormElement() {

	if (document.getElementById("personal_dob")) {
		document.getElementById("personal_dob").onchange = updateCurrentAge;
		document.getElementById("personal_dob-m").onchange = updateCurrentAge;
		document.getElementById("personal_dob-y").onchange = updateCurrentAge;
	}
	
	if (document.getElementById("back-button"))
		document.getElementById("back-button").onclick = function() {
			var aAttributes = location.href.split("/");
			var iCurrentStep = parseInt(aAttributes[aAttributes.length-1]);
			
			if (typeof(bIsInternationalApplicant) != "undefined" 
				&& !bIsInternationalApplicant 
				&& iCurrentStep == 9) 
				
				iCurrentStep = 7;
			
			var sURL = location.href.substr(0, location.href.indexOf(".cfm")) + ".cfm";
			location.href = sURL + "/step/" + (iCurrentStep-1);
		}
		
	initPrimaryCourse();
	initSecondaryCourse();
	initOtherCitizenField();
	initStudentNumber();
	initOtherCountryLast3Years();
	initAddressInUK();
	initFeesDetails();
	initDisabilityDetails();
	initOtherAccommodation();
	initOtherDisability();
	initPersonalHowFindUsOther();
	
	var aIMG = document.getElementsByTagName("IMG");
	for (var i=0; i<aIMG.length; i++) {
		if (aIMG[i].className == "tooltip-info") {
			aIMG[i].onmouseover = function(e) { showTooltip(e, this); };
			aIMG[i].onmouseout = function(e) { hideTooltip(e, this); };
		}
	}
	
	if (document.getElementById("personal_how_find_us")) 
		document.getElementById("personal_how_find_us").onchange = initPersonalHowFindUsOther;
	
	if (document.getElementById("course_primary")) 
		document.getElementById("course_primary").onchange = initPrimaryCourse;
		
	if (document.getElementById("course_secondary")) 
		document.getElementById("course_secondary").onchange = initSecondaryCourse;
		
	if (document.getElementById("citizen")) 
		document.getElementById("citizen").onchange = initOtherCitizenField;
		
	if (document.getElementById("accommodation_where")) 
		document.getElementById("accommodation_where").onchange = initOtherAccommodation;
	
	if (document.getElementById("extra_support_disability")) 
		document.getElementById("extra_support_disability").onchange = initOtherDisability;
		
	if (document.getElementById("education_previously_enrolled-1-1"))
		document.getElementById("education_previously_enrolled-1-1").onclick = initStudentNumber;
	
	if (document.getElementById("education_previously_enrolled-0-3"))
		document.getElementById("education_previously_enrolled-0-3").onclick = initStudentNumber;
		
	if (document.getElementById("status_3yrs_uk_eu-1-1"))
		document.getElementById("status_3yrs_uk_eu-1-1").onclick = initOtherCountryLast3Years;
	
	if (document.getElementById("status_3yrs_uk_eu-0-3"))
		document.getElementById("status_3yrs_uk_eu-0-3").onclick = initOtherCountryLast3Years;
	
	if (typeof(bIsCourseForInternational) != "undefined" 
		&& !bIsCourseForInternational 
		&& document.getElementById("status"))
		
		document.getElementById("status").onchange = function() {
			if (this[this.selectedIndex].value.toLowerCase() == "international student") {
				alert("We are sorry but the course you selected is only available to non-international students.");
				this.selectedIndex = -1;
				return false;
			}
		}
		
	if (document.getElementById("status_living_in_uk_yn-1-1"))
		document.getElementById("status_living_in_uk_yn-1-1").onclick = initAddressInUK;
	
	if (document.getElementById("status_living_in_uk_yn-0-3"))
		document.getElementById("status_living_in_uk_yn-0-3").onclick = initAddressInUK;
		
	if (document.getElementById("extra_support_yn-1-1"))
		document.getElementById("extra_support_yn-1-1").onclick = initDisabilityDetails;
	
	if (document.getElementById("extra_support_yn-0-3"))
		document.getElementById("extra_support_yn-0-3").onclick = initDisabilityDetails;
		
	var aColumns = "payment-1,payment-3,payment-5,payment-7";
	aColumns = aColumns.split(",");
	
	var oCheckbox = "";
	
	for (var i=0; i<aColumns.length; i++) {
		oCheckbox = document.getElementById(aColumns[i]);
		if (oCheckbox) oCheckbox.onclick = initFeesDetails;
	}
}

//This is using the preside-glossary-tooltip.js
function showTooltip(e, obj) {
	followMouseMove(e, obj.alt, true);
	obj.rel = obj.alt;
	obj.alt = "";
}

//This is using the preside-glossary-tooltip.js
function hideTooltip(e, obj) {
	followMouseMove(e, obj.alt, false);
	obj.alt = obj.rel;
	obj.rel = "";
}

function initDisabilityDetails() {
	if (document.getElementById("extra_support_yn-wrapper")) {
		if (document.getElementById("extra_support_yn-1-1") 
			&& document.getElementById("extra_support_yn-1-1").checked) {
			
			document.getElementById("extra_support_disability-wrapper").className = "form-element";
			document.getElementById("extra_support_support_needed-wrapper").className = "form-element";
		}
		else {
			document.getElementById("extra_support_disability-wrapper").className = "hide";
			document.getElementById("extra_support_support_needed-wrapper").className = "hide";
		}
		
		initOtherDisability();
	}
}

function initFeesDetails() {
	var aColumns = "payment-3,payment-5,payment-7";
	aColumns = aColumns.split(",");
	
	var oCheckbox = "";
	var bShow = false;
	
	for (var i=0; i<aColumns.length; i++) {
		oCheckbox = document.getElementById(aColumns[i]);
		if (oCheckbox && oCheckbox.checked && !bShow) {
			bShow = true;
		}
	}
	
	if (bShow) showFeesPayerDetails();
	else hideFeesPayerDetails();
}

function hideFeesPayerDetails() {
	var aColumns = "title,firstname,surname,position,school,building,street,town,county,postcode,telephone,email";
	aColumns = aColumns.split(",");
	
	if (document.getElementById("payer_title-wrapper"))
			document.getElementById("payer_title-wrapper").className = "hide";
	
	for (var i=0; i<aColumns.length; i++) {
		if (document.getElementById("payment_" + aColumns[i] + "-wrapper"))
			document.getElementById("payment_" + aColumns[i] + "-wrapper").className = "hide";
	}
}

function showFeesPayerDetails() {
	var aColumns = "title,firstname,surname,position,school,building,street,town,county,postcode,telephone,email";
	aColumns = aColumns.split(",");
	
	if (document.getElementById("payer_title-wrapper"))
			document.getElementById("payer_title-wrapper").className = "";
	
	for (var i=0; i<aColumns.length; i++) {
		if (document.getElementById("payment_" + aColumns[i] + "-wrapper"))
			document.getElementById("payment_" + aColumns[i] + "-wrapper").className = "";
	}
}

function initAddressInUK() {
	if (document.getElementById("status_living_in_uk_yn-wrapper")) {
		if (document.getElementById("status_living_in_uk_yn-0-3") 
			&& document.getElementById("status_living_in_uk_yn-0-3").checked) {
			
			document.getElementById("status_contact_house_number-wrapper").className = "";
			document.getElementById("status_contact_street-wrapper").className = "";
			document.getElementById("status_contact_town-wrapper").className = "";
			document.getElementById("status_contact_county_city-wrapper").className = "";
			document.getElementById("status_contact_country-wrapper").className = "";
			document.getElementById("status_contact_postcode-wrapper").className = "";
			document.getElementById("contact-address-uk").className = "";
		}
		else {
			document.getElementById("status_contact_house_number-wrapper").className = "hide";
			document.getElementById("status_contact_street-wrapper").className = "hide";
			document.getElementById("status_contact_town-wrapper").className = "hide";
			document.getElementById("status_contact_county_city-wrapper").className = "hide";
			document.getElementById("status_contact_country-wrapper").className = "hide";
			document.getElementById("status_contact_postcode-wrapper").className = "hide";
			document.getElementById("contact-address-uk").className = "hide";
		}
	}
}

function initOtherCountryLast3Years() {
	if (document.getElementById("status_other_countries_3yrs-wrapper")) {
		if (document.getElementById("status_3yrs_uk_eu-0-3") && document.getElementById("status_3yrs_uk_eu-0-3").checked)
			document.getElementById("status_other_countries_3yrs-wrapper").className = "form-element";
		else
			document.getElementById("status_other_countries_3yrs-wrapper").className = "hide";
	}
}

function initStudentNumber() {
	if (document.getElementById("education_student_number-wrapper")) {
		if (document.getElementById("education_previously_enrolled-1-1") && document.getElementById("education_previously_enrolled-1-1").checked)
			document.getElementById("education_student_number-wrapper").className = "form-element";
		else
			document.getElementById("education_student_number-wrapper").className = "hide";
	}
}

function initPersonalHowFindUsOther() {
	var oSelect = document.getElementById("personal_how_find_us");
	
	if (oSelect) {
		if (oSelect[oSelect.selectedIndex].text.substr(0, 5).toLowerCase() == "other") {
			document.getElementById("personal_how_find_other-wrapper").className = "";
		}
		else {
			document.getElementById("personal_how_find_other-wrapper").className = "hide";
		}
	}
}

function initOtherAccommodation() {
	var oSelect = document.getElementById("accommodation_where");
	
	if (oSelect) {
		if (oSelect[oSelect.selectedIndex].text.substr(0, 5).toLowerCase() == "other") {
			document.getElementById("accommodation_other-wrapper").className = "form-element";
		}
		else {
			document.getElementById("accommodation_other-wrapper").className = "hide";
		}
	}
}

function initOtherDisability() {
	var oSelect = document.getElementById("extra_support_disability");
	
	if (oSelect) {
		if (oSelect[oSelect.selectedIndex].text.substr(0, 5).toLowerCase() == "other") {
			document.getElementById("extra_support_disability_other-wrapper").className = "form-element";
		}
		else {
			document.getElementById("extra_support_disability_other-wrapper").className = "hide";
		}
	}
}


function initOtherCitizenField() {
	var oSelect = document.getElementById("citizen");
	
	if (oSelect) {
		if (oSelect[oSelect.selectedIndex].text.substr(0, 5).toLowerCase() == "other") {
			document.getElementById("citizen_other-wrapper").className = "form-element";
		}
		else {
			document.getElementById("citizen_other-wrapper").className = "hide";
		}
	}
}

function initPrimaryCourse() {
	var oSelect = document.getElementById("course_primary");
	
	if (oSelect) {
		if (oSelect[oSelect.selectedIndex].text.substr(0, 7) == "A Level") {
			document.getElementById("course_primary_as_subjects1-wrapper").className = "form-element";
			document.getElementById("course_primary_as_subjects2-wrapper").className = "";
			document.getElementById("course_primary_as_subjects3-wrapper").className = "";
			document.getElementById("course_primary_as_subjects4-wrapper").className = "";
		}
		else {
			document.getElementById("course_primary_as_subjects1-wrapper").className = "hide";
			document.getElementById("course_primary_as_subjects2-wrapper").className = "hide";
			document.getElementById("course_primary_as_subjects3-wrapper").className = "hide";
			document.getElementById("course_primary_as_subjects4-wrapper").className = "hide";
		}
	}
}

function initSecondaryCourse() {
	var oSelect = document.getElementById("course_secondary");
	
	if (oSelect) {
		if (oSelect[oSelect.selectedIndex].text.substr(0, 7) == "A Level") {
			document.getElementById("course_secondary_as_subjects1-wrapper").className = "form-element";
			document.getElementById("course_secondary_as_subjects2-wrapper").className = "";
			document.getElementById("course_secondary_as_subjects3-wrapper").className = "";
			document.getElementById("course_secondary_as_subjects4-wrapper").className = "";
		}
		else {
			document.getElementById("course_secondary_as_subjects1-wrapper").className = "hide";
			document.getElementById("course_secondary_as_subjects2-wrapper").className = "hide";
			document.getElementById("course_secondary_as_subjects3-wrapper").className = "hide";
			document.getElementById("course_secondary_as_subjects4-wrapper").className = "hide";
		}
	}
}

function updateCurrentAge() {
	if (document.getElementById("personal_dob").value && document.getElementById("personal_dob-m").value && document.getElementById("personal_dob-y").value) {
		var begDate = new Date(document.getElementById("personal_dob-y").value + "/" + document.getElementById("personal_dob-m").value + "/" + document.getElementById("personal_dob").value);
		var difDate = enrollmentDate.getTime() - begDate.getTime();
		var days = Math.round(difDate/(24*60*60*1000))-1;
		var age = Math.floor(days/365.25);
		
		if (age > 0) document.getElementById("personal_age").value = age;
		else document.getElementById("personal_age").value = "";
	}
}

if (typeof(addEvent) == 'undefined') {
	addEvent = function(obj, type, fn) {
		if (obj.addEventListener) obj.addEventListener( type, fn, false );
		else if (obj.attachEvent) {
			obj["e"+type+fn] = fn;
			obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
			obj.attachEvent( "on"+type, obj[type+fn] );
		}
	}
}

addEvent(window, "load", initApplicationFormElement);