<!--

function onPageLoaded() { }

var playing = false;
var pathHead = baseprefix;

var imagePathHead=pathHead + "images/";
var gifs = new Array(3);
gifs[0]=new Image();
gifs[0].src= imagePathHead + "question-blue.gif";
gifs[1]=new Image();
gifs[1].src= imagePathHead + "green-check.gif";
gifs[2]=new Image();
gifs[2].src= imagePathHead + "x-red.gif";

var numEqns = 15;
var maxVal = 10;
var numCrt=0;
var numTried = 0;
var playing = false;    // play is currently ongoing 
var has_played = false;    // play has occurred and has ceased (stop was pressed)

var primaryParent;
var statisticsdiv;
var stopstartdiv;
var msgsdiv;
var arithworkdiv;
var numcorrectdiv;
var numtrieddiv;

function initialStartGame()
{
  var textdiv = document.getElementById('intro_text');
  textdiv.style.display='none';
  msgsdiv = document.getElementById('user_messages');
  statisticsdiv = document.getElementById('tried_correct_statistics');
  numcorrectdiv = document.getElementById('numcorrect_div');
  numtrieddiv = document.getElementById('numtried_div');
  statisticsdiv.style.display='block';
  var arith_container_div = document.getElementById('arithmetic_workout_container');
  arith_container_div.style.display='block';

  setupStartStop();
  startGame();
}

function startGame()
{
    numTried = 0;
    numCrt = 0;
    msgsdiv.style.display='none';
    reset_equations(numEqns);
    numtrieddiv.innerHTML = numTried;
    numcorrectdiv.innerHTML = numCrt;
    playing = true;
    reset_timer();
    start_timer();
    setStopStartBtns(false, true);
}

function stopGame() {
    stop_play();
}

function stop_play()
{
    var cur_time = new Date();
    elapsed_time = cur_time.getTime() - start_time.getTime();
    stop_timer();
    playing = false;
    setStopStartBtns(true, false);

    if (numCrt == numEqns){
        numCrt += 5;
        numcorrectdiv.innerHTML = numCrt;
        msgsdiv.innerHTML = "All correct!  Bonus = 5!";
        msgsdiv.style.display='block';
    }
    var userNum = sbCookieData.usernum;
	Record.recordNumCorrect(taskId, numCrt, elapsed_time, userNum);
}

    // ------------- ARITHMETIC EQUATIONS ------

function createequations(){
    equations(numEqns);
}

function clearAnswers()
{
  for (var k=0; k<numEqns; k++){
  var en = "e" + k;
  var dd = document.getElementById(en);
  dd.value="U&&";
  }
}

function equations(n)
{
  for (i=0; i<n; i++){
     equation(i);
  }
}
function reset_equations(n)
{
  for (i=0; i<n; i++){
     reset_equation(i);
  }
}


var oprs = new Array("+", "-", "*", "/");

var eq_opr1 = new Array(numEqns);
var eq_opr =  new Array(numEqns);
var eq_opr2 = new Array(numEqns);
var eq_res =  new Array(numEqns);

function rnd(n) {
  return Math.floor(Math.random() * n);
}

function equation(i)
{
    eq_opr[i] = oprs[rnd(4)];

    if (eq_opr[i] == "+" ){
      eq_opr1[i] = rnd(maxVal);
      eq_opr2[i] = rnd(maxVal);
      eq_res[i]  =  eq_opr1[i] +  eq_opr2[i];
    } else if (eq_opr[i] == "*") {
      eq_opr1[i] = rnd(maxVal);
      eq_opr2[i] = rnd(maxVal);
      eq_res[i]  =  eq_opr1[i] *  eq_opr2[i];
    } else if (eq_opr[i] == "-") {
      eq_opr1[i] = rnd(maxVal);
      eq_opr2[i] = rnd(eq_opr1[i]);
      eq_res[i]  =  eq_opr1[i] -  eq_opr2[i];
    } else {
      eq_opr2[i] = 1 + rnd(maxVal-1);
      eq_opr1[i] = rnd(3) * eq_opr2[i];
      eq_res[i]  =  eq_opr1[i] /  eq_opr2[i];
    }
}
function reset_equation(i)
{
    eq_opr[i] = oprs[rnd(4)];

    var en = "e"+i;
    var dn = "d"+i;

    var de = document.getElementById(en);
    de.value="";


    if (eq_opr[i] == "+" ){
      eq_opr1[i] = rnd(maxVal);
      eq_opr2[i] = rnd(maxVal);
      eq_res[i]  =  eq_opr1[i] +  eq_opr2[i];
    } else if (eq_opr[i] == "*") {
      eq_opr1[i] = rnd(maxVal);
      eq_opr2[i] = rnd(maxVal);
      eq_res[i]  =  eq_opr1[i] *  eq_opr2[i];
    } else if (eq_opr[i] == "-") {
      eq_opr1[i] = rnd(maxVal);
      eq_opr2[i] = rnd(eq_opr1[i]);
      eq_res[i]  =  eq_opr1[i] -  eq_opr2[i];
    } else {
      eq_opr2[i] = 1 + rnd(maxVal-1);
      eq_opr1[i] = rnd(3) * eq_opr2[i];
      eq_res[i]  =  eq_opr1[i] /  eq_opr2[i];
    }
    var el_content = +eq_opr1[i]+" "+eq_opr[i]+" "+eq_opr2[i];
    var el = document.getElementById("el"+i);
    el.innerHTML = el_content;
    document.images[i].src=gifs[0].src;
}

function check_started()
{
    if (!playing){
      alert("Please press Start to begin.");
      return;
    }
}

function checkeqn(i, val)
{
    numTried++;
    var numtrieddiv = document.getElementById('numtried_div');
    numtrieddiv.innerHTML = numTried;
    var k;
    if (val == eq_res[i])
    {
        k = 1;
        numCrt++;
        var numcorrectdiv = document.getElementById('numcorrect_div');
        numcorrectdiv.innerHTML = numCrt;
    } else {
        k=2;
    }
    document.images[i].src=gifs[k].src;


    if (numTried >= numEqns ) 
    {
      if ( allEntered() ){
          stop_play();
      }
    }
}

function allEntered()
{
  var result = true;
  for (var i=0; i<15; i++){
    var eei = document.getElementById("e"+i);
    if (eei.value == null || eei.value.length == 0){
      result = false;
      break;
    }
  }
  return result;
}

function dumpimages()
{
  for (i=0; i<document.images.length; i++){
    document.write(document.images[i].name) + "<br>";
  }
}

    // -----------INSTRUCTIONS ------------

function showInstructions()
{
    var ipath= "arithmetic-workout-instructions";
    var entry_win=window.open(
                     ipath,
                     'Arithmetic_Instructions',
                     'height=300,width=650');
}


function displayPerfData(response)
{
    var note = "There are fifteen(15) equations in each trial, plus a possible bonus of 5, <br>so the maximum correct score is 20 (=15+5) times the number tried.";
    genericDisplayPerfData('Arithmetic Workout', response, note);
    return false;
}

// -->
