// 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 =new Array(9);
matchcolour='wheat';
pickcolour=['green','blue','brown','darkorange','purple','darkslategray','darkgoldenrod', 
            'deepskyblue','teal','darkvioletred'];

function term(t){
 tempid="t"+t; // build up term id
 thet=t;   // store t in a global variable for use in defn function
 
//Check term hasn't already been matched
if  (document.getElementById(tempid).style.backgroundColor==matchcolour)
    {alertboxer('<h1>Information</h1>This Term has already been Matched,<br /> Try another one.')}
else{
     if  (termclicked==1)
         {alertboxer('<h1>Information</h1>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 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('<h1>Information</h1>This Definition has already been Matched<br /> Try another one.')}
else{
     if   (defnclicked==1)
          {alertboxer('<h1>Information</h1>You have already picked a Definition,<br />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

 if(a==x){soundplayer('correct1.swf');
          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('<h1>Correct !</h1><br /><img src="tick.gif" alt="" alt="Correct" />')
          complete();}
 else    {soundplayer('wrong1.swf');
          alertboxer('<h1>Incorrect !, Try Again</h1><br /><img src="cross.gif" alt="" alt="Correct" />')
          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;thet=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('<h1>Congratulations</h1>You Matched all the Terms !');
      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("<h1>You Ran out of Time</h1> Try again.</h2>");
           }
   else { 
           document.forms[0].elements[0].value=x;
            x=x-1;       //drop every second
           //run again every second
           timerid=setTimeout('timer()',1000); 
         }
}
