
// Prototypes added to the Date object

Date.prototype.getTodayName = function( asShort ) {
	var theDay = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][this.getDay()];
	return ( (asShort) ? (theDay.substr(0,3) + '.') : theDay );
}

Date.prototype.getDayNameByNum = function( num, asShort ) {
   var theDay = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][num];
    return ( (asShort) ? (theDay.substr(0,3) + '.') : theDay ) ;
}

Date.prototype.getThisMonthName = function( asShort ) {
   var theMonth = ['January','February','March','April','May','June','July','August','September','October','November','December'][this.getMonth()]; 
    return ( (asShort) ? (theMonth.substr(0,3) + '.') : theMonth );
}

Date.prototype.getThisMonthNameCaps = function( asShort ) {
   var theMonth = ['JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER'][this.getMonth()]; 
   return ( (asShort) ? (theMonth.substr(0,3) + '.') : theMonth );
}

Date.prototype.getMonthByNum = function( num, asShort ) {
   var theMonth = ['January','February','March','April','May','June','July','August','September','October','November','December'][num]; 
   return ( (asShort) ? (theMonth.substr(0,3) + '.') : theMonth );
}

Date.prototype.getDaysInMonth = function( num ) {
   return ['31', '28', '31', '30', '31', '30', '31', '31', '30', '31', '30', '31'][num]; 
}

Date.prototype.getLastQuarter = function( num )
{
	return ['4', '4', '4', '1', '1', '1', '2', '2', '2', '3', '3', '3'][num];
}

Date.prototype.getCurrQuarter = function( num )
{
	return ['1', '1', '1', '2', '2', '2', '3', '3', '3', '4', '4', '4'][num];
}

Date.prototype.getQuarterStart = function( num )
{
	return ['01-01', '04-01', '07-01', '10-01'][num];
}

Date.prototype.getQuarterEnd = function( num )
{
	return ['03-31', '06-30', '09-30', '12-31'][num];
}

function addDate( where )
{
	var theDate = new Date();
	var theDate = theDate.getThisMonthNameCaps() + ' ' + theDate.getDate() + ', ' + theDate.getFullYear();
	document.getElementById( where ).innerHTML = theDate;
}

// This gives the "S" format:   yearNum-monthNum-dayNum   Eg: 1997-04-12
function getDayInSFormat( monthNum, dayNum, year )
{
	var theDate = new Date();
	
	monthNum = (monthNum < 10 ) ? '0' + monthNum : monthNum;
	dayNum = (dayNum < 10 ) ? '0' + dayNum : dayNum;

	return ( year + '-' +  monthNum + '-' + dayNum );
}

// This gives the S format of the date yesterday
function getYesterdayInSFormat()
{
	var theDate = new Date();
	
	// An index 1-31
	var todaysDate = theDate.getDate();
	
	// An index 0-11
	var todaysMonth = theDate.getMonth();

	// A 4 digit year
	var todaysYear = theDate.getFullYear();

	// Modify if needed
	var yesterdaysMonth = (todaysDate == 1) ? todaysMonth - 1 : todaysMonth;
	yesterdaysMonth = (yesterdaysMonth == -1) ? 11 : yesterdaysMonth;
	
	var yesterday = (todaysDate == 1) ? theDate.getDaysInMonth( yesterdaysMonth ) : todaysDate -1 ;
	var yesterdaysYear = (todaysMonth == 0 && todaysDate == 1 ) ? todaysYear - 1 : todaysYear;
		
	return getDayInSFormat( yesterdaysMonth + 1, yesterday, yesterdaysYear );
}

function getTomorrowInSFormat()
{
	var theDate = new Date();
	
	// An index 1-31
	var todaysDate = theDate.getDate();
	
	// An index 0-11
	var todaysMonth = theDate.getMonth();
	var numDaysInMonth = theDate.getDaysInMonth( todaysMonth );
	
	// A 4 digit year
	var todaysYear = theDate.getFullYear();

	// Modify if needed
	var tomorrowsMonth = (todaysDate == numDaysInMonth ) ? todaysMonth + 1 : todaysMonth;
	tomorrowsMonth = (tomorrowsMonth == 12) ? 0 : tomorrowsMonth;
	
//alert( todaysDate == numDaysInMonth + ', ' + todaysDate + ', ' + numDaysInMonth );
	var tomorrow = (todaysDate == numDaysInMonth ) ? 1 : todaysDate + 1 ;
	var tomorrowsYear = (todaysMonth == 11 && todaysDate == numDaysInMonth ) ? todaysYear + 1 : todaysYear;
		
	return getDayInSFormat( tomorrowsMonth + 1, tomorrow, tomorrowsYear );
}

// This gives the S format of the date one Month ago
function getLastMonthStartInSFormat()
{
	var theDate = new Date();
	
	// An index 0-11
	var todaysMonth = theDate.getMonth();

	// A 4 digit year
	var todaysYear = theDate.getFullYear();

	// Modify if needed
	var lastMonthsMonth = todaysMonth - 1;
	lastMonthsMonth = (lastMonthsMonth == -1) ? 11 : lastMonthsMonth;

	var lastMonthsDate = 1;
	var lastMonthsYear = (todaysMonth == 0) ? todaysYear - 1 : todaysYear;
		
	return getDayInSFormat( (lastMonthsMonth + 1), 1, lastMonthsYear );
}

// This gives the S format of the date one Month ago
function getLastMonthEndInSFormat()
{
	var theDate = new Date();
	
	// An index 0-11
	var todaysMonth = theDate.getMonth();

	// A 4 digit year
	var todaysYear = theDate.getFullYear();

	// Modify if needed
	var lastMonthsMonth = todaysMonth - 1;
	lastMonthsMonth = (lastMonthsMonth == -1) ? 11 : lastMonthsMonth;

	var lastMonthsDate = theDate.getDaysInMonth( lastMonthsMonth );
	var lastMonthsYear = (todaysMonth == 0) ? todaysYear - 1 : todaysYear;
		
	return getDayInSFormat( (lastMonthsMonth + 1), lastMonthsDate, lastMonthsYear );
}

function getLastQuarterStartInSFormat()
{
	var theDate = new Date();
	
	// An index 1 to 4
	var quarterNum = theDate.getLastQuarter( theDate.getMonth() );
	var todaysYear = theDate.getFullYear();

	return ( todaysYear + '-' + theDate.getQuarterStart( quarterNum - 1 ) );
}

function getLastQuarterEndInSFormat()
{
	var theDate = new Date();
	
	// An index 1 to 4
	var quarterNum = theDate.getLastQuarter( theDate.getMonth() );
	var todaysYear = theDate.getFullYear();

	return ( todaysYear + '-' + theDate.getQuarterEnd( quarterNum - 1 ) );
}

// This gives the S format of the date one year ago
function getLastYearStartInSFormat()
{
	var theDate = new Date();
	
	// A 4 digit year
	var todaysYear = theDate.getFullYear();

	return getDayInSFormat( 1, 1, (todaysYear-1) );
}

// This gives the S format of the date one year ago
function getLastYearEndInSFormat()
{
	var theDate = new Date();
	
	// A 4 digit year
	var todaysYear = theDate.getFullYear();

	return getDayInSFormat( 12, 31, (todaysYear-1) );
}

// This gives the S format of the date one week ago
function getCurrWeekInSFormat()
{
	var theDate = new Date();
	
	// An index 1-31
	var todaysDate = theDate.getDate();
	
	// An index 0-11
	var todaysMonth = theDate.getMonth();

	// A 4 digit year
	var todaysYear = theDate.getFullYear();

	// Modify if needed
	var currWeeksMonth = (todaysDate < 8) ? todaysMonth - 1 : todaysMonth;
	currWeeksMonth = (currWeeksMonth == -1) ? 11 : currWeeksMonth;

	var currWeeksDate = (todaysDate < 8) ? theDate.getDaysInMonth( currWeeksMonth ) - (7 - todaysDate) : todaysDate - 7 ;
	var currWeeksYear = (todaysMonth == 0 && todaysDate < 8 ) ? todaysYear - 1 : todaysYear;
//alert( theDate.getDaysInMonth( currWeeksMonth ) + ', ' +  currWeeksDate + ', '+ (8 - todaysDate)  );
		
	return getDayInSFormat( currWeeksMonth + 1, currWeeksDate, currWeeksYear );
}

// This gives the S format of the date one Month ago
function getCurrMonthInSFormat()
{
	var theDate = new Date();
	
	// An index 1-31
	var todaysDate = theDate.getDate();
	
	// An index 0-11
	var todaysMonth = theDate.getMonth();

	// A 4 digit year
	var todaysYear = theDate.getFullYear();

	// Modify if needed
	var currMonthsMonth = todaysMonth - 1;
	currMonthsMonth = (currMonthsMonth == -1) ? 11 : currMonthsMonth;

	var currMonthsDate = todaysDate;
	var currMonthsYear = (todaysMonth == 0) ? todaysYear - 1 : todaysYear;
		
//alert( getDayInSFormat( currMonthsMonth + ',' + currMonthsDate + ',' +currMonthsYear ) );
	return getDayInSFormat( currMonthsMonth + 1, currMonthsDate, currMonthsYear );
}

function getCurrQuarterStartInSFormat()
{
	var theDate = new Date();
	
	// An index 1 to 4
	var quarterNum = theDate.getCurrQuarter( theDate.getMonth() );
	var todaysYear = theDate.getFullYear();

	return ( todaysYear + '-' + theDate.getQuarterStart( quarterNum - 1 ) );
}

function getCurrQuarterEndInSFormat()
{
	var theDate = new Date();
	
	// An index 1 to 4
	var quarterNum = theDate.getCurrQuarter( theDate.getMonth() );
	var todaysYear = theDate.getFullYear();

	return ( todaysYear + '-' + theDate.getQuarterEnd( quarterNum - 1 ) );
}

// This gives the S format of the date one year ago
function getCurrYearInSFormat()
{
	var theDate = new Date();
	
	// An index 1-31
	var todaysDate = theDate.getDate();
	
	// An index 0-11
	var todaysMonth = theDate.getMonth();

	// A 4 digit year
	var todaysYear = theDate.getFullYear();

	return getDayInSFormat( todaysMonth + 1, todaysDate, todaysYear - 1 );
}

function getTodayInSFormat()
{
	var theDate = new Date();
	
	// An index 1-31
	var todaysDate = theDate.getDate();
	
	// An index 0-11
	var todaysMonth = theDate.getMonth();

	// A 4 digit year
	var todaysYear = theDate.getFullYear();

	return getDayInSFormat( todaysMonth + 1, todaysDate, todaysYear );
}

// Get the first day of summer, for the summer peak
//		It is the first Sunday of June.
function getFirstDayOfSummer( thisYear )
{
	// Set to June 1
	var theDate = new Date( 'June 01, ' + thisYear );
	
	var date = 1;
	
	var i = 0;
	while( theDate.getDay() != 0 )
		theDate.setDate( date++ );
		
	return theDate;
}

// Get the first day of summer, for the summer peak
//		It is the first Sunday of October.
function getLastDayOfSummer( thisYear )
{
	// Set to October 1
	var theDate = new Date( 'October 01, ' + thisYear );
	
	var date = 1;
	
	var i = 0;
	while( theDate.getDay() != 0 )
		theDate.setDate( date++ );
	
	return theDate;
}

// This parses the time in S format to a Date object format. So the reverse of the above functions
// So it goes from 1997-04-12 into April 12, 1997
function parseSFormat( sTime )
{
	var index = sTime.indexOf( '-', 0 );
	var year = sTime.slice( 0, index );
	var nextIndex = sTime.indexOf( '-', index + 1 );
	var month = Number( sTime.slice( index + 1, nextIndex ) ) - 1;
	var day = sTime.slice( nextIndex + 1 );

	return ( (new Date() ).getMonthByNum( month ) + ' ' + day + ', ' + year );
}
