	
	function Validator(frmHelper)
	{
		this.frm = frmHelper;
		
		this.controlToFocus = null;
		
		this.errors = new Array();
		
		this.errorHeader = "Please check the following error(s) : ";
		
		this.TotalErrors = TotalErrors;
		
		this.GetError = GetError;
		
		this.SetError = SetError;
		
		this.AddError = AddError;
		
		this.GetErrorHeader = GetErrorHeader;
		
		this.SetErrorHeader = SetErrorHeader;
		
		this.IsValid = IsValid;
		
		this.ValidateEmptyField = ValidateEmptyField;
		
		this.ValidateRegularExpression = ValidateRegularExpression;
		
		this.ValidateNumericField = ValidateNumericField;
		
		this.ValidateNumberField = ValidateNumberField;
		
		this.ValidateNumericRange = ValidateNumericRange;
		
		this.ValidateEmailField = ValidateEmailField;
		
		this.ValidateZipField = ValidateZipField;
		
		this.ValidateCheckGroup = ValidateCheckGroup;
		
		this.CustomValidation = CustomValidation;
		
		this.ValidateEqual = ValidateEqual;
		
		this.DisplayErrors = DisplayErrors;
		
		this.PrintErrors = PrintErrors;
		
	}
	
	function TotalErrors()
	{
		return this.errors.length;
	}
	
	function GetError(intIndex)
	{
		return this.errors[intIndex];
	}
	
	function SetError(intIndex, strMessage)
	{
		this.errors[intIndex] = strMessage;
	}
	
	function AddError(strMessage)
	{
		var intNextIndex = this.TotalErrors();
		this.errors[intNextIndex] = strMessage;
	}
	
	function GetErrorHeader()
	{
		return this.errorHeader;
	}
	
	function SetErrorHeader(strHeader)
	{
		this.errorHeader = strHeader;
	}
	
	function IsValid()
	{
		return (this.TotalErrors() > 0)? false : true;
	}
	
	function ValidateEmptyField ( ctlToValidate, strMessage )
	{
		var strValueToValidate = this.frm.GetControlValue(ctlToValidate);
		
		if (strValueToValidate.match(/^\s+$/) || strValueToValidate == "")
		{
			if(this.controlToFocus == null)
			{
				this.controlToFocus = 	ctlToValidate;
			}
			this.AddError(strMessage);
			return false;
		}
		else
		{
			return true;
		}	
	}
	
	function ValidateRegularExpression ( ctlToValidate, strRegExp, strMessage)
	{
		var strValueToValidate = this.frm.GetControlValue(ctlToValidate);
		
		if(strValueToValidate == "")
		{
			return true;
		}
		
		if (strValueToValidate.match(strRegExp))
		{
			return true;
		}
		else
		{
			if(this.controlToFocus == null)
			{
				this.controlToFocus = 	ctlToValidate;
			}
			this.AddError(strMessage);
			return false;
		}
	}
	
	function ValidateNumberField(ctlToValidate, strMessage)
	{
		var strValueToValidate = this.frm.GetControlValue(ctlToValidate);
		
		if(isNaN(strValueToValidate))
		{
			this.AddError(strMessage);
			return false;
		}
		
		if(strValueToValidate.indexOf(".") != -1)
		{
			if(this.controlToFocus == null)
			{
				this.controlToFocus = 	ctlToValidate;
			}
			this.AddError(strMessage);
			return false;
		}
		
		return true;
	}
	
	function ValidateNumericField (ctlToValidate, strMessage)
	{
		var strValueToValidate = this.frm.GetControlValue(ctlToValidate);
		
		if (isNaN(strValueToValidate))
		{
			if(this.controlToFocus == null)
			{
				this.controlToFocus = 	ctlToValidate;
			}
			this.AddError(strMessage);
			return false;
		}
		else
		{
			return true;
		}
	}
	
	function ValidateNumericRange (ctlToValidate, intMaxValue, intMinValue, strMessage)
	{
		var intValueToValidate = this.frm.GetControlValue(ctlToValidate);
		
		if(intValueToValidate == "")
		{
			return true;
		}
		
		if(intValueToValidate > intMaxValue || intValueToValidate < intMinValue)
		{
			if(this.controlToFocus == null)
			{
				this.controlToFocus = 	ctlToValidate;
			}
			this.AddError(strMessage);
			return false;
		}
		else
		{
			return true;
		}
	}
	
	function ValidateEmailField (ctlToValidate, strMessage)
	{
		return this.ValidateRegularExpression(ctlToValidate, /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/, strMessage);
		
	}
	
	function ValidateZipField (ctlToValidate, strMessage)
	{
		var strValueToValidate = this.frm.GetControlValue(ctlToValidate);
		
		if(strValueToValidate == "")
		{
			return true;
		}
		
		if (strValueToValidate.match(/\d{5}(-\d{4})?/))
		{
			return true;
		}
		else
		{
			if(this.controlToFocus == null)
			{
				this.controlToFocus = 	ctlToValidate;
			}
			this.AddError(strMessage);
			return false;
		}
	}
	
	
	function ValidateEqual(ctlToValidate, ctlToCompare, strMessage)
	{
		var strValueToValidate = this.frm.GetControlValue(ctlToValidate);
		var strValueToCompare = this.frm.GetControlValue(ctlToCompare);
		
		if(strValueToValidate == "")
		{
			return true;
		}
		
		if(strValueToValidate != strValueToCompare)
		{
			if(this.controlToFocus == null)
			{
				this.controlToFocus = 	ctlToValidate;
			}
			
			this.AddError(strMessage);
			
			return false;
		}
		
		return true;
	}
	
	
	function ValidateCheckGroup(strControlName, strMessage)
	{
		var controls = this.frm.findControls(strControlName);
		
		for(var i = 0; i < controls.length; i++)
		{
			var ctl = controls[i];
			
			if(ctl.checked)
			{
				return true;	
			}
		}
		
		this.AddError(strMessage);
		
		return false;
	}
	
	
	function CustomValidation(strFunctionName, strMessage)
	{
		var result = eval(strFunctionName);
		if(result)
		{
			return true;
		}
		else
		{
			this.AddError(strMessage);
			return false;
		}
	}
	
	function PrintErrors(lrErrors)
	{
		if(this.IsValid())
		{
			return;
		}
		
		var strTableStart = "<table cellpadding=0 width=100%>";
		var strMessages = "";
		
		for(var i = 0; i < this.TotalErrors(); i++)
		{
			strMessages += "<tr>";
			strMessages += "<td style='color:red;'>";
			strMessages += "  * " + this.GetError(i) + "\n";
			strMessages += "</td>";
			strMessages += "</tr>";
		}
		
		if(this.GetErrorHeader() != "")
		{
			var	headerRow = "<tr>";
			headerRow += "<td>";
			headerRow += "<font color='red'><b>" + this.GetErrorHeader() + "</b></font>";
			headerRow += "</td>";
			headerRow += "</tr>";
			headerRow += "<tr>";
			headerRow += "<td height='2'>";
			headerRow += "</td>";
			headerRow += "</tr>";
			strMessages = headerRow + strMessages;
			
		}
		
		strMessages = strTableStart + strMessages + "</table>";

		this.frm.SetLayerHtml(lrErrors, strMessages);
		
		window.scroll(0,0);
		
		this.frm.PutFocus(this.controlToFocus);
	}
	
	function DisplayErrors()
	{
		if(this.IsValid())
		{
			return;
		}
		
		var strMessages = "";
		
		for(var i = 0; i < this.TotalErrors(); i++)
		{
			strMessages += " * " + this.GetError(i) + "\n";
		}
		
		if(this.GetErrorHeader() != "")
		{
			var header = this.GetErrorHeader()  + "\n\n";
			strMessages = header + strMessages;
		}
		
		alert(strMessages);
		
		frm.PutFocus(this.controlToFocus);
	}
	