/* ***********************************************************
** TIMEDATE.JS - JS Time/Date-Processing Library
** =============================================
** This library contains functions to process time and date
** values. It's yours for free; please maintain this header!
**
** To load this library in an HTML doc, put the following
** line in the doc's HEAD (before any other SCRIPT tags):
**
** <SCRIPT SRC="timedate.js" LANGUAGE="JavaScript"></SCRIPT>
**
** Author      Ver  Date    Comments
** ======      ===  ====    ========
** Rick Scott  1.0  1/1/00  First release
**
** Copyright 2000, Rick Scott, all rights reserved.
*********************************************************** */

var MSECSINSECOND = 1000;
var MSECSINMINUTE = 1000 * 60;
var MSECSINHOUR   = 1000 * 60 * 60;
var MSECSINDAY    = 1000 * 60 * 60 * 24;
var MSECSINWEEK   = 1000 * 60 * 60 * 24 * (365.25/52);
var MSECSINMONTH  = 1000 * 60 * 60 * 24 * (365.25/12);
var MSECSINYEAR   = 1000 * 60 * 60 * 24 * 365.25;
var SECONDS       = "seconds";
var MINUTES       = "minutes"; 
var HOURS         = "hours";
var DAYS          = "days";
var WEEKS         = "weeks";
var MONTHS        = "months";
var YEARS         = "years";

var MonthNames = new Array("January", "February", "March", "April", "May",
    "June", "July", "August", "September", "October", "November", "December");
var DayNames = new Array("Sunday", "Monday", "Tuesday", "Wednesday", 
    "Thursday", "Friday", "Saturday");
function TimeDate(dateStr)
  {
  if (arguments.length > 1)
    return;
  if (arguments.length == 0)
    this.date = new Date();
  else
    this.date = new Date(dateStr);
  this.year = get_year(this.date);
  this.monthNum = this.date.getMonth() + 1;
  this.monthName = MonthNames[this.monthNum-1]; 
  this.dayNum = this.date.getDate();  
  // day-num suffix (st, nd, rd, etc.) - to create strings: 1st, 2nd, 3rd, etc.
  this.dayNumSuffix = get_dayNumSuffix(this.dayNum);
  this.zp_dayNum = zeropad(this.dayNum);
  this.dayName = DayNames[this.date.getDay()];
  this.hours = this.date.getHours();
  this.zp_hours = zeropad(this.hours);
  this.ampmhours = get_ampmHours(this.hours);
  this.ampm = get_ampm(this.hours);
  this.minutes = this.date.getMinutes();
  this.zp_minutes = zeropad(this.minutes); 
  this.seconds = this.date.getSeconds();
  this.zp_seconds = zeropad(this.seconds);
  this.msecs = this.date.getTime();
  }

function get_duration(startDate, endDate, timeUnits)
  {
  var divisor;

  duration = endDate.msecs - startDate.msecs;
  if (timeUnits == YEARS)
    divisor = MSECSINYEAR;
  else if (timeUnits == MONTHS)
    divisor = MSECSINMONTH;
  else if (timeUnits == WEEKS)
    divisor = MSECSINWEEK;
  else if (timeUnits == DAYS)
    divisor = MSECSINDAY;
  else if (timeUnits == HOURS)
    divisor = MSECSINHOUR;
  else if (timeUnits == MINUTES)
    divisor = MSECSINMINUTE;
  else if (timeUnits == SECONDS)
    divisor = MSECSINSECOND;

  var returnVal = duration/divisor;
  if (returnVal >= 0)
    return Math.floor(duration/divisor);
  else
    return Math.ceil(duration/divisor);
  }

function get_year(date)
  {
  var year = date.getYear();
  if (year < 1000)
    year += 1900;
  return year;
  }

function get_ampmHours(hournum)
  {
  if (hournum == 0)   // 12 am
    return 12;
  if (hournum > 12)   // 1-12 (pm)
    return (hournum - 12);
  else                // 1-11 (am)
    return hournum;
  }

function get_ampm(hournum)
  {
  if (hournum < 12)
    return "am";
  else
    return "pm";
  }

function zeropad(num)
  {
  if (num < 10)
    return ("0" + num);
  else
    return num;
  }

function get_dayNumSuffix(num)
  {
  if (num == 1 || num == 21 || num == 31)
    return "st";
  else if (num == 2 || num == 22)
    return "nd";
  else if (num == 3 || num == 23)
    return "rd";
  else
    return "th";
  }