//
// This JavaScript fragment inserts text telling how many days until/since
// some target date.
// Fragment originally written by Bill Gober on 2004-02-21.
// Rewritten 2005-02-14 so that the text isn't confusing on browsers without JavaScript.
// Made into a separate file as a callable function on 2006-02-15.
//

var quarter_frac_chars = new Array( "", "&frac14;", "&frac12;", "&frac34;" )

/////////////////////////////////////////////////////////////////////////
// This function returns the number of whole days for today 
// until/since a target date.  Future dates are positive.
/////////////////////////////////////////////////////////////////////////

function calc_days_till_or_since( year,
                                  month, // January == 1, etc.
                                  day_of_month) { 

   var this_date, target_date, diff_msec, diff_days

   this_date = new Date()    //today's date
   target_date = new Date( year, month - 1, day_of_month, 00, 00, 00)
   diff_msec = target_date - this_date    //difference in milliseconds
   diff_days = Math.ceil( diff_msec / ( 86400 * 1000 ) )    //difference in whole days.

   return diff_days;
}

/////////////////////////////////////////////////////////////////////////
// This function returns the number of whole months for today 
// until/since a target date.  Future dates are positive.
/////////////////////////////////////////////////////////////////////////

function calc_months_till_or_since( year,
                                  month, // January == 1, etc.
                                  day_of_month) { 

   var diff_months
   var diff_days = calc_days_till_or_since( year, month, day_of_month )

   var this_date = new Date()    //today's date
   var this_year = this_date.getFullYear()
   var this_month = this_date.getMonth() + 1
   var this_day = this_date.getDate()

   if ( diff_days > 0 )
      day_offset = this_day <= day_of_month ? 0 : -1
   else
      day_offset = this_day < day_of_month ? 1 : 0

   diff_months = 12 * ( year - this_year ) +
                 ( month - this_month ) +
                 day_offset

   return diff_months;
}


/////////////////////////////////////////////////////////////////////////
// This JavaScript function inserts text telling for today how many days 
// until/since some target date.
// Fragment originally written by Bill Gober on 2004-02-21.
// Rewritten 2005-02-14 so that the text isn't confusing on browsers without JavaScript.
/////////////////////////////////////////////////////////////////////////

function write_days_till_or_since( year, 
                                   month, // January == 1, etc.
                                   day_of_month) { 

   var diff_days = calc_days_till_or_since( year, month, day_of_month )
   
   // Now write the text and number.  

   if ( diff_days > 1 )
      document.write( diff_days + " days from today" )
   else if ( diff_days == 1 )
      document.write( "<b>tomorrow</b>" )
   else if ( diff_days == 0 )
      document.write( "<b>today</b>" )
   else if ( diff_days == -1 )
      document.write( "<b>yesterday</b>" )
   else
      document.write( -diff_days + " days ago" )
}


/////////////////////////////////////////////////////////////////////////
// This JavaScript function inserts text telling for today how many weeks 
// until/since some target date.
/////////////////////////////////////////////////////////////////////////

function write_weeks_till_or_since( year, 
                                    month, // January == 1, etc.
                                    day_of_month) { 

   var diff_days = calc_days_till_or_since( year, month, day_of_month )
   var diff_weeks = Math.floor( Math.abs( diff_days ) / 7 )

   if ( diff_days < 0 )
      diff_weeks = -diff_weeks
   
   // Now write the text and number.  

   if ( diff_weeks > 1 )
      document.write( diff_weeks + " weeks from now" )
   else if ( diff_weeks == 1 )
      document.write( "a week from now" )
   else if ( diff_weeks == 0 )
      document.write( "<b>within a week</b>" )
   else if ( diff_weeks == -1 )
      document.write( "a week ago" )
   else
      document.write( -diff_weeks + " weeks ago" )
}

/////////////////////////////////////////////////////////////////////////
// This JavaScript function inserts text telling for today how many months 
// until/since some target date.
/////////////////////////////////////////////////////////////////////////

function write_months_till_or_since( year, 
                                     month, // January == 1, etc.
                                     day_of_month) { 

   var diff_months = calc_months_till_or_since( year, month, day_of_month )

   // Now write the text and number.  

   if ( diff_months > 1 )
      document.write( diff_months + " months from now" )
   else if ( diff_months == 1 )
      document.write( "a month from now" )
   else if ( diff_months == 0 )
      document.write( "<b>within a month</b>" )
   else if ( diff_months == -1 )
      document.write( "a month ago" )
   else
      document.write( -diff_months + " months ago" )
}

/////////////////////////////////////////////////////////////////////////
// This JavaScript function inserts text telling for today how many years
// (by half years) until/since some target date.
/////////////////////////////////////////////////////////////////////////

function write_years_till_or_since( year, 
                                    month, // January == 1, etc.
                                    day_of_month) { 

   var diff_months = calc_months_till_or_since( year, month, day_of_month )
   var abs_months = Math.abs( diff_months )
   var diff_years = Math.floor( Math.abs( abs_months ) / 12 )
   var quarter_year = quarter_frac_chars[Math.floor((abs_months % 12) / 3)]
   var year_string

   if ( abs_months >= 15 )
      year_string = diff_years + quarter_year + " years"
   else if ( abs_months >= 12 )
      year_string = "a year"
   else if ( abs_months >= 3 )
      year_string = quarter_year + " year"
   else
      year_string = "0 years"

   diff_years = diff_months >= 0 ? diff_years : -diff_years

   // Now write the text and number.  

   if ( diff_years > 0 )
      document.write( year_string + " from now" )
   else if ( diff_years == 0 )
      document.write( "<b>within " + year_string + "</b>" )
   else // diff_years < 0
      document.write( year_string + " ago" )
}

/////////////////////////////////////////////////////////////////////////
// This JavaScript function inserts text telling for today how many days,
// weeks, months, or years until/since some target date.
/////////////////////////////////////////////////////////////////////////

function write_time_till_or_since( year, 
                                   month, // January == 1, etc.
                                   day_of_month) { 

   var diff_days = calc_days_till_or_since( year, month, day_of_month )
   var abs_diff = diff_days > 0 ? diff_days : -diff_days

   if ( abs_diff > (15 * 31) )
      write_years_till_or_since( year, month, day_of_month )
   else if ( abs_diff > 140 )
      write_months_till_or_since( year, month, day_of_month )
   else if ( abs_diff > 20 )
      write_weeks_till_or_since( year, month, day_of_month )
   else
      write_days_till_or_since( year, month, day_of_month )

}


/////////////////////////////////////////////////////////////////////////
// This JavaScript function tests write_days_till_or_since, 
// write_weeks_till_or_since, write_months_till_or_since, and 
// write_time_till_or_since for all dates during the past 18 months and
// next 18 months.
/////////////////////////////////////////////////////////////////////////

function test_time_till_or_since( ) {
   
   var this_date, this_year, this_month, this_dom

   var todays_date = new Date()    //today's date
   var todays_year = todays_date.getFullYear()
   var todays_month = todays_date.getMonth()
   var todays_dom = todays_date.getDate()

   document.writeln( "<table border=\"2\">" )

   for ( offset = -31 * 27; offset < (31 * 18); offset += 1 ) {

      document.writeln( "<tr>" )

      document.writeln( "<td>" + offset + "</td>" )

      this_date = new Date( todays_year, todays_month, todays_dom + offset, 0, 0, 0 )

      this_year = this_date.getFullYear()
      this_month = this_date.getMonth() + 1
      this_dom = this_date.getDate()

      document.writeln( "<td>" + this_date + "</td>" )

      document.writeln( "<td>" )
      write_days_till_or_since( this_year, this_month, this_dom )
      document.writeln( "</td>" )

      document.writeln( "<td>" )
      write_weeks_till_or_since( this_year, this_month, this_dom )
      document.writeln( "</td>" )

      document.writeln( "<td>" )
      write_months_till_or_since( this_year, this_month, this_dom )
      document.writeln( "</td>" )

      document.writeln( "<td>" )
      write_years_till_or_since( this_year, this_month, this_dom )
      document.writeln( "</td>" )

      document.writeln( "<td>" )
      write_time_till_or_since( this_year, this_month, this_dom )
      document.writeln( "</td>" )

      document.writeln( "</tr>" )

   }

   document.writeln( "</table>" )

}