var map = null;
var markers = [];
var iconmarca = new GIcon();
iconmarca.image = "http://maps.google.com/mapfiles/ms/micons/gas.png";
iconmarca.shadow = "http://maps.google.com/mapfiles/ms/micons/gas.shadow.png";
iconmarca.iconSize = new GSize(22,22);
iconmarca.shadowSize = new GSize(22,22);
iconmarca.iconAnchor = new GPoint(11,11);
iconmarca.infoWindowAnchor = new GPoint(16, 0);

icon = new GIcon();
icon.image = "http://labs.google.com/ridefinder/images/mm_20_green.png";
icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
icon.iconSize = new GSize(12, 20);
icon.shadowSize = new GSize(22, 20);
icon.iconAnchor = new GPoint(6, 20);
icon.infoWindowAnchor = new GPoint(5, 1);

	
iconbiodiesel = new GIcon();
iconbiodiesel.image = "images/cocheverde.png";
iconbiodiesel.shadow = "images/cocheverdes.png";
iconbiodiesel.iconSize = new GSize(32, 32);
iconbiodiesel.shadowSize = new GSize(56, 32);
iconbiodiesel.iconAnchor = new GPoint(16, 32);
iconbiodiesel.infoWindowAnchor = new GPoint(16, 0);
	
	
	
	function mostrarprovincia(valor){
		if (valor!='')
		{
			location.href='gasolineras-en-'+valor+'.htm';
		}
	}
	function mostrarpuntosnegros(valor){
		if (valor!='')
		{
			location.href='puntos-negros-en-'+valor+'.htm';
		}
	}
	function buscardireccion(){
		var direccion=document.forms['buscar1'].direccion.value;
		var localidad=document.forms['buscar1'].localidad.value;
		var url = 'direcciones.php?direccion='+direccion+', '+localidad ;
			xmlhttp.open("GET",url,true);
			xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4) {
				var res = xmlhttp.responseText;
				if (res!='error')
					location.href='gasolineras-cerca-de-'+res+'.htm';
				else
					alert('Woop, no encontramos la dirección:\n'+direccion + ', ' + localidad)
				return false;

				}}
			 xmlhttp.setRequestHeader('Accept','message/x-jl-formresult');
			 xmlhttp.send(null);
			return false;
	}
	function buscarradar(){
		var direccion=document.forms['formbuscarradar'].direccion.value;
		var localidad=document.forms['formbuscarradar'].localidad.value;	
		var url = 'direcciones.php?direccion='+direccion+', '+localidad ;
			xmlhttp.open("GET",url,true);
			xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4) {
				var res = xmlhttp.responseText;
				if (res!='error')
					location.href='localizador-de-radares-'+res+'.htm';
				else
					alert('Woop, no encontramos la dirección:\n'+direccion + ', ' + localidad)
				return false;

				}}
			 xmlhttp.setRequestHeader('Accept','message/x-jl-formresult');
			 xmlhttp.send(null);
			return false;
	}
	function buscaritv(){
		var direccion=document.forms['formbuscaritv'].direccion.value;
		var localidad=document.forms['formbuscaritv'].localidad.value;	
		var url = 'direcciones.php?direccion='+direccion+', '+localidad ;
			xmlhttp.open("GET",url,true);
			xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4) {
				var res = xmlhttp.responseText;
				if (res!='error')
					location.href='localizador-de-itv-'+res+'.htm';
				else
					alert('Woop, no encontramos la dirección:\n'+direccion + ', ' + localidad)
				return false;

				}}
			 xmlhttp.setRequestHeader('Accept','message/x-jl-formresult');
			 xmlhttp.send(null);
			return false;
	}
	function mostrarfotos(){
		var mapa = map;
		var bounds = mapa.getBounds();
		var miny = bounds.getSouthWest().lat();
		var minx = bounds.getSouthWest().lng();
		var maxy = bounds.getNorthEast().lat();
		var maxx = bounds.getNorthEast().lng();
		var urlfotos = 'panoramio.php?minx=' + minx + "&miny=" + miny +'&maxx=' + maxx + "&maxy=" + maxy ;
		GDownloadUrl(urlfotos, function(data, responseCode) {
		parseJsonfotos(data);
		});
	}
	function parseJsonfotos(doc2) {
	var panoramio = eval("(" + doc2 + ")");
	var divpanoramio = document.getElementById('panoramio');
	divpanoramio.innerHTML='<table><tr>';
	var entra = 'no';
	for (var i = 0; i < panoramio.photos.length; i++) {
		var entra = 'yes';
		p=panoramio.photos[i];
		divpanoramio.innerHTML="<div class=imgpanoramio><a id='photo_infowin' target='_blank' href='http://www.panoramio.com/photo/" + p.photo_id + "'>" +"<img alt='Imagen de Panoramio llamada "+p.photo_title+"' width='200'  src='http://www.panoramio.com/photos/small/" + p.photo_id + ".jpg'/><\/a></div>" + divpanoramio.innerHTML;
	}
		//var markerfoto = createMarkerpanoramio(panoramio.photos[i]);	
		//markerfoto.photo = panoramio.photos[i];

		//map.addOverlay(markerfoto);
		if (entra=='no')
		divpanoramio.innerHTML=divpanoramio.innerHTML + '<strong>No hemos encontrado imagenes cercanas a esta gasolinera.</strong><br />';
		else
		divpanoramio.innerHTML=divpanoramio.innerHTML + '<div id="bajopanoramio"><a href="http://www.panoramio.com">Las imagenes de Panoramio estan bajo el Copyright de sus autores</a></div>';
	}
	function setDirections(fromAddress, toAddress, locale) {
	  gdir.load("from: " + fromAddress + " to: " + toAddress,
	            { "locale": locale });
	}
	function handleErrors(){
		   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
		     alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
		   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
		     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);

		   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
		     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);

		//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
		//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);

		   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
		     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

		   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
		     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);

		   else alert("An unknown error occurred.");

		}
		function onGDirectionsLoad(){ 
		      // Use this function to access information about the latest load()
		      // results.

		      // e.g.
		      // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
			  // and yada yada yada...
			}
function puntuarmal(idmapa)
			{
				var url = 'puntuarmal.php?idmapa=' + idmapa  ;
				xmlhttp.open("GET",url,true);
			//	window.open (url)
				xmlhttp.onreadystatechange=function() {
				if (xmlhttp.readyState==4) {
					data = xmlhttp.responseText;
					alert('Gracias por  ayudarnos!!!\nHemos tomado nota.');

				}
				}
				xmlhttp.setRequestHeader('Accept','message/x-jl-formresult');
				xmlhttp.send(null);

				return false;	
			}
			var enviar ='no';
				function trim(stringToTrim) {
				return stringToTrim.replace(/^\s+|\s+$/g,"");
			}
			function ltrim(stringToTrim) {
				return stringToTrim.replace(/^\s+/,"");
			}
			function rtrim(stringToTrim) {
				return stringToTrim.replace(/\s+$/,"");
			}
				function IsNumeric(valor) 
					{ 
						var log=valor.length; var sw="S"; 
						for (x=0; x<log; x++) 
							{ v1=valor.substr(x,1); 
							v2 = parseInt(v1); 
							//Compruebo si es un valor numérico 
							if (isNaN(v2)) { sw= "N";} 
							} 
							if (sw=="S") {return true;} else {return false; } 
					} 
					function validarEmail(valor) {
						  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)){
						   return (true)
						  } else {
						      return (false);
						  }
						 }
			function comprobarcontacto(){
			var msgerror='';
			
			if (document.forms['contacto'].nombre.value=='') 
				msgerror = '* Nombre o apodo\n';

				if (document.forms['contacto'].comentario.value=='') 
					msgerror += '* Tu comentario\n';

			if (document.forms['contacto'].email.value=='') 
				msgerror += '* Email\n';
			else
				if (validarEmail(document.forms['contacto'].email.value)==false)
					msgerror = msgerror +'* El Email parece incorrecto\n';
				
			
			 	if (msgerror!='')
				{
					alert('Los siguientes campos son obligatorios:\n\n'+msgerror);
					return false;
				}
				else
				{
					document.forms['contacto'].action='enviaropinion.php';
					return true;
				
					}
			
				}
				
				function votar(tipo,id)
							{
								var url = 'votar.php?tipo='+tipo+'&id=' + id  ;

								xmlhttp.open("GET",url,true);
								xmlhttp.onreadystatechange=function() {
								if (xmlhttp.readyState==4) {
									data = xmlhttp.responseText;
									alert('Gracias por votar!!!\nHemos tomado nota.');

								}
								}
								xmlhttp.setRequestHeader('Accept','message/x-jl-formresult');
								xmlhttp.send(null);

								return false;	
							}
function enviarclick(anuncio,pagina){
	var url = 'anuncios.php?anuncio='+anuncio+'&pagina='+pagina;
	xmlhttp.open("GET",url,true);
	xmlhttp.onreadystatechange=function() {
	if (xmlhttp.readyState==4) {
		return false;
		}
	}
	xmlhttp.setRequestHeader('Accept','message/x-jl-formresult');
	xmlhttp.send(null);
	return false;
}