// version 106004 realtttquiz.js  
// ns 7 focus fix for unselected()
// initialise variables
 var player=0;		// declare player variable with player = 2 for first turn
 var play1color='wheat';	// set color for player 1
 var play2color='sienna';	// set color for player 2
 var randomnumber=0;	// declare this globally
 var winnerstatus=0;	// set winner status to 0
 var turns=0;
 var response='0';	// set response to wrong

function resetboard(){ 	//use form reset event to clear board and reset variables
 	randomnumber=0;		// declare this globally
	winnerstatus=0;		// set winner status to 0
	turns=0;
        response='0';	// set response to wrong

	document.getElementById('a1').style.backgroundColor='white';
	document.getElementById('b1').style.backgroundColor='white';
	document.getElementById('c1').style.backgroundColor='white';
	document.getElementById('a2').style.backgroundColor='white';
	document.getElementById('b2').style.backgroundColor='white';
	document.getElementById('c2').style.backgroundColor='white';
	document.getElementById('a3').style.backgroundColor='white';
	document.getElementById('b3').style.backgroundColor='white';
	document.getElementById('c3').style.backgroundColor='white';

// reset any  O's and X's to z, invisible as the A tag is set to white on white
document.getElementById('a1').firstChild.firstChild.nodeValue='z';
document.getElementById('b1').firstChild.firstChild.nodeValue='z'
document.getElementById('c1').firstChild.firstChild.nodeValue='z'
document.getElementById('a2').firstChild.firstChild.nodeValue='z'
document.getElementById('b2').firstChild.firstChild.nodeValue='z'
document.getElementById('c2').firstChild.firstChild.nodeValue='z'
document.getElementById('a3').firstChild.firstChild.nodeValue='z'
document.getElementById('b3').firstChild.firstChild.nodeValue='z'
document.getElementById('c3').firstChild.firstChild.nodeValue='z'

// reset textcolour to navy
for (i=1;i<=maxterms;i++)
{temp='question'+i;document.getElementById(temp).style.color='navy';}

}

function unselected(x){
   if(response=='1') //ensure player takes their move
        {
         //make drop box lose focus
         document.getElementById('resetbutton').focus();
         alert('Click on a Square to take the move you won')}
   else {     
         if (x.selectedIndex !=0)
            {//window.focus();
         document.getElementById('resetbutton').focus();
             alert('You cannot change an answer');}
        }
}

function assess(answer){ // assess question
 // First we blur the current object to lock in the answer
 // we force loss of focus by sending it to reset button to prevent answer changes
         document.getElementById('resetbutton').focus();


// now check that the game is not over already
if (winnerstatus==1){alert('Game over, Press the New Game Button')}
else {

    // store option value 1 or 0 in response
     response = answer.options[answer.selectedIndex].value;

    //check response, increment score if correct and display result
    if (response=='1')
           {answer.style.color='green';
            alert("Correct, pick a square");
            player=1;}     // players turn
    else   {answer.style.color='red';
            alert ("Incorrect, lose a turn");  //computers turn
            player=2;    // computers turn
            ai();}
   }
}

// cell are named as per spreadsheet layout column letter, row number

function allowplay(x){
	// is player allowed a turn ?
	if (response=='1'){emptycell(x)}
	else {alert("You must answer a question correctly to win a move")}
}

function emptycell(x){      // is the players selected cell, id=x, empty ?

	 if (document.getElementById(x).style.backgroundColor == 'white')
	        {player=1;		// player one needed for colour
	          colourcell(x);}	//okay cell empty - run whose turn is it 
	else {alert("this cell has been filled, pick another")}	
}



function colourcell(x){ //change color of cell
    response='0';    // reset response so player doesn't get another go 	 
    document.getElementById(x).style.backgroundColor=play1color;
    document.getElementById(x).firstChild.firstChild.nodeValue='X';

    player=2;        // change from player to computers turn
    newwinner();  // do we have a winner
}

function colourcellcomputer(x){ //change color of cell
   document.getElementById(x).style.backgroundColor=play2color;
    document.getElementById(x).firstChild.firstChild.nodeValue='O';
   player=1;       // change from computer to players turn
   newwinner();  // do we have a winner
}

function newwinner(){
//assign a value to each row column and diagonal
// check rows
row1score=0;
 if (document.getElementById('a1').style.backgroundColor==play1color){row1score=row1score+1;}
 if (document.getElementById('a1').style.backgroundColor==play2color){row1score=row1score+4;}
 if (document.getElementById('b1').style.backgroundColor==play1color){row1score=row1score+1;}
 if (document.getElementById('b1').style.backgroundColor==play2color){row1score=row1score+4;}
 if (document.getElementById('c1').style.backgroundColor==play1color){row1score=row1score+1;}
 if (document.getElementById('c1').style.backgroundColor==play2color){row1score=row1score+4;}

row2score=0;
 if (document.getElementById('a2').style.backgroundColor==play1color){row2score=row2score+1;}
 if (document.getElementById('a2').style.backgroundColor==play2color){row2score=row2score+4;}
 if (document.getElementById('b2').style.backgroundColor==play1color){row2score=row2score+1;}
 if (document.getElementById('b2').style.backgroundColor==play2color){row2score=row2score+4;}
 if (document.getElementById('c2').style.backgroundColor==play1color){row2score=row2score+1;}
 if (document.getElementById('c2').style.backgroundColor==play2color){row2score=row2score+4;}

row3score=0;
 if (document.getElementById('a3').style.backgroundColor==play1color){row3score=row3score+1;}
 if (document.getElementById('a3').style.backgroundColor==play2color){row3score=row3score+4;}
 if (document.getElementById('b3').style.backgroundColor==play1color){row3score=row3score+1;}
 if (document.getElementById('b3').style.backgroundColor==play2color){row3score=row3score+4;}
 if (document.getElementById('c3').style.backgroundColor==play1color){row3score=row3score+1;}
 if (document.getElementById('c3').style.backgroundColor==play2color){row3score=row3score+4;}

// check columns
col1score=0;
 if (document.getElementById('a1').style.backgroundColor==play1color){col1score=col1score+1;}
 if (document.getElementById('a1').style.backgroundColor==play2color){col1score=col1score+4;}
 if (document.getElementById('a2').style.backgroundColor==play1color){col1score=col1score+1;}
 if (document.getElementById('a2').style.backgroundColor==play2color){col1score=col1score+4;}
 if (document.getElementById('a3').style.backgroundColor==play1color){col1score=col1score+1;}
 if (document.getElementById('a3').style.backgroundColor==play2color){col1score=col1score+4;}

col2score=0;
 if (document.getElementById('b1').style.backgroundColor==play1color){col2score=col2score+1;}
 if (document.getElementById('b1').style.backgroundColor==play2color){col2score=col2score+4;}
 if (document.getElementById('b2').style.backgroundColor==play1color){col2score=col2score+1;}
 if (document.getElementById('b2').style.backgroundColor==play2color){col2score=col2score+4;}
 if (document.getElementById('b3').style.backgroundColor==play1color){col2score=col2score+1;}
 if (document.getElementById('b3').style.backgroundColor==play2color){col2score=col2score+4;}

col3score=0;
 if (document.getElementById('c1').style.backgroundColor==play1color){col3score=col3score+1;}
 if (document.getElementById('c1').style.backgroundColor==play2color){col3score=col3score+4;}
 if (document.getElementById('c2').style.backgroundColor==play1color){col3score=col3score+1;}
 if (document.getElementById('c2').style.backgroundColor==play2color){col3score=col3score+4;}
 if (document.getElementById('c3').style.backgroundColor==play1color){col3score=col3score+1;}
 if (document.getElementById('c3').style.backgroundColor==play2color){col3score=col3score+4;}

diag1score=0;
 if (document.getElementById('a1').style.backgroundColor==play1color){diag1score=diag1score+1;}
 if (document.getElementById('a1').style.backgroundColor==play2color){diag1score=diag1score+4;}
 if (document.getElementById('b2').style.backgroundColor==play1color){diag1score=diag1score+1;}
 if (document.getElementById('b2').style.backgroundColor==play2color){diag1score=diag1score+4;}
 if (document.getElementById('c3').style.backgroundColor==play1color){diag1score=diag1score+1;}
 if (document.getElementById('c3').style.backgroundColor==play2color){diag1score=diag1score+4;}

diag2score=0;
 if (document.getElementById('c1').style.backgroundColor==play1color){diag2score=diag2score+1;}
 if (document.getElementById('c1').style.backgroundColor==play2color){diag2score=diag2score+4;}
 if (document.getElementById('b2').style.backgroundColor==play1color){diag2score=diag2score+1;}
 if (document.getElementById('b2').style.backgroundColor==play2color){diag2score=diag2score+4;}
 if (document.getElementById('a3').style.backgroundColor==play1color){diag2score=diag2score+1;}
 if (document.getElementById('a3').style.backgroundColor==play2color){diag2score=diag2score+4;}

// evaluate board 
message='\nGame Over, Press the New Game button to play again.'
 if (row1score==3||row2score==3||row3score==3){alert('You have a winning row'+message); winnerstatus=1;}
 if (row1score==12||row2score==12||row3score==12){alert('The computer has a winning row'+message); winnerstatus=1;}
 if (col1score==3||col2score==3||col3score==3){alert('You have a winning column'+message); winnerstatus=1;}
 if (col1score==12||col2score==12||col3score==12){alert('The computer has a winning column'+message); winnerstatus=1;}
 if (diag1score==3||diag2score==3){alert('You have a winning diagonal'+message); winnerstatus=1;}
 if (diag1score==12||diag2score==12){alert('The computer has a winning diagonal'+message); winnerstatus=1;}

turns=turns+1;    //  add a turn to number of turns
//alert('turn number '+turns+'player is '+player);
  //  if turns ==9 its a draw, game over reset game
if      (turns==9&&winnerstatus!=1)
        {winnerstatus=1;    // game over set winnerstatus to 1 to stop more questions being answered 
          alert('Its a Draw, Press New game to play again.');} 

else {// no win or draw let computer have a go 
      // First lets stop possible two row column or diagonal wins
            // Rule 0 Lets stop two in a row 1 but only if game not over
               if (row1score==2&&winnerstatus!=1&&player==2){stopwin('a1','b1','c1');}
                  // stop two in a row 2
               if (row2score==2&&winnerstatus!=1&&player==2){stopwin('a2','b2','c2');}
                  // stop two in a row 3
               if (row3score==2&&winnerstatus!=1&&player==2){stopwin('a3','b3','c3');}
                  // stop two in a column 1
               if (col1score==2&&winnerstatus!=1&&player==2){stopwin('a1','a2','a3');}
                  // stop two in a column 2
               if (col2score==2&&winnerstatus!=1&&player==2){stopwin('b1','b2','b3');}
                  // stop two in a column 3
               if (col3score==2&&winnerstatus!=1&&player==2){stopwin('c1','c2','c3');}
                  // stop two in a diagonal 1
               if (diag1score==2&&winnerstatus!=1&&player==2){stopwin('a1','b2','c3');}
                  // stop two in a diagonal 2
               if (diag2score==2&&winnerstatus!=1&&player==2){stopwin('c1','b2','a3');}

              // if still player2 and game not over - find another computer move
             if (player==2&&winnerstatus!=1)
                  {ai(); }// get a random computer move
          
       }

}

function stopwin(a,b,c){ // a,b,c are the cells totalling 2  
  // find white cell and colour it to stop player win

  if (document.getElementById(a).style.backgroundColor=='white'){colourcellcomputer(a);}
  if (document.getElementById(b).style.backgroundColor=='white'){colourcellcomputer(b);}
  if (document.getElementById(c).style.backgroundColor=='white'){colourcellcomputer(c);}
}

function ai(){  // this is where the computer smarts are to go

//okay can computer take the center or top left
	// Rule 1 is center taken ?
	if (document.getElementById("b2").style.backgroundColor == 'white')
		{colourcellcomputer("b2");}	 // take center
		 	
	else{// Rule 2 if center taken is a1 available
		if (document.getElementById("a1").style.backgroundColor == 'white')
		       {colourcellcomputer("a1");}	// take a1
		else{randnum2();}  	// don't know what else to do - pick a square at random!
	      }
}

function randnum2(){   // a random number generator function
	var randomnumber=Math.round(Math.random()*8+1);
	// alert ("new random number generator"+randomnumber);
	randomcell(randomnumber);
}

function randomcell(y){
// create cellref as a new string for each case
 cellref=' ';
 switch (y)
	{ 
	case 1 :	cellref = "a1";
			break; 
	case 2 :	cellref ="b1";
			break; 
	case 3 :	cellref = "c1";
			break; 
	case 4 :	cellref = 'a2';
			break; 
	case 5 :	cellref = 'b2';
			break; 
	case 6 :	cellref = 'c2';
			break; 
	case 7 :	cellref = 'a3';
			break; 
	case 8 :	cellref = 'b3';
			break; 
	case 9 :	cellref = 'c3';
			break; 
	} 

	emptycomputercell(cellref);  //call function that checks if computer's choice of cell is empty
}

function emptycomputercell(x){// is the computers selected cell empty ?

     if (document.getElementById(x).style.backgroundColor == 'white')
	{colourcellcomputer(x);}    //okay cell empty colour it 
     else	{randnum2();}	// cell must have been used, get another random number 
}
