/*
'**************************************************************************************************
'File Name:		_Date.js
'Object Type:	Javascript file
'
'Author Name:	Mark E. Watkins - Daugherty Systems, Inc.
'Creation Date:	11/06/2003
'
'Purpose:		Used as a library of functions pertaining to dates.  The file is included in any 
'				pages needing access to its functions.
'
'
'Include Dependencies:
'_Common.js
'_Math.js
'
'
'Date Changed		Changed By		Reason
'--------------------------------------------------------------------------------------------------
'
'
'**************************************************************************************************
*/

//
//Variable declarations.
var daysInMonth = new Array(13);
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;

///////////////////////////////////////////////////////
//
// FUNCTIONS
//
///////////////////////////////////////////////////////
function dateCompare( sDate1, sDate2 ) { 
	//
	//Purpose:
	//Determine if sDate1 is equal to, greater than, or
	//less than sDate2.
	//
	//Assumes the dates are valid.
	//
	//Owner: Mark E. Watkins
	//
	//Arguments:
	//sDate1 - date to compare.
	//sDate2 - date to be compared against.
	//
	//Return Values:
	// 1 - if sDate1 is greater than sDate2.
	// 0 - if sDate1 is equal to sDate2.
	//-1 - if sDate1 is less than sDate2.
	//
	var dtDateComp1 = new Date(sDate1);
	var dtDateComp2 = new Date(sDate2);
	
	if ( dtDateComp1.getFullYear() == dtDateComp2.getFullYear() ) { 
		//
		if ( dtDateComp1.getMonth() == dtDateComp2.getMonth() ) { 
			//
			if ( dtDateComp1.getDate() == dtDateComp2.getDate() ) { 
				//
				return 0;
			}
			else { 
				if ( dtDateComp1.getDate() > dtDateComp2.getDate() ) { 
					return 1; }
				else { 
					return -1; }
			}
		}
		else { 
			//
			if ( dtDateComp1.getMonth() > dtDateComp2.getMonth() ) { 
				return 1; }
			else { 
				return -1; }
		}
	}
	else { 
		if ( dtDateComp1.getFullYear() > dtDateComp2.getFullYear() ) 
			return 1;
		else 
			return -1;
	}
}
function formatDate( sDate, b2DigitMonthDay, bShowCentury, sDelimiter, bNotifyIfInvalid ) { 
	//
	//Purpose:
	//Formats 'sDate' based on the 'b2DigitMonthDay', 'bShowCentury' 
	//and 'sDelimiter' arguments.
	//
	//Owner: Mark E. Watkins
	//
	//Arguments:
	//sDate - the date to evaluate.
	//
	//b2DigitMonthDay - if true, then the month and day values will
	//be two digits from Jan. through Sep.  If false, then they'll
	//be one digit.
	//
	//bShowCentury - if true, then the year will have the century.  Otherwise, 
	//it will not.
	//
	//sDelimiter - the delimiter to use when the date is formatted.
	//
	//bNotifyIfInvalid - if true, then an alert('...') box will appear if the 
	//sDate argument is not a valid date.  If false, no alert will appear.
	//
	//Returns:
	//A formatted date based on the arguments.  If 'sDate' is not a 
	//valid date, then the user will be notified.
	//
	
	if ( sDate.length == 0 ) return "";
	
	//
	//Validate the arguments.
	if ( isEmpty(sDelimiter) ) { 
		//
		if ( sDate.indexOf("/") != -1 ) { 
			//
			sDelimiter = "/";
		}
		else if (sDate.indexOf("-") != -1 ) { 
			//
			sDelimiter = "-";
		}
		else { 
			//
			sDelimiter = "/";
		}
	}
	
	//
	//Determine if the date is valid.
	if ( bNotifyIfInvalid && !isDate(sDate) ) { 
		//
		alert("The date entered is not valid.");
		return "";
	}
	if ( !isDate(sDate) ) return "";
	
	var asNow = sDate.split("/");
	//		
	//Format the date based on the arguments.
	if ( b2DigitMonthDay ) { 
		//
		if ( asNow[0].length == 1 ) asNow[0] = "0" + asNow[0];
		if ( asNow[1].length == 1 ) asNow[1] = "0" + asNow[1];
	}
	else { 
		//
		if ( asNow[0].length == 2 && asNow[0].substr(0, 1) == "0" ) asNow[0] = asNow[0].substr(1, 1);
		if ( asNow[1].length == 2 && asNow[1].substr(0, 1) == "0" ) asNow[1] = asNow[1].substr(1, 1);
	}
	if ( bShowCentury ) { 
		//
		if ( asNow[2].length != 4 ) { 
			//
			if ( asNow[2] > 50 ) 
				asNow[2] = "19" + asNow[2];
			else 
				asNow[2] = "20" + asNow[2];
		}
	}
	else if ( asNow[2].length == 4 ) { 
		//
		asNow[2] = asNow[2].substr(2, 2);
	}
	//
	//Return the formatted date.
	return asNow[0] + sDelimiter + asNow[1] + sDelimiter + asNow[2];
}
function isDateCurrent(sDate) {
	//
	//Purpose:
	//Returns whether or not 'sDate' is a current date.
	//
	//Owner: Charles L. Hughes
	//
	//Arguments:
	//sDate - the date to validate.
	//
	var isValid = true;
	if( isDate(sDate) ) {
		var today = new Date();
		var compareDate = new Date(sDate);
		//
		if( compareDate < today ) {
			isValid = false;
		}
	}
	return isValid;
}

function isDate( sDate ) { 
	//
	//Purpose:
	//Returns whether or not 'sDate' is a valid date.
	//
	//Owner: Mark E. Watkins
	//
	//Arguments:
	//sDate - the date to validate.
	//
	if ( sDate.length == 0 ) return true;
	//
	//Determine the delimiter used.
	if ( sDate.indexOf("/") != -1 ) var sDelimiter = "/";
	else if ( sDate.indexOf("-") != -1 ) var sDelimiter = "-";
	//
	//Extract each component.
	var saDate = sDate.split(sDelimiter);
	var iMonth = 0;
	var iDay   = 0;
	var iYear  = 0;
	if( saDate.length > 0) { iMonth = saDate[0]; }
	if( saDate.length > 1) { iDay = saDate[1]; }
	if( saDate.length > 2) { iYear = saDate[2]; }
	//
	return ( (isMonth(iMonth, false) && isDay(iMonth, iDay, iYear, false) && isYear(iYear, false)) );
}
function isMonth( iMonth ) {
	//
	//Purpose:
	//Returns whether or not 'iMonth' is a valid month.
	//
	//Owner: Mark E. Watkins
	//
	//Arguments:
	//iMonth - the month to validate.
	//

	if ( isEmpty(iMonth) ) 
		return ( isMonth.arguments[1] == true );
	//
	var rangeOk = isInRange(iMonth, 1, 12);
	return rangeOk
}
function isDay( iMonth, iDay, iYear ) {
	//
	//Purpose:
	//Returns whether or not 'iDay' is a valid day.
	//
	//Owner: Mark E. Watkins
	//
	//Arguments:
	//iMonth - the day to validate.
	//
	if ( isEmpty(iDay) ) 
		return ( isDay.arguments[1] == true );
	if ( iMonth.length == 2 && iMonth.substring(0, 1) == "0" )
		iMonth = iMonth.substring(1);
	//
	var iDaysInMonth = daysInMonth[iMonth];
	
	if ( iMonth == 2 ) 
		iDaysInMonth = daysInFebruary(iYear);
	//
    var rangeOk = isInRange(iDay, 1, iDaysInMonth);
    return rangeOk;
}
function isYear( iYear ) {
	//
	//Purpose:
	//Returns whether or not 'iYear' is a valid year.
	//
	//Owner: Mark E. Watkins
	//
	//Arguments:
	//iMonth - the year to validate.
	//
	
	if ( isEmpty(iYear) ) 
		return ( isYear.arguments[1] == true );
	//
    if ( isNaN(iYear) ) return false;
    if ( iYear.toString().length < 4 && iYear.toString().length != 2 ) return false;
    //
    //Format the year to a four digit year.  Used when comparing to 
    //the 1900 minimum date value.
	if ( iYear.toString().length != 4 ) { 
		//
		iYear = iYear.toString();
		if ( iYear > 50 ) 
			iYear = "19" + iYear;
		else 
			iYear = "20" + iYear;
		iYear = parseInt(iYear)
	}
	//
	//Determine if the entered date is before 1900.
    if( iYear < 1900) { 
		//
		alert("The system only allows dates after 1900.");
		return false;
    }
    return true;
}
function daysInFebruary( iYear ) { 
	//
	//Purpose:
	//Returns the total days in february for a given year.
	//
	//February has 29 days in any year evenly divisible by four,
    //EXCEPT for centurial years which are not also divisible by 400.
	//
	//Arguments:
	//iYear - the year used to determine the number of days in 
	//february.
	//
	
    return ( ((iYear % 4 == 0) && ((!(iYear % 100 == 0)) || (iYear % 400 ==0))) ? 29 : 28 );
}
function dateAdd( psDate, piNum, psAddTo ) { 
	//
	//Purpose:
	//Returns the psDate plus piNum based on one of the pbAddTo vars.
	//
	//Arguments:
	//psDate - the date to add to. 
	//
	//piNum - the amount to add to psDate.
	//
	//psAddTo - possible values are: m, d, y.  Adds piNum to that specified.
	//

	if ( !isDate(psDate) ) return ""
	if ( psAddTo != "m" && psAddTo != "d" && psAddTo != "y" ) return "";
	psAddTo = psAddTo.toLowerCase();
	
	psDate = formatDate(psDate, true, true, "/", false);
	//
	var oDate = new Date(psDate);
	var asDate = psDate.split("/");
	var iDatePart;

	switch(psAddTo) { 
	case "m":
		iDatePart = oDate.getMonth() + 2;
		psDate = iDatePart.toString() + "/" + asDate[1] + "/" + asDate[2];
		psDate = formatDate(psDate, true, true, "/", false);
		break;
		
	case "d":
		iDatePart = oDate.getDate() + 1;
		psDate = asDate[0] + "/" + iDatePart.toString() + "/" + asDate[2];
		psDate = formatDate(psDate, true, true, "/", false);
		break;
		
	case "y":
		iDatePart = oDate.getYear() + 1
		psDate = asDate[0] + "/" + asDate[1] + "/" + iDatePart.toString();
		psDate = formatDate(psDate, true, true, "/", false);
		break;
		
	}
	//
	return psDate;
}
function dateSubtract( psDate, piNum, psSubtractFrom ) { 
	//
	//Purpose:
	//Returns the psDate minus piNum based on one of the pbAddTo vars.
	//
	//Arguments:
	//psDate - the date to add to. 
	//
	//piNum - the amount to add to psDate.
	//
	//psSubtractFrom - possible values are: m, d, y.  Adds piNum to that specified.
	//

	if ( !isDate(psDate) ) return ""
	if ( psSubtractFrom != "m" && psSubtractFrom != "d" && psSubtractFrom != "y" ) return "";
	psSubtractFrom = psSubtractFrom.toLowerCase();
	
	psDate = formatDate(psDate, true, true, "/", false);
	//
	var oDate = new Date(psDate);
	var asDate = psDate.split("/");
	var iDatePart;

	switch(psSubtractFrom) { 
	case "m":
		iDatePart = oDate.getMonth() + 1;
		psDate = iDatePart.toString() + "/" + asDate[1] + "/" + asDate[2];
		psDate = formatDate(psDate, true, true, "/", false);
		break;
		
	case "d":
		iDatePart = oDate.getDate() - 1;
		psDate = asDate[0] + "/" + iDatePart.toString() + "/" + asDate[2];
		psDate = formatDate(psDate, true, true, "/", false);
		break;
		
	case "y":
		iDatePart = oDate.getYear() - 1
		psDate = asDate[0] + "/" + asDate[1] + "/" + iDatePart.toString();
		psDate = formatDate(psDate, true, true, "/", false);
		break;
		
	}
	//
	return psDate;
}
//eof: Mark E. Watkins