var map_content;
var geocoder;
var zones = new Array();
var pdl = 0
var marker = null;
var id = 0;
var wait = 1;
var msg = '';


function isPhone(s) {
	var regex = new RegExp(/^(01|02|03|04|05|06|07|08|09)[0-9]{8}/gi);
	if(regex.test(s)) return true;
	else return false;

}


function map_initialize() {
	if (GBrowserIsCompatible()) {
		map_content = new GMap2(document.getElementById('map'));
		map_content.setMapType(G_NORMAL_MAP);
		map_content.setCenter(new GLatLng(48.4383, 1.465), 14);
		map_content.addControl(new GLargeMapControl());
		map_content.addControl(new GMapTypeControl());
		map_content.enableScrollWheelZoom();
		map_content.addControl(new GScaleControl());
		
		geocoder = new GClientGeocoder();
	}
}


function display_polygon(points, color, commentaire) {
	zones.push(new GPolygon(points, "#" + color, 2, 1, "#" + color, 0.4));
   
	/*GEvent.addListener(zones[zones.length - 1], "click", function(point) {
		map_content.openInfoWindowHtml(point, commentaire);
	});*/
	
	map_content.addOverlay(zones[zones.length - 1]);
}



function display_zone_ftth(xml) {
	var bounds = new GLatLngBounds();
	var zones = 0;
	
	$(xml).find("zone").each(function() {
		p = $(this).find("point").text();
		commentaire = $(this).find("commentaire").text();
		//idpdl = $(this).attr("id");

		p = p.split(';');
		var points = new Array();
		
		for(i = 0; i < p.length; i ++) {
			if(p[i].length > 0) {
				vertex = p[i].split(',');
				if((vertex[0] != 0) && (vertex[1] != 0)) points.push(new GLatLng(vertex[1], vertex[0]));
				bounds.extend(new GLatLng(vertex[1], vertex[0]));
			}
		}

		display_polygon(points, '00ff00', commentaire);
		//else display_polygon(points, 'ff0000', commentaire);
		
		zones ++;
	});
	
	if(zones > 0) {
		var center = bounds.getCenter();
		var zoom = map_content.getBoundsZoomLevel(bounds);
		marker.setLatLng(center);
		map_content.setCenter(center, zoom);
		if(test.length > 0) map_content.openInfoWindowHtml(marker.getLatLng() , msg);
	}
	
	test = '';

}

function getCPostal(xml) {
	$(xml).find("root").each(function() {
		$("#cpostal").empty();
		$("#cpostal").append('<option value="0">Code Postal</option>');
		
		$(this).find("cpostal").each(function() {
			$("#cpostal").append('<option value="' + $(this).text() + '">' + $(this).text() + '</option>');
		});
	});
	if($("#cpostal").val() == 0) getCommune(null);
}


function getCommune(xml) {
	if($("#cpostal").val() > 0) {
		$("#commune").removeAttr("disabled");
		
		$(xml).find("root").each(function() {
			$("#commune").empty();
			$("#commune").append('<option value="0">Commune</option>');
			
			$(this).find("commune").each(function() {
				$("#commune").append('<option value="' + $(this).attr('insee') + '">' + $(this).find("libellemaj3").text() + '</option>');
			});
		});
	}
	else {
		$("#commune").attr("disabled", "disabled");
		$("#commune").empty();
		$("#commune").append('<option value="0">Commune</option>');
	}
	
	if($("#commune").val() == 0) getTypeVoie();
}

function getTypeVoie(xml) {
	if($("#commune").val() > 0) {
		locate();
		$("#type").removeAttr("disabled");
		
		$(xml).find("root").each(function() {
			$("#type").empty();
			$("#type").append('<option value="0">Type de voie</option>');
			
			$(this).find("type").each(function() {
				$("#type").append('<option value="' + $(this).attr('id') + '">' + $(this).find("libelle").text() + '</option>');
			});
		});
	}
	else {
		$("#type").attr("disabled", "disabled");
		$("#type").empty();
		$("#type").append('<option value="0">Type de voie</option>');
	}
	
	$("#type").val(0);
	getVoie(null);
}

function getVoie(xml) {

	if($("#type").val() != 0) {
		$("#voie").removeAttr("disabled");
		
		$(xml).find("root").each(function() {
			$("#voie").empty();
			$("#voie").append('<option value="0">Voie</option>');
			
			$(this).find("voie").each(function() {
				$("#voie").append('<option value="' + $(this).attr('rivoli') + '">' + $(this).find("libelle").text() + '</option>');
			});
		});
	}
	else {
		$("#voie").attr("disabled", "disabled");
		$("#voie").empty();
		$("#voie").append('<option value="0">Voie</option>');
	}
	
	if($("#voie").val() == 0) getNumero();
}

function getNumero() {
	if($("#voie").val() != 0) {
		locate();
		$("#numero").removeAttr("disabled");
		$("#article").removeAttr("disabled");
	}
	else {
		$("#numero").attr("disabled", "disabled");
		$("#article").attr("disabled", "disabled");
	}
	
	getTest();
	
	$("#article").val('');
	$("#numero").val('');
}

function getTest() {
	if($("#numero").val().length > 0 && isNaN($("#numero").val())) {
		$("#numero").val('');
	}

	if($("#voie").val() != 0) $("#test").removeAttr("disabled", "disabled");
	else $("#test").attr("disabled", "disabled");
}


function locate() {
	test = '';
	if($("#cpostal").val() > 0) {
		test += $("#cpostal").val();
	}
	if($("#commune").val() > 0) {
		test += ' ' + $("#commune").find('option:selected').text();
	}
	if($("#type").val() != 0) {
		test += ' ' + $("#type").find('option:selected').text();
	}
	if($("#voie").val() != 0) {
		test += ' ' + $("#voie").find('option:selected').text();
	}
	if($("#numero").val() != '') {
		test += ' ' + $("#numero").val();
		if($("#article").find('option:selected').text() != '') {
			test += $("#article").val();
		}
	}
	
	geocoder.getLatLng(test + " FRANCE" , showLocate);
}

function showLocate(point) {
	map_content.setCenter(point, 14);
	if(marker) map_content.removeOverlay(marker);
	marker = new GMarker(point);
	map_content.addOverlay(marker);
}

function getFtth(xml) {
	
	var result = 0;
	var habs = new Array();
	var list = '';
	var numero = '';
	
	$(xml).find("eligibilite").each(function() {
		result = $(this).attr("result");
		logid = $(this).attr("id");
		
		$(this).find("habitation").each(function() {
			//pdl = $(this).find("habitation").attr('pdl');
			id = $(this).attr("id");
			numero = $(this).find('numero').text() + $(this).find('complement').text();
			
			if(habs.length < 8) {
				list += $(this).find('numero').text() + $(this).find('complement').text() + ' ' + $("#type").find('option:selected').text() + ' ' + $("#voie").find('option:selected').text() + '<br />';
			}
			
			habs.push(id);
		});
	});
	
	if(habs.length >= 8) list += '...';
	
	for(i in zones) map_content.removeOverlay(zones[i]);	
	
	if(result == 0) display_zone(habs);
	

	test = '';
	
	if($("#numero").val() != '') {
		test += ' ' + $("#numero").val();
		if($("#article").find('option:selected').text() != '') {
			test += $("#article").val();
		}
	}
	else test = numero + ' ' + test;
	if($("#type").val() != 0) {
		test += ' ' + $("#type").find('option:selected').text();
	}
	if($("#voie").val() != 0) {
		test += ' ' + $("#voie").find('option:selected').text();
	}
	if($("#cpostal").val() > 0) {
		test += '<br />' + $("#cpostal").val();
	}
	if($("#commune").val() > 0) {
		test += ' ' + $("#commune").find('option:selected').text();
	}
	
	
	if(result == 0) {
		if(habs.length > 1) msg = '<p style="text-align: center; center; margin: 5px; font-weight: bold;"><img style="verticla-align: middle;" src="img/tick.png" /> Adresses éligibles</p>' + list;
		else {
			msg = '<p style="text-align: center; center; margin: 5px; font-weight: bold;"><img style="verticla-align: middle;" src="img/tick.png" />Adresse éligible</p>' + test;
		}
	}
	else {
		msg = '<p style="text-align: center; margin: 5px; font-weight: bold;"><img style="verticla-align: middle;" src="img/cross.png" /> Non &eacute;ligible</p>' + test;
	}
	
	
	if(result > 0) {
		map_content.addOverlay(marker);
		map_content.openInfoWindowHtml(marker.getLatLng() , msg);
		map_content.setCenter(marker.getLatLng());
	}
	else {
		map_content.removeOverlay(marker);
		//map_content.openInfoWindowHtml(marker.getLatLng() , test);
		//map_content.setCenter(marker.getLatLng());
	}
	
	var message;
	var message2;
	
	if(result == 0) {
		message = 'Vous &ecirc;tes &eacute;ligible &agrave; la fibre optique sur le r&eacute;seau C&eacute;li&eacute;no ';
		message += "Contactez l'un des op&eacute;rateurs pr&eacute;sents sur le r&eacute;seau : <br /><br />"; 
		message += '<a href="http://eligibilite-ftth.regies.fr/?fai="><img src="img/ftth/operateurs.png" alt="Liste des op&eacute;rateurs"></a><br /><br />';
		message += 'ou laissez nous vous contacter : ';
		message2 = 'ou recommencez ce test : ';

	}
	else if(result == 1) {
		message = 'Vous n\'&ecirc;tes pas &eacute;ligible &agrave; la fibre optique, ';
		message += 'cependant votre commune &eacute;tant d&eacute;j&agrave; partiellement &eacute;ligible, vous pouvez faire une demande d\'&eacute;tude de raccordement en remplissant le formulaire ci-dessous : ';
		message2 = 'Vous pouvez aussi tester votre &eacute;ligibilit&eacute; ';
		message2 += '&agrave; la Boucle Locale Radio : <br /><br />';
		message2 += '<a href="http://eligibilite-radio.regies.fr"><img src="img/boutonblr.png" alt="Testez votre &eacuteligibilit&eacute; radio"></a><br /><br />';
		message2 += ' ou recommencez ce test : ';
	}
	else {
		message = 'Vous n\'&ecirc;tes pas &eacute;ligible &agrave; la fibre optique, vous pouvez cependant nous laisser vos coordonnées afin que nous puissions vous contacter en cas de changement dans votre commune : ';
		message2 = 'Vous pouvez aussi tester votre &eacute;ligibilit&eacute; ';
		message2 += '&agrave; la Boucle Locale Radio : <br /><br />';
		message2 += '<a href="http://eligibilite-radio.regies.fr"><img src="img/boutonblr.png" alt="Testez votre &eacuteligibilit&eacute; radio"></a><br /><br />';
		message2 += ' ou recommencez ce test : ';
	}
	
	$("#message").html(message);
	$("#message2").html(message2);
	$("#contact").css('display', 'block');
	
	$("#logid").val(logid);
	$("#prenom").val('');
	$("#nom").val('');
	$("#telephone").val('');
	
	$("#nom").removeAttr("disabled");
	$("#prenom").removeAttr("disabled");
	$("#telephone").removeAttr("disabled");
	$("#send").removeAttr("disabled");
	
	$("#recherche").css('display', 'none');
	$("#resultat").css('display', 'block');
}


function restart2() {
	$("#resultat").css('display', 'none');
	$("#contact").css('display', 'none');
	$("#recherche").css('display', 'block');
	
	$("#nom").attr("disabled", "disabled");
	$("#prenom").attr("disabled", "disabled");
	$("#telephone").attr("disabled", "disabled");
	$("#send").attr("disabled", "disabled");
	$("#send").val("Envoyer");
}


function getContact(xml) {
	$("#nom").attr("disabled", "disabled");
	$("#prenom").attr("disabled", "disabled");
	$("#telephone").attr("disabled", "disabled");
	$("#send").attr("disabled", "disabled");
	$("#send").val("Envoyé!");
}


$().ready(function() {
	map_initialize();
	//display_zone();
	init();
});

