function resetFields(whichform)
{
	for (var i=0; i<whichform.elements.length; i++)
	{
		var element = whichform.elements[i];
		if (element.type == "submit") continue;
		if (!element.defaultValue) continue;
		element.onfocus = function()
		{
			if (this.value == this.defaultValue)
				this.value = "";
		}
		element.onblur = function()
		{
			if (this.value == "")
				this.value = this.defaultValue;
		}
	}
}

function validateForm(whichform)
{
	for (var i=0; i<whichform.elements.length; i++)
	{
		var element = whichform.elements[i];
		if (element.className.indexOf("required") != -1)
		{
			if (!isFilled(element))
			{
				alert("Please fill in the "+element.name+" field.");
				return false;
			}
		}
		if (element.className.indexOf("email") != -1)
		{
			if (!isEmail(element))
			{
				alert("The "+element.name+" field must be a valid email address.");
				return false;
			}
		}
		if (element.className.indexOf("default") != -1)
		{
			if (!isAgreed(element))
			{
				alert("You must agree to the Terms and Conditions.");
				return false;
			}
		}
		if (element.className.indexOf("captcha") != -1)
		{
			if (!jcap())
			{
				alert("Enter the code as it is shown.");
				return false;
			}
		}
	}
	return true;
}

function isFilled(field)
{
	if (field.value.length < 1 || field.value == field.defaultValue)
		return false;
	else
		return true;
}

function isEmail(field) 
{
	if (field.value.indexOf("@") == -1 || field.value.indexOf(".") == -1)
		return false;
	else
		return true;
}

function isAgreed(field)
{
	if (field.checked == false)
		return false;
	else
		return true;
}

function jcap()
{
	var uword = hex_md5(document.getElementById('code').value);
	if (uword == cword[anum-1])
		return true;
	else
		return false;
}

function prepareForms()
{
	for (var i=0; i<document.forms.length; i++)
	{
		var thisform = document.forms[i];
		resetFields(thisform);
		thisform.onsubmit = function()
		{
			return validateForm(this);
		}
	}
}

addLoadEvent(prepareForms);
