
schedule("window", initForm);




function initForm()
{
	var downloadForm = document.getElementById("contact_form");

	downloadForm.onsubmit = checkForm;
};




function checkForm()
{
	var labels = this.getElementsByTagName("label");
	var error = false;
	var first = null;

	for (var i = 0; i < labels.length; i++)
	{
		if (labels[i].className.classExists("required"))
		{
			var input = labels[i].getElementsByTagName("input")[0] || labels[i].getElementsByTagName("textarea")[0] || labels[i].getElementsByTagName("select")[0];
			var spans = labels[i].getElementsByTagName("span");
			var errorText = null;
			var labelText = null;

			for (var j = 0; j < spans.length; j++)
			{
				if (spans[j].className.classExists("labelText"))
				{
					var labelText = spans[j];
				}
			}

			if (input != null)
			{
				if (input.value == "")
				{
					errorText = "Please fill in your " + labelText.childNodes[0].nodeValue.toLowerCase().replace(/:/, "");
				}
				else if (labels[i].className.classExists("requiredEmail") && !input.value.validEmail())
				{
					errorText = "Please supply a valid e-mail address";
				}

				if (errorText != null)
				{
					if (first == null)
					{
						first = input;
					}

					error = true;
					writeCorrection(labels[i], errorText);

					if (input.nodeName.toLowerCase() == "input" && input.getAttribute("type") == "text")
					{
						input.onkeyup = checkValidity;
					}
					else if (input.nodeName.toLowerCase() == "select")
					{
						input.onchange = checkValidity;
					}
				}
				else
				{
					writeCorrection(labels[i]);
				}
			}
		}
	}

	if (error)
	{
		first.focus();

		return false;
	}

	return true;
};




function writeCorrection(label, text, correct)
{
	var spans = label.getElementsByTagName("span");
	var input = label.getElementsByTagName("input")[0] || label.getElementsByTagName("textarea")[0] || label.getElementsByTagName("select")[0];
	var image = label.getElementsByTagName("img")[0];

	if (typeof text == "undefined")
	{
		if (image != null)
		{
			label.removeChild(image);
		}

		for (var j = 0; j < spans.length; j++)
		{
			if (spans[j].className.classExists("correctionText"))
			{
				label.removeChild(spans[j]);

				break;
			}
		}
	}
	else
	{
		if (image == null)
		{
			image = document.createElement("img");
			image.className = "correctionIcon";
			image = label.insertBefore(image, input);

			var newText = document.createElement("span");
			newText.className = "correctionText";
			label.appendChild(newText);
		}

		var spans = label.getElementsByTagName("span");

		for (var j = 0; j < spans.length; j++)
		{
			if (spans[j].className.classExists("correctionText"))
			{
				var correctionText = spans[j];

				break;
			}
		}

		if (correct == true)
		{
			image.setAttribute("src", "/img/icon_tick.gif");
			image.setAttribute("alt", "Correct");
			correctionText.className = correctionText.className.removeClass("warning");
		}
		else
		{
			image.setAttribute("src", "/img/icon_cross.gif");
			image.setAttribute("alt", "Incorrect");
			correctionText.className = correctionText.className.addClass("warning");
		}

		writeSpan(correctionText, text);
	}

	return true;
};




function writeSpan(span, text)
{
	var children = span.childNodes;

	for (var i = 0; children.length > 0;)
	{
		span.removeChild(children[i]);
	}

	var textNode = document.createTextNode(text);
	span.appendChild(textNode);

	return true;
};




function checkValidity()
{
	var label = this.parentNode;
	var spans = label.getElementsByTagName("span");
	var labelText = null;

	for (var j = 0; j < spans.length; j++)
	{
		if (spans[j].className.classExists("labelText"))
		{
			var labelText = spans[j];
		}
	}

	if (this.value == "")
	{
		writeCorrection(label, "Please fill in your " + labelText.childNodes[0].nodeValue.toLowerCase().replace(/:/, ""));
	}
	else if (label.className.classExists("requiredEmail") && !this.value.validEmail())
	{
		writeCorrection(label, "Please supply a valid e-mail address");
	}
	else
	{
		writeCorrection(label, "This field is correct", true);
	}

	return true;
};
