function elementByID(elementID) {
	if (document.getElementById) {
		return document.getElementById(elementID);
	} else if (document.all) {
		return document.all[elementID];
	} else {
		return eval("document." + elementID);
	}
}

function changeImage(elementID, image) {
	elementByID(elementID).src = image;
}


// 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) {
		elementByID("error_company").style.display = "none";
		elementByID("error_name").style.display = "none";
		elementByID("error_email").style.display = "none";
		elementByID("error_scale").style.display = "none";
		elementByID("error_business").style.display = "none";
		elementByID("error_quantity").style.display = "none";
		elementByID("error_address").style.display = "none";
		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 == "") {
						elementByID("error_" + element.name).style.display = "inline";

						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 != "") {
					elementByID("error_" + element.name).style.display = "inline"

					valid = 0;
					errorEmail = "You have entered an invalid email address.";
				}
			}
		}
		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;
}

// 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)";
		}
	}
}

function showLanguages(state) {
	if (state == 1) {
		elementByID("languages").style.visibility = "visible";
	} else {
		elementByID("languages").style.visibility = "hidden";
	}
}


function showLang(state, lang) {
	if (state == 1) {
		elementByID("lang" + lang).style.visibility = "visible";
	} else {
		elementByID("lang" + lang).style.visibility = "hidden";
	}
}


window.onload = externalLinks;