// This code was modified to display both the hexclock and normal time by
// Nick Glover <http://hubcap.clemson.edu/~nglover>
// ---------------------------------------------------------------------------
// This code was adapted from the clock on the netscape javascript page
// by Mark Vincent Rogers
// ---------------------------------------------------------------------------
// Hex clock concept is also by Mark Rogers
// ---------------------------------------------------------------------------
// This code can be modified in any way as long as credit is given to the
// original authors, but you can't convert this to use AM or PM, it must
// be on 16-hexhour time.
// ---------------------------------------------------------------------------
// It was further hacked by Beat Bolli to include the Swatch .beats,
// add the hex and decimal seconds and UNIX ticks and clean up the hexclock
// code in general by using the bit shift instead of division and modulo
// operators.

function nibble(dec) {
    return "0123456789ABCDEF".substr(dec & 0x0F, 1);
}

function florence(dec) {
    return "QBPVFZSDTJCGKYXW".substr(dec & 0x0F, 1);
}

function padl(n, w, c) {
    n = String(n);
    while (n.length < w)
        n = c + n;
    return n;
}

function hms(t) {
    return "" +
	padl(t.getHours(), 2, " ") + ":" +
	padl(t.getMinutes(), 2, "0") + ":" +
	padl(t.getSeconds(), 2, "0");
}

function seconds(t) {
    return t.getTime() / 1000 % 86400;	// Seconds since midnight
}

function showtime() {

    // Calculate UTC
    var utc = new Date();
    utc.setTime(utc.getTime() + utc.getTimezoneOffset() * 60 * 1000);
    var utctimeValue = hms(utc);

    // Calculate standard time
    var now = new Date();
    var stdtimeValue = hms(now);
    var sec = seconds(now);

    // Calculate hex time
    var hex = Math.floor(sec * 0x10000 / 86400);
    var hexhours = nibble(hex >> 12);
    var hexminutes = nibble(hex >> 8) + nibble(hex >> 4);
    var hexseconds = nibble(hex);
    var hextimeValue = hexhours + "_" + hexminutes + "_" + hexseconds;

    // Calculate twice sixteen hours hex time (florencetime)
    var flo = Math.floor(sec * 0x20000 / 86400);
    var flohours = (flo < 0x10000 ? ' ' : 'B') + florence(flo >> 12);
    var flominutes = florence(flo >> 8) + florence(flo >> 4);
    var floseconds = florence(flo);
    var flotimeValue = flohours + "." + flominutes + "\u2019" + floseconds.toLowerCase() + " H";

    // Calculate metric time
    var dec = Math.floor(sec * 100000 / 86400);
    var decseconds = dec % 100;
    dec = (dec - decseconds) / 100;
    var decminutes = dec % 100;
    var dechours = (dec - decminutes) / 100;
    var decTimeValue = dechours + "." + padl(decminutes, 2, "0") + "." + padl(decseconds, 2, "0");

    // Calculate Swatch .beats, that is, 1 hour east of UTC
    now.setTime(utc.getTime() + 120 * 60 * 1000);
    var beattime = Math.floor(seconds(now) * 1000 / 86400);
    var beattimeValue = "@" + padl(beattime, 3, "0");

    // Calculate UNIX ticks
    var tickValue = utc.getTime() / 1000;

    // Set the form values
    document.clock.stdface.value = stdtimeValue;
    document.clock.utcface.value = utctimeValue;
    document.clock.hexface.value = hextimeValue;
    document.clock.floface.value = flotimeValue;
    document.clock.decface.value = decTimeValue;
    document.clock.beatface.value = beattimeValue;
    document.clock.tickface.value = tickValue;

    setTimeout("showtime()", 100);
}