function validate(myForm)
{
//need a check hidden

	var hasErrors = false;

	// I should have known IE would not like this
	//var inputs = myForm.getElementsByTagNames("INPUT", "TEXTAREA", "SELECT");
	
	myForm.getElementsByTagNames = function()
	{
		var results = new Array();
		for(var i = 0; i < arguments.length; i++)
		{
			var childElements = this.getElementsByTagName(arguments[i]);

			for(var j = 0; j < childElements.length; j++)
				results.push(childElements[j]);
		}
		return results;
	}

	var inputs = myForm.getElementsByTagNames("INPUT", "TEXTAREA", "SELECT");
	for(var i = 0; i < inputs.length; i++)
	{
		var input = inputs[i];		
		var labels = validate_GetLables(input.id);
		
		if((validate_IsValidated(input)) && (labels.error != undefined) && (validate_IsDisplayed(input)))//&& (labels.description != undefined) 
		{	
			var invalid = false;
			
			if(input.type == "checkbox")
			{
				if(!input.checked)
					invalid = true;
			}
			else
			{
				if((input.value == "") && (input.id != "confirmPassword"))
					invalid = true;
			}

			if((input.id == "username") && (!validateUsername(input.value)))
				invalid = true;
			
			if((input.id == "email") && (!validateEmail(input.value)))
				invalid = true;
							
			if((input.id == "confirmPassword") && (input.value != document.getElementById("password").value))
				invalid = true;
			
			if(invalid)			
			{
				labels.error.style.visibility = "visible";
				
				if(!hasErrors)
					input.focus();

				hasErrors = true;					
			}
			else
			{
				labels.error.style.visibility = "hidden";
			}
		}
	}

	return !hasErrors;
}

function validateEmail(emailAddress)
{
	return (/^(?:\w+[_.-]?)+@(?:\w+[_.-]?)+(?:\.\w{2,3})+$/.test(emailAddress))
}

function validateUsername(username)
{
	return (/^\w(?:\w|\d| |\.|_|\-){2,19}(?:\w|\d)$/.test(username))
}

function validate_GetLables(id)
{
	var description;
	var error;
	
	var labels = document.getElementsByTagName("LABEL");
	for(var i = 0; i < labels.length; i++)
	{
		var label = labels[i];
		if(label.htmlFor == id)
		{		
			if(label.className == "error")
				error = label;
			else
				description = label;
		}
	}
	
	return {description:description, error:error};
}

function validate_IsValidated(input)
{
	switch(input.type)
	{
		case "checkbox":
		case "file":
		case "password":
		case "radio":
		case "select-one":
		case "select-multiple":
		case "text":
		case "textarea":
			return true;
			break;
		default:
			return false;
			break;
	}
}

function validate_IsDisplayed(input)
{
	var isDisplayed = true;
	
	var check = input;
	while((isDisplayed == true) && (check.style != undefined))
	{
		if((check.style.display == "none") || (check.style.visibility == "hidden"))
			isDisplayed = false;
		
		check = check.parentNode;
	}
	
	return isDisplayed;
}
