
function validateform() {

	valid = true;
	firstfield = "";
	errormsg = "Please complete the following fields: \n\n";
	var formName = validateform.arguments[0];


	if(typeof(formName) == "object"){
		var formObj = formName;
	} else {
		var formObj = document[formName];
	}


	for(i=1; i < validateform.arguments.length; i += 2) {

	    	var fieldName = validateform.arguments[i];

		if(typeof(formObj[fieldName]) != "undefined"){
			var fieldObj = formObj[fieldName];
		} else {
			alert("ScriptError: "+fieldName+" is missing in the form");
			return false;
		}
	
		var displayName = validateform.arguments[i+1];

		if (fieldObj.type == "text" || fieldObj.type == "select-one" || fieldObj.type == "password") {
			if (fieldObj.value == "") {
				valid = false;
				errormsg += "- "+displayName + "\n";
				if(firstfield == ""){
					firstfield = fieldObj;
				}
			}
			if (fieldObj.className.indexOf("phone") >= 0) {
				phone = fieldObj.value.split("-").join("");
				phone = phone.split(" ").join("");
				phone = phone.split("(").join("");
				phone = phone.split(")").join("");
				phone = phone.split("+").join("");
				phone = phone.split(".").join("");
				phone2 = parseInt(phone);
				if(phone2 < 1 || phone.length < 7){
					valid = false;
					errormsg += "- "+displayName + "\n";
				}
				
			
			}


			//Check to ensure a valid email address has been entered
			if (fieldName == "email" || fieldObj.className.indexOf("email") >= 0) {
				var emailFilter = /^.+@.+\..{2,3}$/;
				var illegalChars = /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
				emailAddress = fieldObj.value;
				if (!(emailFilter.test(emailAddress))) { 
					valid = false;
					errormsg += "- Please Enter a Valid Email Address\n";
					if(firstfield == ""){
						firstfield = fieldObj;
					}
				}
				if (emailAddress.match(illegalChars)) {
					valid = false;
					errormsg += "- Please Check your email address, it contains illegal characters.\n";
					if(firstfield == ""){
						firstfield = fieldObj;
					}
				}
			}
			//Check for valid second email address matches first email address
			if (fieldName == "email2") {
				emailAddress2 = fieldObj.value;
				if (!(emailAddress2 == emailAddress)) { 
					valid = false;
					errormsg += "- Please ensure your confirmation email address matches your email address.\n";
					if(firstfield == ""){
						firstfield = fieldObj;
					}
				}
			}

			if(fieldName == "password"){
				thepassword = fieldObj.value;

			}

			if(fieldName == "password2" && typeof(thepassword) != "undefined"){
				if(fieldObj.value != thepassword){
					errormsg += "- Please ensure your confirmation password matches the password.\n";
					if(firstfield == ""){
						firstfield = fieldObj;
					}
				}


			}
		} else if  (fieldObj.type == "checkbox"){
			if(!fieldObj.checked){
				valid = false;
				errormsg += "- "+displayName + "\n";
			}


		}
	}

	if (valid == false) {
		alert(errormsg);
		if(firstfield){
			firstfield.focus();
		}

	}
	return valid;
}

