//Reflex Tester- By Andy Scott (based on script by Jasper van Zandbeek)
//http://www.geocities.com/SiliconValley/Station/4320/
//Submitted to Dynamic Drive for inclusion
//Visit http://www.dynamicdrive.com for this script

var startTime=new Date();
var endTime=new Date();
var timingStarted=false;
var testStarted=false;
var timerID;

function ChangeToRed()
{
	if (document.all)
	{
		document.all.redlight.style.backgroundColor="red";
		document.all.amberlight.style.backgroundColor="";
		document.all.greenlight.style.backgroundColor="";
	}
	else if (document.getElementById)
	{
		document.getElementById("redlight").style.backgroundColor="red";
		document.getElementById("amberlight").style.backgroundColor="";
		document.getElementById("greenlight").style.backgroundColor="";
	}
}

function ChangeToAmber()
{
	if (document.all)
	{
		document.all.redlight.style.backgroundColor="";
		document.all.amberlight.style.backgroundColor="yellow";
		document.all.greenlight.style.backgroundColor="";
	}
	else if (document.getElementById)
	{
		document.getElementById("redlight").style.backgroundColor="";
		document.getElementById("amberlight").style.backgroundColor="yellow";
		document.getElementById("greenlight").style.backgroundColor="";
	}
}

function ChangeToGreen()
{
	if (document.all)
	{
		document.all.redlight.style.backgroundColor="";
		document.all.amberlight.style.backgroundColor="";
		document.all.greenlight.style.backgroundColor="green";
	}
	else if (document.getElementById)
	{
		document.getElementById("redlight").style.backgroundColor="";
		document.getElementById("amberlight").style.backgroundColor="";
		document.getElementById("greenlight").style.backgroundColor="green";
	}
}


function StartTiming()
{
	timingStarted=true;
	ChangeToAmber();
	startTime=new Date();
}

function remark(responseTime)
{
	var responseString="Did you fall asleep?"
	if (responseTime < 0.20)
		responseString="Well done!";
	else if (responseTime < 0.30)
		responseString="Nice!";
	else if (responseTime < 0.40)
		responseString="Not bad!";
	else if (responseTime < 0.70)
		responseString="Keep practising!";
	else if (responseTime < 1)
		responseString="Have you been drinking?";

	return responseString;
}

function Action()
{
	clearTimeout(timerID);
	if (testStarted)
	{
		if (timingStarted)
		{
			endTime=new Date();
			var responseTime=(endTime.getTime()-startTime.getTime())/1000;
			ChangeToRed();
			alert("You reacted in " + responseTime + " seconds " + "\n" + remark(responseTime));
		}
		else
		{
			ChangeToRed();
			alert("You pressed too early!");
		}
		testStarted=false;
	}
	else
	{
		ChangeToGreen();
		testStarted=true; 
		timerID=setTimeout('StartTiming()', 1000+(6000*Math.random()));
	}
	timingStarted=false;
}

document.write("<br><div class=test><br>");
document.write("Test your reaction time!<br><br>Click 'ACTION' and when the amber light shows click 'ACTION' again!<br><br>");

document.write("<div id='redlight' class=light onClick='Action()'></div>");
document.write("<div id='amberlight' class=light onClick='Action()'></div>");
document.write("<div id='greenlight' class=light onClick='Action()'></div>");

document.write("<form><input type='button' value='ACTION' onClick='Action()' style='font-weight:bold'></form></div>");