// /******************************************************************************************************
// 	date_funcs.js
// 	-------------
// 	
// 	I created this file for Y2K compliant date functionality because JScript date functions do not 
// 	handle y2k very well.  I base this on running the below compareDates function for dates 01/01/99 
// 	and 01/01/01 without the "makeFourDigitYear" call.  If you do, you will see that JScript returns
// 	99 as being the later date.
// 	
// 	when				who						what
// 	---------------------------------------------------------------------------------------------------
// 	11/05/2001			philip					initial version.
// 	
// /******************************************************************************************************/

function makeFourDigitYear( i_sDate )
{
	// /*************************************************************************************
	// Return the date passed in, except with a four digit year.
	// This function assumes that the date being passed in is valid
	// If its not, the result will be a -1 for whatever is invalid (-1/28/01, 6/5/-1, ...)
	// /*************************************************************************************/
	var nMonth = getMonth(i_sDate);
	var nDay = getDay(i_sDate);
	var nYear = getYear(i_sDate);
	var sFourDigitYearDate = ( nMonth.toString() + "/" + nDay.toString() + "/" + nYear.toString() );
	return sFourDigitYearDate;
}
		
function compareDates(i_sDate1, i_sDate2)
{
	// /*************************************************************************************
	//  Compare dates passed in.  If either date is not valid, ignore processing and return 0.
	//  
	//  Returns:
	// 	 0: dates are the same
	// 	 1: date1 > date2
	// 	-1: date1 < date2	 
	// /*************************************************************************************/
	
	// if the date is not valid, bail out.
	if ( isValidDate(i_sDate1) == false || isValidDate(i_sDate2) == false )
		return 0;
	
	var dtDate1 = Date.parse(makeFourDigitYear(i_sDate1));
	var dtDate2 = Date.parse(makeFourDigitYear(i_sDate2));

	var nDateDiff = dtDate1 - dtDate2;

	if (nDateDiff == 0) return 0;
	else if (nDateDiff > 0)  return 1;
	else if (nDateDiff < 0)  return -1;
}

function compareToToday( i_sDate )
{
	return compareDates(i_sDate, todaysDate());
}

function getMonth(i_sDate)
{
	// get the slash
	var nFirstSlash = i_sDate.indexOf('/');
	if ( nFirstSlash == -1 )
		return -1;
	
	// get the month part
	var sMonth = i_sDate.substring(0,nFirstSlash);
	
	// get the date part as a base-10 number
	var nMonth = parseInt(sMonth, 10);
			
	// make sure that its a number
	if	( typeof(nMonth) != "number" )
		return -1;

	return nMonth;	
}

function getDay(i_sDate)
{
	// get the slashes
	var nFirstSlash = i_sDate.indexOf('/');
	var nSecondSlash = i_sDate.lastIndexOf('/');
	if ( nFirstSlash == -1 || nSecondSlash == -1 )
		return -1;
	
	// get the day part
	var sDay = i_sDate.substring(nFirstSlash+1,nSecondSlash);
	
	// get the day part as a base-10 number
	var nDay = parseInt(sDay, 10);
			
	// make sure that its a number
	if	( typeof(nDay) != "number" )
		return -1;

	return nDay;
}

function getYear(i_sDate)
{
	// make sure we have two slashes
	var nFirstSlash = i_sDate.indexOf('/');
	var nSecondSlash = i_sDate.lastIndexOf('/');
	if ( nFirstSlash == -1 || nSecondSlash == -1 )
		return -1;
	
	// get the date parts
	var sYear = i_sDate.substring(nSecondSlash+1);
	
	// get the date parts as base-10 numbers
	var nYear = parseInt(sYear, 10);
			
	// make sure that they are numbers
	if	( typeof(nYear) != "number" )
		return -1;
	
	// make it four digits
	if ( sYear.length == 2 )
	{
		nYear += ( nYear < 50 ) ? 2000 : 1900;
	}
	else if ( sYear.length != 4 )
	{
		// year must be 2 or 4 chars long.
		return -1;
	}
	
	return nYear;
}

function isValidDate( i_sDate )
{
	// /******************************************************************************************************
	// 	there is an isValidDate() function defined in validate.js but it should be deprecated.  I found that
	// 	it is not Y2K compliant.  I have included this function here to begin the migration.
	// /******************************************************************************************************/

	// get the date parts as base-10 numbers
	var nMonth = getMonth(i_sDate);
	var nDay = getDay(i_sDate);
	var nYear = getYear(i_sDate);

	// check for invalid date parts (really just a check for slashes and 
	// to make sure they are numbers. we'll do the rest below here)
	if ( nMonth == -1 || nDay == -1 || nYear == -1 ) 
		return false;
			
	// now, validate the numbers
	switch ( nMonth )
	{
	case 1:
	case 3: 
	case 5:
	case 7:
	case 8:
	case 10:
	case 12:
		if ( nDay == 0 || nDay > 31 )
			return false; 
		break;
	case 2: 
		if ( nYear%4 == 0 && !(nYear%100 == 0) || nYear%400 == 0 )
		{
			if ( nDay == 0 || nDay > 29 )
				return false; 
		}
		else
		{
			if ( nDay == 0 || nDay > 28 )
				return false; 
		}
		break;
	case 4:
	case 6:
	case 9:
	case 11:
		if ( nDay == 0 || nDay > 30 )
			return false; 
		break;
	default:
		return false;
	}

	return true;
}


function todaysDate()
{
  var today = new Date();
  mm = today.getMonth() + 1;
  if (mm < 10) mm = "0" + mm;
  dd = today.getDate();
  if (dd < 10) dd = "0" + dd;
  yy = today.getYear();
  if (yy > 2000) yy -= 2000;
  if (yy < 10) yy = "0" + yy;
  return mm + "/" + dd + "/" + yy;
}


function isValidTime(i_sTime, i_bMilitary)
{
	// /*************************************************************************************
	//  Checks if time is in HH:MM:SS AM/PM format.
	//  The seconds and AM/PM are optional.
	//  
	//  ( Got this from http://javascript.internet.com )
	//  
	//  -------------------------------------------------------------------------------------
	//  03/08/02		philip		changed to not use alerts -- only return true or false
	//  03/09/02		philip		do a toString() conversion on the i_bMilitary param.  
	// 							that way, the user can pass it in as a string or 
	// 							a boolean and the function will still work the same.
	// /*************************************************************************************/
	var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;

	var matchArray = i_sTime.match(timePat);
	if (matchArray == null) {
		// Time is not in a valid format
		return false;
	}

	hour = matchArray[1];
	minute = matchArray[2];
	second = matchArray[4];
	ampm = matchArray[6];

	if (second=="") { second = null; }
	if (ampm=="") { ampm = null }

	if ( i_bMilitary.toString() == "true" ) {
		if (hour < 0  || hour > 23) {
			// Hour must be between 0 and 23
			return false;
		}
	} else {
		if (hour < 1  || hour > 12) {
			// Hour must be between 1 and 12
			return false;
		}
	}

	if ( i_bMilitary.toString() == "true" ) {
		if ( ampm != null ) {
			// you can't specify AM or PM
			return false;
		}
	} else {
		if ( ampm == null ) {
			// you must specify AM or PM
			return false;
		}
	}
	
	if (minute<0 || minute > 59) {
		// Minute must be between 0 and 59.
		return false;
	}

	if (second != null && (second < 0 || second > 59)) {
		// Second must be between 0 and 59.
		return false;
	}
	
	return true;
}


