
//
//      -- (c)  Copyright John Page 2007 -- 
//



// =================================
//      Browser detect
// =================================
function browserIsIE()
{  var browserName=navigator.appName; 
   return (browserName=="Microsoft Internet Explorer");
}



// =======================================================================
//      Initialize the page.  Called from the onload handler of most pages
// =======================================================================
function initPage(param)
{
//On IE, install the two bookmarking commands on the second command line
if(browserIsIE() && isHosted() ) //only IE has this I believe
  { var cmdDiv = document.getElementById('commandRow2');  //name of second command row
    if(cmdDiv) //fail safe if it happens to be missing
      { cmdDiv.innerHTML += "<a  class='navCmd3' href='javascript:bookmarkPageIE()'>Bookmark this page</a>";
        cmdDiv.innerHTML += "<a  class='navCmd3' href='javascript:bookmarkSiteIE()'>Bookmark this site</a>";
      }
  }
}


function isHosted()
// returns true if the site is hosted on a web server.  False if it is hosted on
// a file system.
{  var loc = location.href.toLowerCase(); 
   return (loc.substring(0,4) == "http");
}


function myUrl()
// returns the url of this page
{ return (window.location.href);
}



// =================================
//      Search Engine routines
// =================================

function validateKeyword(arg)

//  Checks 'keyword' against a list of known good keywords.
//  If it matches or nearly matches one of them, the index of the keyword is returned.
//  Otherwise -1 is returned.

{  var keyword = arg.toLowerCase();
   var smap = mapOf(keyword); 
   var dmap, closestWordIndex, bestScore=999, thisWord;

   for (var wordIndex=0;  wordIndex<dict.length;  wordIndex++)
   {  thisWord = dict[wordIndex];
      if (thisWord == keyword)  
         return wordIndex;
	  else
        { dmap = mapOf(thisWord);
          errors = compare(dmap, smap);
		  if(errors < bestScore)
		    { bestScore = errors;
			  closestWordIndex = wordIndex;
			}
	    }
   }
   var shortest = Math.min(keyword.length , dict[closestWordIndex].length);
   var allowableError = 2 + shortest/2; 
   if (bestScore < allowableError ) return closestWordIndex; else return -1;
}


function mapOf(s)
  { var c,  map = new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
    for ( var i=0; i<s.length; i++)
      { c=s.charCodeAt(i);
        if(c>=97 && c<=122)  // a ..z
            map[c-97]++; 
      }
	return map;
  }


function compare(dmap, smap)
{ var errors=0;
  for (var i=0; i<26; i++)
    {  errors += Math.abs(dmap[i]-smap[i]);
    }
  return errors;
}


function doSearch()
{  var inSubDir = document.getElementById('subdir');
   var prefix ="";
   if (inSubDir)  prefix ="../";
   
   var arg = document.getElementById("searchArg").value;
   var result = validateKeyword(arg);
   if (result == -1)  
      document.location.href = prefix + "common/notfound.html?"+arg;
   else
      document.location.href = prefix + "keywordindices/"+dict[result]+".html?"+arg;
}


function createTitle(dest)
// Stuff title into missing search page
{
    var urlStr = window.location.href;
    var paramLoc = urlStr.indexOf("?");
    if(paramLoc>0)
      var searchStr = urlStr.substr(urlStr.indexOf("?")+1);
    else
      var searchStr = "??";
    document.getElementById(dest).innerHTML = "Search for <span class='searchWord'>" +searchStr+ "</span>&nbsp; found no entries";
}






// =================================
//      Nav bar helpers
// =================================


function checkForEnterKey(e)
   { var key = window.event ? e.keyCode : e.which;
     if(key==13) doSearch();
   }


function bookmarkPageIE()
// IE only: create a bookmark for this page
{ if( window.external ) // it's IE
	 {  window.external.AddFavorite(document.location, document.title); 
	 }	
}


function bookmarkSiteIE()
// IE only: create a bookmark for this site
{ if( window.external ) // it's IE
	 {  window.external.AddFavorite("http://www.mathopenref.com", "Math Open Reference"); 
	 }	
}




// =================================
//      Question hide/reveal
// =================================

function reveal(obj)
{  document.getElementById(obj).style.visibility='visible';
}


function swap(me, obj)
{ me.style.display="none";
  document.getElementById(obj).style.display="inline";
}



// =================================
//       Timer functions
// =================================


function millisecTimeStamp()
//returns a new millsecond time stamp
{  var t = new Date();
   return t.getTime();
}





// =================================
//       Pop up calculator
// =================================

function startCalc()
{ //
  // disabled until we figure out the focus problems
  // window.open('calcsmall.html', 'mywindow', 'width=360,height=355,status=no,resizable=no')
  location.href="calculator.html";
}


function calcRollon(obj)
//when mouse rolls on
{ obj.style.textDecoration='underline'; 
  obj.style.color='blue' 
}


function calcRolloff(obj)
{ obj.style.textDecoration='none'; 
  obj.style.color='#966'
}