// $Header: /Ready Reckoner (2)/asproot/js/dates.js 1     9/12/02 18:08 Thentzsc $ 
//-------------------------------------------------------------------------------- 
// Document	: dates.js
// Purpose 	: Date validation which supports cross browser compatibility with 
//		  NS6.2.3, NS7, IE5.x. 
// Project 	: Ready Reckoner 
// History 	: PA 03-Oct-2002 (Created)
// Author 	: pash 
//-------------------------------------------------------------------------------- 
// Revision History 
//-------------------------------------------------------------------------------- 
// $Revision: 1 $ 
// $Log: /Ready Reckoner (2)/asproot/js/dates.js $ 
//
//1     9/12/02 18:08 Thentzsc
//added from uat server due to corruption problems
//
//2     4/10/02 9:54 Pash
//
//1     3/10/02 15:57 Pash
//Created
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
// Purpose		Date validation of a text string.
//	
// Parameters	strDate - text string
//
// Returns 		Empty string (Success)
//				Populated string (Fail) - String contains error description.			
//
// History		PA 02-Oct-2002 v0.1 (Created) 
//
// Comment		Cross Browser Version. NS6.2.3, NS7,IE5x
//--------------------------------------------------------------------------------
function valDate(strDate) 
{
	var strRe; // Regular expression
	var intDaysStop;
	var intStart;
	var strMonth;
	var strYear;
	var strDay;
	var intYear;
	var dblLeap;
	var strLeap;
	var blnError;
	
	var aDatePatterns = new Array();
	var aMonthPatterns = new Array();
	var aDayPatterns = new Array();
	var aYearPatterns = new Array();

	var aCalendar = new Array(0,31,0,31,30,31,30,31,31,30,31,30,31);
	
	//list of valid regular expression patterns to check against
	aDatePatterns[0] = "\\d{2}/\\d{2}/\\d{4}"; // dd/mm/yyyy
	aDatePatterns[1] = "\\d{1}/\\d{2}/\\d{4}"; // d/mm/yyyy
	aDatePatterns[2] = "\\d{2}/\\d{1}/\\d{4}"; // dd/m/yyyy
	
	aMonthPatterns[0] = "/\\d{2}/"; // /mm/
	aMonthPatterns[1] = "/\\d{1}/"; // /m/
	
	aDayPatterns[0] = "\\d{2}/"; // dd/
	aDayPatterns[1] = "\\d{1}/"; // d/
	
	aYearPatterns[0] = "/\\d{4}"; // /yyyy
	
	//Check to see if the date matches any of the patterns
	if(strDate=="")
	{
		return("Empty string");
	}

	blnError = true;
	for (i=0;i<aDatePatterns.length;i++)
	{
		strRe = new RegExp(aDatePatterns[i],'i');
		strFound = strDate.match(strRe);
		if (strFound!=null)
		{
		  blnError = false;
		}			 
	}
	
	// Test for invalid expression pattern
	if (blnError == true)
	{
		return("Not a valid date format"); // Not a valid date format
	}
	
	// Get the Month section of the date
	strMonth = getDateElement(aMonthPatterns,strDate,1,-2);

	// Get the days section of the date
	strDays = getDateElement(aDayPatterns,strDate,0,-1);

	// Get the year section of the date
	strYear = getDateElement(aYearPatterns,strDate,1,-1);
	
	// Check that the days are greater than 0
	if (parseInt(strDays)==0)
	{
		return("Days must be greater than 0"); 
	}
	
	// Check that the month is below 12
	if (parseInt(strMonth)>12 || parseInt(strMonth)==0)
	{
		return("Month must be greater than 0 less or equal to 12"); 
	}
	// Check the year is 1900 or above
	if (parseInt(strYear)<1900)
	{
		return("Year must be greater or equal to 1900"); 
	}
	
	if (parseInt(strMonth)==2)
		//check for leap year
		   {
		   intYear = parseInt(strYear);
	  	   dblLeap = intYear / 4;
		   //search the string for a decimal point
		   strLeap = dblLeap.toString(10);
  		   strRe = ".";
  		   intSearch = strLeap.lastIndexOf(strRe);
		   //check for floating point
		  if (intSearch==-1)
			 { intDaysStop = 29;}
		  else
			 { intDaysStop = 28;}
		   }
	else
		  intDaysStop = aCalendar[parseInt(strMonth)];		   
	
	// Check our days range is lower or equal to the entered month
	if(parseInt(strDays) > intDaysStop)
	{
		// Error
		return("Maximum days for the month exceeded"); 
	}
	 
	// Date is valid
	return("");
}//valDate

//--------------------------------------------------------------------------------
// Purpose		Searches for regular expressions given a valid date format.
//	
// Parameters	aPatterns - Array of regular expressions to match against
//
// Returns 		Populated string (Success) - String contains found item.
//				Empty String (Fail)			
//
// History		PA 03-Oct-2002 v0.1 (Created)
//
// Comment		Cross Browser Version. NS6.2.3, NS7,IE5x	
//--------------------------------------------------------------------------------
function getDateElement(aPatterns,strDate,intIncr,intDecr)
{
	var	blnMatch = false;
	var strFound;
	var strItem;
	var intStart;
	
	// get the month
	for (i=0;i<aPatterns.length;i++)
	{ 
		strRe = new RegExp(aPatterns[i],'i');
		strFound = strDate.match(strRe);
		
		if(strFound!=null && blnMatch==false)
		{
			blnMatch=true;
			strItem = strFound[0];
			intStart = strFound.index;
			intStart = intStart + intIncr;
			strItem = strDate.substr(intStart,strItem.length+intDecr); 
		}
	}
	
	return(strItem);
}//getDateElement
