/*I developed this to work on topic pages within Microsoft HTMLHELP. I am a script user rather than a writer. 
What I've done here is to combine the elements of three different scripts together as well as a document write 
method to create the actions that I wanted which was to create a find-in-page search box that could appear on each 
page with all the code, including the search box and button residing in the external js file. 
This is a workaround. What I really wanted to do is call the browser find-in-page method which cannot be done 
from a page because of security reasons (at least as of this writing, I have not found a way to do it). 
Each piece of the code here was created to as solutions for other results by some rather knowledgeable programmers. 
I understand what they've done but I could not have written the code myself. My skill is finding what others 
adshave written and modifying it to fit my purposes. John Loupé at www.johnloupe.net*/

//<script language="JavaScript" src="find.js"></script>

/***********************************************
* Floating Top Bar script- © Dynamic Drive (www.dynamicdrive.com)
* Sliding routine by Roy Whittle (http://www.javascript-fx.com/)
* This notice must stay intact for legal use.
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

var persistclose=0 //set to 0 or 1. 1 means once the bar is manually closed, it will remain closed for browser session
var startX = 30 //set x offset of bar in pixels
var startY = 5 //set y offset of bar in pixels
var verticalpos="fromtop" //enter "fromtop" or "frombottom"

function iecompattest(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function get_cookie(Name) {
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) {
offset += search.length
end = document.cookie.indexOf(";", offset);
if (end == -1) end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}

function closebar(){
if (persistclose)
document.cookie="remainclosed=1"
document.getElementById("topbar").style.visibility="hidden"
}

function staticbar(){

  if(IE4){
	  barheight=document.getElementById("topbar").offsetHeight
	  var ns = (navigator.appName.indexOf("Netscape") != -1) || window.opera;
	  var d = document;
	  function ml(id){
	    var el=d.getElementById(id);
	    if (!persistclose || persistclose && get_cookie("remainclosed")=="")
	    el.style.visibility="visible"
	    if(d.layers)el.style=el;
	    el.sP=function(x,y){this.style.left=x+"px";this.style.top=y+"px";};
	    el.x = startX;
	    if (verticalpos=="fromtop")
	    el.y = startY;
	    else{
	    el.y = ns ? pageYOffset + innerHeight : iecompattest().scrollTop + iecompattest().clientHeight;
	    el.y -= startY;
	    }
	    return el;
	  }
	  window.stayTopLeft=function(){
	    if (verticalpos=="fromtop"){
	    var pY = ns ? pageYOffset : iecompattest().scrollTop;
	    ftlObj.y += (pY + startY - ftlObj.y)/8;
	    }
	    else{
	    var pY = ns ? pageYOffset + innerHeight - barheight: iecompattest().scrollTop + iecompattest().clientHeight - barheight;
	    ftlObj.y += (pY - startY - ftlObj.y)/8;
	    }
	    ftlObj.sP(ftlObj.x, ftlObj.y);
	    setTimeout("stayTopLeft()", 10);
	  }
	  ftlObj = ml("topbar");
	  stayTopLeft();
	  placeFocus();
  }
  else
  {
    window.find('', false, true, true, true, false, true);
  }
}



//if (window.addEventListener)
//window.addEventListener("load", staticbar, false)
//else if (window.attachEvent)
//window.attachEvent("onload", staticbar)
//else if (document.getElementById)
//window.onload=staticbar

/*Find In Page Script- By Mike Hall (MHall75819@aol.com)Permission granted to Dynamicdrive.com to feature script in archiveFor full source code, visit http://dynamicdrive.comThis script has been revised by Alan Koontz and replaced at DynamicDrive by a more robust cross browser script */

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

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("String Not found.");
  }

  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("Not found.");
    }
  }

  

  return false;
}

/*When the form opens focus is placed in the text box. I found this code somewhere on the web*/
//<!-- Original:  Tom Khoury (twaks@yahoo.com) -->

//<!-- This script and many more are available free online at -->
//<!-- The JavaScript Source!! http://javascript.internet.com -->

//<!-- Begin
function placeFocus() {
if (document.forms.length > 0) {
var field = document.forms[0];
for (i = 0; i < field.length; i++) {
if ((field.elements[i].type == "text") || (field.elements[i].type == "textarea") || (field.elements[i].type.toString().charAt(0) == "s")) {
document.forms[0].elements[i].focus();
break;
         }
      }
   }
}


/* I discovered this method by chance. It enables me to create the form and button then convert to javascript code and call both from the js file and have the button appear on the page via the link to the js file.*/

info='<style type="text/css">' + 
'#topbar{' + 
'position:absolute;' + 
'border: 2px solid darkblue;' + 
'padding: 2px;' + 
'background-color: #D6D3CE;' + 
'width: 305px;' + 
'visibility: hidden;' + 
'z-index: 100;' +
'font-size: 1.5em;' + 
'}' +
'#topbar input{'+
'font-size: 0.6em;' +
'}'+ 
'</style>' + 
'<div id="topbar">' +
'  <center>'+ 
'    <span>Escriba el texto a buscar</span>' + 
'    <form name="search" onSubmit="return findInPage(this.string.value);">' + 
'      <input name="string" type="text" size="20" onChange="n = 0;">' + 
'      <input type="submit" value="buscar">'+
'      <input type="button" value="cerrar" onClick="closebar(); return false">' + 
'    </form>' +
'  </center>'+ 
'</div>' +
'<input type="button" value=" Buscar en esta p&aacute;gina " onClick="staticbar(); return false">'+
'<p>Tambi&eacute;n puede buscar en la p&aacute;gina pulsando la combinaci&oacute;n de teclas CTRL+F</p>' 


document.write(info)

