// -------------------- Find in page begin ------------------
// Joduba: 03/02/2004
//
// Original from: http://www.jsmadeeasy.com/
// Tested on IE6, Mozilla Firebird.
// ----------------------------------------------------------

var NS4 = (document.layers);    // Which browser?
var IE4 = (document.all);

if (!IE4 && navigator.appCodeName=="Mozilla") {
	NS4=true;
}

var findinpage_win = window;    // window to search.
var findinpage_n   = 0;
var findinpage_msg = "Text not found";

function findInPage(str) {

  var txt, i, found;

  if (str == "")
    return false;

  // Find next occurance of the given string on the page, wrap around to the
  // start of the page if necessary.

  if (NS4) {
 
    // Look for match starting at the current point. If not found, rewind
    // back to the first match.

    if (!findinpage_win.find(str))
      while(findinpage_win.find(str, false, true))
        findinpage_n++;
    else
      findinpage_n++;

    // If not found in either direction, give message.

    if (findinpage_n == 0)
      alert(findinpage_msg);
  }

  if (IE4) {
    txt = findinpage_win.document.body.createTextRange();

    // Find the nth match from the top of the page.

    for (i = 0; i <= findinpage_n && (found = txt.findText(str)) != false; i++) {
      txt.moveStart("character", 1);
      txt.moveEnd("textedit");
    }

    // If found, mark it and scroll it into view.

    if (found) {
      txt.moveStart("character", -1);
      txt.findText(str);
      txt.select();
      txt.scrollIntoView();
      findinpage_n++;
    }

    // Otherwise, start over at the top of the page and find first match.

    else {
      if (findinpage_n > 0) {
        findinpage_n = 0;
        findInPage(str);
      }

      // Not found anywhere, give message.

      else
        alert(findinpage_msg);
    }
  }

  return false;
}

