/*
    Date Time Operations for the Web   
    Version: 1.10a
*/

// Returns: A list of all the native EcmaScript DateTime formats
//          as formatted in your environment (Browser/OS combination).
// Remarks: Returned as XHTML
function GetDateTimeFormats(theDateTime) {
  var result = "";
  
  result += "<tr><td colspan='2'>Local Formats, Browser Determined *******</td> <tr />";
  result += "<tr><td>theDateTime [Defaults to toString()] </td><td>" + theDateTime + "</td></tr>";
  result += "<tr><td>theDateTime.toString()               </td><td>" + theDateTime.toString() + "</td></tr>";
  result += "<tr><td>theDateTime.toDateString()           </td><td>" + theDateTime.toDateString() + "</td></tr>";
  result += "<tr><td>theDateTime.toTimeString()           </td><td>" + theDateTime.toTimeString() + "</td></tr>";
  result += "<tr><td colspan='2'> &#32;</td></tr>";
  result += "<tr><td colspan='2'>Local Formats, System Determined *******</td>";
  result += "<tr><td>theDateTime.toLocaleString()           </td><td>" + theDateTime.toLocaleString() + "</td></tr>";
  result += "<tr><td>theDateTime.toLocaleDateString()       </td><td>" + theDateTime.toLocaleDateString() + "</td></tr>";
  result += "<tr><td>theDateTime.toLocaleTimeString()           </td><td>" + theDateTime.toLocaleTimeString() + "</td></tr>";
  result += "<tr><td></td><td></td></tr>";
  result += "<tr><td colspan='2'>Universal Time Coordinated Formats ******* </td>";  
  result += "<tr><td>theDateTime.toUTCString()           </td><td>" + theDateTime.toUTCString() + "</td></tr>";
  result += "<tr><td>theDateTime.toGMTString()           </td><td>" + theDateTime.toGMTString() + "</td></tr>";
  result += "<tr><td>&#32;</td><td>&#32;</td></tr>";
  result += "<tr><td colspan='2'>Other Formats ******* </td>"; 
  result += "<tr><td>theDateTime.getTime() [milliseconds since 1970-01-01 00:00:00 UTC]</td><td> " + theDateTime.getTime() + "</td></tr>";   
  result += "<tr><td>theDateTime.getMilliseconds() [milliseconds since full previous second]</td><td> " + theDateTime.getMilliseconds() + "</td></tr>";
  result += "<tr><td>theDateTime.getTimezoneOffset [Minutes offset from UTC]           </td><td> " + theDateTime.getTimezoneOffset() + "</td></tr>";
  
  return result;
}


// Returns: When the web page was last modifed in a user friendly format.
// Remarks: Based on information about the page returned from the server.
//          Specifically, based on the LastModified Property returned from the server
//          which is usually the date/time stamp of when the file was uploaded.
//          Go to: http://www.web-caching.com/showheaders.html to determine
//          headers generated by web page.
//          shows the number of minutes if less than an hour,
//          the number of hours if less than a day,
//          "the number of days only if it's less than 15, then the 
//          number of weeks if it's less than 9, 
//          then the number of months if it's less than 25, 
//          and then the number of years. 
//          I only do a rough-and-ready calculation."
// DependsOn: takeYear(theDate)
// Usage:   document.write("Web site" + lastModifiedFriendly())
// Example: -> " last changed 3 days ago."
// Based On:  Paul Koch, quirksmode.ord, Last modified date
//          http://www.quirksmode.org/js/lastmod.html
function lastModifiedFriendly()
{
  
	var lastModifiedDateTime;
	var nowDateTime;
	lastModifiedDateTime  = new Date (document.lastModified);
  //lastModifiedDateTime = new Date(2005,12,04,00,16,18);   // For Testing
	nowDateTime = new Date();
	//nowDateTime = new Date(2005,12,04,00,18,18);   // For Testing
  
  var unit;
	var dateDiff = (nowDateTime - lastModifiedDateTime)
	var daysago = dateDiff/86400000;
	
	// An error. now is before page was modified. 
	if (daysago <= 0)
	{
	  return ' updated error. <br />'
	         + ' Your system date/time ------------------------:  ' + nowDateTime.toUTCString() + '<br />' 
	         + ' is set on or before the page update date/time:  ' + lastModifiedDateTime.toUTCString() + '.';
	} 
	
	var oneMinute = 60 * 1000; 
  var oneHour = oneMinute * 60; 
  var hoursDiff;
  var minutesDiff;
	
	if (daysago > 730) {
		diff = Math.floor(daysago/365);
		unit = 'years';
	} else if (daysago > 60) {
		diff = Math.floor(daysago/30);
		unit = 'months';
	} else if (daysago > 14) {
		diff = Math.floor(daysago/7);
		unit = 'weeks';
	} else if (daysago > 2) {
	  diff = Math.floor(daysago);
	  unit = 'days';
	} else if (daysago < 1) {
	  
	  // If less than 1 hour use minutes.
	  if ((dateDiff/oneHour) < 1 ) {
	    minutesDiff = parseInt(dateDiff/oneMinute);
	  } else {
	    hoursDiff = parseInt(dateDiff/oneHour);
	  }
	}
	
	var towrite = ' updated ';
	
	if (typeof minutesDiff != 'undefined') towrite += minutesDiff + (minutesDiff == 1? ' minute' :' minutes') + ' ago';
	else if (daysago < 1) towrite += hoursDiff + (hoursDiff == 1? ' hour' :' hours') + ' ago';
	else if (daysago >= 1 && daysago <= 2) towrite += 'yesterday';
	else towrite += diff + ' ' + unit + ' ago';
	
	/*
	towrite += '<br /> Your system date/time ------------------------:  ' + nowDateTime.toUTCString() + '<br />' 
	         + ' is set on or before the page update date/time:  ' + lastModifiedDateTime.toUTCString() + '.';
	*/
	return towrite + '.';
}

// Remarks: Utility function for lastModifiedFriendly()
function takeYear(theDate)
{
	x = theDate.getYear();
	var y = x % 100;
	y += (y < 38) ? 2000 : 1900;
	return y;
}

