var words=new Array();
words[0]="BUY";
words[1]="ONE";
words[2]="OUR";
words[3]="HUG";
words[4]="SPY";
words[5]="DRY";
words[6]="ADD";
words[7]="ERR";
words[8]="RUN";
words[9]="FEW";
words[10]="ICE";
words[11]="JAM";
words[12]="JOB";
words[13]="LAB";
words[14]="YET";
words[15]="WIG";
words[16]="TRY";
words[17]="EYE";
words[18]="SEA";
words[19]="SIX";
words[20]="SIN";

var descr=new Array();
descr[0] = "to purchase";
descr[1] = "NUMBER 1";
descr[2] = "belonging to us";
descr[3] = "to hold tightly";
descr[4] = "to watch secretly";
descr[5] = "to reduce moisture";
descr[6] = "to perform addition";
descr[7] = "to make a mistake";
descr[8] = "to move quickly";
descr[9] = "not many";
descr[10] = "to cool with frozen water";
descr[11] = "to block up";
descr[12] = "to do piece work";
descr[13] = "a laboratory";
descr[14] = "in addition";
descr[15] = "to provide with a hairpiece";
descr[16] = "to attempt";
descr[17] = "part of human body";
descr[18] = "an ocean";
descr[19] = "NUMBER 6";
descr[20] = "to commit a moral offence";

var no,count,total;
var myword="";
var curr;
var score=0;
var arr = new Array(0,1,2);

function start()
{
 console.log('started');
 no=count=score=0;
 total=21;
 document.getElementById('score').innerHTML="Score : "+score;
 document.getElementById('skip').innerHTML="Skip";
 myword="";
 document.getElementById('out').value=" = ? ";
 next1();
}

function shuffle(array) {
  var currentIndex = array.length
    , temporaryValue
    , randomIndex
    ;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}

function next1()
{
document.getElementById('skip').style.color="black";
document.getElementById('clear').innerHTML="Clear";
document.getElementById('clear').disabled=false;
document.getElementById('hint').disabled=false;
document.getElementById('hint').innerHTML = "Click here for hint";
 document.getElementById('l1').disabled=false;
 document.getElementById('l3').disabled=false;
 document.getElementById('l2').disabled=false;
 no++;
 if(no==total)
 {
  document.getElementById('skip').innerHTML="Finish";
  if(score>100)
   alert("Congratulations! You are done with this task. :) :) \n\n SCORE : "+score+"");
  else
   alert("Congratulations! You are done with this task :) :) \n\n SCORE : "+score+"\n\n You can try again to improve your score."); 
   document.getElementById('l1').disabled=true;
 document.getElementById('l3').disabled=true;
 document.getElementById('l2').disabled=true;
 document.getElementById('clear').disabled=true;
 document.getElementById('hint').disabled=true;
  no--;
 }
 else
 {
  myword="";
  document.getElementById('out').value=" = ? ";
  currN = Math.floor(Math.random()*20);
  document.getElementById('q').innerHTML = "Question : "+no+"/20";
  document.getElementById('iii').style.visibility="hidden";
  curr=words[currN];
  console.log('initiated');
  shuffle(arr);
  console.log('jumbuled');
  document.getElementById('l1').innerHTML = words[currN][arr[0]];
  document.getElementById('l2').innerHTML = words[currN][arr[1]];
  document.getElementById('l3').innerHTML = words[currN][arr[2]];
 }
 if(no>=total-1)
  document.getElementById('skip').innerHTML="Finish";
 else
  document.getElementById('skip').innerHTML="Skip";
}

function hint()
{
 score = score - 1 ;
 document.getElementById('score').innerHTML="Score : "+score;
 document.getElementById('hint').innerHTML=descr[currN];
}

function check( param )
{
 document.getElementById('l'+param).disabled=true;
 myword = myword + document.getElementById('l'+param).innerHTML;
 document.getElementById('out').value=myword;
 if(curr==myword)
 {
   document.getElementById('iii').style.visibility="visible";
   document.getElementById('iii').src="images/correct.png";
   document.getElementById('skip').innerHTML = 'Next';
   document.getElementById('skip').style.color='green';
   document.getElementById('hint').disabled=true;
   document.getElementById('clear').disabled=true;
   score = score + 10;
   document.getElementById('score').innerHTML="Score : "+score;
 }
 else if(myword.length==3)
 {
   document.getElementById('iii').style.visibility="visible";
   document.getElementById('iii').src="images/incorrect.png";
   document.getElementById('clear').innerHTML="Retry";
   score = score - 1;
   document.getElementById('score').innerHTML="Score : "+score;
 }
}

function clr()
{
document.getElementById('clear').innerHTML="Clear";
 document.getElementById('iii').style.visibility="hidden";
 document.getElementById('hint').innerHTML = "Click here for hint";
 document.getElementById('l1').disabled=false;
 document.getElementById('l3').disabled=false;
 document.getElementById('l2').disabled=false;
 myword=""; 
 document.getElementById('out').value=" = ?";
}

function exit()
{
 if(confirm("Do you really want to exit?"))
  window.location="index.html";
}

function skip()
{
   document.getElementById('skip').innerHTML = 'Skip';
   document.getElementById('skip').style.color='black';
 next1();
}

function next()
{
 next1();
}

function restart()
{
if(confirm("Do you really want to restart game?"))
 {
  start();
 }
}