function elementByID(elementID) {
	if (document.getElementById) {
		return document.getElementById(elementID);
	} else if (document.all) {
		return document.all[elementID];
	} else {
		return eval("document." + elementID);
	}
}

function showEmailForm() {
	elementByID('popBox').style.display = 'block';
}

function hideEmailForm() {
	elementByID('popBox').style.display = 'none';
}

var W3CDOM = (document.createElement && document.getElementsByTagName);

var mouseOvers = new Array();
var mouseOuts = new Array();

function menuRollOvers()
{
	if (!W3CDOM) return;
	var nav = document.getElementById('menuMain');
	var imgs = nav.getElementsByTagName('img');
	for (var i=0;i<imgs.length;i++)
	{
		if (imgs[i].src.indexOf('_on.gif') == -1) { // don't add rollover to active item
			imgs[i].onmouseover = mouseGoesOver;
			imgs[i].onmouseout = mouseGoesOut;
			var suffix = imgs[i].src.substring(imgs[i].src.lastIndexOf('.'));
			mouseOuts[i] = new Image();
			mouseOuts[i].src = imgs[i].src;
			mouseOvers[i] = new Image();
			mouseOvers[i].src = imgs[i].src.substring(0,imgs[i].src.lastIndexOf('.') - 4) + "_on" + suffix;
			imgs[i].number = i;
		}
	}
}

function mouseGoesOver()
{
	this.src = mouseOvers[this.number].src;
}

function mouseGoesOut()
{
	this.src = mouseOuts[this.number].src;
}

// version 1.9
// 29/11/06
// validates forms
// to set a for item as required add a class of "required"
// if the element name includes "email" it will validate the address, if it has "url" it will also validate
// select boxes must have the default value set to -1
// lables are no longer required but elements ids are
function submitForm(formID, validateForm) {
	valid = 1;
	errorText = "";
	errorVaild = "";
	errorEmail = "";
	errorURL = "";
	errorPassword = "";
	if (elementByID("formError")) {
		elementByID("formError").style.display = "none";
	}
	if (validateForm == 1) {
		for (i = 0; i <= document.forms[formID].elements.length - 1; i++) {
			element = document.forms[formID].elements[i];
			// reset errors on submit
			if (element.className.indexOf("formError") != -1) {
				element.className = element.className.replace(/formError/g, "");
				inputLabel = findTag('label', 'for', element.id);
				inputLabel.className = inputLabel.className.replace(/labelError/g, "");
			}
		}
		for (i = 0; i <= document.forms[formID].elements.length - 1; i++) {
			element = document.forms[formID].elements[i];		
			if (element.className.indexOf("required") != -1) {
				if (element.tagName.toLowerCase() != "select") {
					element.value = trim(element.value); // trim element to stop people populating the field with white space
					if (element.value == "") {
						element.className += " formError";
						if(inputLabel = findTag('label', 'for', element.id)) {
							inputLabel.className += " labelError";
							errorVaild += inputLabel.innerHTML + ", ";
						}
						valid = 0;
					}
				} else {
					// validate select boxes, value must be -1
					value = document.forms[formID].elements[element.id].options[document.forms[formID].elements[element.id].options.selectedIndex].value;
					if (value == -1) {
						element.className += " formError";
						if(inputLabel = findTag('label', 'for', element.id)) {
							inputLabel.className += " labelError";
							errorVaild += inputLabel.innerHTML + ", ";
						}
						valid = 0;
					}
				}
			}
			// now validate email
			elementLC = element.name.toLowerCase();
			if (elementLC.indexOf("email") != -1) {
				element.value = trim(element.value); // trim element to stop people populating the field with white space
				if (checkEmail(element.value) == false && element.value != "") {
					element.className += " formError";
					inputLabel = findTag('label', 'for', element.id);
					inputLabel.className += " labelError";
					valid = 0;
					errorEmail = "You have entered an invalid email address.";
				}
			}

			// now validate url
			elementLC = element.name.toLowerCase();
			if (elementLC.indexOf("url") != -1) {
				element.value = trim(element.value); // trim element to stop people populating the field with white space
				if (checkURL(element.value) == false && element.value != "") {
					element.className += " formError";
					inputLabel = findTag('label', 'for', element.id);
					inputLabel.className += " labelError";
					valid = 0;
					errorURL = "You have entered an invalid url.";
				}
			}

			// now test passwords match
			elementLC = element.id.toLowerCase();
			if (elementLC.indexOf("passwordcheck1") != -1) {
				element.value = trim(element.value); // trim element to stop people populating the field with white space
				if (element.value.length < 6) {
					element.className += " formError";
					inputLabel = findTag('label', 'for', element.id);
					inputLabel.className += " labelError";
					valid = 0; 
					errorPassword += "The password should be at least 6 characters long. ";
				}

				// get other passwordcheck and test
				for (j = 0; j <= document.forms[formID].elements.length - 1; j++) {
					element2 = document.forms[formID].elements[j];
					element2LC = element2.id.toLowerCase();
					if (element2LC.indexOf("passwordcheck2") != -1) {
						element2.value = trim(element2.value);
						if (element.value != element2.value) {
							element.className += " formError";
							inputLabel = findTag('label', 'for', element.id);
							inputLabel.className += " labelError";
							
							element2.className += " formError";
							inputLabel2 = findTag('label', 'for', element2.id);
							inputLabel2.className += " labelError";
							valid = 0;
							errorPassword += "The passwords you entered did not match. ";
						}
					}
				}
			}
		}
		if (valid == 1) {
			if (elementByID("formError")) {
				elementByID("formError").innerHTML = "";
			}
			document.forms[formID].submit();
			if (elementByID("formWorking")) {
				formWorking();
			}
		} else {
			if (errorVaild != "") {
				errorText = "You have not correctly completed: "
				errorVaild = trim(errorVaild);
				errorVaild = errorVaild.substr(0, errorVaild.length - 1) + ".";
				errorText += errorVaild + "<br />";
			}
			if (errorEmail != "") {
				errorText += errorEmail;
			}
			if (errorURL != "") {
				errorText += errorURL;
			}
			if (errorPassword != "") {
				errorText += errorPassword;
			}
			
			errorText = errorText.replace(/ \*/g, "");
			
			if (elementByID("formError")) {
				if (errorText != "") {
					elementByID("formError").style.display = "block";
					elementByID("formError").innerHTML = errorText + "<br />";
				}
			}
			return false;
		}
	} else {
		document.forms[formID].submit();
		if (elementByID("formWorking")) {
			formWorking();
		}
	}
}

// version 1
// 28/01/06
// removes whitepace 
function trim(str) {
	return str.replace(/^\s*|\s*$/g,"");
}

// version 1.5
// 28/01/06
// find a tag with the specific attributes and values 
function findTag(tag, attribute, attributeValue) {
	for(j = 0; (currentTag = document.getElementsByTagName(tag)[j]); j++) {
		if (attribute == "for" && currentTag.attributes["for"].value) { // IE Bug: it can't correctly get for attribute from label tag
			if (currentTag.attributes["for"].value == attributeValue) {
				return currentTag;
			}
		} else {
			if (currentTag.getAttribute(attribute) == attributeValue) {
				return currentTag;
			}
		}
	}
}

// version 1
// 20/11/06
// validate url
function checkURL(url) {
	regEx = /^([a-zA-Z])+:\/\/(([a-zA-Z0-9\-_])+\.)+([a-zA-Z0-9]{2,4})+([a-zA-Z0-9-_%&\?\/.=])+$/;
	testAddress = regEx.test(url);
	return testAddress;
}

// version 1
// 20/11/06
// validate email address
function checkEmail(email) {
	regEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	testAddress = regEx.test(email);
	return testAddress;
}

function formWorking() {
	elementByID('formWorking').style.display = 'block';
	elementByID('submit').style.display = 'none';
}

// fade element
eID = '';
eDirection = '';
eOpacity = 1.0;

function popMessageShow() {
	elementByID('popMessage').style.display = 'block';
	eID = 'popMessage';
	eDirection = 'out';
	eOpacity = 1.0;
	start = setTimeout('elementFade()', 3000);
}

function elementFade() {
	if (eDirection == 'in') {
		eID = 'popMessage';
		eDirection = 'in';
		eOpacity += 0.05;
		elementByID(eID).style.opacity = eOpacity;
		elementByID(eID).style.filter = "alpha(opacity=" + (eOpacity * 100) + ")";
		if (eOpacity < 1.0) {
			loop = setTimeout('elementFade()', 20);
		}
	} else {
		eID = 'popMessage';
		eDirection = 'out';
		eOpacity -= 0.05;
		elementByID(eID).style.opacity = eOpacity;
		elementByID(eID).style.filter = "alpha(opacity=" + (eOpacity * 100) + ")";
		if (eOpacity <= 0) {
			elementByID(eID).style.display = 'none';
		} else {
			loop = setTimeout('elementFade()', 20);
		}
	}
}

function hideMessage() {
	elementByID('popMessage').style.display = 'none';
}

function preLoad() {
	// preload images
	var images_arr = new Array("pop_box.png", "text_send_email.gif");
	var loader = new Array();
	for(var i = 0; i < images_arr.length; i++){
		loader[i] = new Image();
		loader[i].src = images_arr[i];
	}
}

// version 1
// 13/05/06
// _target is depracted in xhtml, but we are allowed to add it back with javascript! Make sure we include "external" in the relation attribute of the tag
function externalLinks() {
	// does the browser support the DOM?
	if (!document.getElementsByTagName) { return; }
	// create an array of the anchors present in the document
	var anchors = document.getElementsByTagName("a");
	// loop through the anchor array
	for (var i=0; i<anchors.length; i++) {
	// get various anchor properties
		var anchor = anchors[i];
		var rel = anchor.getAttribute("rel");
		var title = anchor.getAttribute("title");
		// set the regexp to test for
		var external = /external/;
		// if the anchor has a href and a rel attribute containing the string 'external'
		if (anchor.getAttribute("href") && external.test(rel)) {
			// set the anchor target to blank
			anchor.target = "_blank";
		// append a helpful message to the title attribute
		anchor.title += " (opens in a new window)";
		}
	}
}