// Javascript function that work with date and time
// .
// Writen by Mike Cummings
// .
// $Id: time_functions.js 17 2004-05-21 23:18:53Z dragon $
// To use these functions in your webpages you must include the following in
// your page <head> section.
// For example:
// <html>
// <head>
// ...
// <script language="JavaScript" type="text/javascript" src="time_functions.js">
// </script>
// </head>
// ...
// </html>

var DayName = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");				      //Day Names array
var MonthName = new Array("January","Febuary","March","April","May","June","July","August","September","October","November","December"); //Month Names array

// This function returns the local time of the viewer.
// The output is used to show the current local time on webpage.
function ShowLocalTime(secs) {
  var theDate  = new Date();
  var hour    = theDate.getHours();		      //hour
  var ampm    = "" + ((hour > 11) ? "pm" :"am");      //set am/pm
  hour == 0 ? hour = 12 : hour;			      //change 0 to midnight
  hour > 12 ? hour -=12 : hour;			      //24 to 12 hour
  var minute  = theDate.getMinutes();		      //minutes
  var second  = theDate.getSeconds();		      //seconds
  var temp = "" + ((hour < 10) ? " " : "") + hour;    //build return hours
  temp += ((minute < 10) ? ":0" : ":") + minute;      //minutes
  if (secs) {					      //only if not static
    temp += ((second < 10) ? ":0" : ":") + second;      //seconds
  }
  temp += " " + ampm;				      //am or pm
  return temp;					      //Return time and date
}

// This function returns the UTC time of the viewer.
// The output is used to show the current UTC time on webpage.
function ShowZuluTime(secs) {
  var theDate  = new Date();
  var hour    = theDate.getUTCHours();		      //hour
  var ampm    = "" + ((hour > 11) ? "pm" :"am");      //set am/pm
  hour == 0 ? hour = 12 : hour;			      //change 0 to midnight
  hour > 12 ? hour -=12 : hour;			      //24 to 12 hour
  var minute  = theDate.getUTCMinutes();	      //minutes
  var second  = theDate.getUTCSeconds();	      //seconds
  var temp = "" + ((hour < 10) ? " " : "") + hour;    //build return hours
  temp += ((minute < 10) ? ":0" : ":") + minute;      //minutes
  if (secs) {					      //only if true
    temp += ((second < 10) ? ":0" : ":") + second;      //seconds
  }
  temp += " " + ampm;				      //am or pm
  return temp;					      //Return time and date
}

// This function returns the time of the irlp node.
// The output is used to show the current node time on webpage.
function ShowNodeTime(secs) {
  var nodetz   = -7;				      // Node tz offset
  var ds       = 0;				      // daylight savings time
  var theDate  = new Date();
  var day      = theDate.getUTCDate();
  var dayOfWeek= theDate.getUTCDay();		      //which day of the week
  var month    = theDate.getUTCMonth();
  var hour     = theDate.getUTCHours() + nodetz;      //hour
  if ((month > 3) && (month < 9)) { ds = 1;} //US daylight saving time rules
  if ((month == 3) && (((day - 24) > dayOfWeek) || (((day - 24) == dayOfWeek) && (hour > 1)))) { ds = 1; }
  if ((month == 9) && (((day - 25) < dayOfWeek) || (((day - 25) == dayOfWeek) && (hour < 2)))) { ds = 1; }
  hour = hour + ds;
  if (hour > 23) {
    day += 1;
    dayOfWeek += 1;
    hour -= 24;
  }
  if (hour < 0) {
    day -= 1;
    dayOfWeek -= 1;
    hour += 24;
  }
  var ampm    = "" + ((hour > 11) ? "pm" :"am");      //set am/pm
  hour == 0 ? hour = 12 : hour;			      //change 0 to midnight
  hour > 12 ? hour -=12 : hour;			      //24 to 12 hour
  var minute  = theDate.getUTCMinutes();	      //minutes
  var second  = theDate.getUTCSeconds();	      //seconds
  var temp = ((hour < 10) ? " " : "") + hour;	      //build return hours
  temp += ((minute < 10) ? ":0" : ":") + minute;      //minutes
  if (secs) {					      //only if ask
    temp += ((second < 10) ? ":0" : ":") + second;    //seconds
  }
  temp += " " + ampm;				      //am or pm
  return temp;					      //Return time and date
}

// This function decides which time to display in the mouse following
// time area.
function ShowFMTime(secs) {
  switch (followMe) {
    case 'lTime': temp = ShowLocalTime(secs);
      break;
    case 'nTime': temp = ShowNodeTime(secs);
      break;
    case 'zTime': temp = ShowZuluTime(secs);
      break;
    default: temp = ' ';
  }
  return temp;
}

// This function returns the time and date in local time.
// The output is used to show the current time and date on webpage.
function ShowLocalTD(secs) {
  var theDate  = new Date();
  var day   = theDate.getDate();
  var dSuf    =	"th";				      //day suffix
  if (day == 1 || day == 21 || day == 31) dSuf="st";  //change suffix for 1s
  if (day == 2 || day == 22) dSuf = "nd";	      //change suffix for 2s
  if (day == 3 || day == 23) dSuf = "rd";	      //change suffix for 3s
  var hour    = theDate.getHours();		      //hour
  var ampm    = "" + ((hour >= 12) ? "pm" :"am");     //set am/pm
  hour == 0 ? hour = 12 : hour;			      //change 0 to midnight
  hour > 12 ? hour -=12 : hour;			      //24 to 12 hour
  var minute  = theDate.getMinutes();		      //minutes
  var second  = theDate.getSeconds();		      //seconds
  var temp = ((hour < 10) ? " " : "") + hour;	      //build return hours
  temp += ((minute < 10) ? ":0" : ":") + minute;      //minutes
  if (secs) {					      //only if ask
    temp += ((second < 10) ? ":0" : ":") + second;    //seconds
  }
  temp += " " + ampm;				      //am or pm
  temp += " - " + DayName[theDate.getDay()];	      //name of the day
  temp += ", " + MonthName[theDate.getMonth()];	      //name of the month
  temp += " " + day + "<sup>" + dSuf + "</sup>";      //day with suffix
  temp += ", " + theDate.getFullYear();		      //and finally the year
  return temp;					      //Return time and date
}
  
// This function returns the time and date in UTC.
// The output is used to show the current UTC (zulu) time and date on webpage.
//
function ShowZuluTD(secs) {
  var theDate  = new Date();
  var day   = theDate.getUTCDate();
  var dSuf    =	"th";				      //day suffix
  if (day == 1 || day == 21 || day == 31) dSuf="st";  //change suffix for 1s
  if (day == 2 || day == 22) dSuf = "nd";	      //change suffix for 2s
  if (day == 3 || day == 23) dSuf = "rd";	      //change suffix for 3s
  var hour    = theDate.getUTCHours();		      //hour
  var ampm    = "" + ((hour >= 12) ? "pm" :"am");     //set am/pm
  hour == 0 ? hour = 12 : hour;			      //change 0 to midnight
  hour > 12 ? hour -=12 : hour;			      //24 to 12 hour
  var minute  = theDate.getUTCMinutes();	      //minutes
  var second  = theDate.getSeconds();		      //seconds
  var temp = ((hour < 10) ? " " : "") + hour;	      //build return hours
  temp += ((minute < 10) ? ":0" : ":") + minute;      //minutes
  if (secs) {					      //only if ask
    temp += ((second < 10) ? ":0" : ":") + second;    //seconds
  }
  temp += " " + ampm;				      //am or pm
  temp += " - " + DayName[theDate.getUTCDay()];	      //name of the day
  temp += ", " + MonthName[theDate.getUTCMonth()];    //name of the month
  temp += " " + day + "<sup>" + dSuf + "</sup>";      //day with suffix
  temp += ", " + theDate.getUTCFullYear();	      //and finally the year
  return temp;					      //Return time and date
}
  
// This function returns the time and date the webpage was last modified.
// The output is used to show when the page was last updated.
// For example: 
//  <h6>This page was last updated
//    <script language="javascript" type="text/javascript">
//	document.write(ShowUpdateTD());
//    </script>
//    by Mike Cummings
//  </h6>
// ...
function ShowUpdateTD(secs) {
  var theDate = new Date(Date.parse(document.lastModified)); //last modified date
  var day     =	theDate.getDate();		      //day of the month
  var dSuf    =	"th";				      //day suffix
  if (day == 1 || day == 21 || day == 31) dSuf="st";  //change suffix for 1s
  if (day == 2 || day == 22) dSuf = "nd";	      //change suffix for 2s
  if (day == 3 || day == 23) dSuf = "rd";	      //change suffix for 3s
  var hour    = theDate.getHours();		      //hour
  var ampm    = "" + ((hour >= 12) ? "pm" :"am");     //set am/pm
  hour == 0 ? hour = 12 : hour;			      //change 0 to midnight
  hour > 12 ? hour -=12 : hour;			      //24 to 12 hour
  var minute  = theDate.getMinutes();		      //minutes
  var second  = theDate.getSeconds();		      //seconds
  var temp = ((hour < 10) ? " " : "") + hour;	      //build return hours
  if (secs) {					      //only if ask
    temp += ((second < 10) ? ":0" : ":") + second;    //seconds
  }
  temp += ((minute < 10) ? ":0" : ":") + minute;      //minutes
  temp += " " + ampm;				      //am or pm
  temp += " - " + DayName[theDate.getDay()];	      //name of the day
  temp += ", " + MonthName[theDate.getMonth()];	      //name of the month
  temp += " " + day + "<sup>" + dSuf + "</sup>";      //day with suffix
  temp += ", " + theDate.getFullYear();		      //and finally the year
  return temp;					      //Return time and date
}


