/*********************************************************
 *	File:		  date_util.js
 *	Author:		MetaDesign
 *	Created:	March 7, 2007
 *
 *	Description:
 *	Enhances date capabilities in javascript.
 *  Used by calendar controls.
 ********************************************************/

/*--------------------------------------------------------------
 * Static Method: isLeapYear
 *
 * Determines if a year is a leap year.
 *
 * Parameters:
 * pYear    integer   4 digit year
 *
 * Return:
 * boolean    true if pYear is a leap year
 *------------------------------------------------------------*/
Date.isLeapYear = function(pYear) {
  if (0 == pYear % 400) return true;
  if (0 == pYear % 100) return false;
  return (0 == pYear % 4) ? true : false;
}

/*--------------------------------------------------------------
 * Method: isLeapYear
 *
 * Determines if a date is in a leap year.
 *
 * Parameters:
 * None
 *
 * Return:
 * boolean    true if the date is in a leap year
 *------------------------------------------------------------*/
Date.prototype.isLeapYear = function() {
  return Date.isLeapYear(this.getFullYear());
}

/*--------------------------------------------------------------
 * Static Method: getDaysInMonth
 *
 * Returns the number of days in a given month/year.
 *
 * Parameters:
 * pMonth   integer   month index (0 to 11)
 * pYear    integer   4 digit year
 *
 * Return:
 * integer    The number of days in pMonth/pYear
 *------------------------------------------------------------*/
Date.getDaysInMonth = function(pMonth, pYear) {
  var monthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  // Handle all months except February
  if (pMonth != 1) return monthDays[pMonth];
  // Handle February
  return (Date.isLeapYear(pYear)) ? monthDays[pMonth] + 1 : monthDays[pMonth];
}

/*--------------------------------------------------------------
 * Method: getDaysInMonth
 *
 * Returns the number of days in the month of the given date.
 *
 * Parameters:
 * None
 *
 * Return:
 * integer    The number of days in the month of the given date
 *------------------------------------------------------------*/
Date.prototype.getDaysInMonth = function() {
  return Date.getDaysInMonth(this.getMonth(), this.getFullYear());
}

/*--------------------------------------------------------------
 * Method: getPrevMonth
 *
 * Returns a date representing the first day of the previous month.
 *
 * Parameters:
 * None
 *
 * Return:
 * date     The first day of the previous month
 *------------------------------------------------------------*/
Date.prototype.getPrevMonth = function() {
  var currMonth = this.getMonth();
  var currYear = this.getFullYear();
  return (currMonth == 0) ? new Date(currYear - 1, 11, 1) : new Date(currYear, currMonth - 1, 1);
}

/*--------------------------------------------------------------
 * Method: getNextMonth
 *
 * Returns a date representing the first day of the next month.
 *
 * Parameters:
 * None
 *
 * Return:
 * date     The first day of the next month
 *------------------------------------------------------------*/
Date.prototype.getNextMonth = function() {
  var currMonth = this.getMonth();
  var currYear = this.getFullYear();
  return (currMonth == 11) ? new Date(currYear + 1, 0, 1) : new Date(currYear, currMonth + 1, 1);
}


/*--------------------------------------------------------------
 * Method: getPrevDate
 *
 * Returns a date representing the day before.
 *
 * Parameters:
 * None
 *
 * Return:
 * date     The day before the given date
 *------------------------------------------------------------*/
Date.prototype.getPrevDate = function() {
  var currYear = this.getFullYear();
  var currMonth = this.getMonth();
  var currDate = this.getDate();

  if (currDate == 1) {
    if (currMonth == 0) {
      // For Jan 1
      return new Date(currYear - 1, 11, 31);
    } else {
      // For first of the month not including Jan
      return new Date(currYear, currMonth - 1, Date.getDaysInMonth(currMonth - 1, currYear));
    }
  } else {
    return new Date(currYear, currMonth, currDate - 1);
  }
}

/*--------------------------------------------------------------
 * Method: getNextDate
 *
 * Returns a date representing the day after.
 *
 * Parameters:
 * None
 *
 * Return:
 * date     The day after the given date
 *------------------------------------------------------------*/
Date.prototype.getNextDate = function() {
  var currYear = this.getFullYear();
  var currMonth = this.getMonth();
  var currDate = this.getDate();
  
  if (currDate == this.getDaysInMonth()) {
    if (currMonth == 11) {
      // For Dec 31
      return new Date(currYear + 1, 0, 1);
    } else {
      // For last day of the month not including Dec
      return new Date(currYear, currMonth + 1, 1);
    }
  } else {
    return new Date(currYear, currMonth, currDate + 1);
  }
}


/*--------------------------------------------------------------
 * Static Method: isSameMonthYear
 *
 * Determines if two dates are in the same month and year.
 *
 * Parameters:
 * pDateA   date    The first date to compare
 * pDateB   date    The second date to compare
 *
 * Return:
 * boolean    True if both dates are in the same month and year
 *------------------------------------------------------------*/
Date.isSameMonthYear = function(pDateA, pDateB) {
  return (pDateA.getFullYear() == pDateB.getFullYear()) && (pDateA.getMonth() == pDateB.getMonth());
}

/*--------------------------------------------------------------
 * Static Method: getDayName
 *
 * Returns the name of the day ("Sunday", "Monday", etc.).
 *
 * Parameters:
 * pDay   integer   The day index (0 to 7)
 *
 * Return:
 * string   The full name of the day
 *------------------------------------------------------------*/
Date.getDayName = function(pDay) {
  var dayNames = Date.getDayNames();
  return dayNames[pDay];
}

/*--------------------------------------------------------------
 * Method: getDayName
 *
 * Returns the name of the given date ("Sunday", "Monday", etc.).
 *
 * Parameters:
 * None
 *
 * Return:
 * string   The full name of the given date
 *------------------------------------------------------------*/
Date.prototype.getDayName = function() {
  return Date.getDayName(this.getDay());
}

/*--------------------------------------------------------------
 * Static Method: getDayAbbreviation
 *
 * Returns the abbreviated name of the day ("S", "M", etc.).
 *
 * Parameters:
 * pDay   integer   The day index (0 to 7)
 *
 * Return:
 * string   The single character abbreviated name of the day
 *------------------------------------------------------------*/
Date.getDayAbbreviation = function(pDay) {
  var dayAbbrevs = Date.getDayAbbreviations();
  return dayAbbrevs[pDay];
}

/*--------------------------------------------------------------
 * Method: getDayAbbreviation
 *
 * Returns the abbreviated name of the given date ("S", "M", etc.).
 *
 * Parameters:
 * None
 *
 * Return:
 * string   The single character abbreviated name of the given
 *          date
 *------------------------------------------------------------*/
Date.prototype.getDayAbbreviation = function() {
  return Date.getDayAbbreviation(this.getDay());
}

/*--------------------------------------------------------------
 * Static Method: getMonthName
 *
 * Returns the name of the month ("January", "February", etc.).
 *
 * Parameters:
 * pMonth   integer   The month index (0 to 11)
 *
 * Return:
 * string   The full name of the month
 *------------------------------------------------------------*/
Date.getMonthName = function(pMonth) {
  var monthNames = Date.getMonthNames();
  return monthNames[pMonth];
}

/*--------------------------------------------------------------
 * Method: getMonthName
 *
 * Returns the name of the month for the given date
 * ("January", "February", etc.).
 *
 * Parameters:
 * None
 *
 * Return:
 * string   The full name of the month for the given date
 *------------------------------------------------------------*/
Date.prototype.getMonthName = function() {
  return Date.getMonthName(this.getMonth());
}

/*--------------------------------------------------------------
 * Static Method: getMonthAbbreviation
 *
 * Returns the abbreviated name of the month ("Jan", "Feb", etc.).
 *
 * Parameters:
 * pMonth   integer   The month index (0 to 11)
 *
 * Return:
 * string   The three character abbreviated name of the month
 *------------------------------------------------------------*/
Date.getMonthAbbreviation = function(pMonth) {
  var monthAbbrevs = Date.getMonthAbbreviations();
  return monthAbbrevs[pMonth];
}

/*--------------------------------------------------------------
 * Method: getMonthAbbreviation
 *
 * Returns the abbreviated name of the month for the given date
 * ("Jan", "Feb", etc.).
 *
 * Parameters:
 * None
 *
 * Return:
 * string   The three character abbreviated name of the month
 *          for the given date
 *------------------------------------------------------------*/
Date.prototype.getMonthAbbreviation = function() {
  return Date.getMonthAbbreviation(this.getMonth());
}

// TODO:  Enhancement - Separate string arrays into separate constants file to be localized for japanese and chinese.

/*--------------------------------------------------------------
 * Static Method: getDayNames
 *
 * Returns an array of day names.
 *
 * Parameters:
 * None
 *
 * Return:
 * array    Array of day names in index order
 *------------------------------------------------------------*/
Date.getDayNames = function() {
  return ["日曜日", "月曜日", "火曜日", "水曜日", "木曜日", "金曜日", "土曜日"];
}

/*--------------------------------------------------------------
 * Static Method: getDayAbbreviations
 *
 * Returns an array of single character day name abbreviations.
 *
 * Parameters:
 * None
 *
 * Return:
 * array    Array of single character day name abbreviations in
 *          index order
 *------------------------------------------------------------*/
Date.getDayAbbreviations = function() {
  return ["日", "月", "火", "水", "木", "金", "土"];
}

/*--------------------------------------------------------------
 * Static Method: getMonthNames
 *
 * Returns an array of month names.
 *
 * Parameters:
 * None
 *
 * Return:
 * array    Array of month names in index order
 *------------------------------------------------------------*/
Date.getMonthNames = function() {
  return ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"];
}

/*--------------------------------------------------------------
 * Static Method: getMonthAbbreviations
 *
 * Returns an array of three character month name abbreviations.
 *
 * Parameters:
 * None
 *
 * Return:
 * array    Array of three character month name abbreviations in
 *          index order
 *------------------------------------------------------------*/
Date.getMonthAbbreviations = function() {
  return ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"];
}