	
	var filtro = '';
	var video_cat = false;
	var path = '';
//	var path = "/soccer1/includes/"; ME COMPLICA LA VIDA LA /
	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 get_filtro(filtro)
	{
		var r = '';
		switch(filtro)
		{
			case 'country':
			{
				r = '&filtro=AND `place`.`level`operador_igual_a4';
				break;
			}
			case 'city':
			{
				r = "&filtro=AND `place`.`idparent`operador_igual_a(SELECT `place`.`id` FROM `place` WHERE `place`.`name".$langext."`operador_igual_aoperador_comilla_simple"+$('tcountry').value+"operador_comilla_simple)";
				break;
			}
			case 'level-country':
			{
				//r = '&unir= INNER JOIN (`team` INNER JOIN `place` ON `team`.`place`operador_igual_a`place`.`id`) ON `team`.`id`operador_igual_a`team-name`.`idteam`&filtro=AND `team-name`.`current`=1 AND `place`.`level`operador_igual_a4';
				r = '&filtro=AND `team`.`status`=1';
				break;
			}
			case 'national-team':
			{
				r = '&unir= INNER JOIN `national-team` ON `national-team`.`place`operador_igual_a`place`.`id`&filtro=AND `place`.`level`operador_igual_a4';
				break;
			}
			case 'stadium':
			{
				r = '&filtro=AND `stadium-name`.`current`operador_igual_a1';
				break;
			}
		}
		return r;
	}

	function cargaLista(evt, obj, txt, id_lista, tabla, campo_id, campo_label, tipo, txt2) {
		filtro = get_filtro(filtro);
		 if (tipo == null){
		   tipo = 1;
		 }

		if(!video_cat)
		{
			var url = "/includes/autocompletado.php?texto="+txt+'&tabla='+tabla+'&campo_id='+campo_id+'&campo_label='+campo_label+filtro;
			
			if(tipo == 1)
			{
				var url = "/includes/autocompletado.php?texto="+txt+'&tabla='+tabla+'&campo_id='+campo_id+'&campo_label='+campo_label+filtro;
			}
			else
			{
				if(tipo == 2)
				{
					var url = "/includes/autocompletado_nat_team_and_team.php?texto="+txt+'&tabla='+tabla+'&campo_id='+campo_id+'&campo_label='+campo_label+filtro;
				}
				else
				{
					if(tipo == 3)
					{
						var url = "includes/autocompletado_match.php?texto="+txt+'&tabla='+tabla+'&campo_id='+campo_id+'&campo_label='+campo_label+'&txt2='+txt2+filtro;
					}
				}
			}
		}
		else
		{
			var url = "/includes/autocompletado_video_cat.php?texto="+txt+'&video_cat=video_cat';
		}

		filtro = '';

		new Ajax.Request(url, {
		onSuccess: function(e) {

				var datos = e.responseXML;
				var arr_datos = datos.getElementsByTagName("record");
				var listaDatos = new Array();

				if (arr_datos) {
					for (var i=0; i<arr_datos.length; i++) {
						var _id = arr_datos[i].getElementsByTagName("id");
						var _label = arr_datos[i].getElementsByTagName("label");

						listaDatos[listaDatos.length] = new Array(_id[0].firstChild.data, _label[0].firstChild.data);
					}
				}
				escribeLista(obj, listaDatos, id_lista);}, 
		method:"get" ,
		evalScripts: true
		});
		
	}
	
	function escribeLista(obj, lista, id_lista) {
		var html = "";
		var fill = document.getElementById(id_lista);
		
		if (lista.length == 0) {
			// Si la lista es vacia no la mostramos
			fill.style.display = "none";
		} else {
			// Creamos una tabla con 
			// todos los elementos encontrados
			fill.style.display = "block";
			var html='<table cellspacing="0" '+
				'cellpadding="0" border="0" width="100%">';
			for (var i=0; i<lista.length; i++) {
				html += '<tr id="tp'+obj.id+i+
					'" '+(posicionListaFilling == i? 
						' class="fill" ': '')+
					' onmouseover="seleccionaFilling(\'tp'+
					obj.id+'\', '+i+
					')" onmousedown="seleccionaTextoFilling(\'tp'+
					obj.id+'\', '+i+')">';
				html += '<td>'+lista[i][1]+'</td></tr>';
				//html += '<td>'+lista[i][0]+' - '+lista[i][1]+'</td></tr>';
			}
			html += '</table>';
		}

		// Escribimos la lista
		fill.innerHTML = html;
	}
	
	function setMatches(obj, team1, team2)
	{
		var url = "/includes/autocompletado_match.php?team1="+team1+'&team2='+team2;
		new Ajax.Request(url, {
		onSuccess: function(e) {

				var datos = e.responseXML;
				var arr_datos = datos.getElementsByTagName("record");
				var listaDatos = new Array();

				if (arr_datos) {
					for (var i=0; i<arr_datos.length; i++) {
						var _id = arr_datos[i].getElementsByTagName("id");
						var _label = arr_datos[i].getElementsByTagName("label");

						listaDatos[listaDatos.length] = new Array(_id[0].firstChild.data, _label[0].firstChild.data);
					}
				}
				escribeLista(obj, listaDatos, id_lista);}, 
		method:"get" ,
		evalScripts: true
		});
	}

	// Muestra las coincidencias en la lista
	function inputFilling(evt, obj, id_lista, tabla, campo_id, campo_label, tipo, txt2) {
		var fill = document.getElementById(id_lista);

		var elems = datos;
		
		var tecla = "";
		var lista = new Array();
		var res = obj.value;
		var borrar = false;
		
		if (tipo == null){
		   tipo = 1;
		 }
		
		// Almaceno la tecla pulsada
		if (!IE) {
		  tecla = evt.which;
		} else {
		  tecla = evt.keyCode;
		}
		
		var texto;
		// Si la tecla que pulso es una
		// letra o un espacio, o el intro
		// o la tecla borrar, almaceno lo 
		// que debo buscar
		if (!String.fromCharCode(tecla).match(/(\w|\s)/) && 
				tecla != 8 && 
				tecla != 13) {
			texto = textoAnt;
		} else {
			texto = obj.value;
		}
		
		textoAnt = texto;

		// Si el texto es distinto de vacio
		// o se pulsa ARRIBA o ABAJO
		// hago llamada AJAX para que 
		// me devuelva la lista de palabras
		// que coinciden con lo que hay
		// escrito en la caja
		if ((texto != null && texto != "") 
			|| (tecla == 40 || tecla == 38)) {
			cargaLista(evt, obj, texto, id_lista, tabla, campo_id, campo_label, tipo, txt2);
		}
		
		
		// Según la letra que se pulse
		if (tecla == 37) { // Izquierda
			// No hago nada
		} else if (tecla == 38) { // Arriba
			// Subo la posicion en la
			// lista desplegable una posición
			if (posicionListaFilling > 0) {
				posicionListaFilling--;
			}
			// Corrijo la posición del scroll
			fill.scrollTop = posicionListaFilling*14;
		} else if (tecla == 39) { // Derecha
			// No hago nada
		} else if (tecla == 40) { // Abajo
			if (obj.value != "") {
				// Si no es la última palabra
				// de la lista
				if (posicionListaFilling < lista.length-1) { 
					// Corrijo el scroll
					fill.scrollTop = posicionListaFilling*14;
					// Bajo la posición de la lista
					posicionListaFilling++;
				} 
			}
		} else if (tecla == 8) { // Borrar <-
			// Se sube la lista del todo
			posicionListaFilling = 0;
			// Se permite borrar
			borrar = true;
		} else if (tecla == 13) { // Intro
			// Deseleccionamos el texto
			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);
			}
			// Ocultamos la lista
			fill.style.display = "none";
			// Ponemos el puntero de 
			// la lista arriba del todo
			posicionListaFilling = 0;
			// Controlamos el scroll
			fill.scrollTop = 0;
			return true;
		} else {
			// En otro caso que siga
			// escribiendo
			posicionListaFilling = 0;
			fill.scrollTop = 0;
		}	
		
		// Si no se ha borrado
		if (!borrar) {
			if (lista.length != 0) {
				// Seleccionamos la parte del texto
				// que corresponde a lo que aparece
				// en la primera posición de la lista
				// menos el texto que realmente hemos
				// escrito
				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;
	}
  
  
	// Introduce el texto seleccionado
	function setInput(obj, fill) {
//		if(textoAnt!='')
//		{
//			alert('ok');
			obj.value = textoAnt;
//		}
			fill.style.display = "none";
			posicionListaFilling = 0;
			textoAnt='';
	}

  
	// Cambia el estilo de
	// la palabra seleccionada
	// de la lista
	function seleccionaFilling(id, n) {
		document.getElementById(id + 
			n).className = "fill";
		document.getElementById(id + 
			posicionListaFilling).className = "";  	
		posicionListaFilling = n;
	}
  
	// Pasa el texto del filling a la caja
	function seleccionaTextoFilling (id, n) {
		textoAnt = document.getElementById(id + 
			n).firstChild.innerHTML;
		posicionListaFilling = 0;
	}
  	
 
	// Cambia la imagen cuando se pone 
	// encima el raton (nombre.ext 
	// por _nombre.ext)
	function cambiarImagen(obj, ok) {
		var marcada = obj.src.indexOf("/_") > 0;
		
		if (ok) {
			if (!marcada) {
			  var ruta = obj.src.substring(
				0, 
				obj.src.lastIndexOf("/")+1)+
				"_"+obj.src.substring(
					obj.src.lastIndexOf("/")+1);
			  obj.src = ruta;
			}
		} else {
			if (marcada) {
				var ruta = ""+obj.src.substring(
					0, obj.src.lastIndexOf("_"))+
					obj.src.substring(
						obj.src.lastIndexOf("/")+2);
				obj.src = ruta;
			}
		}
	
	}

