//
// Date library.  Supplements Date() object
//
// getCalendarMonth(int month) - Returns month string.
// getCalendarDay(int day)     - Returns day string
// getTimeZone()               - Returns time zone string
// isLeapYear(int year)        - Returns boolean Leap Year indication
// daysInMonth(int month)      - Returns number of days in month

// Returns the full current year.  Input year is checked and processed according to
// the following algorithm.
//
Date.prototype.getCalendarMonth = getCalendarMonth
Date.prototype.getCalendarDay = getCalendarDay
Date.prototype.getTimeZone = getTimeZone
Date.prototype.isLeapYear = isLeapYear
Date.prototype.daysInMonth = daysInMonth

// Given the numeric month, return the string version.
function getCalendarMonth(month) {
  var moy = new Array(12);
  var today = new Date();
  
  // If caller did not supply the month, use the current month.
  if (month == null ) month = today.getMonth();
  moy[0]  = "January";
  moy[1]  = "February";
  moy[2]  = "March";
  moy[3]  = "April";
  moy[4]  = "May";
  moy[5]  = "June";
  moy[6]  = "July";
  moy[7]  = "August";
  moy[8]  = "September";
  moy[9]  = "October";
  moy[10] = "November";
  moy[11] = "December";
  return moy[month];
} // end getCalendarMonth

// Given the numeric day, return the string version
function getCalendarDay(day) {
  var today = new Date();
  var dow = new Array(7);
  
  // If caller did not supply the day, use the current day.
  if (day == null) day = today.getDay();
  dow[0] = "Sunday";
  dow[1] = "Monday";
  dow[2] = "Tuesday";
  dow[3] = "Wednesday";
  dow[4] = "Thursday";
  dow[5] = "Friday";
  dow[6] = "Saturday";
  return dow[day]
} // end GetCalendarDay

// Returns current timezone string (U.S. only for now)
function getTimeZone() {
  var today = new Date();
  var offset = today.getTimezoneOffset()/60;
  if(offset == 5) return "Eastern Time Zone";
    else
      if(offset == 6) return "Central Time Zone";
        else
          if(offset == 7) return "Mountain Time Zone";
            else
              if(offset == 8) return "Pacific Time Zone";
                else
                  return "Unknown time zone for offset = " + offset;
  // I think there is a bug in the getTimezoneOffset method of the Date object.
  // The real offset for this zone is -6, yet getTimezoneOffset returns 6.
  // Program to accept positive numbers, but look into.
} // end getTimeZone

// Leap year indicator.  Returns true if leap year.  Input year is FULL YEAR.
function isLeapYear(year) {
  var today = new Date();
  if(year == null) {
    year = getFullYear(today.getYear());
  }
  // The leap year rules are:
  //   1. Every year evenly divisible by 4 is a leap year.
  //   2. However, every year evenly divisible by 100 is not a leap year.
  //   3. However, every year divisible by 400 is a leap year after all.
  // Algorithm tested correctly with years 1895..2005 inclusive.
  if((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0))) {
    return true;
  } else {
    return false;
  }
} // end isLeapYear

// Given month and year, returns number of days in month.  Input year is years since 1900.
function daysInMonth(month,year) {
  var dim = new Array();
  var today = new Date();
  // If caller didn't supply month, use current month
  if(month == null) {
    month = today.getMonth();
  }
  // If caller didn't supply year, use current year.  We need the year for the Leap Year check.
  if(year == null) {
    year = getFullYear(today.getYear());
  }
  dim[0] =  31;
  // Make adjustment for leap year, if necessary
  if(isLeapYear(year)) {
    dim[1] = 29;
  } else {
    dim[1] = 28;
  }
  dim[2] =  31;
  dim[3] =  30;
  dim[4] =  31;
  dim[5] =  30;
  dim[6] =  31;
  dim[7] =  31;
  dim[8] =  30;
  dim[9] =  31;
  dim[10] = 30;
  dim[11] = 31;
  return dim[month];
} // end daysInMonth

// Create and display a date format like Thursday, March 15th, 2000
function displayDateString() {
  today = new Date();
  day = today.getDate();
  year = today.getFullYear();

  if (day==1 || day==21 || day==31) {
    end="st";
  } else {
    if (day==2 || day==22) {
      end="nd";
    } else {
      if (day==3 || day==23) {
        end="rd";
      } else {
        end = "th";
      }
    }
  }
  day+=end;
  document.write("<center>");
  document.write(getCalendarDay()+", "+getCalendarMonth()+" "+day+", "+year);
  document.write("</center>");
}