function trim(strParam)
{
	do
	{
		if(strParam.charAt(0)==" ")
		{
			strParam=strParam.replace(" ","") ;
		}
	}
	while (strParam.charAt(0)==" ")

	nCX=strParam.length-1 ;
	while(strParam.charAt(nCX)==" ")
	{
		nCX=nCX-1 ;
	}
	strParam=strParam.slice(0,nCX+1) ;

	return strParam ;
}

function FormValidator()
{
	// ----------- Enum Declarations ---------------------------------------------
	
	this.INTEGER		=	101	;	// Data Type Constants
	this.DECIMAL		=	102	;
	this.TEXT			=	103 ;
	this.DATE			=	104	;
	this.TIME			=	105	;
	this.USTEL			=	106	;
	this.CURRENCY 		=	107	;
	this.EMAIL			=	108	;
	this.SSN			=	109	;
	this.NAME			=	110	;
	this.FIRST_NAME		=	111	;
	this.MIDDLE_NAME	=	112	;
	this.LAST_NAME		=	113	;
	this.US_ZIPCODE		=	114	;
	
	this.COMBOBOX		=	201	;	// Form Element Type Constants
	this.TEXTBOX		=	202	;
	this.LISTBOX		=	203	;
	this.RADIO			=	204	;
	this.CHECKBOX		=	205	;
	this.DATECONTROL	=	206	;
	this.TIMECONTROL	=	207	;
	this.USTELCONTROL	=	208	;
	this.NONE			=	209 ;
	
	this.ERR_DISPLAY_INDIVIDUAL		=	301	;	
	this.ERR_DISPLAY_CUMULATIVE		=	302	;
		
	// ----------- Class Properties ----------------------------------------------
	
	this.formElement = null ;			
	this.fieldName = "" ;			
	this.dataType = null ;
	this.elementType = null ;
	this.required = false ;
	
	this.valueToValidate = ""
	
	this.minValue = null ;
	this.maxValue = null ;
	this.minCharLength = null ;
	this.maxCharLength = null ;
	
	this.allowNegative = false ;
	this.allowZero = true ;
	this.allowNewline = true ;
	this.decimalDigits = 2 ;
	this.specialFormat = "" ;

	this.noSelectionIndex = 0 ;
	this.USTelSeparator = "-" ;

	this.setFocusOnError = true ;
	this.errorDisplayMode = this.ERR_DISPLAY_CUMULATIVE ;
	
	// ----------- Class Methods ---------------------------------------------------
	
	this.validate = validate ;
	this.newValidation = newValidation ;
	this.reset = reset ;
	this.insertErrorMessage = insertErrorMessage ;
	this.displayErrors = displayErrors ;
	this.getErrorCount = getErrorCount ;
	this.resetValidations = resetValidations ;
	
	// ----------- Private Members -------------------------------------------------
	
	var arrErrorMessages, nErrorCount ;
	
	arrErrorMessages = new Array() ;
	nErrorCount = 0 ;
	
	// ----------- Private Functions -----------------------------------------------
	
	function processErrorMessage(strErrorMessage)
	{
		if ( this.errorDisplayMode == this.ERR_DISPLAY_CUMULATIVE )
		{
			arrErrorMessages[nErrorCount] = strErrorMessage ;
			nErrorCount++ ;
		}
		else if ( this.errorDisplayMode == this.ERR_DISPLAY_CUMULATIVE )
		{
		}
	}
	
	// ----------- Class Method Definitions ----------------------------------------
	
	function validate()
	{
		var strValue, strErrorMessage, blOptionSelected ;
		
		strValue = "" ;
		
		if ( this.dataType == null || (this.formElement != null && this.elementType == null) )
		{
			// throw an error here ;
			return ;
		}
		
		if ( this.formElement != null )
		{
			if ( this.elementType == this.TEXTBOX || this.elementType == this.DATECONTROL || this.TIMECONTROL || this.elementType == this.USTELCONTROL )
			{
				strValue = this.formElement.value ;
			}
			if ( this.elementType == this.RADIO || this.elementType == this.CHECKBOX )
			{
				blOptionSelected = false ;
				strValue = "" ;
				for ( var nCX = 0 ; nCX < this.formElement.length ; nCX++ )
				{
					if ( this.formElement[nCX].checked )
					{
						strValue = this.formElement[nCX].value ;
						blOptionSelected = true ;
						break ;
					}
				}
			}
		}
		else
		{
			strValue = this.valueToValidate ;
		}
		
		strValue = trim(strValue) ;
		
		if ( this.required )
		{
			if ( (this.elementType == null || this.elementType == this.TEXTBOX || this.elementType == this.DATECONTROL || this.elementType == this.USTELCONTROL) && isEmpty(strValue) )
			{
				strErrorMessage = this.fieldName + " cannot be left blank." ;
				processErrorMessage(strErrorMessage) ;
				return ;
			}
			
			if ( this.elementType == this.COMBOBOX && this.formElement.selectedIndex == this.noSelectionIndex )
			{
				strErrorMessage = this.fieldName + " cannot be left blank." ;
				processErrorMessage(strErrorMessage) ;
				return ;
			}

			if ( this.elementType == this.LISTBOX && this.formElement.selectedIndex == -1 )
			{
				strErrorMessage = this.fieldName + " cannot be left blank." ;
				processErrorMessage(strErrorMessage) ;
				return ;
			}
			
			
			if ( this.elementType == this.RADIO || this.elementType == this.CHECKBOX )
			{
				if ( !blOptionSelected )
				{
					strErrorMessage = "Please select an option for " + this.fieldName + "." ;
					processErrorMessage(strErrorMessage) ;
					return ;
				}
			}
			
		}
		
		if ( (this.elementType == this.DATECONTROL || this.dataType == this.DATE) && !isDate(strValue) )
		{
			strErrorMessage = this.fieldName + " is not a valid date." ;
			processErrorMessage(strErrorMessage) ;
			return ;
		}
		
		if ( this.dataType == this.INTEGER && !isEmpty(strValue) && !isInteger(strValue, true) )
		{
			strErrorMessage = this.fieldName + " is not a valid integer." ;
			processErrorMessage(strErrorMessage) ;
			return ;
		}
		
		if ( this.dataType == this.SSN && !isEmpty(strValue) && !isSSN(strValue))
		{
			strErrorMessage = this.fieldName + " is not a valid Social Security Number." ;
			processErrorMessage(strErrorMessage) ;
			return ;
		}

		if ( this.dataType == this.DECIMAL && !isEmpty(strValue) && !isDecimal(strValue, this.decimalDigits, this.allowNegative) )
		{
			strErrorMessage = this.fieldName + " is not a valid decimal number." ;
			processErrorMessage(strErrorMessage) ;
			return ;
		}
		
		if ( this.dataType == this.CURRENCY && !isEmpty(strValue) && !isDecimal(strValue, 2, false) )
		{
			strErrorMessage = this.fieldName + " is not a valid currency." ;
			processErrorMessage(strErrorMessage) ;
			return ;
		}

		if ( this.dataType == this.EMAIL && !isEmpty(strValue) && !isEmail(strValue) )
		{
			strErrorMessage = this.fieldName + " is not a valid email address." ;
			processErrorMessage(strErrorMessage) ;
			return ;
		}

		if ( this.dataType == this.USTEL && !isEmpty(strValue) && !isUSTel(strValue, this.USTelSeparator) )
		{
			strErrorMessage = this.fieldName + " is not a valid US telephone number." ;
			processErrorMessage(strErrorMessage) ;
			return ;
		}

		if ( this.elementType == this.TEXTBOX &&  this.minCharLength != null && this.minCharLength > 0 && !isEmpty(strValue) )
		{
			if ( strValue.length < this.minCharLength )
			{
				strErrorMessage = this.fieldName + " should be at least " + this.minCharLength + " characters." ;
				processErrorMessage(strErrorMessage) ;
				return ;
			}
		}
	
		if ( this.elementType == this.TEXTBOX && this.maxCharLength !=null && this.maxCharLength > 0 && !isEmpty(strValue) )
		{
			if ( strValue.length > this.maxCharLength )
			{
				strErrorMessage = this.fieldName + " cannot exceed " + this.maxCharLength + " characters." ;
				processErrorMessage(strErrorMessage) ;
				return ;
			}
		}
		if ( this.dataType == this.DATE && this.minValue !=null && !isEmpty(strValue) && !isEmpty(this.minValue) && isDate(strValue) && isDate(this.minValue) )
		{
			var objDate = new Date(strValue) ; 
			var objMinDate = new Date(this.minValue) ;
			var arrTemp = this.minValue.split("/") ;
			if ( objDate < objMinDate )
			{
				strErrorMessage = this.fieldName + " cannot be less than " + arrTemp[1] + "/" + arrTemp[2] + "/" + arrTemp[0] + "." ;
				processErrorMessage(strErrorMessage) ;
				return ;
			}
		}
		
		if ( this.dataType == this.DATE && this.maxValue !=null && !isEmpty(strValue) && !isEmpty(this.maxValue) && isDate(strValue) && isDate(this.maxValue) )
		{
			
			var objDate = new Date(strValue) ; 
			var objMaxDate = new Date(this.maxValue) ;
			var arrTemp = this.maxValue.split("/") ;
			if ( objDate > objMaxDate )
			{
				strErrorMessage = this.fieldName + " cannot be more than " + arrTemp[1] + "/" + arrTemp[2] + "/" + arrTemp[0] + "." ;
				processErrorMessage(strErrorMessage) ;
				return ;
			}
		}

		if ( this.dataType == this.TIME && this.minValue !=null && !isEmpty(strValue) && !isEmpty(this.minValue) )
		{
			arrTemp = strValue.split(":");
			var objTime = new Date(1900, 0, 1, arrTemp[0], arrTemp[1], arrTemp[2], 0) ;
			arrTemp = this.minValue.split(":");
			var objMinTime = new Date(1900, 0, 1, arrTemp[0], arrTemp[1], arrTemp[2], 0) ;
			
			if ( objTime < objMinTime )
			{
				strErrorMessage = this.fieldName + " cannot be less than " + this.minValue + "." ;
				processErrorMessage(strErrorMessage) ;
				return ;
			}
		}
		
		if ( this.dataType == this.TIME && this.maxValue !=null && !isEmpty(strValue) && !isEmpty(this.maxValue) )
		{
			arrTemp = strValue.split(":");
			var objTime = new Date(1900, 0, 1, arrTemp[0], arrTemp[1], arrTemp[2], 0) ;
			arrTemp = this.maxValue.split(":");
			var objMaxTime = new Date(1900, 0, 1, arrTemp[0], arrTemp[1], arrTemp[2], 0) ;
			
			if ( objTime > objMaxTime )
			{
				strErrorMessage = this.fieldName + " cannot be more than " + this.maxValue + "." ;
				processErrorMessage(strErrorMessage) ;
				return ;
			}
		}

		if ( this.dataType == this.INTEGER && !isEmpty(strValue) && !this.allowZero && parseInt(strValue, 10) == 0 )
		{
			strErrorMessage = this.fieldName + " cannot be 0." ;
			processErrorMessage(strErrorMessage) ;
			return ;
		}
		
		if ( (this.dataType == this.INTEGER || this.dataType == this.DECIMAL || this.dataType == this.CURRENCY) && !isEmpty(strValue) && !this.allowNegative && parseFloat(strValue) < 0 )
		{
			strErrorMessage = this.fieldName + " cannot be a negative number." ;
			processErrorMessage(strErrorMessage) ;
			return ;
		}

		if ( (this.dataType == this.INTEGER || this.dataType == this.DECIMAL || this.dataType == this.CURRENCY) && this.minValue !=null && this.minValue > 0 && !isEmpty(strValue) )
		{
			if ( parseFloat(strValue) < parseFloat(this.minValue) )
			{
				strErrorMessage = this.fieldName + " cannot be less than " + this.minValue + "." ;
				processErrorMessage(strErrorMessage) ;
				return ;
			}
		}
		
		if ( (this.dataType == this.INTEGER || this.dataType == this.DECIMAL || this.dataType == this.CURRENCY) && this.maxValue !=null && this.maxValue > 0 && !isEmpty(strValue) )
		{
			if ( parseFloat(strValue) > parseFloat(this.maxValue) )
			{
				strErrorMessage = this.fieldName + " cannot be more than " + this.maxValue + "." ;
				processErrorMessage(strErrorMessage) ;
				return ;
			}
		}
		
	}
	
	function newValidation(objFormElement, nElementType, strFieldName, nDataType, blRequired)
	{
		this.formElement = null ;			
		this.fieldName = "" ;			
		this.dataType = null ;
		this.elementType = null ;
		this.required = false ;
		
		this.minValue = null ;
		this.maxValue = null ;
		this.minCharLength = null ;
		this.maxCharLength = null ;
		
		this.allowNegative = false ;
		this.allowZero = true ;
		this.allowNewline = true ;
		this.decimalDigits = 2 ;
		this.specialFormat = "" ;
	
		this.noSelectionIndex = 0 ;

		this.formElement = objFormElement ;
		this.elementType = nElementType ;
		this.fieldName = strFieldName ;
		this.dataType = nDataType ;
		this.required = blRequired ;
	}

	function resetValidations()
	{
		this.formElement = null ;			
		this.fieldName = "" ;			
		this.dataType = null ;
		this.elementType	= null ;
		this.required = false ;
		
		this.minValue = null ;
		this.maxValue = null ;
		this.minCharLength = null ;
		this.maxCharLength = null ;
		
		this.allowNegative = false ;
		this.allowZero = true ;
		this.allowNewline = true ;
		this.decimalDigits = 2 ;
		this.specialFormat = "" ;
		
		this.noSelectionIndex = 0 ;
	}

	function reset()
	{
		this.formElement = null ;			
		this.fieldName = "" ;			
		this.dataType = null ;
		this.elementType	= null ;
		this.required = false ;
		
		this.minValue = null ;
		this.maxValue = null ;
		this.minCharLength = null ;
		this.maxCharLength = null ;
		
		this.allowNegative = false ;
		this.allowZero = true ;
		this.allowNewline = true ;
		this.decimalDigits = 2 ;
		this.specialFormat = "" ;
		
		this.noSelectionIndex = 0 ;
		
		nErrorCount = 0 ;
		arrErrorMessages.length = 0 ;
	}
	
	function insertErrorMessage(strErrorMessage)
	{
		processErrorMessage(strErrorMessage) ;
	}
	
	function displayErrors()
	{
		var strErrorMessages = "" ;
		
		if ( nErrorCount > 0 )
		{
			for ( var nCX = 0 ; nCX < nErrorCount ; nCX++ )
			{
				strErrorMessages = strErrorMessages + arrErrorMessages[nCX] + "\n" ;
			}
			alert(strErrorMessages) ;
		}
	}
	
	function getErrorCount()
	{
		return nErrorCount ;
	}
	
	// ----------- Initialise ------------------------------------------------------
	
}