/************************************************************
                      Find-in-page script
              By Mark Wilton-Jones 11-12/10/2002
*************************************************************

Please see http://www.howtocreate.co.uk/jslibs/ for details and a demo of this script
Please see http://www.howtocreate.co.uk/jslibs/termsOfUse.html for terms of use

To use:

Inbetween the <head> tags, put:

	<script src="PATH TO SCRIPT/findinpage.js" type="text/javascript" language="javascript1.2"></script>

Wherever you want to put a find in page form, put:

if( ( document.body && document.body.createTextRange ) || window.find ) {
	document.write( '<form action="" onSubmit="searchForText( this.elements[0].value, window ); return false;">'+
		'<input type="text" size="15"> <input type="submit" value="Find in Page"></form>' );
}

Recent versions of Mozilla/Firefox will only find the first result due to a selection
problem when clicking the button. To avoid this bug, put the search form in an iframe,
and make it search the 'window.parent' page.
___________________________________________________________________________________________*/

function searchForText( toFind, frameToSearch ) {
	if( !toFind ) { window.alert( 'You have not entered any text to search for' ); return; }
	if( frameToSearch.focus ) { frameToSearch.focus(); }
	if( window.find ) {
		//Netscape compatible browsers provide the window.find method
		if( document.layers ) {
			//Against the JS spec, Netscape 4 will produce errors if too many arguments are given
			var ifFound = frameToSearch.find( toFind, false, false );
		} else {
			var ifFound = frameToSearch.find( toFind, false, false, true, false, true, false );
		}
	} else if( frameToSearch.document.body && frameToSearch.document.body.createTextRange ) {
		//IE or compatible use various TextRange features
		if( frameToSearch.document.selection && frameToSearch.document.selection.type != 'None' ) {
			//If some text is selected already (previous search or if they have selected it)
			//make that the text range. Then move to the end of it to search beyond it
			var theRange = frameToSearch.document.selection.createRange();
			theRange.collapse( false );
		} else {
			//If no text is selected, start from the start of the document
			var theRange = frameToSearch.document.body.createTextRange();
		}
		//find the next occurrence of the chosen string
		var ifFound = theRange.findText( toFind );
		if( ifFound ) { theRange.select(); }
	} else {
		alert ( 'Please use your browser\'s search facility' );
	}
	if( !ifFound ) { alert ( 'Could not find text:\n' + toFind ); }
}