var GL_FORSE_CERCAVI = 1;
var GL_FORSE_ERRORE_GENERICO = 2;

var gdir = new GDirections(); 

var KMGeo = {
	geoRequestPending : 0
	, clean : function(frm, location) {
		// Pulizia dei campi che contengono le informazioni sull'indirizzo
		frm.elements[location.id + 'Addr'].value = "";
		frm.elements[location.id + 'CName'].value = "";
		frm.elements[location.id + 'CIso'].value = "";
		frm.elements[location.id + 'AName'].value = "";
		frm.elements[location.id + 'SAName'].value = "";
		frm.elements[location.id + 'LName'].value = "";
		frm.elements[location.id + 'DLName'].value = "";		
	}
	, setLocation : function(frm, location, onLocationSetHandler) {
		this.geoRequestPending = this.geoRequestPending - 1;
		// Impostazione di un'indirizzo
		frm.elements[location.id + 'Addr'].value = location.placemark.address;
		frm.elements[location.id + 'CName'].value = location.placemark.AddressDetails.Country.CountryName;
		frm.elements[location.id + 'CIso'].value = location.placemark.AddressDetails.Country.CountryNameCode;
		if (typeof(location.placemark.AddressDetails.Country.AdministrativeArea) != "undefined") {
			frm.elements[location.id + 'AName'].value = location.placemark.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
			if (typeof(location.placemark.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea) != "undefined") {
				frm.elements[location.id + 'SAName'].value = location.placemark.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName;
				if (typeof(location.placemark.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality) != "undefined") {
					frm.elements[location.id + 'LName'].value = location.placemark.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
					if (typeof(location.placemark.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.DependentLocality) != "undefined") {
						frm.elements[location.id + 'DLName'].value = location.placemark.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.DependentLocality.DependentLocalityName;
					}
				}
			}
		}
		
		if (onLocationSetHandler) {
				onLocationSetHandler(frm, location);
		}
	}
	, geoCode : function(frm, location, onLocationSetHandler) {
		/*
		Imposta l'indirizzo restituito da google
		Definire le funzioni clean e onSetLocation
		*/
		KMGeo.clean(frm, location);
		KMGeo.trGReq();
		this.geoRequestPending = this.geoRequestPending + 1;
		var geo = new GClientGeocoder();
		geo.setBaseCountryCode(location.countryCode);	
		geo.getLocations(location.localita + '(' + location.countryCode + ')', function(result) {
			var message = document.getElementById(location.id + 'AddressMessage');
			if (result.Status.code == G_GEO_SUCCESS) {				
				if (result.Placemark.length == 1) {
					// Indirizzo ricercato trovato
					message.innerHTML = '';
					KMGeo.setLocation(frm, { id: location.id, placemark: result.Placemark[0]}, onLocationSetHandler);
				} else {
					// Selezione di uno degli indirizzi proposti
					message.innerHTML = geoLabels[GL_FORSE_CERCAVI] + ':<br>';
					for (var i=0; i<result.Placemark.length; i++) {
						if (result.Placemark[i].AddressDetails.Country.CountryNameCode == location.countryCode) {
							var aText = document.createTextNode(formattaIndirizzo(result.Placemark[i]));
							var a = document.createElement('a');
							a.href = "#";
							a.location = {id: location.id, placemark: result.Placemark[i]};
							a.onclick = function() { KMGeo.setLocation(frm, this.location, onLocationSetHandler); return false; };
							a.appendChild(aText);
							message.appendChild(a);
							var br = document.createElement('br');
							message.appendChild(br);
						}
					}
				}
			} else {
				var errorMessage;
				switch (result.Status.code) {
					case G_GEO_MISSING_ADDRESS, G_GEO_UNKNOWN_ADDRESS :
						errorMessage = reasons[result.Status.code];
						break;
					default :
						errorMessage = geoLabels[GL_FORSE_ERRORE_GENERICO];
						break;
				}
				message.innerHTML = errorMessage;
			}
		});
	}
	, trGReq : function() {
		// Trace richiesta a Google
		var now = new Date();
		var request = GXmlHttp.create();
		request.open("GET", "/support/traceGeoRequest.asp?t=" + now.getTime(), true);
		request.send(null);		
	}
}

// Formattazione dell'indirizzo restituito da google
function formattaIndirizzo(placemark) {
	return placemark.address;
}