/*********************** FORM VALIDATION **************************/	
	
//When form is submitted check for valid fields and stop form being sent if necessary
function formGood() {
  
	//Declare some variables
	var allTagsValid = true; //Are all the required fields are filled in?
	var invalidFieldNames = ""; //All the empty or invalid required fields
	var reqdFields = document.getElementById("required").value; //String of the required fields (i.e. the value of the hidden 'required' field from the form)
	var errors = new Array(); //Build a list of what the errors are
	var errmsg = ""; //The alert shown to the user
	var field = ""; //The name of the field in the error alert
		
	//Most of this script is about these three statements so might as well create a function!
	function makeInvalid(tag) {
	   allTagsValid = false;
		 tag.parentNode.parentNode.className = "invalid";
		 invalidFieldNames += tag.id + " ";
	}
	
	//Make an array of all input tags on page 
  var allTags = document.getElementsByTagName("INPUT");
 
	for (var i=0; i<allTags.length; i++) {
			//assign an onchange handler to remove invalid class when a value is entered
			allTags[i].onchange = resetClass;
	    
			//See if the input's name is in the reqdFields string, and if so, and it 
	    //is empty, set allTagsValid to false, give the parent's parent element (i.e. the table row) 
	    //a class of "invalid" and add the tag's id to the invalidFields variable. 
			if ((reqdFields.indexOf(allTags[i].name) >= 0) && (allTags[i].value=="")) {
				 makeInvalid(allTags[i]);
				 errors[allTags[i].name] = "missing";
			} //if
			//If a value is given, check that it is valid
			else if (allTags[i].value != "") {
			   switch(allTags[i].name) {
				    case "fullname":
						   var re = /^[a-zA-Z \.\-']+ ([a-zA-Z \.\-']+)+$/;
							 if (!re.test(allTags[i].value)) {
		  				    makeInvalid(allTags[i]);	
									errors[allTags[i].name] = "invalid";
							 }
							 break;
					  case "postcode":
						   var re = /^\w+[\w \.\-]+$/;
							 if (!re.test(allTags[i].value)) {
		  				    makeInvalid(allTags[i]);	
									errors[allTags[i].name] = "invalid";
							 }
							 break;
						case "tel":
						   var re = /^[0-9 \.\-\(\)]+$/;
							 if (!re.test(allTags[i].value)) {
		  				    makeInvalid(allTags[i]);
									errors[allTags[i].name] = "invalid";	
							 }
							 break;
				    case "email":
						case "Email":   //Have to have upper case as well as used by Pommo software
						case "newemail":
						case "newemail2":
				       var re = /^[\w ]*\w+([\.\-\+'&]{0,3}\w+)*@\w+([\.-]?\w+)*(\.\w{2,5})+$/;
							 if (!re.test(allTags[i].value)) {
		  				    makeInvalid(allTags[i]);	
									errors[allTags[i].name] = "invalid";
							 }
						   break;
						case "url":
						   var re = /^http(s)?:\/\/\w+([\.-]?\w+)*(\.\w{2,5})+[\w_\/\-\.]*$/;
							 if (!re.test(allTags[i].value)) {
		  				    makeInvalid(allTags[i]);	
									errors[allTags[i].name] = "invalid";
							 }
							 break;
				 } //switch
				 
			} //elseif
			
	} //for
	
	//Now do the same for textareas - can't concat() with allTags as nodelists not real arrays
	var allTexts = document.getElementsByTagName("TEXTAREA");

	for (var i=0; i<allTexts.length; i++) {
	    allTexts[i].onchange = resetClass;
			if ((reqdFields.indexOf(allTexts[i].name) >= 0) && (allTexts[i].value=="")) {
				 makeInvalid(allTexts[i]);	
				 errors[allTexts[i].name] = "missing";
			}
	}
	
	
	//Similarly for select boxes
	var allSelects = document.getElementsByTagName("SELECT");

	for (var i=0; i<allSelects.length; i++) {
	    allSelects[i].onchange = resetClass;
			
			if ((reqdFields.indexOf(allSelects[i].name) >= 0) && (allSelects[i].selectedIndex == "0" || allSelects[i].selectedIndex == "1")) {
				 makeInvalid(allSelects[i]);	
				 allSelects[i].className = "invalid"; //Select box itself needs a class of 'invalid' as well as table row
				 errors[allSelects[i].name] = "missing";
			}
	}
	
	//If anything still needs to be done:
	if (allTagsValid == false) {
	   
		 //Set the proper name of the field (to appear in the error message)
		 var properFieldNames = new Array();
		 properFieldNames['fullname'] = "full name";
		 properFieldNames['linkname'] = "name of the link";
		 properFieldNames['full_hour'] = "cost of an hr session";
		 properFieldNames['half_hour'] = "cost of a 1/2 hr session";
		 properFieldNames['howbookselect'] = "how to book option";
		 properFieldNames['how_to_book_short'] = "short how to book option";
		 

		 //Make an array of the required fields from the required fields string
		 var reqdFieldsArray = reqdFields.split(" ");
		 
		 //Loop through the fields that appear in the invalidFieldNames string to find which are not filled in correctly
		 for (var j=0; j<reqdFieldsArray.length; j++) {
		    
	      //See if the error message should display a different name from the basic field name in the form
				if (properFieldNames[reqdFieldsArray[j]]) field = properFieldNames[reqdFieldsArray[j]];
				else field = reqdFieldsArray[j].replace(/_/g, " ");
				
				//See if this is a public page - i.e. do we display "your" address or "the" address
				var prefix = (document.getElementById("public") ? "your" : "the"); 
				
				//Build error message
				if (errors[reqdFieldsArray[j]] == "missing") errmsg += "Please fill in " + prefix + " " + field + "\n";   
				else if (errors[reqdFieldsArray[j]] == "invalid") errmsg += "The " + field + " you have entered seems to be invalid \n";
		 } //for
		 
		 //Find the first required field that appears in the invalidFieldNames 
		 //string and set the cursor to that input/textarea
		 for (var k=0; k<reqdFieldsArray.length; k++) {
		    if (invalidFieldNames.indexOf(reqdFieldsArray[k]) >= 0) {
				   document.getElementById(reqdFieldsArray[k]).focus();
					 break;
				} //if
		 } //for
  } //if
	
	if (errmsg != "") alert(errmsg);
	return allTagsValid;

}

//Function resets parent element's class to "" (called when tag changed)
function resetClass() {
  this.parentNode.parentNode.className = "";
	this.className = ""; //Reset select boxes (tag has invalid class as well as row)
}
