/*
----------------------------------------------------------------------------------------
*    form_validator.js
*   
*    This JavaScript, like Script 7.15, validates e-mail addresses. But because it uses
*    regular expressions, the script is much shorter.
*   
*    Also validates text area entries
*   
*    Based on script from chapter 8, Javascript & Ajax, 6th edition
*
*    Version 1.00        12/04/08
----------------------------------------------------------------------------------------
*/

add_onload(initForms);

/* Loop through every form on the page.  For each one, add an event handler
to that form's onsubmit to call validateForm.  The form will only be sent to the server script
in the specified in the HTML 'action' if the validateForm function returns TRUE. */
function initForms()
{
	for (var i=0; i< document.forms.length; i++)
    {
		document.forms[i].onsubmit = function() {return validForm();}
	}
}

/* Function to validate form data and return TRUE if all is OK */
function validForm()
{
    document.getElementById("errortext").innerHTML = '';
	var allGood = true;
	// Return an array containing every tag on the page
	var allTags = document.getElementsByTagName("*");
    // Loop through each tag
	for (var i=0; i<allTags.length; i++)
    {
		if (!validTag(allTags[i]))
        {
			allGood = false;
		}
	}
	return allGood;

    // Check a tag to see if there's anything that should prevent form submission
	function validTag(thisTag)
    {
		var outClass = "";
		// Read the class attributes assigned to this tag into array allClasses
		var allClasses = thisTag.className.split(" ");
	    // Loop through each class, and run any validation code required for each
		for (var j=0; j<allClasses.length; j++)
        {
			outClass += validBasedOnClass(allClasses[j]) + " ";
		}
	
		thisTag.className = outClass;
	
		if (outClass.indexOf("invalid") > -1)
        {
			// invalidLabel(thisTag.parentNode);
			thisTag.focus();
			if (thisTag.nodeName == "INPUT")
            {
				thisTag.select();
			}
			return false;
		}
		return true;
		
		/* Examine the class name passed in input argument thisClass.  Do something according to
		the type of tag this is */
		function validBasedOnClass(thisClass)
        {
			var classBack = "";
		
			switch(thisClass)
            {
				case "":
				case "invalid":
					break;
				case "email":
					if (allGood && !validEmail(thisTag.value)) classBack = "invalid ";
					classBack += thisClass;
					break;
				case "required":
					if (allGood && thisTag.value == "") classBack = "invalid ";
					classBack += thisClass;
					break;
				case "textarea":
					if (allGood && validTextArea(thisTag.value))
                    {
                        classBack = "invalid ";
                        document.getElementById("errortext").innerHTML = "We regret that we cannot accept references to other web sites.  Please ensure your submission is free of hyperlinks and try again";
                    }
					classBack += thisClass;
					break;
				default:
					classBack += thisClass;
			}
			return classBack;
		}
		
        /* Validate email address passed in email.  Returns:
            FALSE if the email address is invalid (i.e. the regular expression was not matched);
            TRUE otherwise (i.e. the regular expression was matched) */	
		function validEmail(email)
        {
            if (email == '') return false;
			var regexp = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
			return regexp.test(email);
		}
		
		/* Validate a text area entry.  Returns:
            FALSE if no illegal strings were included (i.e. the regular expression was not matched);
            TRUE if any illegal strings were included (i.e. the regular expression was matched),
                or if nothing was entered */	
		function validTextArea(text)
        {
			var regexp = /\S*(href|http|www|\.com|\.co|\.uk|\.net|\.org)\S*/i;
			return regexp.test(text);
		}
		
		/* Takes in a tag name and checks to see if it's a label.  If is is, add the 'invalid' class name to 
		      its class list 
		function invalidLabel(parentTag)
        {
			if (parentTag.nodeName == "LABEL")
            {
				parentTag.className += " invalid";
			}
		}
		*/
	}
}

