
  formulario="&iquest;Qu&eacute; gasofiki quieres que te ponga?<br/><form action=\"\" method=\"get\">";
  formulario=formulario+"<input type=\"radio\" name=\"ptype\" value=\"GOA\" onclick=\"setPType(0);\" >Gas&oacute;leo A habitual<br>";
  formulario=formulario+"<input type=\"radio\" name=\"ptype\" value=\"G95\" onclick=\"setPType(1);\" >Gasolina 95<br>";
  formulario=formulario+"<input type=\"radio\" name=\"ptype\" value=\"G98\" onclick=\"setPType(2);\" >Gasolina 98";
  formulario=formulario+"</form>";

  var ayuda="Para cambiar el tipo de gasolina, haz un solo click sobre Gasofiki";
 
  /* puntero arrastable */
  var gasofiki = null;
 
  /* map */
  var map = null;
  
  var allStations = new Array();
  var indexStations = 0;

  var pType = -1;
  
  var petrolType = new Array();
  petrolType[0] = "Gas&oacute;leo A habitual";
  petrolType[1] = "Gasolina 95";
  petrolType[2] = "Gasolina 98";

  var petrolCode = new Array();
  petrolCode[0] = "GOA";
  petrolCode[1] = "G95";
  petrolCode[2] = "G98";
  
  var stationListHtml = "";
  
  var zoom = -1;
  
  /* Markers starting z-index */
  var markerZIndex = 500;

  function setPType(num){
	pType = num;
	gasofiki.openInfoWindowHtml("Buscando " + petrolType[pType] + " ...");
	buscaGasofikis(gasofiki, map);
  }

  function parseXML(xml, map) {
  try // Internet Explorer
    {
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async="false";
    xmlDoc.loadXML(xml);
    }
  catch(e)
    {
    try // Firefox, Mozilla, Opera, etc.
      {
      parser=new DOMParser();
      xmlDoc=parser.parseFromString(xml,"text/xml");
  	  
      }
    catch(e) {alert(e.message)}
    } 
  		x=xmlDoc.documentElement.childNodes;
  		var name = "";
  		var times = "";
  		var latitude = -1;
  		var longitude = -1;
  		var color = 0;
  		var priceGoa = -1;
  		var priceG95 = -1;
  		var priceG98 = -1;
  		var price = -1;
		for (i=0;i<x.length;i++)
		{
			if (x[i].nodeType==1)
			{
				y=x[i].childNodes;
			  	name = "";
			  	times = "";
			  	latitude = -1;
			  	longitude = -1;
			  	color = 0;
			  	priceGoa = -1;
			  	priceG95 = -1;
			  	priceG98 = -1;
			  	price = 0;

			  	if (y.length == 0) {
					gasofiki.openInfoWindowHtml("Lo siento, no he encontrado ninguna gasolinera con tus criterios.");
				}
			  	  
				for (j=0;j<y.length;j++) 
				{
					if( y[j].nodeName == "name") 
						name = y[j].childNodes[0].nodeValue;
					if( y[j].nodeName == "latitude") 
						latitude = y[j].childNodes[0].nodeValue;
					if( y[j].nodeName == "longitude") 
						longitude = y[j].childNodes[0].nodeValue;
					if( y[j].nodeName == "goa") 
						priceGoa = y[j].childNodes[0].nodeValue;
					if( y[j].nodeName == "g95") 
						priceG95 = y[j].childNodes[0].nodeValue;
					if( y[j].nodeName == "g98") 
						priceG98 = y[j].childNodes[0].nodeValue;
					if( y[j].nodeName == "color") 
						color = y[j].childNodes[0].nodeValue;
				}
				  
				//find times
				var horarioIndex = name.indexOf("Horario");
				var LDIndex = name.indexOf("L-D:");
				var LSIndex = name.indexOf("L-S:");
				var LVIndex = name.indexOf("L-V:");
				var LMIndex = name.indexOf("L-M:");
				var LIndex = name.indexOf("L:");
				if (horarioIndex > -1) {
					times = name.substring(horarioIndex);
					name = name.substring(0,horarioIndex-1);
				}
				if (LDIndex > -1) {
					times = name.substring(LDIndex);
					name = name.substring(0,LDIndex-1);
				}
				if (LSIndex > -1) {
					times = name.substring(LSIndex);
					name = name.substring(0,LSIndex-1);
				}
				if (LVIndex > -1) {
					times = name.substring(LVIndex);
					name = name.substring(0,LVIndex-1);
				}
				if (LMIndex > -1) {
					times = name.substring(LMIndex);
					name = name.substring(0,LMIndex-1);
				}
				if (LIndex > -1) {
					times = name.substring(LIndex);
					name = name.substring(0,LIndex-1);
				}
				  
				if (pType == 0)
					price = priceGoa;
				else if(pType == 1) 
					price = priceG95;
				else if (pType =2)
					price = priceG98;
				else
					continue;	
				//var pos = new GLatLng(latitude,longitude);
				var newStation = new Station();
				newStation.setName(name);
				newStation.setTimes(times);
				newStation.setZIndex(markerZIndex);
				markerZIndex--;
				newStation.setLatitude(latitude);
				newStation.setLongitude(longitude);
				newStation.setIconUrl("/api/DIcon?c=" + color + ";" + price);
				if (priceGoa > -1)
					newStation.setGoa(priceGoa);
				if (priceG95 > -1)
					newStation.setG95(priceG95);
				if (priceG98 > -1)
					newStation.setG98(priceG98);
				createStation(newStation);
			} 
		}
		  
		var buildLink = "http://www.gasofiki.es/index.php?la=";
		var llat = gasofiki.getLatLng().lat();
		var llon = gasofiki.getLatLng().lng();
		var lpt = petrolCode[pType];
		buildLink = buildLink + llat + "&lo=" + llon + "&pt=" + lpt;
		var linkInput = document.getElementById("linkDirecto");
		linkInput.value = buildLink;
		document.getElementById("listOfStations").innerHTML = stationListHtml;
		document.getElementById("message").innerHTML = "Carburante seleccionado: " + petrolType[pType] + ".<br/>Para cambiar el tipo de gasolina haz un solo click sobre el icono de Gasofiki.<br/><a href=\"#\" onclick=\"show('showLinkBox');centerHelpBox('showLinkBox');return false;\" title=\"Haz click para ver el link directo de esta b&uacute;squeda\">Mostrar link para esta b&uacute;squeda</a>";
	}


    function initialize() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("mapCanvas"));
        map.setCenter(new GLatLng(40.3967,-3.713), 6);
        map.setUIToDefault();

        gasofiki = new GMarker(new GLatLng(41.3967,-3.713), {draggable:true, zIndexProcess:returnTop});
        
        GEvent.addListener(gasofiki, "dragstart", function() {
  			  cleanStationsFromMap(map);
        	  map.closeInfoWindow();
        });
        	        
        GEvent.addListener(gasofiki, "dragend", function() {
        	map.panTo(gasofiki.getLatLng());
            if (pType < 0) {
            	if (zoom < 0) {
            		zoom = 10;
            		map.setCenter(gasofiki.getLatLng(), zoom);
            	} else {
            		map.panTo(gasofiki.getLatLng());
            	}
            	// map.setCenter(gasofiki.getLatLng(), 9);
            	gasofiki.openInfoWindowHtml(formulario);
            } else {
            	gasofiki.openInfoWindowHtml("Buscando " + petrolType[pType] + " ...");
          		buscaGasofikis(gasofiki, map);
          		
            }      
          });

        GEvent.addListener(gasofiki, "click", function() {
            if (pType > -1) {
            	cleanStationsFromMap(map);
            	gasofiki.openInfoWindowHtml(formulario);
            }     
          });

		map.addOverlay(gasofiki);
		
		/* auto search from bookmarks */
		var lat = getParameter('la');
		var lon = getParameter('lo');
		var ptype = getParameter('pt');	
		if (lat > -90 && lat < 90 && lon > -180 && lon < 180 && (ptype=="G95" || ptype=="G98" || ptype=="GOA")) {
			gasofiki.setLatLng(new GLatLng(lat, lon));
			map.panTo(gasofiki.getLatLng());
			zoom = 10;
			map.setCenter(gasofiki.getLatLng(), zoom);
			if (ptype=="GOA")
				setPType(0);
			if (ptype=="G95")
				setPType(1);
			if (ptype=="G98")
				setPType(2);	
		} else {
			gasofiki.openInfoWindowHtml("Hola, soy Gasofiki.<br> Arrastrame a alg&uacute;n punto en el mapa y te mostrar&eacute; las gasofikis m&aacute;s baratas.");
		}
      }
    }
    
    function addStationToListHtml(newStation) {
    	stationListHtml = stationListHtml + "<a href=\"#\" onclick=\"openStationInfo(" + (indexStations - 1) + ");return false;\"><div class=\"stationItem\"><div class=\"stationImage\"><img src=\"" + newStation.getIconUrl() + "\" /></div><div class=\"stationText\"><span class=\"stationName\">" +  newStation.getName() + "</span><br/><span class=\"stationTimes\">" + newStation.getTimes() + "</span>" + "</div><div class=\"clear\"></div></div></a>";
    }
        
    function createStation(newStation) {
    	var myIcon = new GIcon();
    	myIcon.image = newStation.getIconUrl();
    	myIcon.iconAnchor = new GPoint(16, 32);
    	myIcon.infoWindowAnchor = new GPoint (16,0);
    	markerOptions = { icon:myIcon, zIndexProcess:importanceOrder, title:newStation.getName() };
    	var latitude = newStation.getLatitude();
    	var longitude = newStation.getLongitude()
    	var marker = new GMarker(new GLatLng(latitude, longitude), markerOptions);
    	marker.importance = newStation.getZIndex();
    	newStation.setGmarker(marker);
    	allStations[indexStations] = newStation;
    	indexStations++;
    	map.addOverlay(marker);
    	var output = makeInfoWindow(newStation);
    	marker.bindInfoWindowHtml(output);
    	addStationToListHtml(newStation);
    }

    function cleanStationsFromMap(map) {
		for (i=0; i<indexStations;i++) {
			if (allStations[i] != null)
				map.removeOverlay(allStations[i].getGmarker());
			allStations[i] = null;
		}
		indexStations = 0;
		stationListHtml = "";
		document.getElementById("listOfStations").innerHTML = "<h3>Estaciones...</h3>";
		document.getElementById("message").innerHTML = "&nbsp;";
    }

	function buscaGasofikis(marker, map) {
		markerZIndex = 500;
		map.setCenter(gasofiki.getLatLng(), 11);
		if (window.XMLHttpRequest) {  
	  		xmlhttp = new XMLHttpRequest();  
	  	} else {  
	        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");  
	    }  
		var position = marker.getLatLng();
	    
	    xmlhttp.onreadystatechange = function() { 
	  		if (xmlhttp.readyState == 4) {  
	  			marker.closeInfoWindow();
	  			parseXML(xmlhttp.responseText, map);
	        }  
	    }  
	    xmlhttp.open("GET", "/api/Query?latitude=" + position.lat() + "&longitude=" + position.lng() + "&ptype=" + petrolCode[pType] + "&maxdistance=20&output=xml", true);   
	    xmlhttp.send(null);  
	}
	
	function importanceOrder (marker,b) {
        return marker.importance;
	}
	
	function returnTop (marker,b) {
        return 501;
	}
	
	function openStationInfo (stationIndex) {
		var station = allStations[stationIndex];
		var marker = station.getGmarker();
		var output = makeInfoWindow(station);
		marker.openInfoWindowHtml(output);
	}
	
	/* from http://www.netlobo.com/url_query_string_javascript.html */
	function getParameter( name ){
		var name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		var regexS = "[\\?&]"+name+"=([^&#]*)";
		var regex = new RegExp( regexS );
		var results = regex.exec( window.location.href );
		if( results == null )
			return "";
		else
			return results[1];
	}
	
	function makeInfoWindow(station) {
		var output = "<span class=\"stationName\">" +  station.getName() + "</span><br/><span class=\"stationTimes\">" + station.getTimes() + "</span><br/>";
		output = output + "<span class=\"stationPrice\">";
		if (station.hasGoa()) {
			if (pType == 0) {
				output = output + "<b>";
			}
			output = output + "GOA: " +  station.getGoa() + " ";
			if (pType == 0) {
				output = output + "</b>";
			}
		}
		if (station.hasG95()) {
			if (pType == 1) {
				output = output + "<b>";
			}
			output = output + "G95: " +  station.getG95() + " ";
			if (pType == 1) {
				output = output + "</b>";
			}
		}
		if (station.hasG98()) {
			if (pType == 2) {
				output = output + "<b>";
			}
			output = output + "G98: " +  station.getG98();
			if (pType == 2) {
				output = output + "</b>";
			}
		}
		output = output + "</span>";
		return output;
	}
	
	
