//Holiday.js

function check_date()
{
  //var d = document, hol = document.getElementById("holiday");
  //hol.firstChild.data = check_holiday(todaysDate) + " this week";

  var todaysDate = new Date();
  //var todaysDate = new Date("April 1, 2009"); //set the date, use month name for IE

  return check_holiday(todaysDate);
}

function check_holiday(dt_date)
{
  var weeknum = getWeek(dt_date);
  //get the current year
  var thisYear = new Date().getFullYear();
  sHoliday = "";

// Check simple dates (month/date - no leading zeroes)
  var n_date = dt_date.getDate(),
  n_month = dt_date.getMonth() + 1;
  var s_date1 = n_month + '/' + n_date;

  h_newyears = new Date('1/1/'+thisYear);
  h_aprilfools = new Date('4/1/'+thisYear);
  h_flagday = new Date('6/14/'+thisYear);
  h_july4 = new Date('7/4/'+thisYear);
  h_veteransday = new Date('11/11/'+thisYear);
  h_christmas = new Date('12/25/'+thisYear);
  h_stpatricks = new Date('3/17/'+thisYear);

  if(weeknum == getWeek(h_newyears)) sHoliday =   'newyears';     // New Year's Day
  if(weeknum == getWeek(h_aprilfools)) sHoliday =   'april1st';     // April fools and Joppa birthday
  if(weeknum == getWeek(h_flagday)) sHoliday =  'flagday';      // Flag Day
  if(weeknum == getWeek(h_july4)) sHoliday =   'july4th';        // Independence Day
  if(weeknum == getWeek(h_veteransday)) sHoliday = 'veteransday';  // Veterans Day
  if(weeknum == getWeek(h_christmas)) sHoliday = 'christmas';    // Christmas Day
  if(weeknum == getWeek(h_stpatricks)) sHoliday =  'stpatricks';   // St Patricks Day
  //if(s_date1 == '3/17') return sHoliday =  'stpatricks';   // St Patricks Day

// Weekday from beginning of the month (month/num/day)
  var n_wday = dt_date.getDay(),
  n_wnum = Math.floor((n_date - 1) / 7) + 1;
  var s_date2 = n_month + '/' + n_wnum + '/' + n_wday;

  h_martinluther = getDateOfYear(1,0,thisYear,3); //nTargetday, nMonth, nYear, nTh)

  if(weeknum == getWeek(h_martinluther)) sHoliday =   'mlking'; // Birthday of Martin Luther King, third Monday in January
  //if(s_date2 == '1/3/1') sHoliday =  'martinluther'; // Birthday of Martin Luther King, third Monday in January
  if(s_date2 == '2/3/1') sHoliday =  'presidents';   // Washington's Birthday, third Monday in February
  if(s_date2 == '5/3/6') sHoliday =  'armedforces';  // Armed Forces Day, third Saturday in May
  if(s_date2 == '9/1/1') sHoliday =  'laborday';     // Labor Day, first Monday in September
  if(s_date2 == '10/2/1') sHoliday = 'columbus';     // Columbus Day, second Monday in October
  if(s_date2 == '11/4/4') sHoliday = 'thanksgiving'; // Thanksgiving Day, fourth Thursday in November
  if(s_date2 == '5/2/7') sHoliday = 'mothersday';    //Mothers Day, second Sunday in May
  if(s_date2 == '6/3/7') sHoliday = 'fathersday';    //Fathers Day, Third Sunday in June

// Weekday number from end of the month (month/num/day)
  var dt_temp = new Date (dt_date);
  dt_temp.setDate(1);
  dt_temp.setMonth(dt_temp.getMonth() + 1);
  dt_temp.setDate(dt_temp.getDate() - 1);
  n_wnum = Math.floor((dt_temp.getDate() - n_date - 1) / 7) + 1;
  var s_date3 = n_month + '/' + n_wnum + '/' + n_wday;

  if(s_date3 == '5/1/1') sHoliday = 'memorial';  // Memorial Day, last Monday in May

// Misc complex dates
  if(s_date1 == '1/20' && (((dt_date.getFullYear() - 1937) % 4) == 0)) sHoliday = 'inauguration'; // Inauguration Day, January 20th every four years, starting from 1937

  if(n_month == 11 && n_date >= 2 && n_date < 9 && n_wday == 2) sHoliday = 'election'; // Election Day, Tuesday on or after November 2.

// Not a holiday
  if(!sHoliday){
    return sHoliday = false; //'No holiday';
  }
  else {
    return sHoliday; //false
  }
}

function getWeek(tDate)
{
  var onejan = new Date(tDate.getFullYear(),0,1);
  return Math.ceil((((tDate - onejan) / 86400000) + onejan.getDay())/7);
}// end getWeek

//Returns the passed date based on the year
function getDateOfYear(nTargetday, nMonth, nYear, nTh)
{
  nEarliestDate = 1 + 7 * (nTh - 1)

  d = new Date(nYear,nMonth,nEarliestDate);
  nWeekday = d.getDay();

  if(nTargetday == nWeekday)
  {
    nOffset = 0;
  }
  else
  {
    if(nTargetday < nWeekday)
    {
      nOffset = nTargetday + (7 - nWeekday);
    }
    else
    {
      nOffset = (nTargetday + (7 - nWeekday)) - 7;
    }
  }

  return tHolidayDate = new Date(nYear,nMonth,nEarliestDate + nOffset);
}//end getDateOfYear
