/* © 2011 by Markus Kwaśnicki */

/* Class for the DateTimeStamp string functions. */
function IsoTimestampClock(dts)
{
	this.dts = dts;
	this.clock;
	
	/* Starts the DTS clock. */
	this.start = function start()
	{
		var that = this;
		this.clock = window.setInterval(function(){writeDatetimestamp(that.dts);}, 500);
	}
	
	/* Stops the DTS clock. */
	this.stop = function stop()
	{
		window.clearInterval(this.clock);
	}

    /* Writes a DateTimeStamp string to a specified HTML tag. */
    function writeDatetimestamp(dts)
    {
        document.getElementById(dts).innerHTML = getDatetimestamp(new Date());
    }

    /* Returns a DateTimeStamp string value. */
    function getDatetimestamp(date)
    {
        var datetime = new String();
        var separator = new Array('-', 'T', ':');
	
        datetime += date.getFullYear();
        datetime += separator[0];
        datetime += addSignedLeadingZero(false, 2, date.getMonth() + 1);
        datetime += separator[0];
        datetime += addSignedLeadingZero(false, 2, date.getDate());
        datetime += separator[1];
        datetime += addSignedLeadingZero(false, 2, date.getHours());
        datetime += separator[2];
        datetime += addSignedLeadingZero(false, 2, date.getMinutes());
        datetime += separator[2];
        datetime += addSignedLeadingZero(false, 2, date.getSeconds());
	
        datetime += addSignedLeadingZero(true, 2, Math.floor(date.getTimezoneOffset() / -60));
        datetime += separator[2];
        datetime += addSignedLeadingZero(false, 2, date.getTimezoneOffset() % -60);
	
        return datetime;
    }
    
    /* Starts or stops the clock */
    this.toggle = function(element)
    {
        if(element.checked)
        {
            this.start();
        }
        else
        {
            this.stop();
        }
    }
}

/* Initializes the clock */
function initializeIsoTimestampClock()
{
    document.getElementById("isotimestampclockcheckbox").style.display = "inline";
    isoTimestampClock = new IsoTimestampClock("isotimestampclock");
    isoTimestampClock.start();
}

/* Initialize the clock as soon as the document gets loaded */
var isoTimestampClock = null;
if(window.addEventListener)
    window.addEventListener("load", initializeIsoTimestampClock, false);
else if(window.attachEvent)
    window.attachEvent("onload", initializeIsoTimestampClock);
else
    alert("Not supported yet!");
// <--

