function emailValid(value)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if( filter.test(value) )
		return true;
	else
		return false;
}

function validate(form)
{
	var inputs = form.getElementsByTagName('input');
	var textareas = form.getElementsByTagName('textarea');
	var selects = form.getElementsByTagName('select');
	
	var errors = false;
	var msg = "The details you entered contain invalid information. The following fields contain errors:\n\n";
	
	var input = null;
	var condition = null;
	var conditions = null;
	var elements = null;
	var element = null;
	var sibling = null;
	for( var i = 0, length = inputs.length; i < length; i++ )
	{
		input = inputs[i];
		if( input.alt == "required" && input.type == "text" )
		{
			if( input.getAttribute('condition') )
			{
				condition = input.getAttribute('condition');
				conditions = condition.split("=");
				elements = document.getElementsByName(conditions[0]);
				
				if( elements.length > 1 )
				{
					if( elements[0].type == "radio" )
					{
						for( var j = 0, len = elements.length; j < len; j++ )
						{
							if( elements[j].checked )
							{
								element = elements[j];
								continue;
							}
						}
					}
					else
						continue;
				}
				else
					element = elements[0];

				if( element.value != conditions[1] )
					continue;
			}
			
			if( input.value == "" )
			{
				errors = true;
				msg += "- " + input.title + " is empty.\n";
			}
			else if( input.className && input.className == "email")
			{
				if( !emailValid(input.value) )
				{
					errors = true;
					msg += "- " + input.title + " is not a valid email address.\n";
				}
			}
			else if( input.getAttribute('repeat') )
			{
				sibling = document.getElementById(input.getAttribute('repeat'));
				if( sibling.value != input.value)
				{
					errors = true;
					msg += "- " + input.title + " does not match "+ sibling.title +".\n";
				}
			}
		}
		if( input.alt == "required" && input.type == "checkbox")
		{
			if( !input.checked )
			{
				errors = true;
				msg += "- " + input.title + " is not checked.\n";
			}
		}
	}
	
	var textarea = null;
	for( var i = 0, length = textareas.length; i < length; i++ )
	{
		textarea = textareas[i];
		if( textarea.getAttribute('alt') == "required" )
		{
			if( textarea.value == "" )
			{
				errors = true;
				msg += "- " + textarea.title + " is empty.\n";
			}
		}
	}
	
	var select = null;
	for( var i = 0, length = selects.length; i < length; i++ )
	{
		select = selects[i];
		if( select.getAttribute('alt') == "required" )
		{
			if( select.options[select.selectedIndex].value == "-1" )
			{
				errors = true;
				msg += "- " + select.getAttribute('title') + " does not have a valid selection.\n";
			}
		}
	}
	
	msg += "\nPlease fix these errors and click submit again.";
	
	if( errors )
	{
		alert(msg);
		return false;
	}
	else
		return true;
}