// Copyright Neil Ramsden 2004. All rights reserved.

// Word Search scripts
// Neil Ramsden
// search.js
// 5 Oct 04 -> 11 Oct 04

//---------------------------------------------------------------------------

/* Helper Functions
*/


function writeHTMLPrologue(document, title)
//write markup for HTML head section plus body tag
//  document -- target window or frame document
{
  //start cleanly
  document.close();
  document.write();

  //write markup
  var prologue =
'<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">' +
'<html>' +
  '<head>' +
  '  <meta http-equiv="content-type"' +
  '     content="text/html; charset=ISO-8859-1">' +
  '  <title>' + title + '</title>' +
  '  <meta http-equiv="Content-Script-Type" content="text/javascript">' +
  '  <link href="../shared/common.css" rel="stylesheet" type="text/css">' +
  '  <link href="pane.css" rel="stylesheet" type="text/css">' +
  '</head>' +
  '<body>';
  document.write(prologue);
}

function writeHTMLEpilogue(document)
//write final HTML markup including end body tag
//  document -- target window or frame document
{
  var epilogue =
  '</body>' +
'</html>';
  document.write(epilogue);
  document.close();
}


function doSearch(pattern)
//search for pattern and write results to new window
{
  // clear results window
  var result = parent.frame_1;
  result.location = 'blank.html';

  // find matches
  var expression = RegExp(pattern, 'i');
  var matches = Array();
  var word_count = wordList.length;
  for (index = 0; index < word_count; index ++)
  {
    word = wordList[index];
    if (word.search(expression) != -1)
      matches.push(word);
  }

  // write matches to results window  
  var match_count = matches.length;
  writeHTMLPrologue(result.document, 'Search Results');
  result.document.write('<div class="pane-header">Search Results for "' + pattern +
    '"<br>(' + match_count + ' matches) </div>')
  for (index = 0; index < match_count; index ++)
  {
    /*note: use <div> rather than inline+<br> as latter v slow to display;
            need <div> and/or trailing space to avoid one (long) line result
    */
    word = matches[index];
    result.document.write('<div class="found-word">' + word + ' </div>');
  }
  writeHTMLEpilogue(result.document);
}


function resetSearcher()
//reset main form and clear results pane.
{
  parent.frame_0.document.MainForm.reset();
  parent.frame_1.location = 'blank.html';
}