// JavaScript Document

var count = 0;

function timer (starttime,nowtime) {

	// The starttime should come in as the number of seconds since January 1, 1970, 00:00:00
	// Check to see how BST is handled in PHP and javascript
	var phpstartSecs = starttime; // This is the startseconds from PHP - ie. the time the quiz subject was started
	var phpnowSecs = nowtime; // This is the currentseconds from PHP - ie. the time the page was last loaded
	var jsnowSecs = Date.parse(new Date())/1000; // This is the current seconds from local computer
	var offset = jsnowSecs - phpnowSecs - count; // This should be the difference between the local computer time and the PHP time
	// The counter gives the number of seconds since the page was loaded
	
	// Gives current number of seconds since January 1, 1970, 00:00:00
	// (local time on local computer)
	// Problem with this is that there is a difference between local computer time and php time.
	// We need to allow the local coputer just to deal with the incremental time
	// Start with an offset and add this to every question
	// If we do everything in JavaScript they can change the clock part way through
	
	var elapsedSecs = jsnowSecs - phpstartSecs - offset;
	
	var hours = Math.floor( elapsedSecs / 3600 )
	elapsedSecs = elapsedSecs - (hours*3600)
	if (hours < 10) { hours = "0" + hours;} // pad with a leading zero
	
	var minutes = 	Math.floor( elapsedSecs / 60 );
	elapsedSecs = elapsedSecs - (minutes*60);
	if (minutes < 10) { minutes = "0" + minutes;} // pad with a leading zero
	
	var seconds = elapsedSecs
	if (seconds < 10) { seconds = "0" + seconds;} // pad with a leading zero

	// Compose the string for display
	var currentTimeString = hours + ":" + minutes + ":" + seconds;

	// Update the time display
	document.getElementById("clock").firstChild.nodeValue = currentTimeString;

	count = count + 1; // Iterate the counter

	// Repeat

	setTimeout('timer("'+starttime+'","'+nowtime+'")',1000);
	
	// I think the funny stuff round starttime is to cope with the quotes within quotes etc.

}