
/**************************************************/
/* verification du formulaire pour le squelette : */
/* rubrique_outils_particuliers_trouvezcodepostal */
/**************************************************/

function tcp_ValidFormulaire() {
	var OK;
	OK = true;
	if ((document.cpform.commune.value.length == 0) && (document.cpform.cp.value.length == 0)) {
		OK = false;
		tcp_CriteresInvalides();
	}
	else {
		if ((document.cpform.commune.value.length != 0) && (document.cpform.cp.value.length != 0)) {
			if (document.cpform.cp.value.length < 2)  {
				OK = false;
				tcp_CriteresInvalides();
			}
		}
		else {
			if (document.cpform.commune.value.length != 0)  {
				if (document.cpform.commune.value.length < 3)  {
					OK = false;
					tcp_CriteresInvalides();
				}
			}
			else {
				if (document.cpform.cp.value.length < 3)  {
					OK = false;
					tcp_CriteresInvalides();
				}
				else {
					if (!document.cpform.cp.value.match('[0-9]+')) {
						OK = false;
						tcp_CriteresInvalides();
					}
				}
			}
		}
	}
	if (OK) {
		document.cpform.submit();
	}
	else {
		document.cpform.cp.focus();
	}
}

function tcp_CriteresInvalides() {
	alert("CRITERES DE RECHERCHE INVALIDES.\n" +
		"Vous devez saisir au moins : \n" +
		"\t* 2 caractères pour le Code Postal/CEDEX ET 1 caractère pour la commune\n" +
		"OU\t* 3 caractères pour le Code Postal/CEDEX\n" +
		"OU\t* 3 caractères pour la commune\n");
}

function initialize() {
	var address = new Array();
	var libelle = new Array();
	var geocoder = new GClientGeocoder();
	var principal;
	var pos;

	if (GBrowserIsCompatible()) {
		var map = new GMap2(
			document.getElementById("googleMap"),
			{ size: new GSize(306,420) }
		);
	}

	$('.adresse').each(function(k) {

		if($(this).attr('rel') == "principal"){
			address.unshift($(this).val());
			principal = k;
		}
		else
			address[k] = $(this).val();

	});

	$('.adresse').each(function(i) {

		if($(this).attr('rel') == "principal"){
			libelle.unshift($(this).attr('name'));
		}
		else
			libelle[i] = $(this).attr('name');

	});


//	address.unshift(address[principal]);
//	delete(address[principal]);

	var index = 0;

	// Attention, fonction auto-appelante pour parcourir le tableau des adresses.
	function getCoord(elmt,libelle,principal) {
		geocoder.getLatLng(
			elmt,
			function (point) {
	  			if (!point) {
	  				alert("Impossible d'afficher sur la carte l'adresse "+elmt);
					return;
	  			}
				var marker = new GMarker(point);

				var laposteIcon = new GIcon(G_DEFAULT_ICON);
			   	laposteIcon.image = "/design/laposte/images/img/bureau_poste/logo_laposte.png";
			   	markerOptions = { icon:laposteIcon };

			   	index++;

				if(index == 1) {
			   		laposteIcon.iconSize = new GSize(42, 32);
					map.setCenter(new GLatLng(point.lat(), point.lng() ), 15);
			   	}
			   	else laposteIcon.iconSize = new GSize(30, 20);

			   	laposteIcon.shadow = "";

			   	map.addOverlay(createMarker(point,libelle, markerOptions));

				if (index%10) {
					callGetCoord();
				} else {
					window.setTimeout(callGetCoord, 2700);
				}
			}
		);
	}
	function createMarker(point,libelle,markerOptions) {
	  var marker = new GMarker(point,markerOptions);

	  GEvent.addListener(marker, "click", function() {
	    var myHtml = "<div class=\"TexteInfobulle\"><b>"+libelle+"</b></div>";
	    var maxWidth = '80';
	    map.openInfoWindowHtml(point, myHtml, {maxWidth: '20'});
	  });
	  return marker;
	}

	function callGetCoord() {

			if(address.length){

				getCoord(address.shift(),libelle.shift(),principal);
			}

	}

	callGetCoord();

	var typeControl = new GMapTypeControl();
	map.addControl(typeControl);

	var zoom = new GLargeMapControl3D();
	map.addControl(zoom);



	var customUI = map.getDefaultUI();
	customUI.maptypes.hybrid = false;

}