// JavaScript Document
function initialize() {
  map = new GMap2(document.getElementById("mapa_registro"));
  map.removeMapType(G_HYBRID_MAP); 
  mapControl = new GMapTypeControl(); 
  map.addControl(mapControl);
  
  map.addControl(new GLargeMapControl);
  map.setCenter(new GLatLng(30.415457,-3.704281), 2); 
  GEvent.addListener(map, "click", getAddress);
  geocoder = new GClientGeocoder();
};


function initialize_read_only() {
  map = new GMap2(document.getElementById("mapa_registro"));
  map.removeMapType(G_HYBRID_MAP); 
  mapControl = new GMapTypeControl(); 
  map.addControl(mapControl);
  
  map.addControl(new GLargeMapControl());
  map.setCenter(new GLatLng(30.415457,-3.704281), 2); 
  //GEvent.addListener(map, "click", getAddress);
  geocoder = new GClientGeocoder();
};


function showAddress(address) {
  map.clearOverlays();	
  geocoder.getLatLng(
    address,
    function(point) {
      if (point) {
        map.setCenter(point, 13);
	    var marker = new GMarker(point);
        map.addOverlay(marker);
        marker.openInfoWindowHtml(address);
      }
    }
   );
}
function showAddress_menos_zoom(address) {
	
   geocoder.getLatLng(
    address,
    function(point) {
      if (point) {
        map.setCenter(point, 9);
	    var marker = new GMarker(point);
        map.addOverlay(marker);
        marker.openInfoWindowHtml(address);
      }
    }
   );
}


function getAddress(overlay, latlng) {
  if (latlng != null) {
    address = latlng;
    geocoder.getLocations(latlng, convertToAddress);
  }
}

function convertToAddress(response) {
  var adresa_meva;
  map.clearOverlays();
  if (!response || response.Status.code != 200) {
    alert("Status Code:" + response.Status.code);
  } else {
    place = response.Placemark[0];
    point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
    marker = new GMarker(point);
    map.addOverlay(marker);
	
	if(place.AddressDetails!=undefined)
	{
	  if(place.AddressDetails.Country!=undefined)
	  {
		adresa_meva=place.AddressDetails.Country.CountryNameCode;
		if(place.AddressDetails.Country.AdministrativeArea!=undefined)
		{
			if(place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea!=undefined && place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality!=undefined) 
			{
				adresa_meva+=","+place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
				if(place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode!=undefined)
				{
					adresa_meva+=","+place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber;	
					if(document.formulari_registre.checkbox_direccion_completa.checked)
					{
						if(place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare!=undefined)
						{
							adresa_meva+=","+place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare.ThoroughfareName;	
						}
					}
				}		
			}	
		}			
	  }
	  else
	  	adresa_meva="?";	
	}
	else
		adresa_meva="?";
	document.formulari_registre["ubicacion"].value=adresa_meva;
	document.formulari_registre["latitudes"].value=response.name;
	marker.openInfoWindowHtml(adresa_meva);
  }
}

           
function abrir_completo(direccion){
//abrir('.$dir_mensajes.',0,0,0,0,0,1,1,100,100,100,100,1)" />
    var ventana = window.open(direccion,"ver_perfil","Fullscreen=NO,Menubar=YES,Status=YES,Titlebar=YES,Toolbar  =YES,Scrollbars=YES,Resizable=YES,Location=YES,Height="+(screen.Height/2)+",Width="+(screen.Width/2));return false;

}                    
function abrir_popup(direccion){
    var ventana = window.open(direccion,"enviar_mensaje","Fullscreen=NO,Menubar=NO,Status=NO,Titlebar=NO,Toolbar  =NO,Scrollbars=YES,Resizable=YES,Location=NO");return false;

}                    
function confirmarEliminar(destino,mensaje) {
    if (confirm(mensaje)) {
        document.location.href = destino;
    }
}

function IsNumeric(valor) 
{ 
	return (isNaN(valor)) ? false : true;

} 

var primerslap=false; 
var segundoslap=false; 

function formateafecha(fecha) 
{ 
var long = fecha.length; 
var dia; 
var mes; 
var ano; 
if ((long>=2) && (primerslap==false)) { dia=fecha.substr(0,2); 
if ((IsNumeric(dia)==true) && (dia<=31) && (dia!="00")) { fecha=fecha.substr(0,2)+"/"+fecha.substr(3,7); primerslap=true; } 
else { fecha=""; primerslap=false;} 
} 
else 
{ dia=fecha.substr(0,1); 
if (IsNumeric(dia)==false) 
{fecha="";} 
if ((long<=2) && (primerslap=true)) {fecha=fecha.substr(0,1); primerslap=false; } 
} 
if ((long>=5) && (segundoslap==false)) 
{ mes=fecha.substr(3,2); 
if ((IsNumeric(mes)==true) &&(mes<=12) && (mes!="00")) { fecha=fecha.substr(0,5)+"/"+fecha.substr(6,4); segundoslap=true; } 
else { fecha=fecha.substr(0,3);; segundoslap=false;} 
} 
else { if ((long<=5) && (segundoslap=true)) { fecha=fecha.substr(0,4); segundoslap=false; } } 
if (long>=7) 
{ ano=fecha.substr(6,4); 
if (IsNumeric(ano)==false) { fecha=fecha.substr(0,6); } 
else { if (long==10){ if ((ano==0) || (ano<1900) || (ano>2100)) { fecha=fecha.substr(0,6); } } } 
} 
if (long>=10) 
{ 
fecha=fecha.substr(0,10); 
dia=fecha.substr(0,2); 
mes=fecha.substr(3,2); 
ano=fecha.substr(6,4); 
// AÃ±o no viciesto y es febrero y el dia es mayor a 28 
if ( (ano%4 != 0) && (mes ==02) && (dia > 28) ) { fecha=fecha.substr(0,2)+"/"; } 
} 
return (fecha); 
}

function si_enter_buscar_mapa(ubicacion,e)
{
	var tecla = (document.all) ? e.keyCode : e.which;
	if (tecla==13 || e.keyCode==13 || e.which==13)
	{ 
		showAddress(ubicacion);		
		return false;
	}		
	return true;
}


//-->    

