//  April 01, 2007
//
// This script will generate a date stamp that will 
// indicate when the HTML document was last modified.
//
// E.G.: -  
// "Page last changed 7 days ago - Friday, March 23rd, 2007 at 5:45 P.M." 
//
// usage: (HTML) <Script> document.write(lastMod());
//
// It is not dependent on client browser clock settings. 
//
// see: http://www.quirksmode.org/js/lastmod.html
// and: http://www.irt.org/articles/js130/index.htm
// 

function makeArray() {

// Build month array.

for (i = 0; i < makeArray.arguments.length; i++) 
  this[i + 1] = makeArray.arguments[i]; 
}

// Month names.

var modifiedMonth = new makeArray('January','February','March','April',
                                  'May','June','July','August','September',
                                  'October','November','December');

// Make a new Date object -modifiedDate- from
// the date string -document.lastModified-.

var modifiedDate = new Date(document.lastModified);

// Generate day (of week).

function modifiedDay(day) { 
      if (day == 0) return 'Sunday'; 
else  if (day == 1) return 'Monday';
else  if (day == 2) return 'Tuesday'; 
else  if (day == 3) return 'Wednesday';
else  if (day == 4) return 'Thursday'; 
else  if (day == 5) return 'Friday';
else                return 'Saturday'; }

// Generate date numeric suffix. 

function modifiedDateSuffix(date) { 
     if (date == 1 || date == 21 || date == 31) return 'st'; 
else if (date == 2 || date == 22)               return 'nd';  
else if (date == 3 || date == 23)               return 'rd'; 
else                                            return 'th'; }

// Generate 4 digit year.

function getCorrectedYear(year) {
    year = year - 0;
    if (year < 70) return (2000 + year);
    if (year < 1900) return (1900 + year);
    return year;
}

// Elapsed days calculations.

function lastMod() {

// We start by taking -document.lastModified- and making
// this string a Date object.

	var x = new Date (document.lastModified);

// Change the time to GMT, so that the comparision
// between two dates is valid. The client may be in
// another time zone than server. The GMTString is a
// string, so we have to make it a Date object again.

	Modif = new Date(x.toGMTString());

// Then we take the year with the function 

   	Year = takeYear(Modif);

// Take the month, the day, then calculate the number of
// milliseconds and divide by 86,400,000 to get the day
// number since the beginning of Epoch Time.

	Month = Modif.getMonth();
	Day = Modif.getDate();
	Mod = (Date.UTC(Year,Month,Day,0,0,0))/86400000;

// Then do the same for the current date and put the day
// number in -now-.

	x = new Date();
	today = new Date(x.toGMTString());
	Year2 = takeYear(today);
	Month2 = today.getMonth();
	Day2 = today.getDate();
	now = (Date.UTC(Year2,Month2,Day2,0,0,0))/86400000;

// Subtract -Mod- from -now- to get the number of days since
// the document was last modified.

	daysago = now - Mod;

// If -daysago- is less than zero (because the client has a
// wrong system time, for instance), return an empty string
// so that the script doesn't print out a negative number of days.

	if (daysago < 0) return '';

	unit = 'days';


// **** Convert -daysago- to an approximate arbitrary time period -unit-.
//
//	if (daysago > 730)	{
//		daysago = Math.floor(daysago/365);
//		unit = 'years';
//	}
//	else if (daysago > 60) {
//		daysago = Math.floor(daysago/30);
//		unit = 'months';
//	}
//	else if (daysago > 14) {
//		daysago = Math.floor(daysago/7);
//		unit = 'weeks';
// ****	}


// Get the minute of modification.
 
	var modifiedMinute = modifiedDate.getMinutes();

// Prefix a zero if less than ten minutes.
	
	if (modifiedMinute < 10) {
		modifiedMinute= '0' + modifiedMinute;
	}

// Get the hour of modification.

	var modifiedHour = modifiedDate.getHours();

// Set the AM or PM indicator.

	var AP;

	if (modifiedHour >= 12) {
		AP = 'P.M.';
	}
	else 	{
		AP = 'A.M.';
	}

// Convert the 24 hour clock time hours to a 12 hour time.

	if (modifiedHour >= 13) {
		modifiedHour -= 12;
	}
	if (modifiedHour == 0) {
		modifiedHour = 12;
	}

// Create a string -towrite- which is returned to the
// -document.write(lastMod())- command and is written
// into the page.
//
// Build unique text string for "today" and "yesterday",
// or use -daysago- followed by the correct -unit-,
// (default = "days"). 

// Concatenate verbose date text string and return.

	var towrite = 'Page last changed ';
	if (daysago == 0) towrite += 'today';
	else if (daysago == 1) towrite += 'yesterday';
	else towrite += daysago + ' ' + unit + ' ago';

	towrite += 
		' - ' + 
		modifiedDay(modifiedDate.getDay()) + 
		', ' + 
                modifiedMonth[modifiedDate.getMonth() + 1] + 
		' ' + 
                modifiedDate.getDate() + 
                modifiedDateSuffix(modifiedDate.getDate()) + 
		', ' + 
                getCorrectedYear(modifiedDate.getFullYear()) + 
		' at ' +
		modifiedHour + 
		':' +
		modifiedMinute +
		' ' + 
		AP;

	return towrite;
}


// Since it is supported by all browsers, use getYear().
// Divide the outcome by 100 and take the modulus, so that now we have a
// number from 0 to 99. If this number is smaller than 38, add 2000, 
// if it's larger add 1900. This always gives the correct year.
// (Why 38? Because Epoch Time will end in 2038.)

function takeYear(theDate) {
	x = theDate.getYear();
	var y = x % 100;
	y += (y < 38) ? 2000 : 1900;
	return y;
}

// end
