
/**************************************************/
/* 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 latitude = new Array();
	var longitude = new Array();
	var geocoder = new google.maps.Geocoder();
	var principal;
	var pos;

	//if (google.maps.BrowserIsCompatible()) {
		/*var map = new GMap2(
			document.getElementById("googleMap"),
			{ size: new GSize(306,420) }
		);*/

		var latlng = new google.maps.LatLng(48.85, 2.34);
		var maptypecontroloptions = {
			mapTypeIds:[google.maps.MapTypeId.SATELLITE, google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.HYBRID],
			style:google.maps.MapTypeControlStyle.HORIZONTAL_BAR
		};
		var myOptions = {
			size: new google.maps.Size(306,420),
			zoom: 15,
			zoomControl:true,
			/*zoomControlOptions: {
				style:google.maps.ZoomControlStyle.LARGE
			},*/
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			mapTypeControl:true,
			mapTypeControlOptions:maptypecontroloptions,
			navigationControlOptions: {
				style: google.maps.NavigationControlStyle.ZOOM_PAN
			},
			streetViewControl:false,
			panControl:true,
			scaleControl:false
			
		};
		var map = new google.maps.Map(document.getElementById('googleMap'), myOptions);	
			
	//}

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

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

	var index = 0;

	// Attention, fonction auto-appelante pour parcourir le tableau des adresses.
	function getCoord(elmt, libelle, latitude, longitude, principal) {
		var point = new google.maps.LatLng(latitude, longitude);
		if (!point) {
			alert("Impossible d'afficher sur la carte l'adresse "+elmt);
			return;
		}
		
		var laposteIcon = new google.maps.MarkerImage();
		laposteIcon.url = "/design/laposte/images/img/bureau_poste/logo_laposte.png";
		//markerOptions = { icon:laposteIcon };

		index++;

		if(index == 1) {
			laposteIcon.size = new google.maps.Size(42, 32);
			map.setCenter(new google.maps.LatLng(latitude, longitude));
		}
		else{
			laposteIcon.scaledSize = new google.maps.Size(30, 20);
			laposteIcon.size = new google.maps.Size(30, 20);
		}
		
		var markerOptions = {
			map:map,
			icon:laposteIcon,
			position:point,
			title:libelle
		};
		var marker = createMarker(markerOptions, libelle);
		//var marker = new google.maps.Marker(markerOptions);
		//map.addOverlay(createMarker(point, libelle, markerOptions));
		
		if (index%10) {
			callGetCoord();
		} else {
			window.setTimeout(callGetCoord, 2700);
		}
	}
	
	function createMarker(markerOptions, libelle) {
		var marker = new google.maps.Marker(markerOptions);
		
		google.maps.event.addListener(marker, "click", function() {
			var infoWindowOptions = {
				content:"<div class=\"TexteInfobulle\"><b>"+libelle+"</b></div>",
				maxWidth:'20'
			};
			var info = new google.maps.InfoWindow(infoWindowOptions);
			info.open(map, this);
		});
		/*google.maps.event.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(),latitude.shift(), longitude.shift(), principal);
		}

	}

	callGetCoord();

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

}
