var currentindex=null; //whichlink() is always called when a file loads into the contentsframe and whichlink() sets currentindex.
var previousindex=null;
var previousyouarehereid = 'algebrafractionsyouarehere';
var firstindex=5; //there are separate sign in and sign out links with display turned on or off in passwordentry.js (activate/deactivate)
                  //and now there is also a level link 
                  // and now there is a sample link
var maindirectory = top.location.href.substring(0,top.location.href.lastIndexOf("/")+1);

/*
The gotonextsection and gobackonesection functions are called from buttons.html when the user presses a "button" that is 
really a link. If "samplenextbuttonon" is true then the user has signed in and these functions are used. If not, then 
the "button" in buttons.html handle the loading of the new section without ever calling these functions. 

These functions do not take the user to the sign in page. They just loop around from link #2 on the navigation page to the
last link and back. However these functions will work fine FROM the sign in page and will just put the user into the loop 
comprising the actual question sections. 

Everything is now set up so that when a visitor hits the "go to questions" button on the main site page, the fractions questions
are immediately loaded. I have dispensed completely with the instructions page and given up the cute little godzilla question because
many users were not getting to the fractions section.
*/

function gotonextsection()
{  
   if (!top.allframes_loaded()) {alert('Wait a moment and try again. Some files are still loading.'); return;}
   
   var linksarrayref = top.navigationframe.document.links;    
   var nextindex;
   var lastindex = linksarrayref.length - 1;
   var documentref = top.buttonsandcontentsframeset.buttonsframe.document;
   
   //alert('in gotonextsection currentindex = ' + currentindex);
   
   if (currentindex == 1) //sign in page in contentsframe
   {
 	  nextindex = firstindex; // put user in first section
   }
   else if (currentindex < lastindex) 
   {
      nextindex = currentindex + 1;
   }
   else 
   {
      nextindex = firstindex;
   } 
   
   //skip index 0 because it's the home link
   
   top.buttonsandcontentsframeset.contentsframe.location.href = linksarrayref[nextindex].href; 
}

function gobackonesection()
{
   if (!top.allframes_loaded()) {alert('Wait a moment and try again. Some files are still loading.'); return;}
   
   var linksarrayref = top.navigationframe.document.links;    
   var oneindexback;
   var lastindex = linksarrayref.length - 1;
   var documentref = top.buttonsandcontentsframeset.buttonsframe.document;
   
   if (currentindex > firstindex) 
   {
      oneindexback = currentindex - 1;
   }
   else 
   {
      oneindexback = lastindex;
   } 
   
   top.buttonsandcontentsframeset.contentsframe.location.href = linksarrayref[oneindexback].href; 
}

var numberofwhichlinktries = 0;

function whichlink()
{
   if (numberofwhichlinktries>200) {return;}
   
   var loaded = false;
   loaded = top.allframes_loaded();
   
   if (!loaded) 
   {
      numberofwhichlinktries++;
      setTimeout('whichlink()',200);
      return;
   }
   numberofwhichlinktries = 0;
   //now okay to do the function don't know why I don't use the top.allframesloaded flag as in the functions above
   //maybe afraid on reload top variables might not reset? maybe should run the allframe_loaded function for the 
   //button pressing (above two functions) also because asking are all frames loaded now and were all frames loaded
   //at one time are different questions. the allframesloaded flag doesn't get reset onunload. could reset the
   //allframesloaded flag whenever anyone unloads. but then it might not get reset. 
   
   //another solved problem was that not every browser has onloads for frames, just for framesets and documents.
   //so only four flags not six.
 
  var linksarrayref = top.navigationframe.document.links;
  var contentsframeurl = top.buttonsandcontentsframeset.contentsframe.location.href;//okay because whichlink is called by 
  //onload event handler of file being loaded into contentsframe.
  
  var numberoflinks = linksarrayref.length;
  var filenamejustloaded = contentsframeurl.substring(contentsframeurl.lastIndexOf("/")+1,contentsframeurl.length);
  
  for (var index=0; index<numberoflinks; index++)
  {  
     var linksarrayurl = linksarrayref[index].href;  
     var filenamefromlinksarray = 
     linksarrayurl.substring(linksarrayurl.lastIndexOf("/")+1,linksarrayurl.length); //works on comp. and online
     //alert('index is ' + index + ' filename is ' + filenamefromlinksarray);
     
     if (filenamejustloaded == filenamefromlinksarray)
     {
         if (currentindex && (previousindex!=currentindex)) {previousindex = currentindex;} //save before changing
         //alert(previousindex);
         currentindex = index;
         //alert ('about to set output box');
         setoutputbox();
         setyouarehere(filenamejustloaded);
         if (top.navigationframe.difficultylevel == 1) {setcheckeachbutton();}
         else if (top.navigationframe.difficultylevel == 2) {setcheckallbutton();}
         else {setchecktenbutton();}
         return;
     }
  }
  
}

var whichbutton;

function setchecktenbutton()
{
   whichbutton = 'checktenbutton';
   var buttonsref = top.buttonsandcontentsframeset.buttonsframe.document;
   buttonsref.getElementById('checktenbutton').style.display = "block";
   buttonsref.getElementById('checkeachbutton').style.display = "none";
   buttonsref.getElementById('checkallbutton').style.display = "none";
}

function setcheckeachbutton()
{
   whichbutton = 'checkeachbutton';
   var buttonsref = top.buttonsandcontentsframeset.buttonsframe.document;
   buttonsref.getElementById('checkeachbutton').style.display = "block";
   buttonsref.getElementById('checktenbutton').style.display = "none";
   buttonsref.getElementById('checkallbutton').style.display = "none";
}

function setcheckallbutton()
{
   whichbutton = 'checkallbutton';
   var buttonsref = top.buttonsandcontentsframeset.buttonsframe.document;
   buttonsref.getElementById('checkallbutton').style.display = "block";
   buttonsref.getElementById('checktenbutton').style.display = "none";
   buttonsref.getElementById('checkeachbutton').style.display = "none";
}

function setyouarehere(currentfile)
{  
   //called from whichlink so don't need to check loading 
   var filenamenoextension = currentfile.substring(0,currentfile.lastIndexOf(".")); 
   var currentyouarehereid = filenamenoextension + 'youarehere';   
   var documentref = top.navigationframe.document;
   
   //alert(currentindex);
   
   documentref.links[currentindex].style.color = "rgb(120,30,0)";
   
   //alert(previousindex);
   
   if (previousindex && (previousindex!=currentindex)) {documentref.links[previousindex].style.color = "blue";}
    
   documentref.getElementById(previousyouarehereid).src = maindirectory + 'arrow2_blank.gif';
   
   //alert(previousyouarehereid);
   //alert(documentref.getElementById(previousyouarehereid).src);
   
   documentref.getElementById(currentyouarehereid).src = maindirectory + 'arrow2_full.gif';
   
   //alert(currentyouarehereid);
   //alert(documentref.getElementById(previousyouarehereid).src);
   
  // scrollBy(0,0);
   
   //alert('screwed up'); 
   
   previousyouarehereid = currentyouarehereid;
}

var globalresponsestring = '';
var characternumber=0;
var documentrefprocessresponse;
var processresponseid = null;
var delayresponse=false;

function processresponse(responsestring)
{  
   if (processresponseid) {stopprocessing();} //if already processing stop before starting new one
   
   documentrefprocessresponse = top.buttonsandcontentsframeset.buttonsframe.document;
   globalresponsestring = responsestring;
   
   documentrefprocessresponse.getElementById('output').value = '';
   if (delayresponse)
   {
      processresponseid = setInterval('typeresponse();',35);
   }
   else
   {
      processresponseid = setInterval('typeresponse();',35);
   }
}

//it does matter, using top.allframesloaded doesn't save IE 5 mac when 
//you refresh during processresponse. get documentrefprocessresponse 
//doesn't exist error

function typeresponse()
{
   if (characternumber >= globalresponsestring.length || !top.allframes_loaded()) 
   {
      stopprocessing();
   }
   else
   {
      documentrefprocessresponse.getElementById('output').value = globalresponsestring.substring(0,characternumber+1);
      characternumber++;
   }
}

function stopprocessing()
{
      characternumber = 0;
      delayresponse = false; //should be okay because unless a slow response stops a process won't happen bec setoutputbox stops processing
      globalresponsestring = "";
      
      if (processresponseid) {clearInterval(processresponseid);}
      processresponseid = null;
      
}

function clearoutputbox()
{
   top.buttonsandcontentsframeset.buttonsframe.document.getElementById('output').value = "";
}

function setoutputbox()
{     
    // alert ('in setoutput box, currentindex = ' + currentindex);
      
      //called from whichlink so don't need to check loading 
      var documentref = top.buttonsandcontentsframeset.buttonsframe.document;
      
      var sectionnumber = currentindex - 4;
   
      if (processresponseid) {stopprocessing();}
   
      if (currentindex==1) 
      {  
         if (top.samplenextbuttonon)
         {
            
             documentref.getElementById('output').value = " Good luck!";
         
         }
         else
         {
            delayresponse = true;
            processresponse(' Type in your username and password.');
         }
      }
      else
      {  if (currentindex>=firstindex)
          { 
             if (top.navigationframe.difficultylevel == 3)
             {
                documentref.getElementById('output').value = " This is section number " + sectionnumber + ". You are using Level 3.";
             }
             else if (top.navigationframe.difficultylevel == 2)
             { 
                documentref.getElementById('output').value = " This is section number " + sectionnumber + ". You are using Level 2.";
             }  
             else
             {
                documentref.getElementById('output').value = " This is section number " + sectionnumber + ". You are using Level 1.";
             }
          }
          else
          { if (currentindex == 3)
            {         
                delayresponse=true;
             
                processresponse(" If you like the questions, you can get all 200 for $25!");
             }
             //if (currentindex == 6) //works with new navigation file that has fewer links
             //{  //alert("about to type");
             //   processresponse(" Try for 10 out of 10!");
             //}
             //else if (currentindex == 10 )
             //{
              //  processresponse(" CrushTheTest has twenty sections offering concentrated practice.");
             //}  
          }
      }
}

function shadebuttons(ref)
{ 
      
      if (!top.allframes_loaded()) {return false;}
      //var otherbuttonref;
     
      ref.style.background = 'black'; 
      ref.style.color='white';  
     
      if (ref.id == "questionscheckbutton")
      {
         top.buttonsandcontentsframeset.buttonsframe.document.getElementById(whichbutton).style.background = 'black';
         top.buttonsandcontentsframeset.buttonsframe.document.getElementById(whichbutton).style.color = 'white';
      }
      else
      { 
         top.buttonsandcontentsframeset.contentsframe.document.getElementById('questionscheckbutton').style.background = "black";
         top.buttonsandcontentsframeset.contentsframe.document.getElementById('questionscheckbutton').style.color = "white";   
      }
           
}
   
function restorebuttons(ref)
{
      if (!top.allframes_loaded()) {return false;}
      
      ref.style.background = 'rgb(230,150,50)'; 
      ref.style.color='black';  
    
      if (ref.id == "questionscheckbutton")
      {
         top.buttonsandcontentsframeset.buttonsframe.document.getElementById(whichbutton).style.background = 'rgb(230,150,50)';
         top.buttonsandcontentsframeset.buttonsframe.document.getElementById(whichbutton).style.color = 'black';
      }
      else
      {
         top.buttonsandcontentsframeset.contentsframe.document.getElementById('questionscheckbutton').style.background = "rgb(230,150,50)";
         top.buttonsandcontentsframeset.contentsframe.document.getElementById('questionscheckbutton').style.color = "black";        
      }
}


