//A means of adding multiple events to the onload event handler
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

//Brings the associated form field into focus when the text within a label is clicked
function focusLabels() {
	if (!document.getElementsByTagName) return false;
	var labels = document.getElementsByTagName("label");
	for (var i=0; i<labels.length; i++) {
		if (!labels[i].getAttribute("for")) continue;
		labels[i].onclick = function() {
			var id = this.getAttribute("for");
			if (!document.getElementById(id)) return false;
			var element = document.getElementById(id);
			element.focus();
		}
	}
}
addLoadEvent(focusLabels);

//Creates default placeholder text, which disappears and reappears automatically
function resetFields(whichform) {
	for (var i=0; i<whichform.elements.length; i++) {
		var element = whichform.elements[i];
		if (element.type == "reset"||element.type == "submit"||element.type == "radio"||element.type == "checkbox") 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;
			}
		}
	}
}

//Determines whether the input field has been completed, based on whether the default still exists
function isFilled(field) {
	if (field.value.length < 1 || field.value == field.defaultValue) {
		return false;
	} else {
		return true;
	}
}
//Determines whether the select menu is the default index
function isIndex(field) {
	if (field.selectedIndex < 1 || field.value == field.defaultValue) {
		return false;
	} else {
		return true;
	}
}

//Determines whether the email field has been completed correctly, based on the existence of certain characters
function isEmail(field) {
	if (field.value.indexOf("@") == -1 || field.value.indexOf(".") == -1)
	{
		return false;
	} else {
		return true;
	}
}

//Draws upon the previous two functions to validate all form fields, based on whether the class "required" or "email" is present
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 your "+element.name+".");
				element.focus();
				return false;
			}
		}
		if (element.className.indexOf("email") != -1) {
			if (!isIndex(element)) {
				alert("Please enter a valid e-mail address.");
				element.focus();
				return false;
			}
		}
		if (element.className.indexOf("buyingposition") != -1) {
			if (!isIndex(element)) {
				alert("Please choose your present buying position.");
				element.focus();
				return false;
			}
		}
		if (element.className.indexOf("inquirytype") != -1) {
			if (!isFilled(element)) {
				alert("Please select one of the Inquiry Types.");
				element.focus();
				return false;
			}
		}
	}
	// update the _fromaddr hidden field value to use the users email address
	var theUserEmail = document.getElementById("email").value;
	document.forms.mailingListForm._fromaddr.value = theUserEmail;
	return true;
}

//The function to prepare the previous form enhancements for each form on a page
function prepareForms() {
	for (var i=0; i<document.forms.length; i++) {
		var thisform = document.forms[i];
		if (thisform.name == "mailingListForm"){continue;}
		resetFields(thisform);
		thisform.onsubmit = function() {
			return validateForm(this);
		}
	}
}
addLoadEvent(prepareForms);