function valid_mail_form() {
	// email
	if (!txtmessage_email.value.trim().isEmail())
		return "Veuillez entrer une adresse e-mail valide.";
	// message
	if (txtmessage_message.value.trim() == "")
		return "Veuillez saisir un message.";
	// code
	if (txtmessage_code.value.trim().length != 6)
		return "Le code ne correspond pas à celui de l'image.";
		
	return false;
}
function reset_mail_form() {
	$hide(message_loading);
	df_email.reset();
	txtmessage_message.value = "";
	df_image.reset();
	imgcode.src = '/ws/get_codeimage.php?uid=' + Math.round(Math.random() * 1000000);
}
function SendMail() {
	var error = valid_mail_form();
	if (error)
		return alert(error);
	$show(message_loading);
	var datas = { "email" : txtmessage_email.value.trim(), "message" : txtmessage_message.value.trim() , "codeimg" : txtmessage_code.value.trim(), "at_id" : at_id };
	EdLib.makeHTTPRequest("POST", "/ws/sendmail.php", SendMail_Callback, datas);
}
function SendMail_Callback(p_xhr) {
	$hide(message_loading);
	if (p_xhr.responseText == "OK") {
		alert("Le message a été correctement envoyé.");
		reset_mail_form()
	}
	else
		alert("Erreur d'envoi du message : \r\n" + p_xhr.responseText);
}

/******
GOOGLE MAP
******/
var gmap = null;
var geocoder = null;
var gmap_zoom = 13;
var at_adr = null;
var at_cp = null;
var at_ville = null;
var at_lat = null;
var at_lng = null;
function setMapMessage(p_message) {
	map_div.innerHTML = "<p>" + p_message + "</p>";	
}
function LoadGMap() {
	// tester la compatibilité
	if (!GBrowserIsCompatible()) {
 		setMapMessage("Navigateur non compatible.");
		return;
 	}
	
	var has_latlng = at_lat != null && at_lng != null;
	
	if (has_latlng) // charger directement
		LoadGMap_Map(new GLatLng(at_lat, at_lng));	
	else {
		// formater l'adresse	
		var adresse = at_cp.trim() + " " + at_ville;
		if (at_adr != null)
			adresse = at_adr.trim() + " " + adresse;
		adresse = adresse.replace(/[,.]/g, " ");
		adresse = adresse.replace(/ {2,}/g, " ");
		geocoder =  new GClientGeocoder();
		geocoder.getLatLng(
    		adresse,
				function(latlng) {
					if (latlng == null) {
						if (at_adr != null) {
							at_adr = null;
							LoadGMap();
						}
						else
							setMapMessage("L'adresse n'a pas été trouvée.");
					}
					else
						LoadGMap_Map(latlng);
				}
			);
	}
		
}
function LoadGMap_Map(latlng) {
	gmap =  new google.maps.Map2($get("div_google_map_map"));
	gmap.setCenter(latlng, gmap_zoom);
	gmap.addOverlay(getMarker(latlng));
}
function getMarker(latlng) {
	var flag_icon = new GIcon();
	flag_icon.image 			= "http://" + location.hostname + "/img/gmap_drapeau.png";
	flag_icon.iconSize 			= new GSize(26, 46);
	flag_icon.iconAnchor 		= new GPoint(19, 44);

	var markerOptions 	= { "icon" : flag_icon };
	return new GMarker(latlng, markerOptions);
}