// 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

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';

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

}

// 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')} 
   else{
        if (x.selectedIndex !=0){
                document.getElementById('resetbutton').focus(); //make drop box lose focus by focussing elsewhere
	if (gametype=='army'){alert('That answer is locked in Soldier and cannot be changed !');}
	if (gametype=='fleet'){ alert('That answer is locked in Sailor and cannot be changed !');}
	if (gametype=='space'){ alert('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(); 

    // 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';
              alert("Correct, Target a location on the Map and Fire at will !")}

      else  { answer.style.color='red';
              alert("Incorrect, Permission to fire denied !");  }

      questions=questions+1; // increment question(ammo) count - see fleet destroyed section
    }

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')
              {alert('This location has been attacked already, Try another')}
         else {fireshot(x)}
       }
   else{
	if (gametype=='army')
	   {alert("Soldier, before you can fire a shot you must answer a question correctly")}
	if (gametype=='fleet')
           {alert("Sailor, before you can fire a shot you must answer a question correctly")}
	if (gametype=='space')
           {alert("Space cadet, before you can fire a plasma bolt 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")
        {alert("Excellent, You hit part of their Military Base");}
     if (document.getElementById(ship).firstChild.nodeValue=="M")
       {alert("You hit their Missile Launcher");}
     if (document.getElementById(ship).firstChild.nodeValue=="S")
        {alert("You Killed an enemy Soldier");}
     if (document.getElementById(ship).firstChild.nodeValue=="x")
        {alert("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.\n Congratulations on your promotion to Captain !'
                       message=message+'\n Hit the New Game button to play again';

                       alert(message)
                       resetgame() }
        }                  

        // are we out of ammo/questions
        if (questions==ammo&&hits!=7)
           {alert("Looks like you're out of ammo Soldier !,\n Hit the New Game button to play again");}
   }

if (gametype=='fleet')  
   {if (document.getElementById(ship).firstChild.nodeValue=="B")
           {alert("Excellent, You hit their Battleship");}
     if (document.getElementById(ship).firstChild.nodeValue=="M")
           {alert("You hit their Mine Sweeper");}
     if (document.getElementById(ship).firstChild.nodeValue=="S")
           {alert("You destroyed an enemy Submarine");}
     if (document.getElementById(ship).firstChild.nodeValue=="x")
           {alert("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.\n Congratulations on your promotion to Captain !'
                              message=message+'\n Hit the New Game button to play again';

                              alert(message)
                              resetgame() }
                }
     // are we out of ammo/questions
        if (questions==ammo&&hits!=7)
           {alert("Looks like you're out of ammo Sailor !,\n Hit the New Game button to play again");}
      }

if (gametype=='space')  
   {if (document.getElementById(ship).firstChild.nodeValue=="B")
           {alert("Excellent, You hit their Star Base");}
     if (document.getElementById(ship).firstChild.nodeValue=="M")
           {alert("You hit their Mind Control Ship");}
     if (document.getElementById(ship).firstChild.nodeValue=="S")
           {alert("You vapourised a Star Fighter");}
     if (document.getElementById(ship).firstChild.nodeValue=="x")
           {alert("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.\n Congratulations on your promotion to Space Captain !'
                       message=message+'\n Hit the New Game button to play again';

                       alert(message)
                       resetgame()}
                }
     // are we out of ammo/questions
        if (questions==ammo&&hits!=7)
           {alert("Looks like you're out of plasma bolts Space Cadet !,\n Hit the New Game button to play again");}
      }
}

