//<![CDATA[
var xmlhttp
var xmlhttp2
var xmlhttp3
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
  try {
  xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
  xmlhttp2=new ActiveXObject("Msxml2.XMLHTTP");
  xmlhttp3=new ActiveXObject("Msxml2.XMLHTTP");
  tipo ='firefox';
 } catch (e) {
  try {
  	tipo = 'explorer';
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	 xmlhttp2=new ActiveXObject("Msxml2.XMLHTTP");
  xmlhttp3=new ActiveXObject("Msxml2.XMLHTTP");
  } catch (E) {
   xmlhttp=false;
    xmlhttp2=false;
  xmlhttp3=false;
  }
 }
@else
 xmlhttp=false
 xmlhttp2=false;
  xmlhttp3=false;
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
 try {
  xmlhttp = new XMLHttpRequest();
 } catch (e) {
  xmlhttp=false
 }
}
if (!xmlhttp2 && typeof XMLHttpRequest!='undefined') {
 try {
  xmlhttp2 = new XMLHttpRequest();
 } catch (e) {
  xmlhttp2=false
 }
}
if (!xmlhttp3 && typeof XMLHttpRequest!='undefined') {
 try {
  xmlhttp3 = new XMLHttpRequest();
 } catch (e) {
  xmlhttp3=false
 }
}




function load(latitud,longitud) {
  if (GBrowserIsCompatible()) {

    map = new GMap2(document.getElementById("mapa"));
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(latitud,longitud), 15);
	point = new GLatLng(latitud,longitud);
	markers[0]  = new GMarker(point);
	map.addOverlay(markers[0] );
	gdir = new GDirections(map, document.getElementById("directions"));
    GEvent.addListener(gdir, "load", onGDirectionsLoad);
    GEvent.addListener(gdir, "error", handleErrors);

  }
}
function load2(latitud,longitud,zoomxx) {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("mapa"));
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(latitud,longitud), zoomxx,G_SATELLITE_MAP);
	point = new GLatLng(latitud,longitud);
	markers[0]  = new GMarker(point);
	//map.addOverlay(markers[0] );
	gdir = new GDirections(map, document.getElementById("directions"));
    GEvent.addListener(gdir, "load", onGDirectionsLoad);
    GEvent.addListener(gdir, "error", handleErrors);
	mostrarfotos('no');
  }
}
function setDirections(fromAddress, toAddress, locale) {
	document.getElementById("mapa").style.height='400px';
      gdir.load("from: " + fromAddress + " to: " + toAddress,
                { "locale": locale });
    }

    function handleErrors(){
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("Lo sentimos pero no conocemos la dirección especificada. Por favor pruebe con otra. Gracias.");
	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	      alert("Lo sentimos pero no conocemos la dirección especificada. Por favor pruebe con otra. Gracias.");

	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	       alert("Lo sentimos pero no conocemos la dirección especificada. Por favor pruebe con otra. Gracias.");

	   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	      alert("Lo sentimos pero no conocemos la dirección especificada. Por favor pruebe con otra. Gracias.");

	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	      alert("Lo sentimos pero no conocemos la dirección especificada. Por favor pruebe con otra. Gracias.");

	   else   alert("Lo sentimos pero no conocemos la dirección especificada. Por favor pruebe con otra. Gracias.");

	}

	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 mostrarfotos(mostrarencapa){
		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 url = '../panoramio.php?minx=' + minx + "&miny=" + miny +'&maxx=' + maxx + "&maxy=" + maxy ;
		GDownloadUrl(url, function(data, responseCode) {
			var panoramio = eval("(" + data + ")");
			if (mostrarencapa=='yes'){
				var divpanoramio = document.getElementById('panoramio');
				divpanoramio.innerHTML='<table><tr>';
			}
			var entra = 'no';
			for (var i = 0; i < panoramio.photos.length; i++) {
				var entra = 'yes';
				createMarkerfoto(panoramio.photos[i]);

				if (mostrarencapa=='yes')
	divpanoramio.innerHTML="<div class=imgpanoramio><a id='photo_infowin' target='_blank' href='http://www.panoramio.com/photo/" +panoramio.photos[i].photo_id + "'>" +"<img alt='Imagen de Panoramio llamada "+panoramio.photos[i].photo_title+"' width='200'  src='http://www.panoramio.com/photos/small/" + panoramio.photos[i].photo_id + ".jpg'/><\/a></div>" + divpanoramio.innerHTML;
			}
			if (mostrarencapa=='yes'){
			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" style="width:190px;"><a href="http://www.panoramio.com">Las imagenes de Panoramio estan bajo el Copyright de sus autores</a></div>';
			}
			});
	}
	function createMarkerfoto(input) {
		var point = new GLatLng(input.latitude,input.longitude);
		iconpanoramio.image = "http://www.panoramio.com/photos/small/" + input.photo_id + ".jpg";
		var marker = new GMarker(point,iconpanoramio);
		map.addOverlay(marker)
		GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml('<img class="fotomapa" src="http://www.panoramio.com/photos/small/' + input.photo_id + '.jpg" />');});
		return marker;
	}
	
	function IsNumeric(sText)

	{
	   var ValidChars = "0123456789.";
	   var IsNumber=true;
	   var Char;


	   for (i = 0; i < sText.length && IsNumber == true; i++) 
	      { 
	      Char = sText.charAt(i); 
	      if (ValidChars.indexOf(Char) == -1) 
	         {
	         IsNumber = false;
	         }
	      }
	   return IsNumber;

	   }

	function validarEmail(valor) {
	  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)){
	   return (true)
	  } else {
	      return (false);
	  }
	}
			var enviar ='no';
				function trim(stringToTrim) {
				return stringToTrim.replace(/^\s+|\s+$/g,"");
			}
	function comprobarpresupuesto(pagina){
	var msgerror='';
	var telefono = trim(document.f.telefono.value);

	if (document.f.nombre.value=='') 
		msgerror = '* Nombre completo\n';


	if (document.f.fecha1.value=='') 
		msgerror += '* Fecha1\n';
		
		if (document.f.fecha2.value=='') 
			msgerror += '* Fecha2\n';

	if (telefono=='') 
		{
		msgerror = msgerror +'* Teléfono\n';
		}
	else
		{
		if (!IsNumeric(telefono)) 
			msgerror = msgerror +'* El teléfono ha de ser un número\n';
		}

	if (document.f.email.value=='') 
		msgerror += '* Email\n';
	else
		{
		if (validarEmail(document.f.email.value)==false)
			msgerror = msgerror +'* El Email parece incorrecto\n';
		}

	

	if (msgerror==''){
	 if ((telefono.length)<9)
	 	msgerror = '* El teléfono ha de tener 9 cifras como mínimo\n';
		document.f.telefono.focus();
	}
	if (msgerror!=''){
		alert('Los siguientes campos son obligatorios:\n\n'+msgerror);
		return false;
		}
	else{
		if (pagina!='')
		document.f.action=pagina;
		else
		document.f.action='/enviarpresupuesto.php';
		document.f.submit();
		return false;	

	}
	}
function enviarxx(pagina){
	location.href=pagina;
}
//]]>

