// Declare the function "setRequiredObj". This sets the text for the required span tag.
// The span tags should normally be positioned next to the form element with a class value of "required" and be empty.
function setRequiredObj(el, set, text){
	// This function accepts THREE parameters.
	// "el" - This is the current form field that is being validated.
	// "set" - This should be a BOOLEAN value and is used to determine if the required text is to be SET or CLEARED.
	// "text" - This is the text that should be used within the required span tag. 
	//			This will only be used if "set" has a TRUE value.
	//			The default value for an empty "text" string is "&nbsp;*&nbsp;Required";
	// Assign the parent object of the current element "el" to the "obj" varaible
	var obj = el.parentNode
	// Check to see if the "obj" element has any child nodes.
	if(obj.hasChildNodes()){
		// Loop through each of the child nodes in the "obj" element.
		for(i=0; i<obj.childNodes.length; i++){
			// Get the current child node and assign it to the varaible "child".
			var child = obj.childNodes[i];
			// Check to see if the "child" "nodeName" property is EQUAL to "span" and the class name is EQUAL to "required".
			if(child.nodeName.toLowerCase() == "span" && child.className == "required"){
				// Check to see if the element is to be "set" or not
				if(set == true){
					// Check to see if the "text" variable is EQUAL to an empty string.
					if(text == ""){
						// Set the text for the required span tag using the default value.
						child.innerHTML = "&nbsp;*&nbsp;Required";
					}
					else{
						// Set the text for the required span tag using the "text" variable.
						child.innerHTML = text;
					}
					// End of IF Else statement.
				}
				else{
					// Clear the text for the required span tag.
					child.innerHTML = "";
				}
				// End of IF Else statement.
			}
			// End of IF statement.
		}
		// End of FOR Loop.
	}
	// End of IF statement.
}
// End of function "setRequiredObj()".

function markInvalid(el){
	// Increment the "errCount" varaiable by ONE.
	errCount++;
	// Set the "required" text.
	setRequiredObj(el, true, "");
	// Focus the "el" element (but only if the element type is NOT hidden).
	if(el.type !== "hidden") el.focus();
}
// End of function "markInvalid()".

function clearRequired(){
	// Get all of thew SPAN tags.
	var reqTags = document.getElementsByTagName("span");
	// Check to see if there is at least 1 SPAN tag returned.
	if(reqTags.length > 0){
		// Loop through each of the SPAN tags.
		for(i=0; i<reqTags.length; i++){
			// Check to see if the class name of the current SPAN tag is equal to "required".
			if(reqTags[i].className == "required"){
				// Remove the text that was in the current SPAN tag.
				reqTags[i].innerHTML = "";
			}
			// End of IF statement.
		}
		// End of FOR Loop.
	}
	// End of IF statement.
}
// End of function "clearRequired()".

function checkFormElement(el){
	// Check to see if the "type" property is available.
	if(el.type){
		// Select the "type" property of the "el" element (in lowercase).
		switch(el.type.toLowerCase()){
			// Text element.
			case "text":
				// check to see if the value property of the "el" element is EQUAL to an empty string.
				if(el.value == ""){ markInvalid(el); } else{ setRequiredObj(el, false, ""); }
				break;
			// TextArea element.
			case "textarea":
				// check to see if the value property of the "el" element is EQUAL to an empty string.
				if(el.value == ""){ markInvalid(el); } else{ setRequiredObj(el, false, ""); }
				break;
			// Hidden element.
			case "hidden":
				// check to see if the value property of the "el" element is EQUAL to an empty string.
				if(el.value == ""){ markInvalid(el); } else{ setRequiredObj(el, false, ""); }
				break;
			// Checkbox element
			case "checkbox":
				// check to see if the checked property of the "el" element is TRUE.
				if(el.checked == false){ markInvalid(el); } else{ setRequiredObj(el, false, ""); }
				break
			// Radio element
			case "radio":
				break;
			// Select element
			case "select-one":
				// Check to see if the "selectedIndex" property is LESS than or EQUAL to ZERO.
				if(el.selectedIndex <= 0){ markInvalid(el); } else{ setRequiredObj(el, false, ""); }
				break;
			// All other element types.
			default:
				// Do nothing (for now).
		}
		// End of SWITCH statement.
	}
	// End of IF statement.
}
// End of function "checkFormElement()".

function isValidEmail(email){
	// Declare "emailValid" as a local varaible and initialise it to FALSE.
	var emailValid = false;
	// Check to see if the "email" is avaialble and if it DOESN'T have an empty string value.
	if(email && email !== "" && email.indexOf(" ", 0) < 0 && email.indexOf("@", 0) >= 0){
		// Build the EMAIL Regular Expression.
		//var emailRE = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/
		var emailRE = /^([\w\d\-\.]+)@{1}(([\w\d\-]{1,67})|([\w\d\-]+\.[\w\d\-]{1,67}))\.(([a-zA-Z\d]{2,4})(\.[a-zA-Z\d]{2})?)$/;
		// Test the EMAIL string against the regualr expresion to see if the format is valid.
		if(emailRE.test(email)) {
			// Email format IS valid.
			emailValid = true;
		} 
		// End of IF statement.
	}
	// End of IF statement.
	
	// Return TRUE or FALSE, depending on whether or not the EMAIL is valid or not.
	return emailValid;
}
// End of function "isValidEmail()"

// Declare the GLOBAL variable "errCount" and initialise it to ZERO.
// This will be used to count the number of form validation errors.
var errCount = 0;