
	var IE = navigator.appName.toLowerCase().indexOf("microsoft") > -1;
	var Mozilla = navigator.appName.toLowerCase().indexOf("netscape") > -1;

	var textoAnt = "";
	var posicionListaFilling = 0;

	var datos = new Array();
	

	function ajaxobj() {
		try {
			_ajaxobj = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				_ajaxobj = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				_ajaxobj = false;
			}
		}
	   
		if (!_ajaxobj && typeof XMLHttpRequest!='undefined') {
			_ajaxobj = new XMLHttpRequest();
		}
		
		return _ajaxobj;
	}
		var limit = 0;	
	function cargaLista(evt, obj, txt) {

		ajax = ajaxobj();
		ajax.open("GET", "buscador.php?texto=" + txt, true);
		ajax.onreadystatechange=function() {
			if (ajax.readyState==4) {

				var datos = ajax.responseXML;
				var ProLinks = datos.getElementsByTagName("ProNombre");
				var URLLinks = datos.getElementsByTagName("ProIdenti");
				var IMGLinks = datos.getElementsByTagName("ProImagen");

				var listaProduc = new Array();
				var listaUrlink = new Array();
				var listaImagen = new Array();
	
				if (ProLinks) {

					for (var i=0; i<ProLinks.length; i++) { //URLinks
						listaProduc[listaProduc.length] = ProLinks[i].firstChild.data;
						listaUrlink[listaUrlink.length] = URLLinks[i].firstChild.data;
						listaImagen[listaImagen.length] = IMGLinks[i].firstChild.data;
					}
				}
				escribeLista(obj, listaProduc, listaImagen, listaUrlink);
			}
		}
		ajax.send(null);
	}
	
	function escribeLista(obj, lista, liste, listo) {
		var html = "";
		var fill = document.getElementById('lista');
		
		if (lista.length == 0) {

			fill.style.display = "none";
		} else {

			fill.style.display = "block";
			var html='<table cellspacing="0" '+
				'cellpadding="0" border="0" width="100%" id="filtro">';
			for (var i=0; i<lista.length; i++) {

				//(posicionListaFilling == i ? ' class="fill" ': '')

				html += '<tr id="tr'+obj.id+i+
					'" '+(i%2 == 0? ' class="fill" ': '')+
					' onmouseover="seleccionaFilling(\'tr'+
					obj.id+'\', '+i+
					')" onmousedown="abrirpro(\'' + liste[i] + '\',\'' + listo[i] + '\')">';

				html += '<td height="20"><a href="#" onclick="abrirpro(\'' + liste[i] + '\',\'' + listo[i] + '\')"> &nbsp; '+lista[i]+'  </a></td></tr>'; 
			}
			html += '</table>';
		}

		// Escribimos la lista
		fill.innerHTML = html;
	}


	function inputFilling(evt, obj) {
		var fill = document.getElementById('lista');

		var elems = datos;
		
		var tecla = "";
		var lista = new Array();
		var res = obj.value;
		var borrar = false;
		
		if (!IE) {
		  tecla = evt.which;
		} else {
		  tecla = evt.keyCode;
		}
		
		var texto;

		if (!String.fromCharCode(tecla).match(/(\w|\s)/) && 
				tecla != 8 && 
				tecla != 13) {
			texto = textoAnt;
		} else {
			texto = obj.value;
		}
		
		textoAnt = texto;

		if ((texto != null && texto != "") 
			|| (tecla == 40 || tecla == 38)) {
			cargaLista(evt, obj, texto);
		}
		

		if (tecla == 37) { 

		} else if (tecla == 38) { 
			if (posicionListaFilling > 0) {
				posicionListaFilling--;
			}

			fill.scrollTop = posicionListaFilling*14;
		} else if (tecla == 39) { 

		} else if (tecla == 40) { 
			if (obj.value != "") {

				if (posicionListaFilling < lista.length-1) { 

					fill.scrollTop = posicionListaFilling*14;

					posicionListaFilling++;
				} 
			}
		} else if (tecla == 8) { 

			posicionListaFilling = 0;

			borrar = true;
		} else if (tecla == 13) {

			if (obj.createTextRange) {
				var r = obj.createTextRange();
				r.moveStart("character", 
					obj.value.length+1);
				r.moveEnd("character", 
					obj.value.length+1);
				r.select();
			} else if (obj.setSelectionRange) {
				obj.setSelectionRange(
					obj.value.length+1, 
					obj.value.length+1);
			}

			fill.style.display = "none";

			posicionListaFilling = 0;

			fill.scrollTop = 0;
			return true;
		} else {

			posicionListaFilling = 0;
			fill.scrollTop = 0;
		}	
		
		if (!borrar) {
			if (lista.length != 0) {

				obj.value = lista[posicionListaFilling];
				if (obj.createTextRange) {
					var r = obj.createTextRange();
					r.moveStart("character", 
						texto.length);
					r.moveEnd("character", 
						lista[posicionListaFilling].length);
					r.select();
				} else if (obj.setSelectionRange) {
					obj.setSelectionRange(
						texto.length, 
						lista[posicionListaFilling].length);
				}
			}
		}
		return true;
	}
  
  
	function setInput(obj, fill) {
		obj.value = textoAnt;
		fill.style.display = "none";
		posicionListaFilling = 0;
	}

	function seleccionaFilling(id, n) {
		document.getElementById(id + 
			n).className = "fill";
		document.getElementById(id + 
			posicionListaFilling).className = "";  	
		posicionListaFilling = n;
	}
  
	function seleccionaTextoFilling (id, n) {
		textoAnt = document.getElementById(id + 
			n).firstChild.innerHTML;
		posicionListaFilling = 0;

	}
  	

function abrirpro (img,art) {
	location.href = 'articulo.php?image=banco/' + img + '&id_product=' + art;	
}

