
var NS4 = (document.layers);
var IE4 = (document.all);
/* var win = top.contenido.window; // Con frames usar top.nombre.window; */
var n = 0;
var ventana = window;
var finds = 0;
var keycode;



// ******** LIMPIAR *******
function limpiar(str) {
	document.getElementById('string').value = "";
	str = "limpiado";
	BuscarPalabras(str);
}



//***************************//
//***** BUSCAR PALABRAS *****//
//***************************//
function BuscarPalabras(str)
{

	var type;
	var txt, i, found;

	if (str == "") return false;



	// SI ES FIREFOX
	if  (navigator.appName == "Netscape") {
		if (finds == 0) // Ponemos el foco en la capa para que busque dentro.
		{
		    document.getElementById("cBody_TDC").focus();
		}
		buscarEnFF(str); // Llamamos a la función que busca para FF.
	}

	// SI ES EXPLORER
	if (navigator.appName == "Microsoft Internet Explorer") {	

		// SI VENIMOS DE PULSAR LIMPIADO, PONEMOS EL BOTON DE ENCONTRAR Y EMPEZAMOS DE CERO.
		if (str == "limpiado") {
			n = 0;
			document.getElementById('boton').style.backgroundImage="url('Img/buscar.jpg')";
			document.getElementById("string").select();
			return false;
		}

		txt = document.body.createTextRange();
		// Encuentra la coincidencia desde el inicio de pagina.
		for (i = 0; i <= n && (found = txt.findText(str)) != false; i++) {
			txt.moveStart("character", 1);
			txt.moveEnd("textedit");
		}

		// Si lo encuentra, marca el resultado y mueve la barra de desplazamiento para mostrarlo.

		if (found) {

			document.getElementById('boton').style.backgroundImage="url('Img/siguiente.jpg')";

			txt.moveStart("character", -1);
			txt.findText(str);
			try{
			   txt.select();
			   document.getElementById("boton").focus();
			}catch(err){
			   n++;			
			   BuscarPalabras(str);
			}
			txt.scrollIntoView();
			n++;	
		// De otra manera regresa al inicio de la página para buscar otra coinsidencia
		}else {
			if (n > 0) {
				//alert("Se han encontrado " + n + " coincidencias.\r\n     No hay más coincidencias.")
				n = 0;
				alert("No hay más coincidencias.");
				document.getElementById('boton').style.backgroundImage="url('Img/buscar.jpg')";

				//document.getElementById("string").value = "";
				document.getElementById("string").select();
				//BuscarPalabras(str);
			// No se encuentra en ninguna parte envia un mensaje.
			}else
				alert("Palabra no encontrada.");
				document.getElementById("string").select();
		}

	}

	return false;

} // FIN funcion BuscarPalabras





//******************************************//
//***** BUSCAR PALABRAS SÓLO EN FIREFOX*****//
//******************************************//
function buscarEnFF(str) {

	// SI VENIMOS DE PULSAR LIMPIADO, PONEMOS EL BOTON DE ENCONTRAR Y EMPEZAMOS DE CERO.
	if (str == "limpiado") {
		
		document.getElementById('boton').style.backgroundImage="url('Img/buscar.jpg')";
		if(sel.rangeCount > 0) sel.removeAllRanges();
	    document.getElementById("string").focus();
	    document.getElementById("string").select();
		return false;
	}


	// Si ha encontrado alguna coincidencia, 
	// nos quedamos con la posición,
	// borramos los iluminados anteriores e iluminamos el encontrado
	if (finds > 0){
		sel = window.getSelection(); // get selection
		// remove all ranges
		if(sel.rangeCount > 0) sel.removeAllRanges();
		// add last highlighted range
		sel.addRange(range);		
	}

	// window.find(string, caseSensitive, searchBackwards)

	// Si encuenta ...
	if (window.find(str, false, false))
	{
		document.getElementById('boton').style.backgroundImage="url('Img/siguiente.jpg')";
		// In Firefox (not Netscape) when we press the
		// Next or Prev buttons in the DIV it looses the
		// selection, so we have to grab the selection
		// locations so we don't keep searching for only
		// the first found search over and over

		// Nos quedamos con la posición del que encuentra
		// para que después busque a partir de ella.
		sel = window.getSelection(); // get selection
		range = sel.getRangeAt(0); // get object

		// Sumamos 1 al contador de encontrados
		finds++;	
	} else {

		// Si no encuentra más (o ninguno),
		// ponemos el contador a cero,
		// mostramos mensaje,
		// quitamos la marca de los que haya encontrado,
		// ponemos el foco en el input y seleccionamos la palabra a buscar.

		finds = 0;
		alert("No hay más coincidencias.");
		document.getElementById('boton').style.backgroundImage="url('Img/buscar.jpg')";
		if(sel.rangeCount > 0) sel.removeAllRanges();
	    document.getElementById("string").focus();
	    document.getElementById("string").select();
		return false;
	}




}// Fin funcion Buscar en FF 
