function clsValidate()
{
	//Properties
	this.aryError = new Array();
	this.strValue;
	
	//Methods
	this.trim = trim;
	this.CheckAlpha = CheckAlpha;
	this.CheckNumeric = CheckNumeric;
	this.CheckEmail = CheckEmail;
	this.CheckMax = CheckMax;
	this.CheckMin = CheckMin;
	this.CheckChars = CheckChars;
	this.CheckSelect = CheckSelect;
	this.matchPasswords = matchPasswords;

	this.GetError = GetError;
}


/*################################()###################################
Name: trim
Description: strips leading and trailing spaces from value (similar to Java trim method)
Input: string to be trimmed 
Output: trimmed string
#######################################################################*/
function trim(strValue) 
{

	/* Strip leading spaces from input */
	startposn = 0;

	while((strValue.charCodeAt(startposn) == 32) && (startposn < strValue.length)) 
		{startposn++;}

	if(startposn == strValue.length) 
		{strValue = '';} 
	else 
	{
		/* If anything left of string after stripping leading spaces strip trailing spaces as well */
		strValue = strValue.substring(startposn, strValue.length);
		endposn = (strValue.length) - 1;
		
		while(strValue.charCodeAt(endposn) == 32) 
			{endposn--;}
		
		strValue = strValue.substring(0, endposn + 1);
	}
	
	return(strValue);
}


/*################################()###################################
Name: CheckAlpha
Description: checks a string for alpha characters, 
			allows spaces in between chars
Input: 
			- strValue: string to be checked
			- strErrorMsg: message to display if an error occurs
#######################################################################*/
function CheckAlpha(strValue, strErrorMsg, blnAllowSpace)
{
	var intSrtPos = 0;
	var blnError = false;
	
	strValue = this.trim(strValue);
	
	if(strValue.length > 0)
	{
		if(strErrorMsg == null || strErrorMsg == '')
			{strErrorMsg = 'One of the form text fields contains non-alpha characters';}

		strValue = strValue.toLowerCase();
		
		if(blnAllowSpace == false)
		{
			while((strValue.charCodeAt(intSrtPos) > 96 && strValue.charCodeAt(intSrtPos) < 123) && (intSrtPos < strValue.length))
				{intSrtPos++;}
		}
		else
		{
			while((strValue.charCodeAt(intSrtPos) > 96 && strValue.charCodeAt(intSrtPos) < 123 || strValue.charCodeAt(intSrtPos) == 32) && (intSrtPos < strValue.length))
				{intSrtPos++;}
		}
		
		if(intSrtPos != strValue.length)
		{
			this.aryError[this.aryError.length + 1] = strErrorMsg;
			blnError = true;
		}
	}
	else
		{blnError = true;}
		
	return(blnError);
}



/*################################()###################################
Name: CheckNumeric
Description: checks a string for numeric characters

Input: 
			- strValue: string to be checked
			- strErrorMsg: message to display if an error occurs
#######################################################################*/
function CheckNumeric(strValue, strErrorMsg, blnAllowSpace)
{
	var intSrtPos = 0;
	var blnError = false;
	
	strValue = this.trim(strValue);
	
	if(strValue.length > 0)
	{
		if(strErrorMsg == null || strErrorMsg == '')
			{strErrorMsg = 'One of the form text fields contains non-numeric characters';}
		
		strValue = strValue.toLowerCase();
		
		if(blnAllowSpace == false)
		{
			while((strValue.charCodeAt(intSrtPos) > 47 && strValue.charCodeAt(intSrtPos) < 58) && (intSrtPos < strValue.length))
				{intSrtPos++;}
		}
		else
		{
			while((strValue.charCodeAt(intSrtPos) > 47 && strValue.charCodeAt(intSrtPos) < 58 || strValue.charCodeAt(intSrtPos) == 32) && (intSrtPos < strValue.length))
				{intSrtPos++;}
		}
		
		if(intSrtPos != strValue.length)
		{
			this.aryError[this.aryError.length + 1] = strErrorMsg;
			blnError = true;
		}
	}
	else
		{blnError = true;}
		
	return(blnError);
}


/*################################()###################################
Name: CheckEmail
Description: checks a string if it is a valid email

Input: 
			- strValue: string to be checked
			- strErrorMsg: message to display if an error occurs
#######################################################################*/
function CheckEmail(strValue, strErrorMsg)
{
	var blnError = false;

	strValue = this.trim(strValue);
	
	if(strValue.length > 0)
	{
		if(strErrorMsg == null || strErrorMsg == '')
			{strErrorMsg = 'You have mistyped an email in one of the form fields';}
			
		if((strValue.indexOf('@', 1) == -1) || (strValue.indexOf('.', 2) == -1))
		{
			this.aryError[this.aryError.length + 1] = strErrorMsg;
			blnError = true;
		}
	}
	else
		{blnError = true;}
	
	return(blnError);
}


/*################################()###################################
Name: CheckMax
Description: checks the maximum amount of chars allowed

Input: 
			- strValue: string to be checked
			- strErrorMsg: message to display if an error occurs
			- intMaxNo: max length
#######################################################################*/
function CheckMax(strValue, strErrorMsg, intMaxNo)
{
	var blnError = false;
	
	strValue = this.trim(strValue);
	
	if(intMaxNo != '' || intMaxNo != null)
	{
		if(strErrorMsg == null || strErrorMsg == '')
			{strErrorMsg = 'You have exceeded the maximum character limit in one of the fields';}
			
		if(strValue.length > intMaxNo)
		{
			this.aryError[this.aryError.length + 1] = strErrorMsg;
			blnError = true;
		}
	
	}
	else
		{blnError = true;}
		
	return(blnError);
}


/*################################()###################################
Name: CheckMin
Description: checks the minimum amount of chars allowed

Input: 
			- strValue: string to be checked
			- strErrorMsg: message to display if an error occurs
			- intMinNo: min length
#######################################################################*/
function CheckMin(strValue, strErrorMsg, intMinNo)
{
	var blnError = false;
	
	strValue = this.trim(strValue);
	
	if(intMinNo != '' || intMinNo != null)
	{
		if(strErrorMsg == null || strErrorMsg == '')
			{strErrorMsg = 'One of the fields does not satisfy the minimum value required';}
			
		if(strValue.length < intMinNo)
		{
			this.aryError[this.aryError.length + 1] = strErrorMsg;
			blnError = true;
		}
	
	}
	else
		{blnError = true;}
		
	return(blnError);
}



/*################################()###################################
Name: CheckChars
Description: checks a string for particular characters

Input: 
			- strValue: string to be checked
			- strErrorMsg: message to display if an error occurs
			- aryChars: an array of valid chars to look for
#######################################################################*/
function CheckChars(strValue, strErrorMsg, aryChars)
{
	var blnError = false;
	var intSrtPos = 0;
	var blnFound = false;
	var i = 0;
	
	strValue = this.trim(strValue);
	
	if(strValue.length > 0 || aryChars == '' || aryChars == null || typeof(aryChars) != 'object')
	{
		while((intSrtPos < strValue.length) && (blnError == false))
		{
			blnFound = false;
			i = 0;
			
			while((i < aryChars.length) && (blnFound == false))
			{
				if(strValue.charAt(intSrtPos) == aryChars[i])
					{blnFound = true;}
				else
					{blnFound = false;}

				i++;
			}
			
			if(blnFound == false)
			{
				this.aryError[this.aryError.length + 1] = strErrorMsg;
				blnError = true;
			}
			
			intSrtPos++;
		}	
	}
	else
		{blnError = true;}

	return(blnError);
}


/*################################()###################################
Name: CheckSelect
Description: To determine wether a predifined value has been selected from
			within a select box
Input: 
			- strValueToCheck: the value to check
			- intAction: specifies the action to be carried out see below
			- strErrorMsg: message to display if an error occurs
			- objSelectBox: reference to the select box
#######################################################################*/
function CheckSelect(strValueToCheck, intAction, strErrorMsg, objSelectBox)
{
	//=============================================================
	//					A	C	T	I	O	N	S
	//=============================================================
	//1. Make sure a particular value has not been selected
	//2. Make sure that a particular value exists within a select box
	//3. Make sure that a particualr value has been selected	
	var C_VALUE_NOT_SELECTED = 1;
	var C_VALUE_EXISTS = 2;
	var C_VALUE_IS_SELECTED = 3;
	
	var blnError = false;
	
	switch (intAction) 
	{
		case C_VALUE_NOT_SELECTED:
			
			if(objSelectBox == strValueToCheck)
			{
				this.aryError[this.aryError.length + 1] = strErrorMsg;
				blnError = true;			
			}
			
			break;
		case C_VALUE_EXISTS:
			//program me
			break;		
		case C_VALUE_IS_SELECTED:
			//program me
			break;		
	default :
		//Execute: C_VALUE_NOT_SELECTED
		if(objSelectBox == strValueToCheck)
		{
			this.aryError[this.aryError.length + 1] = strErrorMsg;
			blnError = true;			
		}	
		
		break;
	} 

	return blnError;
}


// ############################################################
function matchPasswords(sPass1, sPass2) {

	if ( sPass1 != sPass2 ) {
		this.aryError[this.aryError.length + 1] = "Password do not match";
		blnError = true;
	}
	else
		blnError = false;
	
	return blnError;
}


/*################################()###################################
Name: GetError
Description: displays all the encountered errors
#######################################################################*/
function GetError()
{
	var blnReturn = false;
	
	if(this.aryError.length > 0)
	{
		var strErrorMsg = '===========================================================\n' +
						  '\t\tSorry, your request was not successful\n' +
						  '===========================================================\n' +						  
						  '\nYour request has been declined due to the following errors:';

		var strErrors = '';
		
		for(var i = 1, z = 1; i < this.aryError.length; i+=2, z++)
			{strErrors = strErrors + '\n\t' + z + ') ' + this.aryError[i];}
		
		alert(strErrorMsg + '\n' + strErrors);
	}
	else
		{blnReturn = true;}
	
	return(blnReturn);

}
