function mySetCoordsInFields(theLatLng) {
	document.getElementById('lat').value = theLatLng.lat();
	document.getElementById('lng').value = theLatLng.lng();
}

function myResetCoordsInFields() {
	document.getElementById('lat').value = '';
	document.getElementById('lng').value = '';
}

// Funzione per aggiungere marker draggabile
function myAddDraggableMarker(theLatLng) {
	
	var marker = new GMarker(theLatLng, markerOptions);
	marker.myHtml = '<h3>Tu sei qui</h3>';
	marker.bindInfoWindowHtml(marker.myHtml, myInfoWindowOptions);
	
	GEvent.addListener(marker, "dragend", function() {
		mySetCoordsInFields(marker.getLatLng());
		showBtnReposition();
	});
	
	gMap.addOverlay(marker);
}

function showBtnReposition() {
	$('btnReposition').style.display = 'inline';
}

function hideBtnReposition() {
	$('btnReposition').style.display = 'none';
}

function showMap() {
	$('mapBtnsMapHidden').style.display = 'none';
	$('mapBtnsMapShown').style.display = 'block';
	$('geo_mappa').style.display = 'block';
	if (!initialized) {
		initialize();
		initialized = true;
	}
}
function hideMap() {
	$('mapBtnsMapHidden').style.display = 'block';
	$('mapBtnsMapShown').style.display = 'none';
	$('geo_mappa').style.display = 'none';
	$('geovis').checked = '';
}

function myGeocode(lat, lng) {
	
	// Esegue il geocode dell'indirizzo (deve arrivare ben formattato) 
	function aggiungiMarkerDraggabile(responseLatLng) {
		
		gMap.clearOverlays();
		
		if (responseLatLng == null) {
			alert(registr_geo_error);
			myResetCoordsInFields();
			return false;
		}
		
		// Aggiunge il marker
		myAddDraggableMarker(responseLatLng);
		gMap.setCenter(responseLatLng, 14);
		
		// Inserisce i dati del geocode negli hidden
		mySetCoordsInFields(responseLatLng);
		hideBtnReposition();
		
	}
	
	if (lat != '' && lng != '') {
		var gll = new GLatLng(lat, lng, true);
		aggiungiMarkerDraggabile(gll);
	}
	else
	{
		nazT = $('geo0').getSelected().get('text');
		provT = $('geo1').value;
		locT = $('geo2').value;
		capT = $('geo3').value;
		indT = $('geo4').value;
		indirizzo = nazT+', '+provT+', '+locT+', '+capT+', '+indT;

		var geocoder = new GClientGeocoder();
		geocoder.getLatLng(indirizzo, aggiungiMarkerDraggabile);
	}
}

function switchProfile(argProfileId) {
	//if (!confirm('Cambiando profilo si perderanno i dati inseriti. Continuare?')) return false;
	document.getElementById('goingPro').innerHTML = 'Attendere...';
	window.location = 'registrazione.php?ucid='+argProfileId;
}

function fillSelectField(op, val, upd) {
	var el = $(upd);
	var req3 = new Request.JSON({
		url: "auth/profile_service.php?op="+op+"&val="+val, 
		onRequest: function() {
			$each(el.getChildren(),function(el) {
				el.dispose();
			});
		},
		onComplete: function(opt) {
			var res = '';
			$each(opt,function(item){
				new Element('option').set('id', item.id).set('html',item.specializzazione).inject(el);
			})
		}
	}).get();

}

function shOtherSpecField(id) {
	var myDisplay = (id==-1) ? 'block' : 'none';
	$('otherSpecBox').style.display = myDisplay;
}

var gMap;
var gMarkers; // i markers sono punti nella mappa, non sono gli overlay.
var myInfoWindowOptions = {maxWidth:200};
var myIcon = myGetGenericIcon(); // Create our "tiny" marker icon, and use it 
markerOptions = {icon:myIcon, draggable: true};
initialized = false;


// CODICE DA ESEGUIRE SULL'EVENTO DOM READY
window.addEvent('domready', function(){
	new FormCheck('formular');
	//showMap();
	//myGeocode('', '');

	var status = {
		'true': 'Nascondi',
		'false': 'Mostra'
	};

	var myVerticalSlide = new Fx.Slide('shipaddr');
	
	$('toggle_shipaddr').addEvent('click', function(e){
		e.stop();
		myVerticalSlide.toggle();
	});
	
	// When Vertical Slide ends its transition, we check for its status
	// note that complete will not affect 'hide' and 'show' methods
	myVerticalSlide.addEvent('complete', function() {
		$('toggle_shipaddr').set('html', status[myVerticalSlide.open]);
	});
	
	var myVerticalSlide2 = new Fx.Slide('contactdata');
	
	$('toggle_contactdata').addEvent('click', function(e){
		e.stop();
		myVerticalSlide2.toggle();
	});
	
	// When Vertical Slide ends its transition, we check for its status
	// note that complete will not affect 'hide' and 'show' methods
	myVerticalSlide2.addEvent('complete', function() {
		$('toggle_contactdata').set('html', status[myVerticalSlide2.open]);
	});
	
	myVerticalSlide.hide();
	myVerticalSlide2.hide();
	
});