window.onload = function hideAll() { //hides all extra details sections and shows 'add extra ...' buttons
	var theElements = document.getElementsByTagName('*'); //all elements in document

	//hide all extra internal and external investigator
	/**************************** NOTE: SHOULD UPDATE THIS TO USE getElementByClassName() FUNCTION ******************************/
	for (i=0;i<theElements.length;i++) {
		var theClass = theElements[i].className;
		var sample = theClass.substring(0,6); //looking for "toHide" class - MUST be first class listed
		var innerElements = theElements[i].getElementsByTagName("input"); //get all input elements within the current element
		if ((sample == "toHide") && (innerElements[0].value == "")){
			theElements[i].className = "displayDiv hide"; //hides all elements with "toHide" class
		}
		else if (sample == "toShow") {
			theElements[i].className = ""; //shows all elements with "toShow" class, by removing the default "hide" class
		}
	}
}
function addSection(section) { //adds relevant section
	var theSection = section;
	var theElements = document.getElementById(theSection).getElementsByTagName("div");
	var showFlag = false;

	var visibleElements = getElementsByClassName(document.getElementById(theSection), "div", "show");

	if(visibleElements.length == 9) {
		switch (theSection) {
			case "external": alert("You have reached the maximum number of first name investigators");
			break;
			case "internal": alert("You have reached the maximum number of internal investigators");
			break;
		}
	} else {
		for (i=0;i<theElements.length;i++) {
			if (showFlag != true) {
				var classnameLength = theElements[i].className.length; //get the current element's class name length
				var sample = theElements[i].className.substring((classnameLength-4),classnameLength); //get the last 4 chars of current element's class name
	
				if (sample == "hide") { //if the last 4 characters of the current element's class name are "hide"
					theElements[i].className = "displayDiv show"; //set element's class to "show" to show it
					showFlag = true; //so that the loop stops after showing just one element
					
					//need to add the newly-visible inputs to the form's "required" field
					if (theSection == "internal") {
						var inputs = theElements[i].getElementsByTagName("input"); //create array of input fields within newly-visible div
						for (i=0;i<inputs.length;i++) {
							document.getElementById("required").value = document.getElementById("required").value + "," + inputs[i].id; //add these inputs' id's to the required field's value
						}
					}
				}
			}
		}
	}
}
function removeSection(section) { //removes relevant section
	var theSection = section;
	var theElements = document.getElementById(theSection).getElementsByTagName("div");
	var hideFlag = false;

	for (i=theElements.length-1;i>=0;i--) {
		if (hideFlag != true) {
			var classnameLength = theElements[i].className.length; //get the current element's class name length
			var sample = theElements[i].className.substring((classnameLength-4),classnameLength); //get the last 4 chars of current element's class name

			if (sample == "show") { //if the last 4 characters of the current element's class name are "show"
				theElements[i].className = "displayDiv hide"; //set element's class to "hide" to hide it
				//then need to remove any values that might have been input into the removed fields
				var innerElements = theElements[i].getElementsByTagName("input");
				for (i=0;i<innerElements.length;i++) {
					innerElements[i].value = "";
					innerElements[i].style.background = "#fff";
				}
				hideFlag = true;
				
				/************************* no longer used as this is taken care of by custom php file **************************
				//need to remove the newly-hidden elements from the form's "required" field
				if (theSection =="internal") {
					var requiredField = document.getElementById("required");
					var requiredArr = requiredField.value.split(","); //convert the required field's values into an array

					requiredArr.splice(requiredArr.length-2,2); //remove the last two values of the array
					requiredField.value = requiredArr.join(); //convert the array back to a string and set required field's value to this
				}
				*/
			}
		}
	}
}
function percentage(theSection) {
	var theFields = getElementsByClassName(document.getElementById(theSection), "input", "percentage");
	var totalField = getElementsByClassName(document.getElementById(theSection), "input", "total");
	var count = 0;
	
	for (i=0;i<theFields.length;i++) {
		if (theFields[i].value != null) {
			count = (count*1) + (theFields[i].value*1);
		}
	}
	totalField[0].value = count;
	if (count > 100) {
		totalField[0].style.background = "#db0000";
		alert("Please ensure percentages total 100%");
	} else {
		totalField[0].style.background = "#fff";
	}
}
function setRecipient(theSelect) {
	var recipient = theSelect.value;
	document.getElementById("recipients").value = document.getElementById("recipients").value + "," + recipient;
	alert(document.getElementById("recipients").value);
}
function check(checkbox) {
	if (checkbox.id == "noneAbove") {
		document.getElementById("creativeWork").checked = false;
		document.getElementById("newApplication").checked = false;
		document.getElementById("originalInvestigation").checked = false;
	} else if (checkbox.id == "creativeWork" || checkbox.id == "newApplication" || checkbox.id == "originalInvestigation") {
		document.getElementById("noneAbove").checked = false;
	} else if (checkbox.id == "ethicsNotApplicable") {
		document.getElementById("humanEthicsCommittee").checked = false;
		document.getElementById("animalEthicsCommittee").checked = false;
		document.getElementById("gmsc").checked = false;
		document.getElementById("delayedApplication").checked = false;
	} else if (checkbox.id == "humanEthicsCommittee" || checkbox.id == "animalEthicsCommittee" || checkbox.id == "gmsc") {
		document.getElementById("delayedApplication").checked = false;
		document.getElementById("ethicsNotApplicable").checked = false;
	} else if (checkbox.id == "delayedApplication") {
		document.getElementById("humanEthicsCommittee").checked = false;
		document.getElementById("animalEthicsCommittee").checked = false;
		document.getElementById("gmsc").checked = false;
		document.getElementById("ethicsNotApplicable").checked = false;
	}
}
function checkLength(textfield, length) {
	if (textfield.value != "" && textfield.value.length != length) {
		textfield.style.background = "#db0000";
		switch (length) {
			case 4: alert("All cost centre numbers must be 4 digits in length");
			break;
			case 6: alert("All FOR and SEO codes must be 6 digits in length");
			break;
			case 8: alert("All staff and student ID's must be 8 digits in length");
			break;
		}
	} else {
		textfield.style.background = "#fff";
	}
}
function investigatorCheckRadio(theElement) {
	if (theElement.checked) {
		if (theElement.value == "external") {
			document.getElementById("studentInvestigator").checked = false;
		} else {
			document.getElementById("externalInvestigator").checked = false;
		}
	}
}