/* countdown.js - Countdown timer
05/20/04 wws Initial version
05/21/04 wws Get rid of global variable so multiple instances can
.            appear on the same page.

Call doCountdown(countdownDate, prefix, suffix) to start.
  countdownDate: The date to which to count down
  prefix:        Some html to put before the countdown field
  suffix:        Some html to put after the countdown field
Example:
  doCountdown("1 Jan 2100", "Twenty-second century in: ", "");

Copyright 2004 - Bill St. Clair, bill@billstclair.com
Released under the GNU Library General Public License
http://www.gnu.org/copyleft/lgpl.html
*/

function fmt(num) {
  s = num.toString();
  if (s.length < 2) s = "0" + s;
  return s;
}

function setCountdown(formName, countdownDate) {
   d = Date.parse(countdownDate);
   now = new Date();
   nowt = now.getTime();
   tot = d - nowt
   diff = tot;
   minus = false;
   if (diff < 0) {
     minus = true;
     diff = (- diff);
   }
   millis = tot % 1000;
   tot = (tot - millis) / 1000;
   sec = tot % 60;
   tot = (tot - sec) / 60;
   min = tot % 60;
   tot = (tot - min) / 60;
   hrs = tot % 24
   days = (tot - hrs) / 24;
   document.forms[formName].countdown.value =
   days.toString() +  ((days == 1) ? " day, " : " days, ") + fmt(hrs) + ":" + fmt(min) + ":" + fmt(sec);
   setTimeout('setCountdown("' + formName + '", "' + countdownDate + '")',
              1010 - (nowt % 1000));
}

function doCountdown(formName, countdownDate, prefix, suffix) {
  document.write('<form name="' + formName + '">');
  document.write(prefix);
  document.write('<input type="text" name="countdown" value=""');
  document.write('style="font-weight: bold" readonly="true" size="19">');
  document.write(suffix);
  document.write('</form>');
  setCountdown(formName, countdownDate);
}

