// A bunch of functions to check for valid date.
//  Adapted from ColdFusion functions.

function checkdate(object_value) 
{ 
//----------------------------------------------------------------------------------------
	//Returns true if value is a date format or is NULL
	//otherwise returns false	
//----------------------------------------------------------------------------------------
	if (object_value.length == 0)  return true; 

	//Returns true if value is a date in the mm/dd/yyyy format
	isplit = object_value.indexOf('/');

	if (isplit == -1 || isplit == object_value.length)	
		return false;
	sMonth = object_value.substring(0, isplit);
	isplit = object_value.indexOf('/', isplit + 1);

	if (isplit == -1 || (isplit + 1 ) == object_value.length)
		return false;

	sDay = object_value.substring((sMonth.length + 1), isplit);
	sYear = object_value.substring(isplit + 1);

	// The day must be 2 digits
	//if (sDay.length != 2)
	//	return false;
		
	// The month must be 2 digits	
	//if (sMonth.length != 2)
	//	return false;


	rtn = checkinteger(sMonth); //check month
		if (rtn == false)
		return false;
	if (! checkinteger(sDay)) //check day
		return false;
	if (! checkinteger(sYear)) //check year
		return false;
	if (! numberrange(sMonth, 1, 12)) //check month range
		return false;
	if (! numberrange(sYear, 0, 9999)) //check year range
		return false;
	if (! checkyearspecial(sYear)) // more year checks
	    return false;
	if (! checkday(sYear, sMonth, sDay)) // check day
		return false;
	
		return true;
}//END FUNCTION
//----------------------------------------------------------------------------------------

function checkday(checkYear, checkMonth, checkDay) 
{
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
	maxDay = 31;

	if (checkMonth == 4 || checkMonth == 6 ||
		checkMonth == 9 || checkMonth == 11)
		maxDay = 30;
	else if (checkMonth == 2) {
		if (checkYear % 4 > 0)
			maxDay =28;
		else if (checkYear % 100 == 0 && checkYear % 400 > 0)
			maxDay = 28;
		else
			maxDay = 29;
	}
	return numberrange(checkDay, 1, maxDay); //check day
}//END FUNCTION
//----------------------------------------------------------------------------------------


function checkinteger(object_value)
{
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
	var numbers = "0123456789";
	var isitlisted; //if isitlisted >=0, then were good.  if isitlisted < 0 that means nan
	
	//Characters can be only digits.
	for (i = 0; i < object_value.length; i++) 
	{
		isitlisted = numbers.indexOf(object_value.charAt(i));
		//alert(isitlisted);
		if(isitlisted < 0)
		return false;
	}	
	
	//All tests passed, so...
	return true;
}// END FUNCTION
//----------------------------------------------------------------------------------------


function numberrange(object_value, min_value, max_value) 
{
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
	// check minimum
	if (min_value != null) {
		if (object_value < min_value)
			return false;
	}

	// check maximum
	if (max_value != null) {
		if (object_value > max_value)
			return false;
	}

	//All tests passed, so...
	return true;
}//END FUNCTION
//----------------------------------------------------------------------------------------


function checkyearspecial(syear) 
{
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
	// The year must be 4 digits
	if (syear.length != 4) {
		return false;
	}

	// For the database the year must be >= 1753 and <= 9999
	if (syear.length == 4) {
	    if (syear < 1753 || syear > 9999) 
		return false;
	}
	//All tests passed, so...
	return true;
}//END FUNCTION
//----------------------------------------------------------------------------------------



function checkalldates(formname, dates)
{
//----------------------------------------------------------------------------------------
//  FORMNAME	- name of the form the controls belong to
//  DATES		- names of the controls that you want to check (if they are dates)
//  notes:		- the dates parameter should be an array of strings,
//				  enclosed by double quotes.
//				- there is a call to error() in this function, the error() function
//				  resides in globalutility.js
//	example:  	checkalldates('formname', "'date1', 'date2', 'date3', 'date4', 'date5'")
//----------------------------------------------------------------------------------------
	var datemsg = 'Please Enter a Valid Date. Format = 1/1/2002';

	eval('datearray = new Array(' + dates + ');');
	for( var i=0; i<datearray.length; i++)
	{
		rtn = checkdate(eval('document.' + formname + '.' + datearray[i] + '.value'));
		if(rtn == false)
		return error(datemsg, formname, datearray[i]);
	}
	return true;
} //END FUNCTION
//----------------------------------------------------------------------------------------

function date_compare(date1, date2){
// date1 and date2 are string values in the mm/dd/yyyy format or m/d/yyyy - valid dates all work.
// Returns -1 if date1 is earlier than date2
// Returns  0 if date1 is the same date as date2
// Returns  1 if date1 is later than date2

	var returnval; 

	// Start by parsing out the date1 fields
	var dateobj1 = new Date(date1);
	var dateobj2 = new Date(date2);
	
	if(dateobj1.getTime() == dateobj2.getTime()) return  0; 	
	if(dateobj1.getTime() <  dateobj2.getTime()) return -1; 	
	if(dateobj1.getTime() >  dateobj2.getTime()) return  1; 		
} //END FUNCTION
//----------------------------------------------------------------------------------------
 

