/* The programme calculates the time and updates the display every
   repeat_time milliseconds. The actual loop time depends on the
   execution time, repeat_time and the operating system so it is
   impossible to ensure that the display is really updated every
   second. We set repeat_time to 900 ms so that most updates see a
   change of one second but occasionally the display will seem to skip
   one second.  On the other hand, a very small repeat_time gives an
   accurate display but uses a lot of CPU in the client machine. 
   This programme is used with the kind permission of Owen Boyle
   (http://owen.zipzap.ch), Franziska Jungen (http://www.fran.ch)
   and those they copied it from...
 */
 
var repeat_time   = 900;   // in milliseconds

// startclock(): Called from the HTML page
// ---------------------------------------
function startclock() { 

    y2k    = new Date("August 1, 2003 18:00:00");
    left   = new Object();
    sec    = new Object();

    sec.day  = 86400000;
    sec.hour = 3600000;
    sec.min  = 60000;
    sec.sec  = 1000;

    showtime(); 
}

// showtime(): Counts down and handles the timer
// ---------------------------------------------
function showtime() {

    now    = new Date();

/* Calculate the times left for the various units. After Y2K we count
   up again. */

    left.ms = (y2k.getTime() - now.getTime());
    if (left.ms < 0) {
        left.ms -= 2*left.ms;
    }
 
    left.days  = (left.ms - (left.ms%sec.day))/sec.day;
    left.ms   -= left.days * sec.day;

    left.hours = (left.ms - (left.ms%sec.hour))/sec.hour;
    left.ms   -= left.hours * sec.hour;

    left.mins  = (left.ms - (left.ms%sec.min))/sec.min;
    left.ms   -= left.mins * sec.min;

    left.secs  = (left.ms - (left.ms%sec.sec))/sec.sec;

    make_display(left.days, left.hours, left.mins, left.secs);
    setTimeout("showtime();", repeat_time); 
}

// make_display()
//-------------------------------------------------------------------
function make_display(days, hours, mins, secs) {

/* Force the data into strings in %02d format (not needed if JS could
   do this automatically!). This is done by adding 100, converting to
   a string and then returning only the last two characters */

    days  += 1000;
    hours += 100;
    mins  += 100;
    secs  += 100;

    days   = days.toString();
    hours  = hours.toString();
    mins   = mins.toString();
    secs   = secs.toString();

    days   = days.substring(1,4);
    hours  = hours.substring(1,3);
    mins   = mins.substring(1,3);
    secs   = secs.substring(1,3);

/* Make up the display and assign it to the output screens */ 

    window.status  = days  +':'
                    +hours +':'
                    +mins  +':'
                    +secs;

    document.display.days.value  = days;
    document.display.hours.value = hours;
    document.display.mins.value  = mins;
    document.display.secs.value  = secs;
}
