
var removeHighlightMe;
var allOk;

removeHighlightMe = function(){
	//alert('remove highlight');
	this.className=this.className.replace(" highlight", "");
	this.onchange = "";
}

function highlightMe(element){ 
	element.focus(); 
	element.className+=" highlightElem";
	element.onchange = removeHighlightMe;
}

function highlightMeByID(elementID){
	var elementObj = document.getElementById(elementID);
	highlightMe(elementObj);
	return false;
}

function isEmail(strng) {
  var error = ""
  var flag = true;
  var emailFilter=/^.+@.+\..{2,3}$/;
  if (!(emailFilter.test(strng))) { 
    flag = false;
  }
  var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
  if (strng.match(illegalChars)) {
    flag = false;
  }
  return flag;
}

function displayErrorReport(message){
	// Put 'errorReport' into object
	var errorReport = document.getElementById("errorReport");
	//Check for any previose errorReport content
	if (errorReport.innerHTML != ""){
	errorReport.innerHTML += "<br />";
	}	
	errorReport.innerHTML += message;
	errorReport.className = " highlight";
	window.location = "#errorReport";
}
 
function validateRequest(tagType, allOk){
		
		var navRoot = document.getElementById("form").getElementsByTagName(tagType);

		// Loop through all 'tagType's in form
		for (var i = 0; i <  navRoot.length  ; i++)  {
			// check for REQUIRED fields
			if (navRoot[i].className.indexOf("required") >= 0){

				// CHECK TEXT BOXES AND TEXTAREAS
				if ((navRoot[i].getAttribute("type") == "text")||(navRoot[i].tagName.toLowerCase() == "textarea")){
					// check for empty
					if (navRoot[i].value==''){
						displayErrorReport(formErrorTextPart1 + '<a href="#" onclick="highlightMeByID(\'' + navRoot[i].id + '\')">' + navRoot[i].getAttribute("title") + '</a>' + formErrorTextPart2);
						// not used on cranmore: //highlightMe(navRoot[i]);
						allOk = "false";
					}else{ // else check other stuff
						// check for email field
						if ((navRoot[i].id == "email")||(navRoot[i].className.indexOf("email") >= 0)){
							// check email valid
							if (!isEmail(navRoot[i].value)){
								displayErrorReport(formErrorEmailPart1 + '<a href="#" onclick="highlightMeByID(\'' + navRoot[i].id + '\')">' + navRoot[i].getAttribute("title") + '</a>' + formErrorEmailPart2);
								// not used on cranmore: //highlightMe(navRoot[i]);
								allOk = "false";					
							}
						}		
					}
				}
				
				// CHECK CHECK BOXES
				if (navRoot[i].getAttribute("type") == "checkbox"){
					// check for not checked
					if (!navRoot[i].checked){
						displayErrorReport(formErrorCheckPart1  + navRoot[i].getAttribute("title") + formErrorCheckPart2);
						// not used on this site: //highlightMe(navRoot[i]);
						allOk = "false";					
					}		
				}
					
			}// if required field
						
		}// for
		
		return allOk;
		
}// function

function addValidation(){
		allOk = "true";
		
		//Empty current errorReport  to start new.
		document.getElementById("errorReport").innerHTML = "";	
		
		allOk = validateRequest("input", allOk);
		allOk = validateRequest("textarea", allOk);
		
		if(allOk == "false"){submitReturn = "false"};
}//end function

addToOnsubmit("addValidation()");
