
var DAY;
var OBJ;

function getDays(month, year) {
  // create array to hold number of days in each month
  var ar = new Array(12);
  ar[0] = 31; // December
  ar[1] = 31; // January
  ar[2] = ((year % 4 == 0) && (year % 400 != 0)) ? 29 : 28; // February
  ar[3] = 31; // March
  ar[4] = 30; // April
  ar[5] = 31; // May
  ar[6] = 30; // June
  ar[7] = 31; // July
  ar[8] = 31; // August
  ar[9] = 30; // September
  ar[10] = 31; // October
  ar[11] = 30; // November

  // return number of days in the specified month (parameter)
  return ar[month];
}

function updateTimer(day, obj) {
  DAY = day;
  OBJ = obj;
  _updateTimer();
}

function _updateTimer() {
  startDate = new Date(DAY);
  now       = new Date();

  years = now.getFullYear() - startDate.getFullYear();
  months = now.getMonth() - startDate.getMonth();
  days = now.getDate() - startDate.getDate();

  if (months < 0) {
    months = 12 + months;
    years--;
  } else if (months == 0 && days < 0) {
    months = 12 + months;
    years--;
  }
 
  if (days < 0) {
    days = days + getDays(now.getMonth(), now.getFullYear());
    months--;
  }

  text = "";

  if (years == 1) {
    text = text + years + " year";
  } else if (years > 1) {
    text = text + years + " years";
  }

  if (months != 0) {
    if (years > 0) {
      text = text + ", ";
    }
    if (months == 1) {
      text = text + months + " month";
    } else if (months > 1) {
      text = text + months + " months";
    }
  }

  if (days != 0) {
    if ((years > 0 && months == 0) || months > 0) {
      text = text + ", ";
    }
    if (days == 1) {
      text = text + days + " day";
    } else if (days > 1) {
      text = text + days + " days";
    }
  }

  OBJ.nodeValue = text;

  midnight = new Date(now.getTime() + 24 * 60 * 60 * 1000);
  midnight.setHours(0);
  midnight.setMinutes(0);
  midnight.setSeconds(0);
  timeTillMidnight = midnight.getTime() - now.getTime();

  setTimeout("_updateTimer()", timeTillMidnight);
}

