// adjusted 18/07/07 added tick and cross images to alert box, stopped game reload if timed out
// adjusted 29/05/06 rewritten to use a static html file and new question file structure
// adjusted 12/11/03 update to multiple colours 
// adjusted 12/11/03 to allow a 2 minute timer to be added
// adjusted 16/05/03 to do randomising of terms via javascript not perl
// adjusted 12/11/03 to allow a 2 minute timer to be added
// match.js - external javascript to support the Match the Term and Definitions file match.htm
// file do not adjust.

termclicked=0 // term has been selected
defnclicked=0 // definition has been selected
termvalue=0   // value of the term id without t 
defnvalue=0   // value of the term id without d 
thed=0        // holds the defn id 
//pickcolour='green'
pickcolour =new Array(9);
matchcolour='wheat';
pickcolour[0]='green';
pickcolour[1]='blue';
pickcolour[2]='brown';
pickcolour[3]='darkorange';
pickcolour[4]='purple';
pickcolour[5]='darkslategray';
pickcolour[6]='darkgoldenrod';
pickcolour[7]='deepskyblue';
pickcolour[8]='teal';
pickcolour[9]='darkvioletred';

function term(t){
 tempid="t"+t // build up term id
 
//Check term hasn't already been matched
if  (document.getElementById(tempid).style.backgroundColor==matchcolour)
    {alertboxer('This Term has already been Matched, try another one.')}
else{
     if  (termclicked==1)
         {alertboxer('You have already picked a Term, Pick a Definition')}
     else{ 
          termclicked=1;  // term clicked flag
          document.getElementById(tempid).style.color='yellow';
          termvalue= t;  //get id, number only, of clicked term

          if (defnclicked==1)
             { //if defn has also been clicked run match function?
               // send termid to match the defnvalue and send the thed to restyle
               match(t,thed,defnvalue)
             }
          }
    }
}

function defn(d){
  tempid="d"+numbers[d]; //build up defn id
  thed=d   // store d in a global variable for use in term function
  
//Check defn hasn't already been matched
if  (document.getElementById("d"+d).style.backgroundColor==matchcolour)
    {alertboxer('This Definition has already been Matched, Try another one.')}
else{
     if   (defnclicked==1)
          {alertboxer('You have already picked a Definition, Pick a Term')}
     else {defnclicked=1;
           document.getElementById("d"+d).style.color='yellow';
           defnvalue= numbers[d];
                
           if (termclicked==1)
              {match(termvalue,d,defnvalue)}
          }
    }
}

function match(a,b,x){  
   // where a is termvalue (id number part only) and b is defnvalue
   // d is the number of the definition element id
   // visual efect only

 if(a==x){document.getElementById('t'+a).style.backgroundColor=matchcolour;
          document.getElementById('t'+a).style.color=pickcolour[a-1];
          document.getElementById('d'+b).style.backgroundColor=matchcolour;
          document.getElementById('d'+b).style.color=pickcolour[a-1];
          alertboxer('<br /><img src="tick.gif" alt="" alt="Correct" /><br />Correct !')
          complete();}
 else    {alertboxer('<br /><img src="cross.gif" alt="" alt="Correct" /><br />Incorrect !, Try again ')
          document.getElementById('t'+a).style.color='white';
          document.getElementById('d'+b).style.color='white';
         }

 //reset variables
 termclicked=0 
 defnclicked=0 
 termvalue=0 
 defnvalue=0 
 thed=0
 a=0;b=0;d=0
}

function complete(){
  finish=1;
  for (i=1;i<=maxterms;i++)
      {if (document.getElementById('t'+i).style.backgroundColor!=matchcolour)
          {finish=0} //set flag if any term is unmatched
      }
  if (finish==1)
     {alertboxer('<h2>Congratulations<br />You Matched all the Terms !</h2>');
      if (timerflag==1){clearTimeout(timerid);}
     }
}

   x=120            // 120 second countdown
   y=0            // stop time in 120 secs

function timer(){
   // display current time
   if (x<y)  //time's up
           {clearTimeout(timerid);
             alertboxer("<h2>You ran out of Time,<br /> Try again.</h2>");
              // reset game
    //         window.location.reload()   // dont restart if out of time
           }
   else { 
           document.forms[0].elements[0].value=x
            x=x-1 ;       //drop every second
           //run again every second
           timerid=setTimeout('timer()',1000); 
         }

}