if(Forms == null) var Forms = {};

Forms.Validation = {

	DoValidate	: true
	,	
	IsValid : true
	,
	Setup : function()
	{
		
	}
	,
	Start : function()
	{
		if(Forms.Validation.DoValidate == true)
		{	
			displayFeedback.hide();
			displayFeedback.clear();	
							
			Forms.Validation.IsValid = true;				
							
			Forms.Validation.ValidateRequest("input");
			Forms.Validation.ValidateRequest("textarea");				
								
			if(!Forms.Validation.IsValid)
			{
				displayFeedback.show();
				displayFeedback.focus();
				submitReturn = false;

				if(Forms.Buttons) setTimeout("Forms.Buttons.Reset()", 1000);
				//if(Forms.Buttons) Forms.Buttons.Reset();
			}
		}
	}
	,
	ValidateRequest : function(tagType)
	{
		var allControls = document.getElementById("form").getElementsByTagName(tagType);
	
		for (var i = 0; i <  allControls.length  ; i++)  {
			
			var thisControl = allControls[i];
			
			//check that controls are TEXTBOXES
			if ((thisControl.getAttribute("type") == "text")||(thisControl.getAttribute("type") == "password")||(thisControl.tagName.toLowerCase() == "textarea")){
			
				// check for REQUIRED fields
				if (thisControl.parentNode.className.indexOf("required") >= 0){
					if (thisControl.value==''){
						Forms.Validation.AddError("The ", thisControl, " field is required and must be completed.");			
						Forms.Validation.IsValid = false;
					}
				}
				
				// check for EMAIL field
				if (thisControl.parentNode.className.indexOf("email") >= 0){
					if (!thisControl.value==''){
						if (!Forms.Validation.IsEmail(thisControl.value)){
							Forms.Validation.AddError("The ", thisControl, " field must contain valid email addresses.");
							Forms.Validation.IsValid = false;				
						}
					}
				}
				
				// check for DATE field
				if (thisControl.parentNode.className.indexOf("date") >= 0){
					if (!thisControl.value==''){
						if (!Forms.Validation.IsDate(thisControl.value)){
							Forms.Validation.AddError("The ", thisControl, " field must be a date.");
							Forms.Validation.IsValid = false;				
						}
					}
				}
				
				// check for NUMERIC field			
				if (thisControl.parentNode.className.indexOf("numeric") >= 0){
					if (!thisControl.value==''){
						if (!Forms.Validation.IsNumeric(thisControl.value)){
							Forms.Validation.AddError("The ", thisControl, " field must be a numeric value.");
							Forms.Validation.IsValid = false;				
						}
					}
				}
				
				// check for POSTCODE field			
				if (thisControl.parentNode.className.indexOf("postcode") >= 0){
					if (!thisControl.value==''){
						if (!Forms.Validation.IsPostcode(thisControl.value)){
							Forms.Validation.AddError("The ", thisControl, " field must have a space in it.");
							Forms.Validation.IsValid = false;			
						}
					}
				}				
				
			}// end TEXTBOXES
			
			//check controls are CHECKBOXES
			if(thisControl.getAttribute("type") == "checkbox")
			{
				// check for REQUIRED fields
				var className = "";
				if(thisControl.parentNode.parentNode.tagName.toLowerCase() == "label")
				{
					className = thisControl.parentNode.parentNode.className;				
				}
				else if(thisControl.parentNode.parentNode.parentNode.tagName.toLowerCase() == "label")
				{
					className = thisControl.parentNode.parentNode.parentNode.className;		
				}				
				if (className.indexOf("required") >= 0){
					if (!thisControl.checked){
						Forms.Validation.AddError("The ", thisControl, " field is required and must be checked.");			
						Forms.Validation.IsValid = false;
					}
				}
			}// end CHECKBOXES
						
		}// for		
	
	}
	,
	/* Error Reporting */
	AddError : function(messagePart1, errorControl, messagePart2)
	{
		var newErrorLink = document.createElement("a");
		var title = errorControl.getAttribute("title");
		if(title == null) title = errorControl.parentNode.getAttribute("title");
		newErrorLink.appendChild(document.createTextNode(title));
		newErrorLink.setAttribute("control", errorControl.id);
		newErrorLink.setAttribute("href", "#" + errorControl.id);
		newErrorLink.style.fontWeight = "600";		
		newErrorLink.onclick = function(){
			Forms.Validation.ElementFocus(document.getElementById(this.getAttribute("control")));
			return false;
		}
		
		var newError = document.createElement("span");
		newError.appendChild(document.createTextNode(messagePart1));
		newError.appendChild(newErrorLink);
		newError.appendChild(document.createTextNode(messagePart2));
		
		displayFeedback.addNode(FeedbackKind.Warning, newError);
	}
	,
	ElementFocus : function(element)
	{
		element.focus(); 
		if(element.parentNode.tagName.toLowerCase() == "label")
		{
			document.addCssClass(element.parentNode, "highlight");
		}
		else if(element.parentNode.parentNode.parentNode.tagName.toLowerCase() == "label")
		{
			document.addCssClass(element.parentNode.parentNode.parentNode, "highlight");
		}
		element.onchange = 
		element.onblur = 
		function() { Forms.Validation.ElementRemoveFocus(this) }; 	
	}
	,
	ElementRemoveFocus : function(element)
	{
		if(element.parentNode.tagName.toLowerCase() == "label")
		{
			document.removeCssClass(element.parentNode, "highlight");
		}
		else if(element.parentNode.parentNode.parentNode.tagName.toLowerCase() == "label")
		{
			document.removeCssClass(element.parentNode.parentNode.parentNode, "highlight");
		}
		element.onchange = 
		element.onblur = 
		"";
	}
	,
	
	/* Type Functions */ 	
	IsEmail : function(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;
	}
	,
	IsNumeric : function(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;
	}	
	,
	IsPostcode : function(sText){
		var IsPostcode=true;

		if (sText.indexOf(" ") < 0){
			IsPostcode=false;
		}
		
		return IsPostcode;
	}	
	,
	IsDate : function(sText){
		var IsDate = true;
		
		var validMonths = " January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sept Oct Nov Dec ";
		
		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;

	}	



}
window.addToOnload(Forms.Validation.Setup);
window.addToOnsubmit(Forms.Validation.Start);
