function nuevoAjax(){
	var xmlhttp=false;
	try{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}catch(e){
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}catch(E){
			xmlhttp = false;
		}
	}

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	
	return xmlhttp;
}

function buscarDato(){
bus=document.form1.dato.value;
if (bus.length>1){
	resul = document.getElementById('resultado');
	lista=document.getElementById('resTd');
	var encontrado= true;
	if(lista != null){
		for(var i=0; i < lista.length; i++) {
		if(lista[i].match(resul))
			encontrado= false;
		}
	}
	if(encontrado){
	
	
	ajax=nuevoAjax();
	ajax.open("POST", "busqueda.php",true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			resul.innerHTML = ajax.responseText
			document.getElementById('resultado').style.display='block';
		}
	}
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	ajax.send("busqueda="+bus)
}
}
}

var geocoder;


function codeAddress() {
	geocoder = new google.maps.Geocoder();
    var address = document.getElementById("address").value;
    if (geocoder) {
      geocoder.geocode({'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
			

            
			
			var fA=results[0].address_components;
			var ciudad=getDato(fA,'locality','long_name');
			var region=getDato(fA,'administrative_area_level_1','long_name');
			var pais=getDato(fA,'country','long_name');
			

			document.getElementById("ciudad").value=ciudad;
			document.getElementById("region").value=region;
			document.getElementById("pais").value=pais;
			document.getElementById("address").value= ciudad+' '+region+' '+pais;
			document.getElementById("info").style.display='none';
			document.getElementById("info").innerHTML='';
            
          } else {
            document.getElementById("info").style.display='block';
			document.getElementById("info").innerHTML="No se ha encontado el lugar introducido";
          }
	   	
        } else {
            document.getElementById("info").style.display='block';
			document.getElementById("info").innerHTML="Error al buscar el lugar introducido";
          }
		
		
      });
    }
  }
  
  function codeAddressBusq() {
	geocoder = new google.maps.Geocoder();
    var address = document.getElementById("addressBusq").value;
    if (geocoder) {
      geocoder.geocode({'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
			

            
			
			var fA=results[0].address_components;
			var ciudad=getDato(fA,'locality','long_name');
			var region=getDato(fA,'administrative_area_level_1','long_name');
			var pais=getDato(fA,'country','long_name');
			

			if (ciudad=='')
				if (region=='')
					if (pais=='')
						document.getElementById("addressBusq").value='';
					else
						document.getElementById("addressBusq").value=pais;
				else
					document.getElementById("addressBusq").value=region;
			else
			document.getElementById("addressBusq").value=ciudad;
			
			document.getElementById("infoBusq").style.display='none';
			document.getElementById("infoBusq").innerHTML='';
            
          } else {
            document.getElementById("infoBusq").style.display='block';
			document.getElementById("infoBusq").innerHTML="No se ha encontado el lugar introducido";
			document.getElementById("addressBusq").value='';
          }
	   	
        } else {
            document.getElementById("infoBusq").style.display='block';
			document.getElementById("infoBusq").innerHTML="Error al buscar el lugar introducido";
			document.getElementById("addressBusq").value='';
          }
		
		
      });
    }
  }
  function getDato(obj, tipo,size){
  var address;
  var salida='';
	for (var i=0; i<obj.length; i++) {
		address=obj[i];
		var name='';
		for (var j in address){
			if(j==size){
				name=address[j];
			}
			if(j=='types'){
				if (address[j][0] == tipo){
				salida=name;
					break;
				}
				
			}
			
		}
	}
	return salida;
  }

