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


// 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';
}
