/*=======================================================================*
 *	The Following functions validate a set of forms depending	 *
 *	on the passed in "Description" of the validation that should	 *
 *	be done on the elements in the form.				 *
 *	File Name: validation.js					 *
 *	Original Concept developed for Special Collections by:		 *
 *	 					 			 *	
 *		Ernest Smith						 *
 *		Ryan Foster						 *
 *		Bisceglia White						 *
 *		Kimberly Benton						 *
 *									 *
 *	Synopsis:						 	 *
 *	A form may have multiple fields. Those fields may be valid 	 *
 *	in multiple orders: ie a user has an option of entering    	 * 
 *	either a: "username, password, phone number" or    	    	 *
 *	"username, password, address" and the form be 'valid'.	     	 *
 *	This 'multiOption' object will hold each fields validity   	 *
 *	and error message, which can be later applied to create    	 *
 *	a larger array that will hold each fields validity and 	    	 *
 *	error message. This larger array can be used to validate    	 *
 * 	a 'set' of valid options.				   	 *
 *									 *
 *	Georgia Southern University					 *
 *	Date of Creation: March 27, 2004				 *
 *									 *
 * --------------------------------------------------------------------- *
 *	Update: March 30, 2004					         *
 *	Reason: Form not considered valid if user inserted data into     *
 *		a field that is not required, and that data is invalid.  *
 *	Updater: ESMITH							 *
 * --------------------------------------------------------------------- *
 *	Update: April 03, 2004					 	 *
 *	Reason: Insert Null case for data not being considered	         *
 *		Added, "number" case					 *
 *	Updater: ESMITH							 *
 * --------------------------------------------------------------------- *
 *	Update: April 03, 2004					 	 *
 *	Reason: Added validation for: only spaces in a requried field    *
 *		Field will be validated incorrectly			 *
 *	Updater: ESMITH							 *
 * --------------------------------------------------------------------- *
 *	Update: April 03, 2004					 	 *
 *	Reason: Error in validating object instead of value in function	 *
 * 		validateArray						 *
 *	Updater: ESMITH							 *
 *									 *
 * --------------------------------------------------------------------- *
 *	Update: April 11, 2004					 	 *
 *	Reason: Added new options in validateData	 		 *
 * 		fromDate, toDate, oldPass, newPass 			 *
 *	Updater: ESMITH							 *
 *									 *
 * --------------------------------------------------------------------- *
 *	Update: April 15, 2004					 	 *
 *	Reason: Updated validation to include city, state, address, zip 	 *
 * 			all of which are just considered required fields if they	 *
 *			are grouped together										 *
 *	Updater: ESMITH							 *

 *=======================================================================*/ 
 
  /*=======================================================================*
   *	Function: field()	  		 	   		   *
   *	Arguments: 							   *
   *		none							   *
   *	Methods:							   *
   *		setValid(validity)					   *
   *		setError(strError)					   *
   *		setDescription(strDesc)					   *
   *		setEmpty(bVal)						   *
   *		getValid()						   *
   *		getError()						   *
   *		getDescription()					   *
   *		getEmpty()						   *
   *	Members:							   *
   *		valid - Validity of field				   *
   *		strErr - Error Message to print if valid = 'false'         *
   *		strDesc - Description of field element			   *
   *		bEmpty - Is field empty? Initialy true for empty field 	   *
   *=======================================================================*/
 function field()
 {
 	this.valid = true
 	this.strErr = ''
 	this.strDesc = ''
 	this.bEmpty = true
 	
 	this.setValid = function(validity)
 	{
 		this.valid = validity
 	}
 	
 	this.setError = function(strError)
 	{
 		this.strErr = strError
 	}
 	
 	this.setDescription = function(strDesc)
 	{
 		this.strDesc = strDesc
 	}
 	
 	this.setEmpty = function(bVal)
 	{
 		this.bEmpty = bVal
 	}
 	
 	this.getError = function()
 	{
 		return this.strErr
 	}
 	
 	this.getValid = function()
 	{
 		return this.valid
 	}
 	
 	this.getDescription = function()
 	{
 		return this.strDesc
 	}
 	
 	this.getEmpty = function()
 	{
 		return this.bEmpty
 	}
 }
 
  /*=======================================================================*
   *	Function: option()	  		 	   		   *
   *	Arguments: 							   *
   *		none							   *
   *	Methods:							   *
   *		setLength(nLength)					   *
   *		setValue(nIndex, nValue)				   *
   *		changeValid(bVal)					   *
   *		getLength()						   *
   *		getValid()						   *
   *		getValue(nValue)					   *
   *	Members:							   *
   *		valid - Validity of option				   *
   *		length - Number of 'Descriptions that  			   *
   *		   are used to validate option	         		   *
   *		value - Array of Descriptions 				   *
   *=======================================================================*/
  function option()
  {
  	this.valid = true
  	this.length = 1
  	this.value = new Array(this.length)
  	
  	for( var i = 0; i < this.length; i ++)
  	{
  		this.value[i] = ''
  	}
  	
  	this.getLength = function()
  	{
  		return this.length
  	}
  	
  	this.getValid = function()
  	{
  		return this.valid
  	}
  	
  	this.getValue = function(nValue)
  	{
  		return this.value[nValue]
  	}
  	
  	this.changeValid = function(bVal)
  	{
  		this.valid = bVal
  	}
  	
  	this.setValue = function(nIndex, nValue)
  	{
  		this.value[nIndex] = nValue
  	}
  	
  	this.setLength = function(nLength)
  	{
  		this.length = nLength
  		this.value = new Array(this.length)
  		
  		for( var i = 0; i < this.length; i++)
  		{
  			this.value[i] = ''
  		}
  	}
  }

  /*=======================================================================*
   *	Function: fieldArray(nSize)  		 	   		   *
   *	Arguments: 							   *
   *		nSize - Size of Array of fields				   *
   *	Methods:							   *
   *		setError(nField, strError)				   *
   *		setValid(nField, bVal)					   *
   *		setFieldDesc(nField,strDesc)				   *
   *		setFieldEmpty(nField, bEmpty)				   *
   *		getFieldValid(nField)					   *
   *		getFieldError(nField)					   *
   *		getFieldDesc(nField)					   *
   *		getEmpty(nField)					   *
   *		getLength()					           *
   *	Members:							   *
   *		nLength - Length of Array				   *
   *		field - Array of Fields		  			   *
   *=======================================================================*/
 function fieldArray(nSize)
 {
 	this.nLength = nSize
 	this.field = new Array(nSize)
	
 	for( var i = 0; i < this.nLength; i ++)
 	{
 		this.field[i] = new field()
 	}
 	
 	this.setError = function(nField, strError)
 	{
 		this.field[nField].setError(strError)
 	} 	
 	
 	this.setValid = function(nField, bVal)
 	{
 		this.field[nField].setValid(bVal)
 	}
 	
 	this.setFieldDesc = function(nField, strDesc)
 	{
 		this.field[nField].setDescription(strDesc)
 	}
 	
 	this.setFieldEmpty = function(nField, bEmpty)
 	{
 		this.field[nField].setEmpty(bEmpty)
 	}
 	
  	this.getFieldValid = function(nField)
 	{
 		return this.field[nField].getValid()
 	}
	
 	this.getFieldError = function(nField)
 	{
 		return this.field[nField].getError()
 	}
 	
  	this.getFieldDesc = function(nField)
  	{
  		return this.field[nField].getDescription()
 	}
 	
 	this.getLength = function()
 	{
 		return this.nLength
 	} 
 	
 	this.getEmpty = function(nField)
 	{
 		return this.field[nField].getEmpty()
  	} 	
 }
 
  /*=======================================================================*
   *	Function: optionArray(nSize)  		 	   		   *
   *	Arguments: 							   *
   *		nSize - Size of Array of options			   *
   *	Methods:							   *
   *		getNumOptions()						   *
   *		getOptionValue(nOption, nValue)			      	   *
   *		getOptionValid(nOption)				   	   *
   *		getOptionLength(nOption)				   *
   *		setOptionValue(nOption, nFields, strArray)		   *
   *		setOptionValid(nOption,bVal)				   *
   *		setOptionLength(nOption, nLength)			   *
   *	Members:							   *
   *		numOptions - number of option in Array			   *
   *		option - Array of Options	  			   *
   *=======================================================================*/
 function optionArray(nSize)
 {
 	this.numOptions = nSize
 	this.option = new Array(nSize)
 	
 	for( var i = 0; i < this.numOptions; i++)
 	{
 		this.option[i] = new option()
 	}
	
	this.getOptionValue = function(nOption, nValue)
	{
		return this.option[nOption][nValue]	
	}
	
	this.getNumOptions = function()
	{
		return this.numOptions
	}
	
	this.getOptionValid = function(nOption)
	{
		return this.option[nOption].getValid()
	}
	
	this.getOptionLength = function(nOption)
	{
		return this.option[nOption].getLength()
	}

 	this.setOptionValue = function(nOption, nFields, strArray)
 	{
 		var nFields = nFields
		this.option[nOption].setLength(nFields)
		
 	 	for(var i = 0; i < nFields; i++)
 	 	{
 	 		this.option[nOption][i] = strArray[i]
 	 	}
 	}
 	
 	this.setOptionValid = function(nOption,bVal)
 	{
 		this.option[nOption].changeValid(bVal)
 	}
 	
 	this.setOptionLength = function(nOption, nLength)
 	{
 		this.option[nOption].setLength(nLength)
 	}
 }

  /*=======================================================================*
   *	Function: validationArray(nFields,nOptions) 	   		   *
   *	Arguments: 							   *
   *		nFields - Number of fields in form			   *
   *		nOptions - Number of options in form			   *
   *	Methods:							   *
   *		getOptionValid(nOption)					   *
   *		getOptionValue(nOption,nValue)				   *
   *		getOptionLength(nOption)				   *
   *		setOptionValid(nOption, bValue)				   *
   *		setOptionValue(nOption, nFields, strArray)		   *
   *		getFieldError(nField)					   *
   *		getFieldValid(nField)					   *
   *		getFieldEmpty(nField)					   *
   *		getFieldDescription()				           *
   *		setField(nField, bValid, strError, strDesc, bEmpty)	   *
   *		setFieldValid(nField,bVal)				   *
   *		setFieldError(nField,strErr)				   *
   *		setFieldDescription(nField,strDesc)		           *
   *		validateOptions()					   *
   *	Members:							   *
   *		numFields - Number of fields in form			   *
   *		numOptions - Number of options in form			   *
   *		field - Array of fields in form			   	   *
   *		option - Array of options in form			   *
   *=======================================================================*/ 
 function validationArray(nFields,nOptions)
 {
 	this.numFields = nFields
 	this.numOptions = nOptions
 	this.field = new fieldArray(this.numFields)
 	this.option = new optionArray(this.numOptions)
	
	
	this.getOptionValid = function(nOption)
	{
		return this.option.getOptionValid(nOption)	
	}
 	
 	this.getOptionValue = function(nOption,nValue)
 	{
 		return this.option.getOptionValue(nOption,nValue)
 	}
 	
 	this.getOptionLength = function(nOption)
 	{
 		return this.option.getOptionLength(nOption)
 	}
 	
	this.setOptionValid = function(nOption, bValue)
	{
		this.option.setOptionValid(nOption,bValue)
	}
 	
 	this.setOptionValue = function(nOption, nFields, strArray)
 	{
 		this.option.setOptionValue(nOption, nFields, strArray)
 	}
 	
 	this.getFieldError = function(nField)
 	{
 		return this.field.getFieldError(nField)
 	}
 	
 	this.getFieldValid = function(nField)
 	{
 		return this.field.getFieldValid(nField)
 	}
 	
 	this.getFieldEmpty = function(nField)
 	{	
 		return this.field.getFieldEmpty(nField)
 	}
 	
 	this.getFieldDescription = function(nField)
	{
		return this.field.getFieldDesc(nField)
 	}
 	
  	this.setField = function(nField, bValid, strError, strDesc, bEmpty)
 	{ 		
 		this.field.setError(nField,strError)
 		this.field.setValid(nField,bValid)
 		this.field.setFieldDesc(nField,strDesc)
 		this.field.setFieldEmpty(nField,bEmpty)
 	}
 	
 	this.setFieldValid = function(nField,bVal)
 	{
 		this.field.setValid(nField,bVal)
 	}
 	
	this.setFieldError = function(nField,strErr)
	{
		this.field.setError(nField,strErr)
	}

 	this.setFieldDescription = function(nField,strDesc)
	{
		return this.field.setFieldDesc(nField,strDesc)
 	}
	
	this.validateOptions = function()
	{
		var strErr = ''

		for( var i = 0; i < this.option.getNumOptions(); i++)
		{
			for( var n = 0; n < this.option.getOptionLength(i); n++)
			{
				for( var x = 0; x < this.numFields; x++)
				{
					if( this.option.getOptionValue(i,n) == 	this.field.getFieldDesc(x))
					{
						<!-- Validation -->
						//alert("OpV: " + this.option.getOptionValue(i,n) + "\nFDesc: " + this.field.getFieldDesc(x) + "\nValid " + this.field.getFieldValid(x))
						<!-- End Validation -->
						 
						if( this.field.getFieldValid(x) == false)
						{
							this.option.setOptionValid(i,false)
						}
					}
				}
			}
		}
		
		for( var n = 0; n < this.option.getNumOptions(); n++)
		{
		 	<!-- Validation -->
			//alert("OpValid?: " + this.option.getOptionValid(n))
			<!-- End Validation -->
			if( this.option.getOptionValid(n) == true)
			{
				var strValid = ''
				var bInvalidField = false
				
				/* ----------------------------------------------
				  If the user entered data into a field
					it is still considered an error although
					the other fields are 'valid', inform user
					of the error.

					Updated: March 30, 2004 ESMITH
					Error found: March 30, 2004 ESMITH 					
				------------------------------------------------ */				
				for( var i = 0; i < this.numFields; i ++)
				{
				 	  <!-- Validation -->
					  //alert("FValid?: " + this.field.getFieldValid(i) + "\nEmpty?: " + this.field.getEmpty(i))
					  <!-- End Validation -->
					if( (this.field.getFieldValid(i) == false) && (this.field.getEmpty(i) == false))
					{
						strValid += this.field.getFieldError(i)
						bInvalidField = true
					}
				}
				
				if(bInvalidField == false)
				{
					<!-- Validation -->
					//alert("Form Validated!")
					<!-- End Validation -->
					return true
				}
				else
				{
					alert("Can not submit form because of following error(s)\n" + strValid)
					return false
				}
			}
		}

		for( var n = 0; n < this.option.getNumOptions(); n++)
		{
			var bErrOccured = false
			if( this.option.getOptionValid(n) == false)
			{
				for( var x = 0; x < this.option.getOptionLength(n); x++)
				{
					for( var i = 0; i < this.numFields; i ++)
					{
						if( this.option.getOptionValue(n,x) == 	this.field.getFieldDesc(i))
						{
							if( this.field.getFieldValid(i) == false)
							{
								strErr += this.field.getFieldError(i)
								bErrOccured = true							
							}

						}					
					}
				}
				
				if(bErrOccured == true)
				{
					alert(strErr)
					return false
				}
			}
		}
		
		alert("Error in Validation: Validation Option Error\nvalidation.js\nvalidationArray()")
	}
 }
 
 /*=======================================================================*
  *	Function: ValidateForm(objFrm,arrObjDesc) 	  		  *
  *	Arguments: 							  *
  *		objFrm	Form to be validated				  *
  *		arrObjDesc Array Describing validation to be done	  *  
  *=======================================================================*/
 function validateForm(objFrm,arrObjDesc) 
 {
 	var numFields = arrObjDesc.length - 1
 	var numOptions = arrObjDesc[0].length 
	arrOption = new validationArray(numFields, numOptions)
	
	for(var n = 0; n < numOptions; n++)
	{
		arrOption.setOptionValue(n, arrObjDesc[0][n].length, arrObjDesc[0][n])
	}
	<!-- Validation -->
	//alert("Num Options: " + numFields)
	<!-- End Validation -->
	
 	for(var i = 0; i < numFields; i++)
 	{	
 		var bValid = true
 		var bEmpty = true
 		
    		if(objFrm.length <= i) 
    		{ 
		 	alert("Error in Validation: Obj descriptor for a non existent form element\nvalidation.js\nvalidateForm()"); 
	 		return false; 
    		}
    		
				<!-- Validation -->
    				//alert("AOD: " + arrObjDesc[i+1][0] + "\nOE: " + objFrm.elements[i].name)
				<!-- End Validation -->
				if( validateData(arrObjDesc[i+1][0],objFrm.elements[i]) == false)
    		{
    			bValid = false 
    		}

		/* ----------------------------------------------
		   Error in checking if field is empty. Checking
		   if object is empty, always false, not if the 
		   value in the object is empty

	           Updated: April 03, 2004 ESMITH
		   Error found: April 04, 2004 ESMITH 
		------------------------------------------------ */
    		bEmpty = isEmpty(objFrm.elements[i].value)
				
     		<!-- Validation -->
    		//alert("Empty?: " + bEmpty + "\nName: " + objFrm.elements[i].name)
		<!-- End Validation -->
				   		
    		arrOption.setField(i,bValid,arrObjDesc[i+1][1],arrObjDesc[i+1][0],bEmpty)     		
    	}
 	
 	return arrOption.validateOptions()	
}


 /*=======================================================================*
  *	Function: validateData(strValidateStr,objValue,strError) 	  *
  *	Arguments: 							  *
  *		strValidateStr	validation string describing validation	  *
  *			to be performed on field			  *
  *		objValue value to check	  				  *  
  *=======================================================================*/
function validateData(strDescription,objValue) 
{ 
	var strDes      = strDescription
	var strValue    = objValue.value
	var strName	= objValue.name
	var nLength	= objValue.value.length
	
	switch(strDes)
	{
		case "date":
			
			if(!isEmpty(validateDate(strValue)))
			{
				return false				
			}
			else
			{
				return	true
			}
		break;
		
		/* -------------------------------------
			Error found when validating certain
			dates, there should be two specific
			cases.
			
			1) A date that is checked to see
				if it is a valid date.
			2) Case 1 && The date should also
				not be less than the current
				date.
				
			For the first case 'date' should
			be used, otherwise fromDate or toDate
			should be used, i.e. you are checking
			a date that may be in a range less
			than the current date.
			
			 Updated: April 20, 2004 ESMITH
		   Error found: April 10, 2004 ESMITH 
		--------------------------------------*/
		case "fromDate":
		case "toDate":
		
			if(isEmpty(stripCharsInBag(strValue,' ')) || !isDate(strValue))
			{
				return false
			}
			else
			{
				return true
			}
		break;
			
		case "email":
		
			if((strValue.charAt(nLength - 5) == '@') ||
				(strValue.indexOf('@') == 0) ||
				(strValue.indexOf('@') == -1) ||
				(strValue.indexOf('.') == -1) ||
				(strValue.charAt(nLength - 4) != '.'))
			{
				   return false
			}
			else
			{
				   return true
			}
		break;
		
		case "phone":
		
			if( isEmpty(strValue) ||
			    ((nLength != 7) && 
				(nLength != 10)) ||
				isEmpty(stripCharsInBag(strValue,' ')) ||
			    !isNumber(strValue))
			{
				return false
			}
			else
			{
			   	return true
			}
		break;
		
		case "name":
		case "required":
		case "oldPass":
		case "newPass":
		case "state":
		case "city":
		case "address":
				/* ----------------------------------------------
				 If the user entered a single space, or only
				 spaces into a field it should not be a valid
				 entry. This will catch a single space in a 
				 text field, as well as multiple spaces.	

				 Updated: April 03, 2004 ESMITH
				 Error found: April 2, 2004 RFOSTER 
				------------------------------------------------ */		
			if( isEmpty(strValue) || 
			 	isEmpty(stripCharsInBag(strValue,' ')) ||
				isDoubleSpace(strValue))
			{
				return false				
			}
			else
			{
				return true		            
			}
		break;

		case "null":
			return true
		break;
		
		case "number":
			if(isEmpty(strValue) || !isNumber(strValue))
			{
				return false				
			}
			else
			{
				return true		            
			}
		break;
		
		
		case "zip":
			if(!testZip(strValue))
			{
				return false
			}
			else
			{
				return true
			}
		break;
		
		default:
			alert("Error in Validation: Default error for a non existent description\nvalidation.js\nvalidateData()\n" + strDes)
			return false;
	}
}

function testZip( nZip )
{
	if( nZip.length == 5 || nZip.length == 10 )
	{
		var n = nZip.length
		
		if( n == 5 )
		{
			return testZip5( nZip )
		}
		if( n == 10)
		{
			return testZip10( nZip )
		}
	}
	else
	{
		return false;
	}
}
function testZip5( nFive )
{
	if( !isNumber(nFive))
	{
		return false
	}
	
	return true
}
function testZip10( nTen )
{
	if( nTen.charAt( 5 ) == "-" )
	{
		for( k = 0; k < ( nTen.length ); k++ ) 
		{
			var digit = nTen.charAt(k)
			if( digit < "0" || digit > "9")
			{
				if( digit == "-" && k == 5 )
				{
				}
				else
				{
					return false
					break
				}
			}
		}
		return true
	}
	else
	{
		return false
	}
}


function isDoubleSpace(strContent)
{
	var strTemp = strContent;
	
	for(var i =0; i < strTemp.length; i++)
	{
		if(strTemp[i] == ' ')
		{
			if(i+1<strTemp.length)
			{
				if(strTemp[i+1] == ' ')
				{
					return true;
				}
			}
		}
	}
	
	return false;
}
 /*=======================================================================*
  *	Function: isEmpty(strContent)				 	  *
  *	Arguments: 							  *
  *		strContent Content to validate length for  		  *
  *=======================================================================*/
function isEmpty(strContent)
{	
	if( strContent == '')
	{
		return true;
	}
	else
	{
		return false;
	}
}

 /*=======================================================================*
  *	Function: isNumber(nEntry)				 	  *
  *	Arguments: 							  *
  *		strContent Content to validate length for  		  *
  *=======================================================================*/
function isNumber(nEntry)
{
	for(var i=0; i < nEntry.length; i++)
	{
	    var digit = nEntry.charAt(i)
		
		if(digit < "0" || digit > "9")
		{
			return false;
		}
	}

	return true;
}

 /*=======================================================================*
  *	Function: daysInFebruary(nYear)				 	  *
  *	Arguments: 							  *
  *		nYear Year to check how many days are in the month	  *
  *	Synopsis: 							  *
  *		February has 29 days in any year evenly divisible by four *
  *		EXCEPT for centurial years which are not also divisible   *
  *		by 400							  *
  *=======================================================================*/
 function daysInFebruary(nYear)
 {
     return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
 }

 /*=======================================================================*
  *	Function: DaysArray(nValue)				 	  *
  *	Arguments: 							  *
  *		nValue Year to assign how many days are in the month	  *
  *=======================================================================*/
 function DaysArray(nValue)
 {
	for (var i = 1; i <= nValue; i++)
	{
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11)
		{
			this[i] = 30
		}

		if (i==2)
		{
			this[i] = 29
		}
	}

    return this;
}

 /*=======================================================================*
  *	Function:stripCharsInBag(strValue, strBag)		 	  *
  *	Arguments: 							  *
  *		strValue Value to search through			  *
  *		strBag Bag of Characters				  *
  *	Synopsis: 							  *
  *		Remove charcters in bag from string			  *
  *		Search through strings characters one by one		  *
  *  		If character is not in bag, append to returnString        *
  *=======================================================================*/
function stripCharsInBag(strValue, strBag)
{
    var i
    var returnString = ''

    for (i = 0; i < strValue.length; i++)
    {
        var c = strValue.charAt(i);
        
        if (strBag.indexOf(c) == -1)
        {
            returnString += c;
        }
    }

    return returnString;
}

 /* ----------------------------------------------- Date Validation --------------------------------------------------------------- */
 
  /*=======================================================================*
   *	Function: isDate (strDate)				 	  *
   *	Arguments: 							  *
   *		strDate date to validate				  *
   *	Synopsis: 							  *
   *		Validate a given date					  *
   *		Return Error String					  *
   *=======================================================================*/
  function isDate (strDate)
 {  
 	var arrStrDate = strDate.split("/");

	/* ---- Format mm/dd/yr or mm/dd/yyyy ---- */
 	var month = arrStrDate[0];
	var day = arrStrDate[1];
 	var year = arrStrDate[2];
 
 	if(!(year && day	&& month))
	{
		return false;
	}
	
 	var daysInMonth = new Array(12);
 		daysInMonth[1] = 31;
 		daysInMonth[2] = 29;   // must programmatically check this
 		daysInMonth[3] = 31;
 		daysInMonth[4] = 30;
 		daysInMonth[5] = 31;
 		daysInMonth[6] = 30;
 		daysInMonth[7] = 31;
 		daysInMonth[8] = 31;
 		daysInMonth[9] = 30;
 		daysInMonth[10] = 31;
 		daysInMonth[11] = 30;
 		daysInMonth[12] = 31;
 		
  // catch invalid years (not 2- or 4-digit) and invalid months and days.
     if (! (isYear(year) && isMonth(month) && isDay(day))) 
 	{
 		return false;
 	}
 
     // Explicitly change type to integer to make code work in both
     // JavaScript 1.1 and JavaScript 1.2.
     var intYear = parseInt(year);
     var intMonth = parseInt(month);
     var intDay = parseInt(day);
 
     /* ---- catch invalid days, except for February ---- */
     if (intDay > daysInMonth[intMonth]) 
 	{
 		return false; 
 	}
 
     if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) 
 	{
 		return false;
 	}
 
     return true;
 }
 
 function isMonth (nMonth)
 {
     return isIntegerInRange (nMonth, 1, 12);
 }
 
 
 function isDay (nDay)
 {   
     return isIntegerInRange (nDay, 1, 31);
 }
 
 function isYear (nYear)
{ 
    return ((nYear.length == 2) || (nYear.length == 4));
}
 function isIntegerInRange (s, a, b)
 {   
     // Catch non-integer strings to avoid creating a NaN below,
     // which isn't available on JavaScript 1.0 for Windows.
     if (!isNumber(s)) 
 	{
 		return false;
 	}
 
     // Now, explicitly change the type to integer via parseInt
     // so that the comparison code below will work both on 
     // JavaScript 1.2 (which typechecks in equality comparisons)
     // and JavaScript 1.1 and before (which doesn't).
     var num = parseInt (s);
 	
     return ((num >= a) && (num <= b));
 }
 	/* ----------------------------------------- End Validate Date ----------------------------------------- */

 

 /*=======================================================================*
  *	Function:validateDate(strDate)				 	  *
  *	Arguments: 							  *
  *		strDate date to validate				  *
  *	Synopsis: 							  *
  *		Validate a given date					  *
  *		Return Error String					  *
  *=======================================================================*/
function validateDate(strDate)
{
	/**
	 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
	 */

	var scDate = new Date()
	todayYear = '' + (scDate.getFullYear())
	todayDay = '' + (scDate.getDate())
	todayMonth = '' + (scDate.getMonth()+1)

	// Declaring valid date character, minimum year and maximum year
	var dtCh= '/';
	var minYear=todayYear;
	var maxYear=eval(todayYear) + 200;

  var iDMessage=''
  var dateString = strDate
	var daysInMonth = DaysArray(12)
	var pos1 = dateString.indexOf(dtCh)
	var pos2 = dateString.indexOf(dtCh,pos1+1)
	var strMonth = dateString.substring(0,pos1)
	var strDay = dateString.substring(pos1+1,pos2)
	var strYear = dateString.substring(pos2+1)
	strYr = strYear

	var month = parseInt(strMonth)
	var day = parseInt(strDay)
	var year = parseInt(strYr)

	if ((strDay.charAt(0) =='0') &&
	    (strDay.length>1))
	{
		strDay = strDay.substring(1)
	}

	if ((strMonth.charAt(0)=='0') &&
	    (strMonth.length>1))
	{
		strMonth=strMonth.substring(1)
	}

	for (var i = 1; i <= 3; i++)
	{
		if ((strYr.charAt(0)=='0') &&
		    (strYr.length>1))
		{
			strYr=strYr.substring(1)
		}
	}

	if ((pos1==-1) ||
	     (pos2==-1))
	{
		return 'The date format should be : mm/dd/yyyy\n'
	}

	if ((strMonth.length<1) ||
	     (month<1 || month>12))
	{
		iDMessage = iDMessage + 'Please enter a valid month\n'
	}

	if ((strDay.length<1) ||
	    (day<1 || day>31) ||
	    (month==2 && day>daysInFebruary(year)) ||
	    (day > daysInMonth[month]))
	{
		iDMessage = iDMessage + 'Please enter a valid day\\n'
	}

	if ((strYear.length != 4) ||
	    (year==0) ||
	    (year < minYear) ||
	    (year > maxYear))
	{
		var message = 'Please enter a valid 4 digit year between ' + minYear + ' and ' + maxYear + '\n'
		iDMessage = iDMessage + message
	}

	if ((dateString.indexOf(dtCh,pos2+1) !=-1) ||
	    (isNumber(stripCharsInBag(dateString, dtCh))==false))
	{
		iDMessage = iDMessage + 'Please enter a valid date\n'
	}

	// check to see if the date is greater or equal to today's
	if ( year == minYear )
	{
		if ((day > 0) &&
		    (day < todayDay) &&
		    (month == todayMonth))
		{
			iDMessage = iDMessage + 'Please enter a valid day\n'

		}

		if ((month > 0) &&
		    (month < todayMonth))
		{
			iDMessage = iDMessage + 'Please enter a valid month\n'
		}
	}

	return iDMessage
}