/* copyright all content Thomas Evdokimoff 2006.  Use without written permission
 * is strickly prohibited  this game is the no sound version*/


    // create parallel arrays for terms and definitions

    var gameTerms = prelimTerms;
    var   gameDefinitions = prelimDefinitions;

// array of choices id
	var choicesId = new Array(4);
	    choicesId[0] = "ans1";
	    choicesId[1] = "ans2";
	    choicesId[2] = "ans3";
	    choicesId[3] = "ans4";

        var term, definition;
var right, wrong, accuracy, numTerms;

var index = new Array(); // random index for term definition
selectLevel('0'); // set default level not sure if necessary
var bindex = new Array(4); //random index for printing choices 

function startGame()
{
     right = 0;
     wrong = 0;
     accuracy = 0;
     numTerms = 0;
    nextTerm();
}
function nextTerm()
{
    clearAll();
    updateStats();
    populateArray(index);
    populateArray(bindex);
    term = gameTerms[index[0]];
    definition = gameDefinitions[index[0]];
    document.getElementById("term").innerHTML = term;
//prints the term and choices
    for (i=0; i< 4; i++) {
        document.getElementById(choicesId[bindex[i]]).innerHTML=gameDefinitions[index[i]];
    }
    ++numTerms;
   }
//clear id=term and ans1..ans4. called before starting a new game or new term
//definition
function clearAll()
{
    document.getElementById("term").innerHTML="";
    for (i = 0; i < 4; ++i ) {
      document.getElementById(choicesId[i]).innerHTML = "";
      document.getElementById(choicesId[i]).style.color="black";
      document.getElementById(choicesId[i]).style.textDecoration="none";
    }
      document.getElementById("mssg").innerHTML="Choose the Correct\
      Key Signature Below";
      document.getElementById("mssg").style.color="navy";
}




//Creates an index of random numbers without repetition in the range of the
//array's length


function populateArray(myarray)
{
    for ( i= 0; i < myarray.length; i++) {
        myarray[i] = Math.floor(Math.random() * myarray.length);
        checkNumber(myarray, i); // check for duplicate
    }
}

// recursive function that checks for duplicates while populating an array with
// random numbers
function checkNumber(myarray, i)
{
   for ( y = 0; y < i; y++ ) {
       if (myarray[i] == myarray[y]) {
        myarray[i] = Math.floor(Math.random() * myarray.length);
        checkNumber(myarray, i);
       }
   }
}
//utility that prints an array's contents.  Use for testing results of
//populateArray()
function writeIndex(myarray)
{
    for (p = 0; p < myarray.length; p++) {
        document.write(myarray[p]+"<br>");
    }
}
// check users selection

function guess(answer)
{
  if ( answer.innerHTML == definition ) {
      correctAnswer(answer);
  } else {
      wrongAnswer(answer);
  }
}

function correctAnswer(answer)
{
     answer.style.color="blue";
     answer.style.textDecoration="blink";
     document.getElementById("mssg").innerHTML="Excellent!";
     document.getElementById("mssg").style.color="blue";
     ++right;

}

function wrongAnswer(answer)
{
      answer.style.color="red";
      document.getElementById("mssg").innerHTML="OOPS! Try Again";
     document.getElementById("mssg").style.color="red";
     ++wrong;
     
}

function updateStats()
{
 document.getElementById("terms_left").innerHTML="Number of Keys: "+numTerms;
   // update scoreboard this is done in the wrong place 
    document.getElementById("correct").innerHTML="Correct Responses: "+right;
    document.getElementById("wrong").innerHTML="Incorrect Responses: "+wrong;
    accuracy = ( (right-wrong)/(numTerms))*100;
    accuracy = parseInt(accuracy);
    if (isNaN(accuracy) ) { accuracy= 0; }
    document.getElementById("accuracy").innerHTML="Accuracy: "+accuracy+" %";
}

function selectLevel(level)
{
    if (level.value == "0" ) {
        gameTerms = prelimTerms;
        gameDefinitions = prelimDefinitions;
    } else if (level.value == "1"  ) {
        gameTerms = gradeOneTerms;
        gameDefinitions = gradeOneDefinitions;
    }
    index.length = gameTerms.length;
}

function toggleSound () {

alert ("Sorry, there is no sound for your browser");
}
