
//copyright Mike Capstick - 12/11/2003 update to multiple colours 
// 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 termdefn.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 
//pickcolour='green'
pickcolour =new Array(9);
matchcolour='goldenrod';
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){

//first check term hasn't already been matched
if ( document.getElementById(t).style.backgroundColor==matchcolour)
    {alert('This item has already been matched, pick another one.')}
else{
         if (termclicked==1){alert('You have already picked a country, pick a religion')}
         else{ 
                termclicked=1;  // term clicked flag
                document.getElementById(t).style.color='yellow';
                //document.getElementById(t).style.backgroundColor=matchcolour;
                 termvalue= t.substr(1);  //get id, number only, of clicked term
                 if (defnclicked==1){     
                     //if defn has also been clicked run match function?
                     match(termvalue,defnvalue)}
               }
       }
}

function defn(d){
//first check defn hasn't already been matched
if ( document.getElementById(d).style.backgroundColor==matchcolour)
    {alert('This item has already been matched, pick another one.')}
else{
        if (defnclicked==1){alert('You have already picked a religion, pick a country')}
        else {defnclicked=1;
                 document.getElementById(d).style.color='yellow';
                 //document.getElementById(d).style.backgroundColor=matchcolour;
                 defnvalue= d.substr(1);
                 if (termclicked==1){
                   // alert('defnvalue '+defnvalue+'termvalue'+termvalue);
                  match(termvalue,defnvalue) }
                
               }
      }
}

function match(a,b){  // where a is termvalue (id number part only) and b is defnvalue
// visual efect only

 if(a==b){
                 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];
                 alert('Correct')
                 complete();
	 
	}
 else  {alert('Incorrect,try again')
            document.getElementById('t'+a).style.color='white';
            document.getElementById('t'+a).style.backgroundColor='darkolivegreen';
            document.getElementById('d'+b).style.color='white';
            document.getElementById('d'+b).style.backgroundColor='darkolivegreen';

          }

 //reset variables
 termclicked=0 
 defnclicked=0 
 termvalue=0 
 defnvalue=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)
      {alert('Congratulations you matched all the terms! Click a button at the bottom of the screen to look at the photos, read a story, or go to another activity on Multicultural Education through Miniatures.');
        window.location.reload() 
       }

}

   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);
             alert("You ran out of time, try again");
              // reset game
             window.location.reload() 
           }
   else { 
           document.forms[0].elements[0].value=x
            x=x-1 ;       //drop every second
           //run again every second
           timerid=setTimeout('timer()',1000); 
         }

}