var isOpera = navigator.userAgent.indexOf("Opera") > -1;
var isIE = navigator.userAgent.indexOf("MSIE") > 1 && !isOpera;
var isMoz = navigator.userAgent.indexOf("Mozilla/5.") == 0 && !isOpera;
function init() {
	if (!document.getElementById) return false;
		var f = document.getElementById('auto_off');
		var u = f.elements[4];
		f.setAttribute("autocomplete", "off");
	
} 
function pulsabuscador(x){
	//alert(document.getElementById('dato'+x).value);
	document.getElementById('resultados').style.visibility='hidden';
	document.form1.texto1.value = document.getElementById('dato'+x).value;
	document.form1.texto2.value = document.form1.texto1.value;
	comprobar();
}

function setCursorPos( x ) {

    var txtRange = x.createTextRange();
    txtRange.moveStart( "character", x.value.length - 2 );
    txtRange.moveEnd( "character", 0 );
    txtRange.select();
}

function setSelectionRange(input, selectionStart, selectionEnd) {
  if (input.setSelectionRange) {
    input.focus();
    input.setSelectionRange(selectionStart, selectionEnd);
  }
  else if (input.createTextRange) {
    var range = input.createTextRange();
    range.collapse(true);
    range.moveEnd('character', selectionEnd);
    range.moveStart('character', selectionStart);
    range.select();
  }
}


var celdaactual =0;
var totalfilas= 0;
var res=new Array('');
var tabla='';

function mostrartexto(num){
document.form1.celdaactual.value = num;
if (document.getElementById('dato'+num)){
	document.form1.texto1.value=document.getElementById('dato'+num).value;
}
else
document.form1.submit();

document.getElementById('resultados').innerHTML='';

}

function textboxSelect (oTextbox, iStart, iEnd) {

   switch(arguments.length) {
       case 1:
           oTextbox.select();
           break;

       case 3:
           
           if (isIE) {
               var oRange = oTextbox.createTextRange();
               oRange.moveStart("character", iStart);
               oRange.moveEnd("character", -oTextbox.value.length + iEnd);      
               oRange.select();                                              
           } else if (isMoz) {
               oTextbox.setSelectionRange(iStart, iEnd);
           }                    
   }
}

function buscar(txtbuscar,e){

if (txtbuscar.length==''){
 	if(document.getElementById('resultados'))
  		document.getElementById('resultados').style.visibility='hidden';
		document.getElementById('resultados').innerHTML= '';
	}
if (txtbuscar.length>2)
	{
	if(document.getElementById('resultados'))
	document.getElementById('resultados').style.visibility='visible';	


if (totalfilas>0)
var code = (document.all) ? e.keyCode : e.which; 


switch (code){
case 46:
	document.form1.texto2.value = document.form1.texto1.value;
break;
case 8:
	document.form1.texto2.value = document.form1.texto1.value;
break;
case 13: case 9:
	comprobar()
break;


case 40:
	
	celdaactual =parseInt(document.form1.celdaactual.value) ;
	celdaanterior = celdaactual;
	celdaactual +=1;
	
	if (document.getElementById('celda'+celdaactual)) {
		document.form1.celdaactual.value = celdaactual;
		document.getElementById('tr'+celdaanterior ).style.backgroundColor='#ffffff';
		document.getElementById('tr'+celdaanterior ).style.color='#000000';
		document.getElementById('tr'+celdaactual).style.backgroundColor='#1E1B16';
		document.getElementById('tr'+celdaactual).style.color='#ffffff';
		document.getElementById('celda2'+celdaanterior ).style.backgroundColor='#ffffff';
		document.getElementById('celda2'+celdaanterior ).style.color='green';
		document.getElementById('celda2'+celdaactual).style.backgroundColor='#1E1B16';
		document.getElementById('celda2'+celdaactual).style.color='#000000';
		var inicio = document.form1.texto2.value.length;
	document.form1.texto1.value=document.getElementById('dato'+celdaactual).value;
	var fin = document.form1.texto1.value.length;	
	textboxSelect(document.form1.texto1,inicio,fin);
	document.form1.focus();
		
		}
		
	break;

case 38:
	celdaactual =parseInt(document.form1.celdaactual.value) ;
	celdaanterior = celdaactual;
	celdaactual -=1;
	if (document.getElementById('celda'+celdaactual)) {
	document.form1.celdaactual.value = celdaactual;
	document.getElementById('tr'+celdaanterior ).style.backgroundColor='#ffffff';
	document.getElementById('tr'+celdaanterior ).style.color='#000000';
	document.getElementById('tr'+celdaactual).style.backgroundColor='#1E1B16';
	document.getElementById('tr'+celdaactual).style.color='#ffffff';
	document.getElementById('celda2'+celdaanterior ).style.backgroundColor='#ffffff';
	document.getElementById('celda2'+celdaanterior ).style.color='#1E1B16';
	document.getElementById('celda2'+celdaactual).style.backgroundColor='#1E1B16';
	document.getElementById('celda2'+celdaactual).style.color='#000000';
	var inicio = document.form1.texto2.value.length;
	document.form1.texto1.value=document.getElementById('dato'+celdaactual).value;
	var fin = document.form1.texto1.value.length;	
	textboxSelect(document.form1.texto1,inicio,fin);
	document.form1.focus();
		
	}
	break;

default:
document.form1.texto2.value = document.form1.texto1.value;
if (document.form1.enviar.value =='yes')
document.form1.enviar.value ='no';
if (txtbuscar==''){
document.form1.totalfilas.value = '0';
document.getElementById('resultados').innerHTML='';
}
else
{


var url = 'suggest/ajaxbuscador.php?value=' + txtbuscar ;

	xmlhttp.open("GET",url,true);
	totalfilas=0;
	
	xmlhttp.onreadystatechange=function() {
	if (xmlhttp.readyState==4) {
		res = xmlhttp.responseText.split('|');
		document.getElementById('resultados').innerHTML= res[0];
		totalfilas = res[1];
		primerres = res[2];
		
		document.form1.totalfilas.value = totalfilas;
		
		if (totalfilas>0)
		{
			
			document.getElementById('resultados').style.visibility='visible';
			
			tabla = document.getElementById('tablaresultados');
			
			document.form1.celdaactual.value ='1';
			celdaactual = '1';
			//alert(res[1]);
			
			document.getElementById('tr'+celdaactual).style.backgroundColor='#1E1B16';
			document.getElementById('tr'+celdaactual).style.color='#ffffff';
			document.getElementById('celda2'+celdaactual).style.backgroundColor='#1E1B16';
			document.getElementById('celda2'+celdaactual).style.color='#ffffff';
			
			if (res[0]!=''){
				var inicio = document.form1.texto2.value.length;
				if (document.getElementById('dato'+celdaactual))
					document.form1.texto1.value=document.getElementById('dato'+celdaactual).value;
					
				var fin = document.form1.texto1.value.length;	
					textboxSelect(document.form1.texto1,inicio,fin);
			}
			}
		else{
			document.getElementById('resultados').style.visibility='hidden';
		}
		}
		}
	xmlhttp.setRequestHeader('Accept','message/x-jl-formresult');
	xmlhttp.send(null);

	return false;
	}
	break;
}
}	}


function URLencode(sStr) {
    return escape(sStr); 

  }
  
function comprobar(){
if (document.form1.texto1.value.length>0)
	{
	if (document.getElementById('url'+celdaactual))
	location.href= 'http://www.quetiempo.es/prevision/' + document.getElementById('url'+celdaactual).value;
	else
	alert('Woop, no tenemos datos para la localidad que busca.\nPuede buscar en una localidad cercana');
	}
return false;
}
function nuevaventana(numero){
	if (numero==1)
		ventana = 'http://www.tarotdealicia.com/';
		if (numero==2)
			ventana = 'http://www.fernandosanchezbufete.com/';
			if (numero==3)
				ventana = 'http://www.advierte.net/';
			window.open(ventana)
	
}

