/*
 (c)1999-2000 Open Objects Software
 Search scripts

 There are 2 types of search pages:
 1.Search Index Pages - these are the pages that the users first see when they hit a 'search' hyperlink etc.  They 
   only contain a search form.
 2.Search Results Pages - these are the pages that the users see after they've entered a search from a Search Index 
   page.  They contain both a search form, as well as navigation bars for sorting the results, hiding summaries, 
   going to the next results page etc.
 */

var inSearch = false;
var resPage = 0;
var sWords = new Array("of","the","in","from","etc","for","by","&","to","other","no.","at","if","it","on");


// Stop Words

function removeStopWords(string){
  var swString = "", word;
  if (string.length > 0){
    var wordsArray = string.split(" ");
    for (i=0;i<wordsArray.length;i++){
      word=wordsArray[i];
      if (word.length > 0){
        if (!isStopWord(word)){
          swString=swString + " " + word;
        }
      }
    }
  }
  return swString;
}


function isStopWord(string){
  var lcString=string.toLowerCase();
  for (j=0;j<sWords.length;j++){
    if (lcString == sWords[j]){
      return true;
    }
  }
  return false;
}



// Search


function performSearch(){
  if (inSearch){
    return;
  } else {
    inSearch = true;
    allocateResultsArray(0);
    setCurrentPage(0);

    var query = removeStopWords(getQueryText());

    if( query == null || query.length == 0 )  {
      query = "";
    }

    //Now initialize the applet
    parent.code.document.wrsearch.loadCollection(1);
    parent.code.document.wrsearch.setSortBy(sortTypes[getSortOrder()]);
    parent.code.document.wrsearch.setMaxHits(getMaxNumberOfHits());
    if(isMatchingAllSearchTerms()){
      parent.code.document.wrsearch.setDefaultOperator("and");
    } else {
      parent.code.document.wrsearch.setDefaultOperator("or");
    }

    //Configure callbacks for the highlighter applet
    if( navigator.appName.indexOf( "Netscape" ) != -1 && navigator.appVersion >= "4" ){
      parent.code.document.wrsearch.setUseCallbacks(false);
      parent.code.document.highlight.setUseCallbacks(false);
    }

    // return the number of results - in case of an error, we just say that we have no search results.
    var results = Math.max(0 , parent.code.document.wrsearch.startSearch(query));

    setWordsToHighlight(parent.code.document.wrsearch.getKeywords());
    populateResults(results);
    loadResultsPage(0);
    inSearch = false;
  }
}



function resultRecord (relevance, location, title, description) {
  this.relevance = relevance;
  this.location = location;
  this.title = title;
  this.description = description;
}



function populateResults(result){
  var wrSearch = parent.code.document.wrsearch;
    
  //Allocate room in the results array
  allocateResultsArray(result);

  for (i=0;i<result;i++){
    parent.resultsArray[i] = new parent.resultRecord(wrSearch.getHitField(i, "relevance"),
                                                         wrSearch.getHitField(i, "location"),
                                                         wrSearch.getHitTitle(i),
                                                         wrSearch.getHitField(i, "description"));
  }
}



function allocateResultsArray(size){
  resultsArray = new Array(size);
}



function findNamedWindow(startWindow, name){
  var frames = startWindow.frames;

  if (startWindow.name == name){
    return startWindow;
  } else if (frames.length == 0) {
    return null
  } else {
    for (i = 0; i < frames.length; i++){
      var win = findNamedWindow(frames[i], name);

      if ((win != null) && (win.name == name)){
        return win;
      } 
    }

    return null;
  }
}




//Utility fns for determining which is the first or last of the results pages
function isFirstSearchResultsPage(startResultForPage){
  return (0 == startResultForPage);
}


function isLastSearchResultsPage(endResultForPage, totalHits){
  return (endResultForPage == totalHits - 1);
}


function getFirstHitForPage(pageNumber, resultsPerPage){
  return (resultsPerPage * pageNumber);
}


function getLastHitForPage(pageNumber, startResult, resultsPerPage, numberOfSearchHits){
  return Math.min(numberOfSearchHits -1, startResult + resultsPerPage - 1);
}





// Eof
