var hour, minute, seconds, milliseconds, weekday, day, month, yy, year, startTime, popup, field;

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

var weekdays = new makeArray('Sunday','Monday','Tuesday',
    'Wednesday','Thursday','Friday','Saturday');

date = new Date();
hour = date.getHours();
minute = date.getMinutes();
seconds = date.getSeconds();
milliseconds = date.getMilliseconds();
weekday  = date.getDay() + 1;
day  = date.getDate();
month = date.getMonth() + 1;
yy = date.getYear();
year = (yy < 1000) ? yy + 1900 : yy;
startTime = date.getTime();

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

function showGreeting() {
  var s
  if (hour < 12) s = "Good Morning";
  if (hour >= 12 & hour < 17) s = "Good Afternoon";
  if (hour >= 17) s = "Good Evening";
  document.write(s);
}

function showDay(intDayOfWeek, intMonth, intYear, intEarliest) {
// intEarliest is the earliest day on which the holiday can occur.
// for example, if November 1st is a Thursday, then Thanksgiving
// is on the 22nd (the 4th Thursday), which is the earliest it 
// can occur. It can occur as late as the 28th.
// intDayOFWeek is the numeric day of the week. eg Thanksgiving
// is on a Thursday which is the 5th day of the week.
	for (i = intEarliest; i <= intEarliest + 6; i++) {
		dtDate = new Date(intMonth + '/' + i + '/' + intYear)
		if (dtDate.getDay() + 1 == intDayOfWeek){
			document.write(weekdays[dtDate.getDay() + 1] + ", " + months[intMonth] + " " + i + ", " + intYear); 
		}
	}
}

function showTime(type) {
// showTime available types.....
//		"c" - civilian time eg. 4:30 PM
//		"m" - military time eg. 1400 hours
  var xhour = hour;
  var xminute = minute;
  if (xminute < 10) xminute = "0" + xminute;
  switch (type) {
    case "c":
      var ampm = "AM";
      if (hour >= 12) {
        ampm = "PM";
        if (hour != 12) xhour = hour - 12;
      }
      time = xhour + ":"+ xminute + " " + ampm;
      break;
    case "m":
      if (xhour < 10) xhour = "0" + xhour;
      time = xhour + "" + xminute + " hours ";
      break;
  }
  document.write(time);
}

// showDate available formats.....
//		"mm/dd/yyyy"
//		"dd/mm/yyyy"
//		"dddd, mm dd, yyyy"
//		"dddd, mm dd, yyyy hh:ss"

function showDate (format) {
  switch (format) {
    case "mm/dd/yyyy":      document.write(month + "/" + day + "/" + year);      break;
    case "dd/mm/yyyy":      document.write(day + "/" + month + "/" + year);      break;
    case "dddd, mm dd, yyyy":      document.write(weekdays[weekday] + ", " + months[month] + " " + day + ", " + year);      break;
    case "dddd, mm dd, yyyy hh:ss":      document.write(weekdays[weekday] + ", " + months[month] + " " + day + ", " + year  + " " + time);      break;
    default:      document.write(month + "/" + day + "/" + year);      break;
  }

}

function showCalendar() {
var tabl = "";
  date = new Date();
  date.setDate(1);
  var startDay  = date.getDay();
  var daysinmonth = new makeArray(31,28,31,30,31,30,31,31,30,31,30,31);
  if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) daysinmonth[2] = 29;
  var nDays = daysinmonth[month];
  tabl = tabl + ("<TABLE BORDER=\"1\">");
  tabl = tabl + ("<TR><TH COLSPAN=\"7\">");
  tabl = tabl + (months[month] + ",&nbsp;&nbsp;&nbsp;" + year);
  tabl = tabl + ("</TH></TR><TR><TH>Sun</TH><TH>Mon</TH><TH>Tue</TH><TH>Wed</TH><TH>Thu</TH><TH>Fri</TH><TH>Sat</TH></TR>");
  tabl = tabl + ("<TR>");
  column = 0;
  for (i=0; i<startDay; i++) {
    tabl = tabl + ("<TD>&nbsp;</TD>");
    column++;
  }
  for (i=1; i<=nDays; i++) {
    tabl = tabl + ("<TD>");
    if (i == day) {
      tabl = tabl + ("<FONT COLOR=\"#FF0000\">");
      tabl = tabl + (i);
      tabl = tabl + ("</FONT>"); }
    else {
      tabl = tabl + (i);
    }
      column++;
    tabl = tabl + ("</TD>");
    if (column == 7) {
      tabl = tabl + ("</TR><TR>"); 
      column = 0;
    }
  }
  for (i=0; i<7-column; i++) {
    tabl = tabl + ("<TD>&nbsp;</TD>");
  }
  tabl = tabl + ("</TR></TABLE>");
  document.write(tabl);
}

function showTimer() {
  var present, presenttime, elapsed, melapsed , selapsed , timerID
  present = new Date();
  presenttime = present.getTime();
  elapsed = (presenttime - startTime) / 1000;
  melapsed  = Math.floor(elapsed / 60);
  selapsed  = Math.floor(elapsed -(melapsed * 60));
  status=(melapsed  + " minutes " + selapsed  + " seconds");
  timerID = setTimeout("showTimer()",1000); 
}

function showDays(when) {
  var present, presenttime, future, futuretime
  present = new Date();
  presenttime = present.getTime();
  future = new Date(when);
  futuretime = future.getTime();
  duration = Math.floor((futuretime - presenttime) / 86400000.);
  if (duration < 0) {
    future.setYear(year + 1);
    futuretime = future.getTime();
    duration = Math.floor((futuretime - presenttime) / 86400000.);
  }
  document.write(duration)
}

function selectDate(day) {
var day
field.value=(day);
}

function showMonth(offset, e) {
if (e != '') field = e;
date.setMonth(date.getMonth() + offset);
popup = window.open("","Select","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=165,height=185");
popup.document.open();
popupCalendar(date);
popup.document.close();
}

function popupCalendar(when) {
  date = new Date(when);
  month=date.getMonth()+1;
  date.setDate(1);
  yy = date.getYear();
  year = (yy < 1000) ? yy + 1900 : yy;
  var startDay  = date.getDay();
  var daysinmonth = new makeArray(31,28,31,30,31,30,31,31,30,31,30,31);
  if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) daysinmonth[2] = 29;
  var nDays = daysinmonth[month];
  popup.document.write("<TABLE BORDER=1>");
  popup.document.write("<TR><TH  style=\"font-family: Verdana; font-size: 8pt\" COLSPAN=7>");
  popup.document.write("<a style=\"text-decoration: none;\" href=\"javascript:opener.showMonth(-1, '');\"><</a>" + months[month] + ",&nbsp;" + year + "<a style=\"text-decoration: none;\" href=\"javascript:opener.showMonth(1, '');\">></a>");
  popup.document.write("<TR><TH style=\"font-family: Verdana; font-size: 8pt\">S<TH style=\"font-family: Verdana; font-size: 8pt\">M<TH style=\"font-family: Verdana; font-size: 8pt\">T<TH style=\"font-family: Verdana; font-size: 8pt\">W<TH style=\"font-family: Verdana; font-size: 8pt\">T<TH style=\"font-family: Verdana; font-size: 8pt\">F<TH style=\"font-family: Verdana; font-size: 8pt\">S");
  popup.document.write("<TR>");
  column = 0;
  for (i=0; i<startDay; i++) {
    popup.document.write("<TD style=\"font-family: Verdana; font-size: 8pt\">&nbsp;");
    column++;
  }
  for (i=1; i<=nDays; i++) {
    popup.document.write("<TD style=\"font-family: Verdana; font-size: 8pt\">");
      popup.document.write("<a href=\"javascript:opener.selectDate('" + months[month] + " " + i + ", " + year + "');this.close();\">" + i + "</a>");
      column++;
    if (column == 7) {
      popup.document.write("<TR>"); 
      column = 0;
    }
  }
  for (i=0; i<7-column; i++) {
     popup.document.write("<TD style=\"font-family: Verdana; font-size: 8pt\">&nbsp;");
  }
  popup.document.write("</TABLE>");
}

