// version war2.js 210303
// All code and interface design are copyright Mike Capstick 2002/3
// but if you want to use it you probably only have to ask   ;-)
// global variables
var response='0';       // set response to wrong
var hits=0;             // used to count hits
var questions=0;        // number of questions in this quiz (review)
ammo=maxterms;

function loadquestions(){
   // build questions and distractors into a string and randomise location of correct answer

   selectstring='';
   for (i=0;i<maxterms;i++)  //loop through questions
       {
          stem='q'+(i+1);  //build stem id

          // build stem and start select boxes
          selectstring=selectstring+"<span id='"+stem+"'>"+stemarray[i]+"</span> "
          selectstring=selectstring+"<select onchange='assess(this);' size='1' onfocus='unselected(this)' "
          selectstring=selectstring+"id='question"+i+1+"'>";
          selectstring=selectstring+"<option value='0'></option>"

          // build the distractors A to D - 1st check if distractor is correct answer
          if (answer[i]=="A"){value=1} else{value=0}
          optionA="<option value='"+value+"'>"+adistract[i]+"</option>"

          if (answer[i]=="B"){value=1} else{value=0}
          optionB="<option value='"+value+"'>"+bdistract[i]+"</option>"

          if (answer[i]=="C"){value=1} else{value=0}
          optionC="<option value='"+value+"'>"+cdistract[i]+"</option>"

          if (answer[i]=="D"){value=1} else{value=0}
          optionD="<option value='"+value+"'>"+ddistract[i]+"</option>"

          selectstring=selectstring+optionA+optionB+optionC+optionD+"</select> "+tail[i]+"<br />"

       }
   document.getElementById('questionsdiv').innerHTML=selectstring
   //quiz title
   document.getElementById('title').firstChild.nodeValue=title;
}


function resetgame(){
 response='0';  	// set response to wrong
 hits=0;        	// used to count hits
 questions=0;   	// set number of questions to 0
        document.getElementById('a1').style.visibility='hidden';
        document.getElementById('b1').style.visibility='hidden';
        document.getElementById('c1').style.visibility='hidden';
        document.getElementById('d1').style.visibility='hidden';
        document.getElementById('a2').style.visibility='hidden';
        document.getElementById('b2').style.visibility='hidden';
        document.getElementById('c2').style.visibility='hidden';
        document.getElementById('d2').style.visibility='hidden';
        document.getElementById('a3').style.visibility='hidden';
        document.getElementById('b3').style.visibility='hidden';
        document.getElementById('c3').style.visibility='hidden';
        document.getElementById('d3').style.visibility='hidden';
        document.getElementById('a4').style.visibility='hidden';
        document.getElementById('b4').style.visibility='hidden';
        document.getElementById('c4').style.visibility='hidden';
        document.getElementById('d4').style.visibility='hidden';

}

// The unselected function stops users from changing a selected answer
// It is called by an onfocus event. If an answer was picked it focus is sent to the window

function unselected(x){
   
   if(response=='1') //check the player has fired their correct amswer shot 
      {document.getElementById('resetbutton').focus(); //make drop box lose focus by focussing elsewhere
       //alert('You must fire a shot by Clicking  on a Map location')
       alertboxer('You must fire a shot by Clicking  on a Map location');
       } 
   else{
        if (x.selectedIndex !=0){
                document.getElementById('resetbutton').focus(); //make drop box lose focus by focussing elsewhere
	if (gametype=='army')  {alertboxer('That answer is locked in Soldier and cannot be changed !');}
	if (gametype=='fleet') {alertboxer('That answer is locked in Sailor and cannot be changed !');}
	if (gametype=='space') {alertboxer('That answer is locked in Space Cadet and cannot be changed !');}
        }
   }
}

function assess(answer){             // assess a selected response
 // First we blur the current object to lock in the answer
 // we force loss of focus to prevent changes by focusing on the reset button
        //window.focus(); didn't  work for ns7, needs focus 
        document.getElementById('resetbutton').focus(); 

      questions=questions+1; // increment question(ammo) count - see fleet destroyed section
    // answer = this,  passed from onChange event by user selection
    // stores option value, 1 or 0 (correct or incorrect), in variable   response
     response = answer.options[answer.selectedIndex].value;

    //check response, increment score if correct and display result
      if (response=='1')
          { answer.style.color='green';
              alertboxer("Correct, Target a location on the Map and Fire at will !");
          }
    else  { answer.style.color='red';
            alertboxer("Incorrect, Permission to fire denied !"); 
            if (questions==ammo&&hits!=7)  // are we out of ammo
               {outofammo();}
          }
    }

function outofammo(){ // check is no more questions available

        if (gametype=='army')
           { alertboxer("Looks like you're out of ammo Soldier !,<br /> Hit the New Game button to play again")}
        if (gametype=='fleet')  
           { alertboxer("Looks like you're out of ammo Sailor !,<br /> Hit the New Game button to play again");}
        if (gametype=='space')  
           { alertboxer("Looks like you're out of plasma bolts Space Cadet ,<br /> Hit the New Game button to play again");}
}

function allowplay(x){
 // is player allowed a turn ?
   if(response=='1')
       {// allowed a turn, has cell been targeted before
         if(document.getElementById(x).style.visibility=='visible')
              { alertboxer('This location has been attacked already, Try another');}
         else {fireshot(x)}
       }
   else{
	if (gametype=='army')
	   {alertboxer("Soldier, before you can fire a shot you must answer a question correctly")}
	if (gametype=='fleet')
           {alertboxer("Sailor, before you can fire a shot you must answer a question correctly")}
	if (gametype=='space')
           {alertboxer("Space cadet, before you can fire a shot you must answer a question correctly")}
	}
}

function fireshot(ship){//getting the value of a text node in a span with an id of  ship
                        // document.getElementById(ship).firstChild.nodeValue
        response='0'    // reset response to zero - must get another question right to fire again

        document.getElementById(ship).style.visibility="visible";
if (gametype=='army')
   {if (document.getElementById(ship).firstChild.nodeValue=="B")
        { alertboxer("Excellent, You hit part of their Military Base"); }
     if (document.getElementById(ship).firstChild.nodeValue=="M")
       {  alertboxer("You hit their Missile Launcher"); }
     if (document.getElementById(ship).firstChild.nodeValue=="S")
        { alertboxer("You Killed an enemy Soldier");}
     if (document.getElementById(ship).firstChild.nodeValue=="x")
        { alertboxer("You missed, Soldier");
          }
 
     // Is the army destroyed  - count hits if any cell has a vehicle that is hit
     if (document.getElementById(ship).firstChild.nodeValue=="B"||
         document.getElementById(ship).firstChild.nodeValue=="M"||
         document.getElementById(ship).firstChild.nodeValue=="S")
        {hits=hits+1;
          if (hits==7)
             {message=''
               message='The Army is destroyed.<br /> Congratulations on your promotion to Captain !'
               message=message+'<br /> Hit the New Game button to play again';

               alertboxer(message);
               resetgame() }
        }                  
               if (questions==ammo&&hits!=7)  // are we out of ammo
                   {outofammo();}
   }

if (gametype=='fleet')   
   {if (document.getElementById(ship).firstChild.nodeValue=="B")
           { alertboxer("Excellent, You hit their Battleship"); }
     if (document.getElementById(ship).firstChild.nodeValue=="M")
           { alertboxer("You hit their Mine Sweeper"); }
     if (document.getElementById(ship).firstChild.nodeValue=="S")
           { alertboxer("You destroyed an enemy Submarine"); }
     if (document.getElementById(ship).firstChild.nodeValue=="x")
           { alertboxer("You missed, Sailor"); }
                          
      // Is the naval fleet destroyed  - count hits if any cell has a ship that is hit
        if (document.getElementById(ship).firstChild.nodeValue=="B"||
           document.getElementById(ship).firstChild.nodeValue=="M"||
           document.getElementById(ship).firstChild.nodeValue=="S")
                {hits=hits+1;
                 if (hits==7){message=''
                              message='The Fleet is destroyed.<br /> Congratulations on your promotion to Captain !'
                              message=message+'<br /> Hit the New Game button to play again';
                             
                              alertboxer(message);
                              resetgame() }
                }
               if (questions==ammo&&hits!=7)  // are we out of ammo
                   {outofammo();}
      }

if (gametype=='space')  
   {if (document.getElementById(ship).firstChild.nodeValue=="B")
           { alertboxer("Excellent, You hit their Star Base"); }
     if (document.getElementById(ship).firstChild.nodeValue=="M")
           { alertboxer("You hit their Mind Control Ship"); }
     if (document.getElementById(ship).firstChild.nodeValue=="S")
           { alertboxer("You vapourised a Star Fighter"); }
     if (document.getElementById(ship).firstChild.nodeValue=="x")
           { alertboxer("You missed, Space cadet"); }
            
     // Is the space fleet destroyed  - count hits if any cell has a ship that is hit
        if (document.getElementById(ship).firstChild.nodeValue=="B"||
            document.getElementById(ship).firstChild.nodeValue=="M"||
            document.getElementById(ship).firstChild.nodeValue=="S")
                {hits=hits+1;
                 if (hits==7){message=''
                       message='The Alien Fleet is destroyed.<br /> Congratulations on your promotion to Space Captain !'
                       message=message+'<br /> Hit the New Game button to play again';
                       
                       alertboxer(message);
                       resetgame()}
                }
               if (questions==ammo&&hits!=7)  // are we out of ammo
                   {outofammo();}
      }
}
