/*
* Javascript internal search engine by Gary Stimson 2002
*
* This script utilises three string arrays to allow searching
* for keywords.
*/

/* uses docscript.js, printstyle.css, screenstyle.css, search_data.js */

// Below are the global variables:
var oResult;
var sText;
var lSuccess;
var sFound;
// ***

function doSearch()
{
  sText    = document.search.keySearch.value.toLowerCase(); // wont work in netscape!

  lSuccess = false;
  sFound   = '';
  if (sText != '')
  {
    makePage();
    startSearch();
    if (!lSuccess)
    {
      oResult.document.write('<Center>The search returned no results.<P></Center>');
    }
    finishPage();
  }
  else
  {
    alert('Please enter a search keyword.');
  }
  if (iBrowser != 2)
  {
    document.search.searchImage.src = sPPath + 'images/search_off.gif';
  }
}

function startSearch()
{
  var iPriIndex = 0;
  var iSecIndex = 0;
  var saTokens = new Object();

  if (sText.indexOf(' ') == -1)
  {
    checkWord(sText);
  }
  else  // i.e if the string contains many words...
  {
    while (iPriIndex < sText.length) // below will tokenise the string
    {
      if (sText.charAt(iPriIndex) == ' ')
      {
        checkWord(sText.substring(iSecIndex, iPriIndex));
        iSecIndex = iPriIndex + 1;
      }
      iPriIndex++;
    }
    checkWord(sText.substring(iSecIndex, iPriIndex));
  }
}

// Below gets the results:
function checkWord(sWord)
{
  for (iCount = 0; iCount <= iEntries-1; iCount++)
  {
     if (saKeywords[iCount].indexOf(sWord) != -1 && sFound.indexOf(iCount) == -1)
     {
       oResult.document.write ('<Li><A HREF=' + sRefPath + saPage[iCount] + ' target="opener">' +
                               saName[iCount] + '</A><BR>' + saNotes[iCount] + '<P>');
       sFound   = sFound + ' ' + iCount;
       lSuccess = true;
     }
  }
}

// Below generates the results page:
function makePage()
{
  var sWinStats  = 'toolbar=no,location=no,directories=no,status=no,menubar=no,height=300,width=300,scrollbars=yes,resizable=yes';
  var sTempStats = 'toolbar=no,location=no,directories=no,status=no,menubar=no,height=1,width=1,scrollbars=no,resizable=no';

  // Below disposes of the old window:
  if (iBrowser != 3)
  {
    if (oResult != null && iBrowser != 2)
    {
      oResult.close();
    }
    else
    {
      // Below creates the search results page and populates it:
      oResult = window.open ('','msgWindow',sTempStats);
      oResult.close();
    }
  }
  // Below creates the search results:
  oResult = window.open ('','msgWindow',sWinStats);
  oResult.opener = window;
  oResult.opener.name = 'opener';
  oResult.document.write('<html><head><LINK REL=STYLESHEET TYPE="text/css" HREF="' + sPPath + 'screenstyle.css" MEDIA="screen">' +
                         '<LINK REL=STYLESHEET TYPE="text/css" HREF="' + sPPath + 'printstyle.css" MEDIA="print">' +
                         '<title>Search Results</title></head>');
  oResult.document.write('<body Class="XP"><Table class="H1" width="100%"><TR><TD>&nbsp;' +
                         '<Img id="topImage" Src="' + sPPath + 'images/search_off.gif" align="absmiddle"></Img>&nbsp;' +
                         '<Font color=black>' + sSTitle + '</font></TD></TR></Table><P><SPAN ID=MainDoc>' +
                         '<DIV class="XPText"><P><CENTER><TABLE width="85%">' +
                         '<TD class="BlueLine" COLSPAN="2"></TD></TABLE></CENTER><P>');
}

// The function below generates the results page:
function finishPage()
{
  oResult.document.write('<CENTER><TABLE width="85%"><TD class="BlueLine" COLSPAN="2"></TD></TABLE></CENTER>' +
                         '<FORM><CENTER><INPUT type="button" value="Close" ' +
                         'onClick="self.close()"></CENTER></FORM></DIV>' +
                         '</Span></body></html>');
}
