{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}}
{\*\generator Msftedit 5.41.15.1507;}\viewkind4\uc1\pard\f0\fs20 /* This script and many more are available free online at\par
The JavaScript Source!! http://javascript.internet.com\par
Created by: Robin Winslow | http://www.robinwinslow.me.uk */\par
// Find and select all text in the document that matches the "value" of the element passed\par
// If no element if passed, it will attempt to use the value of "this"\par
// Only works in a browser that supports the "window.getSelection" method, as other selection methods don't support multiple selectionsz\par
function performMultiSearch(elem,searchElem) \{\par
    // set up variables\par
    var searchString; // Will hold the text to search for\par
    var theSelection; // Will hold the document's selection object\par
    var textNodes; // Will hold all the text nodes in the document\par
\par
    // Set it to search the entire document if we haven't been given an element to search\par
    if(!searchElem || typeof(searchElem) == 'undefined') searchElem = document.body;\par
\par
    // Get the string to search for\par
    if(elem && elem.value) searchString = elem.value;\par
    else if(this && this.value) searchString = this.value;\par
\par
    // Get all the text nodes in the document\par
    textNodes = findTypeNodes(searchElem,3);\par
\par
    // Get the selection object\par
    if(window.getSelection) theSelection = window.getSelection(); // firefox\par
    else \{ // some other browser - doesn't support multiple selections at once\par
        alert("sorry this searching method isn't supported by your browser");\par
        return;\par
    \}\par
\par
    // Empty the selection\par
    theSelection.removeAllRanges(); // We want to empty the selection regardless of whether we're selecting anything\par
\par
    if(searchString.length > 0) \{ // make sure the string isn't empty, or it'll crash.\par
        // Search all text nodes\par
        for(var i = 0; i < textNodes.length; i++) \{\par
            // Create a regular expression object to do the searching\par
            var reSearch = new RegExp(searchString,'gmi'); // Set it to 'g' - global (finds all instances), 'm' - multiline (searches more than one line), 'i' - case insensitive\par
            var stringToSearch = textNodes[i].textContent;\par
            while(reSearch(stringToSearch)) \{ // While there are occurrences of the searchString\par
                // Add the new selection range\par
                var thisRange = document.createRange();\par
                thisRange.setStart(textNodes[i],reSearch.lastIndex - searchString.length); // Start node and index of the selection range\par
                thisRange.setEnd(textNodes[i],reSearch.lastIndex); //  End node and index of the selection\par
                theSelection.addRange(thisRange); // Add the node to the document's current selection\par
            \}\par
        \}\par
    \}\par
\par
    return;\par
\}\par
\par
// Will find and select the first instance of the value of the passed element, then when called again will moves on to the next instance\par
// If no element passed it will try to get the value from "this"\par
function performSingleSearch(elem,searchElem) \{\par
    // set up variables\par
    var searchString; // Will hold the text to search for\par
    var theSelection; // Will hold the document's selection object\par
    var textNodes; // Will hold all the text nodes in the document\par
\par
    // Set it to search the entire document if we haven't been given an element to search\par
    if(!searchElem || typeof(searchElem) == 'undefined') searchElem = document.body;\par
\par
    // Get the string to search for\par
    if(elem && elem.value) searchString = elem.value;\par
    else if(this && this.value) searchString = this.value;\par
\par
    if(searchString && searchString.length > 0) \{ // make sure the string isn't empty, or it'll crash.\par
        if(window.getSelection) \{ // Firefox\par
            // Get the selection\par
            theSelection = window.getSelection();\par
\par
            // Get all the text nodes in the document\par
            textNodes = findTypeNodes(searchElem,3);\par
\par
            // If there's already a selection, and it's the string we're searching for\par
            var searchMatch = new RegExp(searchString,'i');\par
            if(theSelection.rangeCount == 1 && searchMatch(theSelection.getRangeAt(0).toString())) \{\par
                var currentRange = theSelection.getRangeAt(0);\par
                theSelection.removeAllRanges();\par
                // Move on to the next occurrence of it by iterating through text nodes...:\par
                for(var i = 0; i < textNodes.length; i++) \{\par
                    // If this text node is before the currentRange, ignore it and carry on to the next one\par
                    if(currentRange.comparePoint(textNodes[i],0) == -1 && currentRange.startContainer != textNodes[i]) continue;\par
                    // If this text node is the same as the currentRange, find the point in the currentRange\par
                    else if((currentRange.comparePoint(textNodes[i],0) == -1 && currentRange.startContainer == textNodes[i]) || (currentRange.comparePoint(textNodes[i],0) == 0)) \{\par
                        // Create a regular expression object to do the searching\par
                        var reSearch = new RegExp(searchString,'gmi'); // Set it to 'g' - global (finds all instances), 'm' - multiline (searches more than one line), 'i' - case insensitive\par
                        var stringToSearch = textNodes[i].textContent;\par
                        var newRange = null;\par
                        while(reSearch(stringToSearch)) \{ // While there are occurrences of the searchString\par
                            // Test if the index is after the currentRange's position\par
                            if(reSearch.lastIndex - searchString.length > currentRange.startOffset) \{\par
                                // This is the new search position - empty the old selection and add the new selection range\par
                                theSelection.removeAllRanges();\par
                                newRange = document.createRange();\par
                                newRange.setStart(textNodes[i],reSearch.lastIndex - searchString.length); // Start node and index of the selection range\par
                                newRange.setEnd(textNodes[i],reSearch.lastIndex); //  End node and index of the selection\par
                                theSelection.addRange(newRange); // Add the node to the document's current selection\par
                                break; // We're not interested in the other results, so break out of this while loop.\par
                            \}\par
                        \}\par
                        if(newRange) break; // If we found a new range, break out of this for loop, cos there's nothing more to do.\par
                        else continue; // Otherwise continue\par
                    \}\par
                    // If this text node is after the current one, search to see if it has any occurrences of the searchString\par
                    else if(currentRange.comparePoint(textNodes[i],0) == 1) \{\par
                        // Create a regular expression object to do the searching\par
                        var reSearch = new RegExp(searchString,'gmi'); // Set it to 'g' - global (finds all instances), 'm' - multiline (searches more than one line), 'i' - case insensitive\par
                        var stringToSearch = textNodes[i].textContent;\par
                        // If we had a find, use it\par
                        if(reSearch(stringToSearch)) \{\par
                            // This is the new search position - empty the old selection and add the new selection range\par
                            theSelection.removeAllRanges();\par
                            newRange = document.createRange();\par
                            newRange.setStart(textNodes[i],reSearch.lastIndex - searchString.length); // Start node and index of the selection range\par
                            newRange.setEnd(textNodes[i],reSearch.lastIndex); //  End node and index of the selection\par
                            theSelection.addRange(newRange); // Add the node to the document's current selection\par
                            break; // We're not interested in the other results, so break out of this while loop.\par
                        \} else continue;\par
                    \}\par
                \}\par
            \}\par
\par
            // If we don't already have a selection, just find the first instance\par
            else \{\par
                // Search all text nodes\par
                for(var i = 0; i < textNodes.length; i++) \{\par
                    // Create a regular expression object to do the searching\par
                    var reSearch = new RegExp(searchString,'gmi'); // Set it to 'g' - global (finds all instances), 'm' - multiline (searches more than one line), 'i' - case insensitive\par
                    var stringToSearch = textNodes[i].textContent;\par
                    if(reSearch(stringToSearch)) \{ // If there are occurrences of the searchString\par
                        // This is the new search position - empty the old selection and add the new selection range\par
                        theSelection.removeAllRanges();\par
                        // Add the new selection range\par
                        var thisRange = document.createRange();\par
                        thisRange.setStart(textNodes[i],reSearch.lastIndex - searchString.length); // Start node and index of the selection range\par
                        thisRange.setEnd(textNodes[i],reSearch.lastIndex); //  End node and index of the selection\par
                        theSelection.addRange(thisRange); // Add the node to the document's current selection\par
                        break; // We're done\par
                    \}\par
                \}\par
            \}\par
        \}\par
        else if(document.selection) \{ // Internet Explorer\par
            theSelection = document.selection;\par
            var currentRange = theSelection.createRange();\par
            // Empty the current selection\par
            theSelection.empty();\par
            if(currentRange && currentRange.text && currentRange.text.match(eval('/'+searchString+'/i'))) \{\par
                // Move start position of range past this word\par
                currentRange.moveStart('word');\par
                // Move end position to end of document\par
                currentRange.moveEnd('textEdit');\par
                // Search again\par
                if(currentRange.findText(searchString)) \{\par
                    // Select\par
                    currentRange.select();\par
                    return true;\par
                \}\par
                return false;\par
            \} else \{\par
                // Get a new range\par
                var searchRange = document.body.createTextRange();\par
                // Make it encompass the whole document\par
                searchRange.expand('textedit');\par
                // Find the first occurrence of the searchString\par
                if(searchRange.findText(searchString)) \{\par
                    // Select\par
                    searchRange.select();\par
                    return true;\par
                \} else \{\par
                    return false;\par
                \}\par
            \}\par
        \} else alert("Sorry your browser doesn't support a supported selection object");\par
    \}\par
\}\par
\par
// Recursively find all text nodes within an element\par
function findTypeNodes(elem,type) \{\par
    // Remove superfluous text nodes and merge adjacent text nodes\par
    elem.normalize();\par
\par
    var typeNodes = new Array();\par
    // Search all children of this element to see which ones are the right type of node\par
    for(var nodeI = 0; nodeI < elem.childNodes.length; nodeI++) \{\par
        if(elem.childNodes[nodeI].nodeType == type) typeNodes.push(elem.childNodes[nodeI]); // If it is a the right type of node, add it to the array\par
        else \{\par
            // If not a the right type of node, search it in turn\par
            typeNodes = typeNodes.concat(findTypeNodes(elem.childNodes[nodeI],type));\par
        \}\par
    \}\par
    return typeNodes; // return the array\par
\}\par
\par
// Multiple onload function created by: Simon Willison\par
// http://simon.incutio.com/archive/2004/05/26/addLoadEvent\par
function addLoadEvent(func) \{\par
  var oldonload = window.onload;\par
  if (typeof window.onload != 'function') \{\par
    window.onload = func;\par
  \} else \{\par
    window.onload = function() \{\par
      if (oldonload) \{\par
        oldonload();\par
      \}\par
      func();\par
    \}\par
  \}\par
\}\par
\par
addLoadEvent(function() \{\par
  // Create a button\par
  var theInput = document.getElementById('searchInput');\par
  var theButton = document.getElementById('performSearch');\par
  var elementToSearch = document.body; // Use getElementById('yourIDName') to only allow search for a specific id.\par
  theButton.onclick = function() \{ // Set the onclick function\par
    performSingleSearch(theInput,elementToSearch);\par
  \}\par
\});\par
}
 