

/* function to allow multiple functions to be appended to the window.onload event handler */
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}


/* Function to clear the text out of type='text' and textarea elements on focus */
function clearText(){
 	
 	if (!document.getElementsByTagName) return false;
	
	// For all the input elements
	var inputs = document.getElementsByTagName("input");
 	for(var i = 0; i < inputs[i].length; i++){
  		// If of type text
  		if(inputs[i].type.toLowerCase() == 'text'){
   			// Add the onclick function
   			inputs[i].onclick = function clearMe(){ this.value = '';};
		}
	}
 
 	// For all the input elements
	var textareas = document.getElementsByTagName("textarea");
 	for(var j = 0; j < textareas.length; j++){
  		// Add the onclick function
  		textareas[j].onclick = function clearMe(){ this.value = ''; };
 	}
}


/* Causes external links to open in a new window */
function externalLinks() 
{
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") anchor.target = "_blank";
	}
}


/* Call the relevant DOM functions */
function initialisePage(){
	//clearText();
	externalLinks();
	setAlternateListItemClass();
	//setButtonClass();
	setDTAndDDHeights();
	setTransparent();
}

/* Function to test if a number is odd (as opposed to even) */
function isOdd(number)
{
	if (number % 2 != 0)
		return true;
	
	return false;
}


/* Function to set class of alternate list items */
function setAlternateListItemClass()
{
	if (!document.getElementsByTagName) return false;
	var ulists = document.getElementsByTagName("ul");
	for(var i = 0; i < ulists.length; i++)
	{
		var items = ulists[i].getElementsByTagName("li");
		for(var j = 0; j < items.length; j++)
		{
			if(isOdd(j))
			{
				// Set the class to button
				items[j].className = "alt";
			}
		}
	}
	
	var dlists = document.getElementsByTagName("dl");
	for(var i = 0; i < dlists.length; i++)
	{
		var types = dlists[i].getElementsByTagName("dt");
		for(var j = 0; j < types.length; j++)
		{
			if(isOdd(j))
			{
				// Set the class to button
				types[j].className = "alt";
			}
		}
		var data = dlists[i].getElementsByTagName("dd");
		for(var k = 0; k < data.length; k++)
		{
			if(isOdd(k))
			{
				// Set the class to button
				data[k].className = "alt";
			}
		}
	}
}


/* Function to set heights of dt elements */
function setDTAndDDHeights()
{
	if (!document.getElementsByTagName) return false;
	var dlists = document.getElementsByTagName("dl");
	for(var i = 0; i < dlists.length; i++)
	{
		var data = dlists[i].getElementsByTagName("dd");
		var types = dlists[i].getElementsByTagName("dt");
		
		// NB: This is only going to work when each DT tag has only 1 DD tag associated with it.
		for(var j = 0; j < types.length; j++)
		{
			data[j].style.height = "auto";
			types[j].style.height = "auto";
			
			if (document.all) {
				var ddHeight = data[j].offsetHeight;
				var dtHeight = types[j].offsetHeight;
			}
			else {
				var ddHeight = data[j].offsetHeight - 10;
				var dtHeight = types[j].offsetHeight - 10;
			}
			
			if (ddHeight > dtHeight)
			{
				types[j].style.height = ddHeight;
			}
			else
			{
				data[j].style.height = dtHeight;
			}
		}	
	}
}


/* Function to set the class of 'button' and 'submit' input elements  */
function setButtonClass()
{
	if (!document.getElementsByTagName) return false;
	var inputs = document.getElementsByTagName("input");
	for(var i = 0; i < inputs.length; i++)
	{
		if(inputs[i].type != "")
		{
			// If of type button or submit
			if(inputs[i].type.toLowerCase() == 'button' || inputs[i].type.toLowerCase() == 'submit')
			{
				// Set the class to button
				inputs[i].className = "button";
			}
		}
	}
}


/* Function to set the background of radio and checkbox elements to transparent */
function setTransparent(){
	// For all the input elements
	if (!document.getElementsByTagName) return false;
	var inputs = document.getElementsByTagName("input");	
	for(var i = 0; i < inputs.length; i++){
		if(inputs[i].type != ""){
			// If of type radio or checkbox
			if(inputs[i].type.toLowerCase() == 'checkbox' || inputs[i].type.toLowerCase() == 'radio'){
				// Set the background to transparent
				inputs[i].style.backgroundColor = "transparent";
				inputs[i].style.paddingLeft = "10px";
				inputs[i].style.border = "0px";
			}
		}
	}
}


addLoadEvent(initialisePage);
