active=false

function setComment(message)
{
  currentMessage=message
  document.playfield.message.value=message
}

function setMarker(index)
{
  currentMarker=index
  document.playfield.marker0.checked=(index==0)
  document.playfield.marker1.checked=(index!=0)
}

function lastWait()
{
  if (phase==2)
    setComment("Your reaction time is more than 1 second")
  else
    if (phase==3)
      setComment("Your reaction time is 975 milli-seconds")
  phase=0
  setMarker(0)
}

function endWait(time)
{
  if (phase==3)
  {
    phase=4
    setComment("Your reaction time is "+time+" milli-seconds")
  }
}

function startWait()
{
  phase=2
  setMarker(1)
  setTimeout("endWait(25)",50)
  setTimeout("endWait(75)",100)
  setTimeout("endWait(125)",150)
  setTimeout("endWait(175)",200)
  setTimeout("endWait(225)",250)
  setTimeout("endWait(275)",300)
  setTimeout("endWait(325)",350)
  setTimeout("endWait(375)",400)
  setTimeout("endWait(425)",450)
  setTimeout("endWait(475)",500)
  setTimeout("endWait(525)",550)
  setTimeout("endWait(575)",600)
  setTimeout("endWait(625)",650)
  setTimeout("endWait(675)",700)
  setTimeout("endWait(725)",750)
  setTimeout("endWait(775)",800)
  setTimeout("endWait(825)",850)
  setTimeout("endWait(875)",900)
  setTimeout("endWait(925)",950)
  setTimeout("lastWait()",1000)
}

function resultChange()
{
  if (active)
    document.playfield.message.value=currentMessage
}

function markerClick()
{
  if (active)
    setMarker(currentMarker)
}

function startClick()
{
  if (active)
    if (phase==0)
    {
      phase=1
      timeoutId=setTimeout("startWait()",3000+10000*Math.random())
      setComment("Click 'Detect' when the markers swap place")
    }
    else
      if (phase>=3)
        setComment("Wait until the markers have swapped back")
      else
        setComment("Already started")
}

function detectClick()
{
  if (active)
    if (phase==1)
    {
      phase=0
      setComment("You clicked 'Detect' too early")
      clearTimeout(timeoutId)
    }
    else
      if (phase==2)
        phase=3
}

function enableScript()
{
  phase=0
  setMarker(0)
  setComment("Click 'Start' to start")
  active=true
}
