
var removeHighlightMe;
var allOk;
var buttonID;


thisButtonClick = function() {
				buttonID=this.id;
				//alert("buttonclick " + this.id);
}

function setUpButtonClicks(){
	var allButtons = document.getElementsByTagName('input');
	for(var c = 0 ; c < allButtons.length; c++) {
		//alert("input found " + allButtons[c].id);
		if(allButtons[c].type=='submit') {
			allButtons[c].onclick = thisButtonClick;
			//alert("button found " + allButtons[c].id);
		}
	}
}

addToLoader("setUpButtonClicks()");


removeHighlightMe = function(){
	this.className=this.className.replace(" highlight", "");
	this.onchange = "";
}

function highlightMe(element){ 
	element.focus(); 
	element.className+=" highlightElem";
	element.onchange = removeHighlightMe; //only remove highlight on change, so signify a new value entered. otherwise it must still be wrong.
}

function highlightMeByID(elementID){
	var elementObj = document.getElementById(elementID);
	highlightMe(elementObj);
}

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;
}

var errorReportArray = new Array();

function addToErrorReport(message){
	//add new error to list
	errorReportArray.push(message);
}

function displayErrorReport(){
	// Put 'errorReport' into object
	var errorReport = document.getElementById("errorReport");
	errorReport.className = "displayFeedback error"
	var errorString = ""
	
	errorString = "<ul>";
	// Loop through errors in array - add to string
	//  not to innerHTML - that breaks it.
	for (i = 0; i < errorReportArray.length; i++) {
		errorString+= "<li>" + errorReportArray[i] + "</li>";
	}
	errorString += "</ul>";
	// not add to innerHTML
	errorReport.innerHTML = errorString;
	
	//clear error array
	errorReportArray.length = 0;
	// focus on errors on page
	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==''){
						addToErrorReport(formErrorTextPart1 + '<a href="#" onclick="highlightMeByID(\'' + navRoot[i].id + '\'); return false;">' + navRoot[i].getAttribute("title") + '</a>' + formErrorTextPart2);
						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)){
								addToErrorReport(formErrorEmailPart1 + '<a href="#" onclick="highlightMeByID(\'' + navRoot[i].id + '\'); return false;">' + navRoot[i].getAttribute("title") + '</a>' + formErrorEmailPart2);
								allOk = "false";					
							}
						}		
					}
				}
				
				// CHECK CHECK BOXES
				if (navRoot[i].getAttribute("type") == "checkbox"){
					// check for not checked
					if (!navRoot[i].checked){
						addToErrorReport(formErrorCheckPart1  + navRoot[i].getAttribute("title") + formErrorCheckPart2);
						// not used on this site: //highlightMe(navRoot[i]);
						allOk = "false";					
					}		
				}
					
			}// if required field
						
		}// for
		
		return allOk;
		
}// function

function startValidation(){

		allOk = "true";		
		//alert(document.getElementById(buttonID).className);		
		if(!(document.getElementById(buttonID).className.indexOf("noValidate") >= 0)){
			//Empty current errorReport  to start new.
			document.getElementById("errorReport").innerHTML = "";	
			allOk = validateRequest("input", allOk);
			allOk = validateRequest("textarea", allOk);
			
			if(allOk == "false"){
				submitReturn = "false"
				displayErrorReport();
			};							
		}
		
}//end function

addToOnsubmit("startValidation()");
