//global variables
var errorID = "errormsg";
var errDiv = "formErrMsg";
var webSite = "http://www.wrh.org.uk/";

function toggleDiv(divid){
    if(document.getElementById(divid).style.display == 'none'){
      document.getElementById(divid).style.display = 'block';
    }else{
      document.getElementById(divid).style.display = 'none';
    }
  }
  
  
function addHints(field){
		 //get the first field
		 var firstField = document.getElementById(field);
		 //set the focus to this field
		 firstField.focus();
		 //show the hint on the first field
		 firstField.parentNode.getElementsByTagName("span")[0].style.display = "inline";
		 //set the style on all the input fields
		 var inputs = document.getElementsByTagName("input");
		 for (var i=0; i<inputs.length; i++){
		 	 if (inputs[i].parentNode.getElementsByTagName("span")[0]){
			 	//on focus function
				inputs[i].onfocus = function (){
					this.parentNode.getElementsByTagName("span")[0].style.display ="inline";
				}
				//on blur function
				inputs[i].onblur = function(){
					this.parentNode.getElementsByTagName("span")[0].style.display = "none";
				}
			 }
		 }
		 //get the select boxes
		 var selects = document.getElementsByTagName("select");
		 for (var j=0; j<selects.length; j++){
		if(selects[j].parentNode.getElementsByTagName("span")[0]){
		
			selects[j].onfocus = function(){
				this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			}
			selects[j].onblur = function(){
				this.parentNode.getElementsByTagName("span")[0].style.display = "none";
			}
		}
	}
		
}


function validateFormOnSubmit(form){
	var isValid = true;
	var wholeErrMsg = "The following errors must be sorted before the form can be submitted:";
	var thisForm = form;
	var str = "";
	var ft = "";
 	var fv = "";
 	var fn = "";
 	var els = "";
 	var errCount = 0;
	
	//get rid of any error messages currently on the screen
 	clearErr(thisForm);
	
	//set up the error div in case we need it later
	newDiv = document.createElement('div');
	newDiv.id = errorID;
	//create a paragraph
	newP = document.createElement('p');
	//create a text node
	newText = document.createTextNode(wholeErrMsg);
	newP.appendChild(newText);
	
	//loop through fields
	for(var i = 0;i < form.elements.length;i++) {
		var thisErrMsg = "";
		var thisErrFlag = 0;
  		els = form.elements[i];
  		ft = els.title;
  		fv = els.value;
  		fn = els.name;
  		fs = els.selectedIndex
		
		switch(els.type) {
			case "text":
  			case "hidden":
  			case "password":
  			case "textarea":
				 // is it a required field?
  				if(encodeURI(ft) == "required")  {
					if (encodeURI(fv).length < 1){
        			   //it must have a value in it
					   		errType = "n";	  
							thisErrFlag = 1;
					 }
				}
				if (encodeURI(fv).length > 1){
								//if it has a value is it valid
								if (fn == "email"){
									   //check its a valid email address
									   errValid = checkValidEmail(fv);
				  					    if(!errValid){
													   //the email is not valid
													    errType = "v";
														 thisErrFlag = 1;
				 						}
									}
				}
			break;
			case "checkbox":
				 if (els.checked == true){ 
				 		if (ft == "Other"){
						   //there needs to be a value in the other field
						   if (form.other.value == ""){
						   	  errType = "o";
							  thisErrFlag = 1;
						    }
						 }
				}
			break;
		}//end of switch
		if (thisErrFlag > 0){
		   //there are errors
		   //add the error message
		   errCount++;	
		   var thisErrMsg = addErr(els, errType, "field");
		  //add the error message to the error div
		  newP = addLineToErrMsg(newP, thisErrMsg);
		  //add the error message from the last field
			wholeErrMsg += thisErrMsg;
		}
	}// end of for loop
	
	if (errCount > 0){
	   isValid = false;
	   newDiv.appendChild(newP);
		document.getElementById(errDiv).appendChild(newDiv);
	}
	return isValid;
}


//*****************************************************************************************
//*****************************************************************************************
//function to clear error fields from the form
function clearErr(form){
		 if(document.getElementById(errorID)){
		 	//set up the variables
			var errorDivName = "";
			var img = "";
			var inputField = "";
			//there are errors on the screen
			var errDivMsg = document.getElementById(errorID);
			//delect the message
			errDivMsg.parentNode.removeChild(errDivMsg);
			//now get the error images
			for(var k = 0;k < form.elements.length;k++) {
					els = form.elements[k];
					ft = els.title;
					if (els.className == "error"){
					   //change the class
					   els.className = "";
					   //get the name of the error field
					   fn = els.id;
					   errorDivName = fn+"Err";
					   //get the field
					   f = document.getElementById(errorDivName);
					   delete_img(f)
					}				
			}
		 }
}

//**********************************************************************************
//**********************************************************************************
function delete_img(field){
	//has the field got an image
	children = field.hasChildNodes();
	if(children){
		//yes it has got an image
		img = field.firstChild;
		//delete it
		img.parentNode.removeChild(img);
	}
}

//*****************************************************************************************
//*****************************************************************************************
function addErr(field, type, input_type){
	//set up the variables
	var returnMsg = ""
	var fieldName1 = ""
	var errorDivName = ""
	var fieldName = ""
	var errorAlt = "Error";
	var errorSrc = webSite+"images/error.gif";
	var errorTitle = "";
	var errorClass = "error";
	var errorSrcTitle = "";
	var errFieldMsg = "";
	var errValidMsg = "";
	var errEMailMsg = "The value you entered is not a proper email address";
	
	if (field.title == "Other"){
	   errorDivName = "otherErr";
	   document.getElementById("other").setAttribute("class", errorClass);
	 } else {
	   		errorDivName = field.id+"Err";
	   		//change the class of the field
			 field.className = errorClass;
			 fieldName = field.name;
	  }
	errFieldMsg = fieldName;
	switch(fieldName){
					  
	}
	//what type of error message is it
	switch(type){
		case 'n':
			errorTitle = "You must enter a value in the "+ errFieldMsg + " field.";
		break;
		case 's':
			errorTitle = "You must select a " + errFieldMsg;
		break;
		case 'v':
			errorTitle = "Invalid format in the " + errFieldMsg + " field";
		break;
		case 'o':
			errorTitle = "You have selected Other on the Interests checkbox and have not entered a value in the Other field";
		break;
	}
	//create the error message image
	var img = document.createElement("img");
	img.src = errorSrc;
	img.alt = errorAlt;
	//create the error message text
	returnMsg = errorTitle;
	img.title = errorTitle;
	//add the image to the screen
	document.getElementById(errorDivName).appendChild(img);
	return returnMsg;
	
}

//*****************************************************************************************
//*****************************************************************************************
function addLineToErrMsg(para, errMsg){
	newBr = document.createElement("br");
	para.appendChild(newBr);
	newText = document.createTextNode(errMsg);
	para.appendChild(newText);
	return para;
}

//*****************************************************************************************
//*****************************************************************************************
//function to check valid email
function checkValidEmail(email){
	var status = true;
	var at="@"
	var dot="."
	var lat=email.indexOf(at)
	var lstr=email.length
	var ldot=email.indexOf(dot)
	
	if (email.indexOf(at)==-1){
		status = false;
	}
	if (email.indexOf(at)==-1 || email.indexOf(at)==0 || email.indexOf(at)==lstr){
		status = false
	}
	if (email.indexOf(dot)==-1 || email.indexOf(dot)==0 || email.indexOf(dot)==lstr){
		status = false;
	}
	if (email.indexOf(at,(lat+1))!=-1){
		status = false;
	}
	if (email.substring(lat-1,lat)==dot || email.substring(lat+1,lat+2)==dot){
		status = false;
	}
	if (email.indexOf(dot,(lat+2))==-1){
	    status = false;
	 }
	if (email.indexOf(" ")!=-1){
		status = false;
	 }

	return status;
}
