

/*
Find In Page Script- 
By Mike Hall (MHall75819@aol.com)
Permission granted to Dynamicdrive.com to feature script in archive
For full source code, visit http://dynamicdrive.com
*/

var NS4 = (document.layers);    // Which browser?
var IE4 = (document.all);

fdStyle=document.createStyleSheet();
fdStyle.addRule('.Find', 'text-align:center; position:absolute; cursor:pointer;width:200px; BORDER-BOTTOM:#000020 solid 2px;BORDER-LEFT:#d7ffff solid 2px;BORDER-RIGHT:#000020 solid 2px;BORDER-TOP:#d7ffff solid 2px;');

var win = window;    // window to search.
var n   = 0;

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 (!win.find(str))
      while(win.find(str, false, true))
        n++;
    else
      n++;

    // If not found in either direction, give message.

    if (n == 0)
      alert("\""+str+"\" werd niet gevonden!");
  }

  if (IE4) {
    txt = win.document.body.createTextRange();

    // Find the nth match from the top of the page.

    for (i = 0; i <= 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();
      n++;
    }

    // Otherwise, start over at the top of the page and find first match.

    else {
      if (n > 0) {
        n = 0;
        findInPage(str);
      }

      // Not found anywhere, give message.

      else 
		alert( "\"" + str + "\" werd niet gevonden!");
    }
  }

  return false;
}

// drag function

var dragapproved=false
var zcor,xcor,ycor
function moveFind(){
if (event.button==1&&dragapproved){
zcor.style.pixelLeft=tempvar1+event.clientX-xcor
zcor.style.pixelTop=tempvar2+event.clientY-ycor
leftpos=document.all.Find.style.pixelLeft-document.body.scrollLeft
toppos=document.all.Find.style.pixelTop-document.body.scrollTop
return false
}
}

function dragFind(){
if (!document.all)
return
if (event.srcElement.id=="Find"){
dragapproved=true
zcor=Find
tempvar1=zcor.style.pixelLeft
tempvar2=zcor.style.pixelTop
xcor=event.clientX
ycor=event.clientY
document.onmousemove=moveFind
}
}
document.onmousedown=dragFind
document.onmouseup=new Function("dragapproved=false")

// keep on same position

var w=document.body.clientWidth-215
var h=0
w+=document.body.scrollLeft
h+=document.body.scrollTop
var leftpos=w
var toppos=h
Find.style.left=w
Find.style.top=h

function staticize(){
w2=document.body.scrollLeft+leftpos
h2=document.body.scrollTop+toppos
Find.style.left=w2
Find.style.top=h2
}
window.onscroll=staticize

function replaceFind() {
document.all.Find.style.left=document.body.clientWidth-215
}
window.onresize=replaceFind

// Change background-color and cursor

function doColor(item, color, bg) {
	item.style.color = color; // changes text color
	item.style.backgroundColor = bg; // changes background
	item.style.cursor = 'hand'; // changes cursor
	item.style.textDecoration= 'underline'; 
}
function undoColor(item) {
	item.style.color = ""; // sets color back to default
	item.style.backgroundColor = ""; // sets background to default
	item.style.cursor = 'default'; // sets cursor to default
	item.style.textDecoration= ''; 
}
