
var okValidate = "true";

function SetupButtons(){
	var allButtons = document.getElementsByClassName("button");
	for(var i = 0 ; i < allButtons.length; i++) {
		if(allButtons[i].className.indexOf("noValidate") >= 0) {
			allButtons[i].onclick = function(){okValidate = "false";}
		}else{
			allButtons[i].onclick = function(){okValidate = "true";}			
		}
	}
}

var errorReport;
var theForm;

function SetupErrorReportObject(){
	errorReport = document.createElement("div");
	errorReport.id = "errorReport";
	errorReport.className = "displayFeedback warning";
	errorReport.style.display = "none";
	errorReport.appendChild(document.createElement("h4"));
	errorReport.firstChild.appendChild(document.createTextNode("Warning"));
	
	errorReport.appendChild(document.createElement("span"));	//will be removed on first use
	
	theForm = document.getElementById("form");
	theForm.parentNode.insertBefore(errorReport, theForm);
	
}

//addToLoader("SetupButtons()"); //skiping this for now - dont need the noValidate functionality yet - messes up Please Wait Buttons.
addToLoader("SetupErrorReportObject()");


var allOk;
var validateBox = new Array();
 
function validateRequest(tagType){
		
	for (var a = 0; a <  validateBox.length  ; a++)  {
	
		var currentBox = document.getElementById(validateBox[a]);
		
		var allControls = currentBox.getElementsByTagName(tagType);

		for (var i = 0; i <  allControls.length  ; i++)  {
			
			var thisControl = allControls[i];
			
			if(currentBox.id != "form"){
				thisControl.setAttribute("contentbox", currentBox.id);
			}else{
				thisControl.setAttribute("contentbox", "");
			}
			
			//check that controls are TEXTBOXES
			if ((thisControl.getAttribute("type") == "text")||(thisControl.tagName.toLowerCase() == "textarea")){
			
				// check for REQUIRED fields
				if (thisControl.className.indexOf("required") >= 0){
					if (thisControl.value==''){
						addToErrorReport("The ", thisControl, " field is required and must be completed.");			
						allOk = "false";
					}
				}
				
				// check for EMAIL field
				if (thisControl.className.indexOf("email") >= 0){
					if (!thisControl.value==''){
						if (!IsEmail(thisControl.value)){
							addToErrorReport("The ", thisControl, " field must contain valid email addresses.");
							allOk = "false";					
						}
					}
				}
				
				// check for DATE field
				if (thisControl.className.indexOf("date") >= 0){
					if (!thisControl.value==''){
						if (!IsDate(thisControl.value)){
							addToErrorReport("The ", thisControl, " field must be a date.");
							allOk = "false";					
						}
					}
				}
				
				// check for NUMERIC field			
				if (thisControl.className.indexOf("numeric") >= 0){
					if (!thisControl.value==''){
						if (!IsNumeric(thisControl.value)){
							addToErrorReport("The ", thisControl, " field must be a numeric value.");
							allOk = "false";					
						}
					}
				}
				
			}// end TEXTBOXES
						
		}// for	
	
	}
				
}// function

function StartValidation(){

	if (typeof contentArray != 'undefined') {
		validateBox = contentArray;
	}else{
		validateBox.length = 0;
		validateBox.push("form");
	}

	allOk = "true";		
	if(okValidate == "true"){
		validateRequest("input");
		validateRequest("textarea");
		if(allOk == "false"){
			submitReturn = "false";
			displayErrorReport();
			if(typeof pleaseWait != 'undefined'){pleaseWait.parentNode.removeChild(pleaseWait);}
		}					
	}
		
}//end function

addToOnsubmit("StartValidation()");

/* ERROR REPORT */

	var errorReportArray = new Array();
	
	function addToErrorReport(messagePart1, errorControl, messagePart2){
		//add new error to list
		
		var newErrorLink = document.createElement("a");
		if (errorControl.getAttribute("contentbox").length > 0){
			var contentbox = document.getElementById(errorControl.getAttribute("contentbox"));
			var boxtitle = contentbox.getElementsByTagName("span")[0].innerHTML;
			newErrorLink.appendChild(document.createTextNode(boxtitle + " - "));
			newErrorLink.setAttribute("contentbox", contentbox.id);
		}else{
			newErrorLink.setAttribute("contentbox", "");		
		}
		newErrorLink.appendChild(document.createTextNode(errorControl.getAttribute("title")));
		newErrorLink.setAttribute("control", errorControl.id);
		newErrorLink.setAttribute("href", "#");
		newErrorLink.onclick = function(){
			if(this.getAttribute("contentbox").length != 0){
				linkArray[validateBox.indexOf(this.getAttribute("contentbox"))].onclick();
			}
			highlightMeByID(this.getAttribute("control"));
			return false;
		}
		newErrorLink.style.fontWeight = "600";
		
		var newError = document.createElement("li");
		newError.appendChild(document.createTextNode(messagePart1));
		newError.appendChild(newErrorLink);
		newError.appendChild(document.createTextNode(messagePart2));
		
		errorReportArray.push(newError);		
	}
	
	function displayErrorReport(){
		// Turn it on
		errorReport.style.display = "block";
		// Remove Old List (or default lastChild added onload)
		errorReport.removeChild(errorReport.lastChild);

		// Create new list	
		var errorList = document.createElement("ul");
		// Loop through errors in array - add to list
		for (i = 0; i < errorReportArray.length; i++) {
			errorList.appendChild(errorReportArray[i]);
		}
		// add list to errorReport
		errorReport.appendChild(errorList);	
		
		//clear error array
		errorReportArray.length = 0;
		// focus on errorReport
		window.location = "#errorReport";
	}
	

/* HIGHLIGHT FUNCTIONS */

	var removeHighlightMe;

	removeHighlightMe = function(){
		this.className=this.className.replace(" highlightElem", "");
		this.onchange = "";
	}
	
	function highlightMeByID(elementID){highlightMe(document.getElementById(elementID));}	
	function highlightMe(element){ 
		element.focus(); 
		if(element.className.indexOf("highlightElem") == -1){
		element.className+=" highlightElem";		
		}
		element.onchange = removeHighlightMe; 
		//only remove highlight on change, so signify a new value entered. otherwise it must still be wrong.
	}
	
	
/* TYPE FUNCTIONS */

	function IsEmail(sText) {
	  var IsEmail = true;
	  var strings = sText.split(";");
	  for (var i = 0; i <  strings.length; i++)  {
	  	  var thisString = strings[i];
		  if (thisString.length > 0){
			  var emailFilter=/^.+@.+\..{2,3}$/;
			  if (!(emailFilter.test(thisString))) { 
			    IsEmail = false;
			  }
			  var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
			  if (thisString.match(illegalChars)) {
			    IsEmail = false;
			  }			  
		  }
	  }
	  return IsEmail;
	}
	
	function IsNumeric(sText){
		var ValidChars = "0123456789.";
		var IsNumber=true;
		var Char;
		for (i = 0; i < sText.length && IsNumber == true; i++) { 
			Char = sText.charAt(i); 
			if (ValidChars.indexOf(Char) == -1) {
			   IsNumber = false;
			}
		}
		return IsNumber;
	}	
	
	function IsDate(sText){
		var IsDate = true;
		
		var validMonths = "January February March April May June July August September October November December";
		
		var strings = sText.split(" ");
		if ((strings.length > 3)||(strings.length < 2)){
			IsDate = false;
		}
		
		if(strings.length >= 2){
			if (!IsNumeric(strings[0])){
				IsDate = false;
			}
			if (validMonths.indexOf(strings[1]) == -1){
				IsDate = false;
			}		
		}		
			
		if(strings.length == 3){	
			if (!IsNumeric(strings[2])){
				IsDate = false;
			}	
		}	
				
		return IsDate;

	}	
