// ===================================================================
// Orignal source code downloaded from
// Author: Matt Kruse <matt@mattkruse.com>
// WWW: http://www.mattkruse.com/
// 
// Modifications for use in Genesys Agent Scripting by:
// John Conneely <jconneely@RiverStarSoftware.com>
// Randy Meacham <rmeacham@RiverStarSoftware.com>
// ===================================================================

// HISTORY
// ------------------------------------------------------------------
// March 11, 2003: Added "NNN" formatting option. Doesn't match up
//                 perfectly with SimpleDateFormat formats, but 
//                 backwards-compatability was required.
// January, 2004: Removed functions not being used by Agent Scripting.
//		           Remaining functions are simply used to validate a
//		           Date.  All other functions have been revmoved.
// April 01, 2004:	@01 - Added to function "isDate" input string parameters
//					containing the month names and weekday names along
//					with their abbreviations.
// April 21, 2004:  @02 - Added keywords "EEEE" (full weekday name) and "MMMM"
//					(full month name).  Changed "MMM" to abbreviated month name.
// ------------------------------------------------------------------
// These functions use the same 'format' strings as the 
// java.text.SimpleDateFormat class, with minor exceptions.
// The format string consists of the following abbreviations:
// 
// Field        | Full Form           | Short Form
// -------------+---------------------+-----------------------
// Year         | yyyy (4 digits)     | yy (2 digits), y (2 or 4 digits)
// Month        | MMMM (name or abbr.)| MM (2 digits), M (1 or 2 digits)
//              | MMM (abbr.)         |
//              | NNN (abbr.)         |
// Day of Month | dd (2 digits)       | d (1 or 2 digits)
// Day of Week  | EEEE (name)         | E (abbr)
//              | EE (name)           |
// Hour (1-12)  | hh (2 digits)       | h (1 or 2 digits)
// Hour (0-23)  | HH (2 digits)       | H (1 or 2 digits)
// Minute       | mm (2 digits)       | m (1 or 2 digits)
// Second       | ss (2 digits)       | s (1 or 2 digits)
// AM/PM        | a                   |
//
// NOTE THE DIFFERENCE BETWEEN MM and mm! Month=MM, not mm!
// Examples:
//  "MMM d, y" matches: January 01, 2000
//                      Dec 1, 1900
//                      Nov 20, 00
//  "M/d/yy"   matches: 01/20/00
//                      9/2/00
//  "MMM dd, yyyy hh:mm:ssa" matches: "January 01, 2000 12:30:45AM"
// ------------------------------------------------------------------

var MONTH_NAMES=new Array(24);	//@01
var DAY_NAMES=new Array(14);	//@01

// ------------------------------------------------------------------
// isDate ( date_string, format_string )
// Returns true if date string matches format of format string and
// is a valid date. Else returns false.
// It is recommended that you trim whitespace around the value before
// passing it to this function, as whitespace is NOT ignored!
// ------------------------------------------------------------------
function isDate(val,format,monthNames,weekDayNames) //@01
{
	var i_val=0;
	var i_format=0;
	var c="";
	var token="";
	var token2="";
	var x,y;
	var now=new Date();
	var year=now.getYear();
	var month=now.getMonth()+1;
	var date=1;
	var hh=0;
	var mm=0;
	var ss=0;
	var ampm="";

	if( val.length == 0 )
	{
		return true;
	}

	// Parse month list for the month names and abbreviations.  Store each		//@01	
	// parsed item in the MONTH_NAMES array										//@01	
	var idxStart = 0;															//@01	
	var idxEnd;																	//@01	
	for (i=0; i < 24; i++)														//@01	
	{																			//@01	
		idxEnd	= monthNames.indexOf(";",idxStart);								//@01		
		MONTH_NAMES[i] = monthNames.substr(idxStart,idxEnd-idxStart);			//@01	
		idxStart	= idxEnd+1;													//@01		
	}																			//@01		
	
	// Parse weekday list for the weekday names and abbreviations.  Store each	//@01	
	// parsed item in the DAY_NAMES array										//@01		
	idxStart = 0;																//@01			
	for (i=0; i < 14; i++)														//@01		
	{																			//@01		
		idxEnd	= weekDayNames.indexOf(";",idxStart);							//@01	
		DAY_NAMES[i] = weekDayNames.substr(idxStart,idxEnd-idxStart);			//@01	
		idxStart	= idxEnd+1;													//@01	
	}																			//@01		
		
	while (i_format < format.length)										
	{																	
		// Get next token from format string
		c=format.charAt(i_format);
		token="";
		while ( (format.charAt(i_format) == c) 
		&&      (i_format < format.length) ) 
		{
			token += format.charAt(i_format++);
		}
		// Extract contents of value based on format token
		if (token=="yyyy" || token=="yy" || token=="y") 
		{
			if(token=="yyyy") 
			{ 
				x=4;
				y=4; 
			}
			else if(token=="yy")   
			{ 
				x=2;
				y=2; 
			}
			else if (token=="y")    
			{ 
				x=2;
				y=4; 
			}
			year=_getInt(val,i_val,x,y);
			if(year==null)
			{ 
				return false; 
			}
			i_val += year.length;
			if (year.length==2) 
			{
				if(year > 70) 
				{ 
					year=1900+(year-0); 
				}
				else 
				{ 
					year=2000+(year-0); 
				}
			}
		}
		else if(token=="MMMM" || token=="MMM" || token=="NNN")  //@02
		{
			month=0;
			for( var i=0; i<MONTH_NAMES.length; i++ ) 
			{
				var month_name = MONTH_NAMES[i];
				if( val.substring(i_val, i_val+month_name.length).toLowerCase() == month_name.toLowerCase() ) 
				{
					if ( token=="MMMM" || ( token=="MMM" && i >11 ) || ( token=="NNN" && i >11 ) ) //@02
					{
						month=i+1;
						if (month>12) 
						{ 
							month -= 12; 
						}
						i_val += month_name.length;
						break;
					}
				}
			}
			if( (month < 1) 
			||  (month >12) )
			{
				return false;
			}
		}
		else if( token=="EEEE" || token=="EE" || token=="E" ) //@02
		{
			for( var i=0; i<DAY_NAMES.length; i++) 
			{
				var day_name=DAY_NAMES[i];
				if( val.substring(i_val,i_val+day_name.length).toLowerCase() == day_name.toLowerCase() ) 
				{
					i_val += day_name.length;
					break;
				}
			}
		}
		else if(token=="MM" || token=="M") 
		{
			month=_getInt(val,i_val,token.length,2);
			if( month==null || ( month < 1 ) || ( month > 12) )
			{
				return false;
			}
			i_val+=month.length;
		}
		else if( token=="dd" || token=="d") 
		{
			date=_getInt(val,i_val,token.length,2);
			if( date==null || ( date < 1) || (date > 31))
			{
				return false;
			}
			i_val+=date.length;
		}
		else if( token=="hh" || token=="h") 
		{
			hh=_getInt(val,i_val,token.length,2);
			if( hh==null || ( hh<1 ) || ( hh>12 ))
			{
				return false;
			}
			i_val+=hh.length;
		}
		else if( token=="HH" || token=="H" ) 
		{
			hh=_getInt(val,i_val,token.length,2);
			if( hh==null || ( hh < 0 ) || ( hh > 23 ))
			{
				return false;
			}
			i_val+=hh.length;
		}
		else if( token=="mm" || token=="m" ) 
		{
			mm=_getInt(val,i_val,token.length,2);
			if( mm==null || ( mm < 0 ) || ( mm > 59 ) )
			{
				return false;
			}
			i_val+=mm.length;
		}
		else if( token=="ss" || token=="s" ) 
		{
			ss=_getInt(val,i_val,token.length,2);
			if( ss==null || ( ss < 0 ) || ( ss > 59 ) )
			{
				return false;
			}
			i_val+=ss.length;
		}
		else if( token=="a" ) 
		{
			if( val.substring(i_val,i_val+2).toLowerCase()=="am" ) 
			{
				ampm="AM";
			}
			else if( val.substring(i_val,i_val+2).toLowerCase()=="pm") 
			{
				ampm="PM";
			}
			else
			{
				return false;
			}
			i_val+=2;
		}
		else 
		{
			if( val.substring(i_val,i_val+token.length) != token) 
			{
				return false;
			}
			else
			{
				i_val+=token.length;
			}
		}
	}
	// If there are any trailing characters left in the value, it doesn't match
	if (i_val != val.length) 
	{ 
		return false; 
	}
	// Is date valid for month?
	if( month==2 ) 
	{
		// Check for leap year
		if ( ( (year%4==0)&&(year%100 != 0) ) || (year%400==0) ) 
		{ 
			// leap year
			if( date > 29 )
			{ 
				return false; 
			}
		}
		else
		{ 
			if (date > 28) 
			{ 
				return false; 
			} 
		}
	}
	else if( (month==4) || (month==6) || (month==9) || (month==11) ) 
	{
		if(date > 30) 
		{ 
			return false; 
		}
	}
	return true;
}


	
// ------------------------------------------------------------------
// Utility functions for parsing dates
// ------------------------------------------------------------------
function _isInteger(val) {
	var digits="1234567890";
	for(var i=0; i < val.length; i++) 
	{
		if( digits.indexOf(val.charAt(i))==-1) 
		{ 
			return false; 
		}
	}
	return true;
}

function _getInt(str,i,minlength,maxlength) 
{
	for( var x=maxlength; x>=minlength; x--) 
	{
		var token=str.substring(i,i+x);
		if(token.length < minlength) 
		{ 
			return null; 
		}
		if( _isInteger(token) ) 
		{ 
			return token; 
		}
	}
	return null;
}
	



