/* 
Date Extras Javascript Library 0.6.3
Copyright (c) 2005 Christian Romney <xmlblog@gmail.com / http://www.xml-blog.com>
Licensed under the terms of the MIT License:

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files 
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, 
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
*/

// Global Now() function
function Now() {return new Date();}

// Global Constants
var JANUARY   = 0;
var FEBRUARY  = 1;
var MARCH     = 2;
var APRIL     = 3;
var MAY       = 4;
var JUNE      = 5;
var JULY      = 6;
var AUGUST    = 7;
var SEPTEMBER = 8;
var OCTOBER   = 9;
var NOVEMBER  = 10;
var DECEMBER  = 11;

var SUNDAY    = 0;
var MONDAY    = 1;
var TUESDAY   = 2;
var WEDNESDAY = 3;
var THURSDAY  = 4;
var FRIDAY    = 5;
var SATURDAY  = 6;

// Conversion factors as varants to eliminate all the multiplication
var SECONDS_CF     = 1000;
var MINUTES_CF     = 60000;          // 60 * 1000
var HOURS_CF       = 3600000;        // 60 * 60 * 1000
var DAYS_CF        = 86400000;       // 24 * 60 * 60 * 1000
var WEEKS_CF       = 604800000;      // 7 * 24 * 60 * 60 * 1000
var FORTNIGHTS_CF  = 1209600000;     // 14 * 24 * 60 * 60 * 1000
var MONTHS_CF      = 2592000000;     // 30 * 24 * 60 * 60 * 1000  (approx = 1 month)
var QUARTERS_CF    = 7776000000;     // 90 * 24 * 60 * 60 * 1000  (approx = 3 months)
var YEARS_CF       = 31557600000;    // 365 * 24 * 60 * 60 * 1000 (approx = 1 year)
var DECADES_CF     = 315576000000;   // 10 * 365 * 24 * 60 * 60 * 1000 (approx = 1 decade)
var CENTURIES_CF   = 3155760000000;  // 100 * 365 * 24 * 60 * 60 * 1000 (approx = 1 century)

var PPKey = new Date('10/28/2002');

Date.prototype.PPStart = function() {

	if (this.getDay() == 0)
		var PP = this.addDays(-6).midnight();
	else if (this.getDay() == 1)
		var PP = this.midnight();
	else
		var PP = this.addDays(1-this.getDay()).midnight();
		
	if (new TimeSpan(PP - PPKey).weeks % 2 != 0) PP = PP.addDays(-7);
	
	return PP;
	
}

Date.prototype.PPEnd = function() {
	return this.PPStart().addDays(13);
}

// Non-destructive instance methods
Date.prototype.addMilliseconds = function(ms){  
  return new Date(new Date().setTime(this.getTime() + (ms)));  
}
Date.prototype.addSeconds = function(s){
  return this.addMilliseconds(s * SECONDS_CF);
}
Date.prototype.addMinutes = function(m){
  return this.addMilliseconds(m * MINUTES_CF);
}
Date.prototype.addHours = function(h){
  return this.addMilliseconds(h * HOURS_CF);
}
Date.prototype.addDays = function(d){
  return this.addMilliseconds(d * DAYS_CF);  
}
Date.prototype.addWeeks = function(w){
  return this.addMilliseconds(w * WEEKS_CF);  
}
Date.prototype.addFortnights = function(w){
  return this.addMilliseconds(w * FORTNIGHTS_CF);  
}
Date.prototype.addMonths = function(mm){
  return this.addMilliseconds(mm * MONTHS_CF);  
}
Date.prototype.addYears = function(yy){
  return this.addMilliseconds(yy * YEARS_CF);  
}
Date.prototype.subtractMilliseconds = function(ms){  
  return new Date(new Date().setTime(this.getTime() - (ms)));   
}
Date.prototype.subtractSeconds = function(s){
  return this.subtractMilliseconds(s * SECONDS_CF);
}
Date.prototype.subtractMinutes = function(m){
  return this.subtractMilliseconds(m * MINUTES_CF);
}
Date.prototype.subtractHours = function(h){
  return this.subtractMilliseconds(h * HOURS_CF);
}
Date.prototype.subtractDays = function(d){
  return this.subtractMilliseconds(d * DAYS_CF);  
}
Date.prototype.subtractWeeks = function(w){
  return this.subtractMilliseconds(w * WEEKS_CF);  
}
Date.prototype.subtractFortnights = function(w){
  return this.subtractMilliseconds(w * FORTNIGHTS_CF);  
}
Date.prototype.subtractMonths = function(mm){
  return this.subtractMilliseconds(mm * MONTHS_CF);  
}
Date.prototype.subtractYears = function(yy){
  return this.subtractMilliseconds(yy * YEARS_CF);  
}

// Destructive instance methods (change the value of the current Date object)
Date.prototype._addMilliseconds = function(ms){  
  this.setTime(this.getTime() + (ms));  
  return this;
}
Date.prototype._addSeconds = function(s){
  return this._addMilliseconds(s * SECONDS_CF);  
}
Date.prototype._addMinutes = function(m){
  return this._addMilliseconds(m * MINUTES_CF);
}
Date.prototype._addHours = function(h){
  return this._addMilliseconds(h * HOURS_CF);
}
Date.prototype._addDays = function(d){
  return this._addMilliseconds(d * DAYS_CF);  
}
Date.prototype._addWeeks = function(w){
  return this._addMilliseconds(w * WEEKS_CF);  
}
Date.prototype._addFortnights = function(w){
  return this._addMilliseconds(w * FORTNIGHTS_CF);  
}
Date.prototype._addMonths = function(mm){
  return this._addMilliseconds(mm * MONTHS_CF);  
}
Date.prototype._addYears = function(yy){
  return this._addMilliseconds(yy * YEARS_CF);  
}
Date.prototype._subtractMilliseconds = function(ms){  
  this.setTime(this.getTime() - (ms));  
  return this;
}
Date.prototype._subtractSeconds = function(s){
  return this._subtractMilliseconds(s * SECONDS_CF);
}
Date.prototype._subtractMinutes = function(m){
  return this._subtractMilliseconds(m * MINUTES_CF);
}
Date.prototype._subtractHours = function(h){
  return this._subtractMilliseconds(h * HOURS_CF);
}
Date.prototype._subtractDays = function(d){
  return this._subtractMilliseconds(d * DAYS_CF);  
}
Date.prototype._subtractWeeks = function(w){
  return this._subtractMilliseconds(w * WEEKS_CF);  
}
Date.prototype._subtractFortnights = function(w){
  return this._subtractMilliseconds(w * FORTNIGHTS_CF);  
}
Date.prototype._subtractMonths = function(mm){
  return this._subtractMilliseconds(mm * MONTHS_CF);  
}
Date.prototype._subtractYears = function(yy){
  return this._subtractMilliseconds(yy * YEARS_CF);  
}
Date.prototype.getMonthName = function() {
  var index = (0 == arguments.length) ? this.getMonth() : arguments[0];
  switch(index)
  {
    case JANUARY: 
      return "January";
    case FEBRUARY: 
      return "February";
    case MARCH: 
      return "March";
    case APRIL: 
      return "April";
    case MAY: 
      return "May";
    case JUNE: 
      return "June";
    case JULY: 
      return "July";
    case AUGUST: 
      return "August";
    case SEPTEMBER: 
      return "September";
    case OCTOBER: 
      return "October";
    case NOVEMBER: 
      return "November";
    case DECEMBER: 
      return "December";
    default:
      throw "Invalid month index: " + index.toString();
  }
}
Date.prototype.getMonthAbbreviation = function() {
  var index = (0 == arguments.length) ? this.getMonth() : arguments[0];
  switch(index)
  {
    case JANUARY: 
      return "Jan";
    case FEBRUARY: 
      return "Feb";
    case MARCH: 
      return "Mar";
    case APRIL: 
      return "Apr";
    case MAY: 
      return "May";
    case JUNE: 
      return "Jun";
    case JULY: 
      return "Jul";
    case AUGUST: 
      return "Aug";
    case SEPTEMBER: 
      return "Sep";
    case OCTOBER: 
      return "Oct";
    case NOVEMBER: 
      return "Nov";
    case DECEMBER: 
      return "Dec";
    default:
      throw "Invalid month index: " + index.toString();
  }
}
Date.prototype.getDayName = function() {
  var index = (0 == arguments.length) ? this.getDay() : arguments[0];
  switch(index)
  {
    case SUNDAY: 
      return "Sunday";
    case MONDAY: 
      return "Monday";
    case TUESDAY: 
      return "Tuesday";
    case WEDNESDAY: 
      return "Wednesday";
    case THURSDAY: 
      return "Thursday";
    case FRIDAY: 
      return "Friday";
    case SATURDAY: 
      return "Saturday";    
    default:
      throw "Invalid day index: " + index.toString();
  }
}
Date.prototype.getDayAbbreviation = function() {
  var index = (0 == arguments.length) ? this.getDay() : arguments[0];
  switch(index)
  {
    case SUNDAY: 
      return "Sun";
    case MONDAY: 
      return "Mon";
    case TUESDAY: 
      return "Tue";
    case WEDNESDAY: 
      return "Wed";
    case THURSDAY: 
      return "Thu";
    case FRIDAY: 
      return "Fri";
    case SATURDAY: 
      return "Sat";
    default:
      throw "Invalid day index: " + index.toString();
  }
}
Date.prototype.getCivilianHours = function() {
  return (this.getHours() < 12) ? this.getHours() : this.getHours() - 12;
}
Date.prototype.getMeridiem = function() {
  return (this.getHours() < 12) ? "AM" : "PM";
}
Date.prototype.to_s = Date.prototype.toString;


/* 
    Ultra-flexible date formatting

    %YYYY = 4 digit year         (2005)
    %YY   = 2 digit year         (05)
    %MMMM = Month name           (March)
    %MMM  = Month abbreviation   (March becomes Mar)
    %MM   = 2 digit month number (March becomes 03)
    %M    = 1 or 2 digit month   (March becomes 3)
    %DDDD = Day name             (Thursday)
    %DDD  = Day abbreviation     (Thu)
    %DD   = 2 digit day          (09) 
    %D    = 1 or 2 digit day     (9)
    %HH   = 2 digit 24 hour      (13)
    %H    = 1 or 2 digit 24 hour (9)
    %hh   = 2 digit 12 Hour      (01)
    %h    = 1 or 2 digit 12 Hour (01)
    %mm   = 2 digit minute       (02)
    %m    = 1 or 2 digit minute  (2)
    %ss   = 2 digit second       (59)
    %s    = 1 or 2 digit second  (1)
    %nnn  = milliseconds
    %p    = AM/PM indicator
*/
Date.prototype.format = function(fs) {

   fs = fs.replace(/%YYYY/, this.getFullYear().toString());
   fs = fs.replace(/%YY/, this.getFullYear().toString().substr(2,2));
   
   fs = fs.replace(/%MMMM/, this.getMonthName(this.getMonth()).toString());
   fs = fs.replace(/%MMM/, this.getMonthAbbreviation(this.getMonth()).toString());
   fs = fs.replace(/%MM/, (this.getMonth() + 1) > 9 ? (this.getMonth() + 1).toString() : "0" + (this.getMonth() + 1).toString());
   fs = fs.replace(/%M/, (this.getMonth() + 1).toString());
   
   fs = fs.replace(/%DDDD/, this.getDayName(this.getDay()).toString());
   fs = fs.replace(/%DDD/, this.getDayAbbreviation(this.getDay()).toString());
   fs = fs.replace(/%DD/, this.getDate() > 9 ? this.getDate().toString() : "0" + this.getDate().toString());
   fs = fs.replace(/%D/, this.getDate().toString());
   
   fs = fs.replace(/%HH/, this.getHours() > 9 ? this.getHours().toString() : "0" + this.getHours().toString());
   fs = fs.replace(/%H/, this.getHours().toString());
   fs = fs.replace(/%hh/, this.getCivilianHours() > 9 ? this.getCivilianHours().toString() : "0" + this.getCivilianHours().toString());
   fs = fs.replace(/%h/, this.getCivilianHours());

   fs = fs.replace(/%mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : "0" + this.getMinutes().toString());
   fs = fs.replace(/%m/, this.getMinutes().toString());

   fs = fs.replace(/%ss/, this.getSeconds() > 9 ? this.getSeconds().toString() : "0" + this.getSeconds().toString());
   fs = fs.replace(/%s/, this.getSeconds().toString());

   fs = fs.replace(/%nnn/, this.getMilliseconds().toString());
   fs = fs.replace(/%p/, this.getMeridiem());
   return fs;
}

Date.prototype.getDaysInMonth = function () {

  switch(this.getMonth())
  {
    case JANUARY:
      return 31;
    case FEBRUARY:
      return this.isLeapYear() ? 29 : 28;
    case MARCH:
      return 31;
    case APRIL:
      return 30;
    case MAY:
      return 31;
    case JUNE:
      return 30;
    case JULY:
      return 31;
    case AUGUST:
      return 31;
    case SEPTEMBER:
      return 30;
    case OCTOBER:
      return 31;
    case NOVEMBER:
      return 30;
    case DECEMBER:
      return 31;
  }
}

// Give toString more flexibility
Date.prototype.toString = function() {
  if (0 == arguments.length || 1 < arguments.length) return this.to_s();
  return this.format(arguments[0].toString());
} 

Date.prototype.isLeapYear = function() {
  if (0 == this.getFullYear() % 400) return true;
  if (0 == this.getFullYear() % 100) return false;
  return (0 == this.getFullYear() % 4) ? true : false;
}

Date.prototype.midnight = function() {
  var dt = this.clone();
  dt.setHours(0);
  dt.setMinutes(0);
  dt.setSeconds(0);
  dt.setMilliseconds(0);
  return dt;
}

Date.prototype.noon = function() {
  var dt = this.clone();
  dt.setHours(12);
  dt.setMinutes(0);
  dt.setSeconds(0);
  dt.setMilliseconds(0);
  return dt;
}

Date.prototype.firstDayOfMonth = function() {  
  return new Date(this.getFullYear(), this.getMonth(), 1, 12, 0, 0);
}
Date.prototype.lastDayOfMonth = function() {  
  return new Date(this.getFullYear(), this.getMonth(), this.getDaysInMonth(), 12, 0, 0);
}

Date.prototype.clone = function() {
  var dt = new Date();
  dt.setTime(this.getTime());  
  return dt;
}

Date.fromString = function(dateString) {
	d = new Date(dateString);
	if (8 == dateString.length) {
		n = Date.today();
		while (d.getFullYear() < (Math.round(n.getFullYear()/100)*100)-50) d.setFullYear(d.getFullYear()+100);
	}
	return d;
}

Date.fromXSDDateString = function(dateString) {

	var re = /^(-)?(\d{4})-(\d{2})-(\d{2})(T(\d{2}):(\d{2}):(\d{2})(\.\d+)?)?Z?(([\+-])((\d{2}):(\d{2})))?$/;
	return eval(dateString.replace(re,'new Date(new Date(\'$3/$4/$2 $6:$7:$8 GMT$11$13$14\').getTime() + (1000 * 0$9))'));

}

// Static methods
Date.yesterday = function()
{  
  return new Date().midnight().subtractDays(1);
}
Date.today = function()
{  
  return new Date().midnight();
}
Date.tomorrow = function()
{  
  return new Date().midnight().addDays(1);
}
Date.lastWeek = function()
{  
  return new Date().midnight().subtractWeeks(1);
}
Date.nextWeek = function()
{  
  return new Date().midnight().addWeeks(1);
}
Date.lastMonth = function()
{  
  return new Date().midnight().subtractMonths(1);
}
Date.nextFortnight = function()
{  
  return new Date().midnight().addFortnights(1);
}
Date.lastFortnight = function()
{  
  return new Date().midnight().subtractFortnights(1);
}
Date.nextMonth = function()
{   
  return new Date().midnight().addMonths(1);
}
Date.lastYear = function()
{  
  return new Date().midnight().subtractYears(1);
}
Date.nextYear = function()
{
  return new Date().midnight().addYears(1);
}

// Number class instance methods (Rails-inspired)
Number.prototype.millisecondsFromNow = function() {
  return new Date().addMilliseconds(this);  
}
Number.prototype.millisecondFromNow = function() {
  return this.millisecondsFromNow();  
}
Number.prototype.secondsFromNow = function() {
  return new Date().addSeconds(this);  
}
Number.prototype.secondFromNow = function() {
  return this.secondsFromNow();  
}
Number.prototype.minutesFromNow = function() {
  return new Date().addMinutes(this);  
}
Number.prototype.minuteFromNow = function() {
  return this.minutesFromNow();  
}
Number.prototype.daysFromNow = function() {
  return new Date().addDays(this);  
}
Number.prototype.dayFromNow = function() {
  return this.daysFromNow();  
}
Number.prototype.weeksFromNow = function() {
  return new Date().addWeeks(this);  
}
Number.prototype.weekFromNow = function() {
  return this.weeksFromNow();  
}
Number.prototype.fortnightsFromNow = function() {
  return new Date().addFortnights(this);  
}
Number.prototype.fortnightFromNow = function() {
  return this.fortnightsFromNow();  
}
Number.prototype.monthsFromNow = function() {
  return new Date().addMonths(this);  
}
Number.prototype.monthFromNow = function() {
  return this.monthsFromNow();  
}
Number.prototype.yearsFromNow = function() {
  return new Date().addYears(this);  
}
Number.prototype.yearFromNow = function() {
  return this.yearsFromNow();  
}

Number.prototype.millisecondsAgo = function() {
  return new Date().subtractMilliSeconds(this);  
}
Number.prototype.millisecondAgo = function() {
  return this.millisecondsAgo();  
}
Number.prototype.secondsAgo = function() {
  return new Date().subtractSeconds(this);  
}
Number.prototype.secondAgo = function() {
  return this.secondsAgo();  
}
Number.prototype.minutesAgo = function() {
  return new Date().subtractMinutes(this);  
}
Number.prototype.minuteAgo = function() {
  return this.minutesAgo();  
}
Number.prototype.hoursAgo = function() {
  return new Date().subtractHours(this);  
}
Number.prototype.hourAgo = function() {
  return this.hoursAgo();  
}
Number.prototype.daysAgo = function() {
  return new Date().subtractDays(this);  
}
Number.prototype.dayAgo = function() {
  return this.daysAgo();  
}
Number.prototype.weeksAgo = function() {
  return new Date().subtractWeeks(this);  
}
Number.prototype.weekAgo = function() {
  return this.weeksAgo();  
}
Number.prototype.fortnightsAgo = function() {
  return new Date().subtractFortnights(this);  
}
Number.prototype.fortnightAgo = function() {
  return this.fortnightsAgo();  
}
Number.prototype.monthsAgo = function() {
  return new Date().subtractMonths(this);  
}
Number.prototype.monthAgo = function() {
  return this.monthsAgo();  
}
Number.prototype.yearsAgo = function() {
  return new Date().subtractYears(this);  
}
Number.prototype.yearAgo = function() {
  return this.yearsAgo();  
}

// TimeSpan class exposes helper methods 
// for rounding milliseconds to other granularities of time
// Note: These measurements are subject to subtle rounding errors!

// var d1 = (1).dayFromNow();
// var d2 = (10).daysFromNow(); 
// var ts = new TimeSpan(d2 - d1);
// assert(9 == ts.days);
// var ts2 = new TimeSpan(d1 - d2); // order doesn't matter!
// assert(9 == ts2.days); 

function TimeSpan(ms) {
  this.milliseconds = Math.abs(ms);
  this.seconds      = Math.round(this.milliseconds / SECONDS_CF);
  this.minutes      = Math.round(this.milliseconds / MINUTES_CF);
  this.hours        = Math.round(this.milliseconds / HOURS_CF);
  this.days         = Math.round(this.milliseconds / DAYS_CF);
  this.weeks        = Math.round(this.milliseconds / WEEKS_CF);  
  this.fortnights   = Math.round(this.milliseconds / FORTNIGHTS_CF);  
  this.months       = Math.round(this.milliseconds / MONTHS_CF);      // rounding errors!
  this.quarters     = Math.round(this.milliseconds / QUARTERS_CF);    // rounding errors!
  this.years        = Math.round(this.milliseconds / YEARS_CF);       // rounding errors!
  this.decades      = Math.round(this.milliseconds / DECADES_CF);     // rounding errors!  
  this.centuries    = Math.round(this.milliseconds / CENTURIES_CF);   // rounding errors!
}

TimeSpan.prototype.toString = function() {
  return this.milliseconds.toString();
}