///////////////////////////////////////////////////////////////////////////
// draw a clock on an HTML page. The timestring generated is of the format: 
// 'Saturday 11th February 2006 13:20:40' and it will be written to a page 
// element with an id of 'clk'
// Author: Bill Stennett
// Date: 11/02/06
// v1.0
///////////////////////////////////////////////////////////////////////////

var clockID = 0;	// a timer object to control when the clock updates

// set up arrays of the possible days and years
var d = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
var m = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");

////////////////////////////////////////////////////////////
// called when a timeout occurs to update the clock display
//
function UpdateClock() {

	// clear the timeout that called us
	if(clockID) {
    	clearTimeout(clockID);
      	clockID  = 0;
   	} // end if

	// read the values for the current date and time
	today = new Date();
	day = today.getDate();
	year = today.getYear();
	hours = today.getHours();
	minutes = today.getMinutes();
	seconds = today.getSeconds();

	// sort out leading zeroes as required for house minutes and seconds less than 10	
	if ( hours < 10 ) {
		hours = "0" + hours;
	}; 
	if ( minutes < 10 ) {
		minutes = "0" + minutes;
	}; 
	if ( seconds < 10 ) {
		seconds = "0" + seconds;
	}; 

	// in case of time warp and we go back to pre-millenium
	if (year < 2000) {    
	year = year + 1900;
	};
	 

	// add appropriate ending to day i.e.rd, th, st,
	end = "th";	// default to 'th'
	if (day==1 || day==21 || day==31) end="st";
	if (day==2 || day==22) end="nd";
	if (day==3 || day==23) end="rd";
	day+=end;

	// set the value of the element 'clk' to the current time string
   document.getElementById('clk').innerHTML = ""+d[today.getDay()]+" "+day+" "+m[today.getMonth()]+" "+year+ " " + hours + ":" + minutes + ":" + seconds;

   // start the next timer to trigger the next update
   // we update the clock once every second
   clockID = setTimeout("UpdateClock()", 1000);
   
} // end function UpdateClock

/////////////////////////////////////////////////////////
// start a timer running to trigger the next clock update
//
function StartClock() {

	// when the clock is first startd we trigger an update as quickly ss possible (1mS)
   clockID = setTimeout("UpdateClock()", 1);
   
} // end function StartClock

///////////////////////////////////////
// clear the timeout to stop the clock
//
function KillClock() {

	// if the clockID is not 0 we assume it is
	// a timer object and attempt to clear that timer (stopping further clock updates)
   if(clockID) {
      clearTimeout(clockID);
      clockID  = 0;
   } // end if
}; // end function KillClock

