// Note: these functions need to be written for reuse (i.e. given
// two dates, return the difference in days, hours, minutes, hours).
//

function howlong(WHEN, EVENT) {
   var d = calcDateDiff(WHEN);
   if (null == d) 
      return "";
   if (null == EVENT || "" == EVENT) {
		d.days++;
      return d;
	}
   if (-1 == d.days) {
      d.days="It's Here!"}

      
   document.write("<div class='alert'>Days Until Halloween</div>");
   document.write("<div class='on_black'>");
   document.write( d.days );
   document.write("</div>");
}

function calcDateDiff(when) {
   var MILLISECS_PER_SECOND = 1000;
   var SECS_PER_MINUTE = 60;
   var MINUTES_PER_HOUR = 60;
   var HOURS_PER_DAY = 24;
   var SECS_PER_HOUR = SECS_PER_MINUTE * MINUTES_PER_HOUR;
   var SECS_PER_DAY = SECS_PER_HOUR * HOURS_PER_DAY;
   var MILLISECS_PER_MINUTE = SECS_PER_MINUTE * MILLISECS_PER_SECOND;
   var MILLISECS_PER_HOUR = SECS_PER_HOUR * MILLISECS_PER_SECOND;//
   var MILLISECS_PER_DAY = SECS_PER_DAY * MILLISECS_PER_SECOND;
   var now = new Date();
   var then = new Date(when);
//   if (now > then) return null;
   var diff = then - now;
   var days = Math.floor(diff / MILLISECS_PER_DAY);
   var adj = days * MILLISECS_PER_DAY;
   var hours = Math.floor((diff - adj) / MILLISECS_PER_HOUR);
   adj += hours * MILLISECS_PER_HOUR;
   var minutes = Math.floor((diff - adj) / MILLISECS_PER_MINUTE);
   adj += minutes * MILLISECS_PER_MINUTE;
   var seconds = Math.floor((diff - adj) / MILLISECS_PER_SECOND);
   return new DateDiff(days, hours, minutes, seconds);
}

function DateDiff(d, h, m, s) {
   this.days = d;
   this.hours = h;
   this.minutes = m;
   this.seconds = s;
}
