//liste des markers
var map;
var geocoder;
var lastlettre = '';
var nbrequest = 0;
var maxrequest = 3;
var myactivity = "ALL";
var lastaddress = "";
var lastCP = "";
var lastCommune = "";
var pointlist = [];
var markers = [];
var markers_on_map = [];
var markerno = 0;
var mgr;
var baseIcon;
var gdir;
var ent_description;
var streetviewclient;
var streetviewpano;
var streetworking = false;
var catimages = new Array();
var catoverlays = new Array();
function InitializeControl() {
}
InitializeControl.prototype = new GControl();

// Creates a one DIV for each of the buttons and places them in a container
// DIV which is returned as our control element. We add the control to
// to the map container and return the element for the map class to
// position properly.
InitializeControl.prototype.initialize = function(map) {
	var container = document.createElement("div");

	var initDiv = document.createElement("div");
	// this.setButtonStyle_(initDiv);
	container.appendChild(initDiv);
	if (document.getElementById("lg").value == "en"
			|| document.getElementById("lg").value == "EN"
			|| document.getElementById("lg").value == "En") {
		initDiv.appendChild(document.createTextNode("Initialize"));
	} else {
		initDiv.appendChild(document.createTextNode("Initialiser"));
	}
	GEvent.addDomListener(initDiv, "click", function() {
		// GUnload();
			initialize();
		});

	this.setButtonStyle_(initDiv);
	map.getContainer().appendChild(container);
	return container;
}

InitializeControl.prototype.setButtonStyle_ = function(button) {
	button.style.textDecoration = "none";
	button.style.color = "#000000";
	button.style.backgroundColor = "white";
	button.style.font = "small Arial";
	button.style.border = "1px solid black";
	button.style.padding = "1px";
	button.style.marginBottom = "3px";
	button.style.textAlign = "center";
	button.style.width = "6em";
	button.style.cursor = "pointer";
	button.style.fontWeight = "bold";
}

// By default, the control will appear in the top left corner of the
// map with 7 pixels of padding.
InitializeControl.prototype.getDefaultPosition = function() {
	return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(100, 9));
}

function showHideExtras(catid, show) {
	if (catid == 0) {
		catoverlays.each( function(el) {
			if (el != undefined && el != null) {
				el.each( function(element) {
					if (element != undefined && element != null) {
						if (show)
							element.show();
						else {
							element.closeInfoWindow();
							element.hide();
						}
					}
				});
			}
		});
	} else {
		catoverlays[catid].each( function(el, index) {
			if (el != undefined && el != null) {
				if (show)
					el.show();
				else {
					el.closeInfoWindow();
					el.hide();
				}
			}
		});
	}

}

function addExtrasToMap() {
	if (catoverlays != undefined && catoverlays != null
			&& catoverlays.length > 0) {
		catoverlays.each( function(el) {
			if (el != undefined && el != null && el.length > 0) {
				el.each( function(element) {
					if (element != undefined && element != null)
						map.addOverlay(element);
				});
			}
		});
	}
}

function initialize() {
	$('searchinput').value = '';
	$('activiteselect').value = 'TOUTES-ALL';
	map = new GMap2(document.getElementById("map_canvas"));
	var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10, 10));
	map.addControl(new GMenuMapTypeControl(), topRight);
	map.addControl(new GLargeMapControl());
	map.addControl(new GOverviewMapControl());
	map.addControl(new InitializeControl());
	map.setCenter(new GLatLng(43.61607, 7.051163), 13);
	geocoder = new GClientGeocoder();
	var pointSW = new GLatLng(43.51619059561272, 6.910400390625);
	var pointNE = new GLatLng(43.73637542794859, 7.307624816894531);
	geocoder.setViewport(new GLatLngBounds(pointSW, pointNE));
	baseIcon = new GIcon();
	baseIcon.shadow = "administrator/images/shadow.png";
	// baseIcon.iconSize = new GSize(20, 34);
	baseIcon.iconAnchor = new GPoint(9, 34);
	baseIcon.infoWindowAnchor = new GPoint(9, 2);
	baseIcon.infoShadowAnchor = new GPoint(22, 30);
	mgr = new MarkerManager(map);
	addEntreprises();
	streetviewclient = new GStreetviewClient();
	var topRightExtra = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(200,
			9));
	map.addControl(new ExtrasControl(), topRightExtra);
	addExtrasToMap();
	GEvent.addDomListener(map, "click", function() {
		$('extracats').hide();
	});
}

function enleveBatAdresse(adresse, nbMots) {
	var chaine = adresse;
	var returnVal = "";
	var reg = new RegExp("[ ,]+", "g");
	var tableau = chaine.split(reg);
	var i = 0;
	var loopNum = tableau.length - nbMots;
	if (loopNum <= 3)
		loopNum = 3;
	while (i < loopNum) {
		returnVal = returnVal + ' ' + tableau[i];
		i++;
	}
	return returnVal;
}

// addAddressToMap() is called when the geocoder returns an
// answer. It adds a marker to the map with an open info window
// showing the nicely formatted version of the address and the country code.
function addAddressToMap(id, response) {
	// map.clearOverlays();
	if (streetviewpano != null && !streetviewpano.isHidden())
		hideSView();
	if (streetworking) {
		setTimeout( function() {
			addAddressToMap(id, response)
		}, 500);
	} else {

		if (!response || response.Status.code != 200) {
			if (!response || nbrequest >= maxrequest) {
				nbrequest = 0;
				alert(document.getElementById("mesGMAP").value);
				findLocation(id, "Sophia Antipolis 06560");
			} else if (response) {
				nbrequest++;
				findLocation(id, enleveBatAdresse(lastaddress, nbrequest) + ' '
						+ lastCP + ' ' + lastCommune);
				return false;
			}

		} else {
			var place = response.Placemark[0];
			if (place.Point.coordinates[1] > 43.51619059561272
					&& place.Point.coordinates[0] > 6.910400390625
					&& place.Point.coordinates[1] < 43.73637542794859
					&& place.Point.coordinates[0] < 7.307624816894531) {
				var point = new GLatLng(place.Point.coordinates[1],
						place.Point.coordinates[0]);

				var marker = new GMarker(point, {
					draggable : true
				});
				marker.disableDragging();
				var adaffichelg = "Adresse affich&eacute;e";
				if (document.getElementById("lg").value == "en") {
					adaffichelg = "Address on map";
				}
				var infotabs = [];
				infotabs
						.push(new GInfoWindowTab(
								"Description",
								'<div class="infobulle" id="infobulle_'
										+ id
										+ '">'
										+ ent_description
										+ '<br/>'
										+ adaffichelg
										+ ' : '
										+ place.address
										+ "<br/>"
										+ "<b>GPS : </b>Lat:"
										+ point.lat()
										+ ", Lng:"
										+ point.lng()
										+ "</div>"
										+ "<table width='100%'><tr>"
										+ "<td style='text-align:left;'>"
										+ "<div id='street_link_"
										+ id
										+ "' class='street_view_link'>Loading StreetView...</div>"
										+ "<script language='javascript'>hasStreetView(new GLatLng("
										+ point.lat()
										+ ","
										+ point.lng()
										+ "), "
										+ id
										+ ");</script>"
										+ "</td><td style='text-align:right;'><span style='vertical-align: super;font-size:10px;color:#d00;font-weight:bold;'>"
										+ ($('lg').value.toLowerCase() == 'fr' ? "Nouveau"
												: "New")
										+ "&nbsp;</span><a href='#' onclick=\"editEntreprise('"
										+ id
										+ "');\">"
										+ ($('lg').value.toLowerCase() == 'fr' ? "C'est votre entreprise ?"
												: "You own this company ?")
										+ "</a></td></tr>"
										+ (url_sf != undefined
												&& url_sf != null ? "<tr>"
												+ "<td colspan='2' style='text-align:center;border-top:solid 1px #ccc;'><a href='"
												+ url_sf
												+ "' target='_blank' style='text-decoration:none;'>"
												+ ($('lg').value.toLowerCase() == 'fr' ? "Commandez ou réservez en ligne sur "
														: "Command or reserve online with ")
												+ "<img src='http://www.searchfood.com/images/logo_wis.png'/></a><br/>"
												+ ($('lg').value.toLowerCase() == 'fr' ? "partenaire de WhereInSophia"
														: "partner of WhereInSophia")
												+ "</td>" + "</tr>"
												: "") + "</table>"));

				if (document.getElementById("lg").value == "en"
						|| document.getElementById("lg").value == "EN"
						|| document.getElementById("lg").value == "En") {
					infotabs
							.push(new GInfoWindowTab(
									document.getElementById("versval").value,
									'<div id="versdiv" onclick="fermeWindows();"><div id="mesavancementvers">Type company\'s name</div><br/><span class="tofrominfo">Start <input type="radio" name="entoraddressvers" id="addressradiovers" value="address" onclick="javascript:addressSelected();"/><span class="tofrominfodis" id="addressChoiceVers"> address: </span><input type="radio" name="entoraddressvers" id="entrepriseradiovers" value="entreprise" onclick="javascript:entrepriseSelected();" checked><span class="tofrominfo" id="entrepriseChoiceVers"> company:</span></span><br/><input type="text" name="toaddress" class="adressinput" id="toaddress" onkeyup="javascript:addresstyped();"/><br/><div class="entSelectionHidden" id="entSelectionDivVers"></div><br/><span class="tofrominfo">End address: </span><br/><b>'
											+ place.address
											+ '</b><br/><br/><input type="button" name="GoTrace" onclick="javascript:traceRoute(\''
											+ place.address
											+ '\');return false;" value="Go!" id="Govers" disabled><input type="hidden" id="addressvalvers" name="addressvalvers"></div>'));
					infotabs
							.push(new GInfoWindowTab(
									document.getElementById("apartirval").value,
									'<div id="apartirdiv" onclick="fermeWindows();"><div id="mesavancementapartir">Type company\'s name</div><br/><span class="tofrominfo">Start address: </span><br/><b>'
											+ place.address
											+ '</b><br/><br/><span class="tofrominfo">End <input type="radio" name="entoraddress" id="addressradioapartir" onclick="javascript:addressSelected();"/><span class="tofrominfodis" id="addressChoiceApartir"> address: </span><input type="radio" name="entoraddress" id="entrepriseradioapartir" onclick="javascript:entrepriseSelected();" checked><span class="tofrominfo" id="entrepriseChoiceApartir"> company:</span></span><br/><input type="text" class="adressinput" name="fromaddress" id="fromaddress" onkeyup="javascript:addresstyped();"/><br/><div class="entSelectionHidden" id="entSelectionDivApartir"></div><br/><input type="button" name="GoTrace" onclick="javascript:traceRoute(\''
											+ place.address
											+ '\');return false;" value="Go!" id="Goapartir" disabled><input type="hidden" id="addressvalapartir" name="addressvalapartir"></div>'));
				} else {
					infotabs
							.push(new GInfoWindowTab(
									document.getElementById("versval").value,
									'<div id="versdiv" onclick="fermeWindows();"><div id="mesavancementvers">Tapez le nom de l\'entreprise</div><br/><span class="tofrominfo"><input type="radio" name="entoraddressvers" id="addressradiovers" value="address" onclick="javascript:addressSelected();"/><span class="tofrominfodis" id="addressChoiceVers">Adresse </span><input type="radio" name="entoraddressvers" id="entrepriseradiovers" value="entreprise" onclick="javascript:entrepriseSelected();" checked><span class="tofrominfo" id="entrepriseChoiceVers"> Entreprise</span> de départ : </span><br/><input type="text" name="toaddress" class="adressinput" id="toaddress" onkeyup="javascript:addresstyped();"/><br/><div class="entSelectionHidden" id="entSelectionDivVers"></div><br/><span class="tofrominfo">Adresse d\'arrivée : </span><br/><b>'
											+ place.address
											+ '</b><br/><br/><input type="button" name="GoTrace" onclick="javascript:traceRoute(\''
											+ place.address
											+ '\');return false;" value="Go!" id="Govers" disabled><input type="hidden" id="addressvalvers" name="addressvalvers"></div>'));
					infotabs
							.push(new GInfoWindowTab(
									document.getElementById("apartirval").value,
									'<div id="apartirdiv" onclick="fermeWindows();"><div id="mesavancementapartir">Tapez le nom de l\'entreprise</div><br/><span class="tofrominfo">Adresse de départ : </span><br/><b>'
											+ place.address
											+ '</b><br/><br/><span class="tofrominfo"><input type="radio" name="entoraddress" id="addressradioapartir" onclick="javascript:addressSelected();"/><span class="tofrominfodis" id="addressChoiceApartir"> Adresse </span><input type="radio" name="entoraddress" id="entrepriseradioapartir" onclick="javascript:entrepriseSelected();" checked><span class="tofrominfo" id="entrepriseChoiceApartir"> Entreprise </span>d\'arrivée : </span><br/><input type="text" class="adressinput" name="fromaddress" id="fromaddress" onkeyup="javascript:addresstyped();"/><br/><div class="entSelectionHidden" id="entSelectionDivApartir"></div><br/><input type="button" name="GoTrace" onclick="javascript:traceRoute(\''
											+ place.address
											+ '\');return false;" value="Go!" id="Goapartir" disabled><input type="hidden" id="addressvalapartir" name="addressvalapartir"></div>'));
				}

				marker.bindInfoWindowTabsHtml(infotabs, {
					maxWidth : 350
				});

				map.addOverlay(marker);
				marker.openInfoWindowTabsHtml(infotabs, {
					maxWidth : 350
				});
				markers_on_map[id] = marker;

				nbrequest = 0;
			} else {
				nbrequest = 0;
				alert(document.getElementById("mesGMAP").value);
				findLocation(id, "Sophia Antipolis 06560");
			}
		}
	}
}

function editEntreprise(id) {
	var m = markers_on_map[id];
	var content = "";
	if ($('lg').value.toLowerCase() == 'fr')
		content = "<p>Votre entreprise est mal placée ?</p>"
				+ "<p><a href='#' onclick=\"relocateEnt('" + id
				+ "')\">Relocalisez la en cliquant ici !</a></p>"
				+ "<p>Il y a des informations manquantes ou incorrectes ?</p>"
				+ "<p><a href='#' onclick=\"correctEnt('" + id + "','"
				+ m.getLatLng().lng() + "','" + m.getLatLng().lat()
				+ "')\">Cliquez ici pour les corriger !</a></p>";
	else
		content = "<p>Location of your company is inaccurate ?</p>"
				+ "<p><a href='#' onclick=\"relocateEnt('"
				+ id
				+ "')\">Click here to move it!</a></p>"
				+ "<p>Are there some incorrect or inaccurate informations ?</p>"
				+ "<p><a href='#' onclick=\"correctEnt('" + id + "','"
				+ m.getLatLng().lng() + "','" + m.getLatLng().lat()
				+ "')\">Click here to correct them !</a></p>";

	m.openInfoWindowHtml(content);
}
var ex_latlng = null;
var moving = null;
var closewin_event = null;
function relocateEnt(id) {
	var m = markers_on_map[id];
	m.enableDragging();
	moving = m;
	m
			.openInfoWindowHtml("<div>"
					+ "<p>Déplacez la punaise à l'emplacement où vous désirez que votre entreprise se positionne."
					+ "</p></div>");
	GEvent.addListener(m, "dragstart", function(latlng) {
		ex_latlng = latlng;
		m.closeInfoWindow();
	});
	GEvent
			.addListener(
					m,
					"dragend",
					function(latlng) {
						m
								.openInfoWindowHtml("<div style='width:350px;' id='send_result_"
										+ id
										+ "'><p>Pour enregistrer la nouvelle emplacement de votre entreprise veuillez renseigner votre nom et prénom accompagé de votre adresse e-mail. Notez que les modifications seront d'abord validées par un administrateur."
										+ "</p><p>"
										+ "<label for='nom_prenom'>Votre nom et prénom : </label><input type='text' name='nom_prenom' id='nom_prenom'/>"
										+ "</p><p>"
										+ "<label for='email'>Votre adresse e-mail : </label><input type='text' name='email' id='email'/>"
										+ "</p><p>"
										+ "<input type='button' value='Annuler' onclick=\"moving.closeInfoWindow();moving.setLatLng(new GLatLng("
										+ ex_latlng.lat()
										+ ","
										+ ex_latlng.lng()
										+ "));GEvent.trigger(moving,'click');\"/>"
										+ "<input type='button' value='Enregistrer' onclick=\"if($('email').value!='' && $('nom_prenom').value!=''){sendLocationData($('nom_prenom').value,$('email').value,'"
										+ id
										+ "',"
										+ latlng.lat()
										+ ", "
										+ latlng.lng()
										+ ");}\"/>"
										+ "</p></div>");
					});
	closewin_event = GEvent.addListener(m, "infowindowclose", function() {
		m.closeInfoWindow();
		m.setLatLng(new GLatLng(ex_latlng.lat(), ex_latlng.lng()));
		GEvent.removeListener(closewin_event);
	});
}

function correctEnt(id, lo, la) {
	showNewEditEnt(id, lo, la);
}

function sendLocationData(name, email, id, lat, lng) {
	new Ajax.Updater(
			'send_result_' + id,
			'/save_ent_proposition.php',
			{
				parameters : "name=" + name + "&email=" + email + "&id=" + id
						+ "&la=" + lat + "&lo=" + lng
						+ "&onlylocation=true&exlo=" + ex_latlng.lng()
						+ "&exla=" + ex_latlng.lat(),
				onSuccess : function() {
					var m = markers_on_map[id];
					m.closeInfoWindow();
					m.setLatLng(new GLatLng(ex_latlng.lat(), ex_latlng.lng()));
					m.disableDragging();
					m
							.openInfoWindowHtml('<div style="width:350px;">Les informations ont bien été enregistrées. Elles seront mises en ligne le plus rapidement possible. Nous vous en remercions.</div>')
					moving = null;
					ex_latlng = null;
				},
				onFailure : function() {
					var m = markers_on_map[id];
					// m.closeInfoWindow();
				m.setLatLng(new GLatLng(ex_latlng.lat(), ex_latlng.lng()));
				m.disableDragging();
				// m.openInfoWindowHtml('<div style="width:350px;">Une erreur
				// est survenue, veuillez nous en excuser et reéssayer un peu
				// plus tard.</div>')
				moving = null;
				ex_latlng = null;
			}
			});
	GEvent.removeListener(closewin_event);

}

function addPinOnMap(id, la, lo, address) {

	var point = new GLatLng(Number(la), Number(lo));

	var marker = new GMarker(point, {
		draggable : true
	});
	marker.disableDragging();
	var adaffichelg = "Adresse affich&eacute;e";
	if (document.getElementById("lg").value == "en") {
		adaffichelg = "Address on map";
	}
	var infotabs = [];
	infotabs
			.push(new GInfoWindowTab(
					"Description",
					'<div class="infobulle" id="infobulle_'
							+ id
							+ '">'
							+ ent_description
							+ '<br/>'
							+ adaffichelg
							+ ' : '
							+ address
							+ "<br/>"
							+ "<b>GPS : </b>Lat:"
							+ point.lat()
							+ ", Lng:"
							+ point.lng()
							+ "</div>"
							+ "<table width='100%'><tr>"
							+ "<td style='text-align:left;'>"
							+ "<div id='street_link_"
							+ id
							+ "' class='street_view_link'>Loading StreetView...</div>"
							+ "<script language='javascript'>hasStreetView(new GLatLng("
							+ point.lat()
							+ ","
							+ point.lng()
							+ "), "
							+ id
							+ ");</script>"
							+ "</td><td style='text-align:right;'><a href='#' onclick=\"editEntreprise('"
							+ id
							+ "');\">"
							+ ($('lg').value.toLowerCase() == 'fr' ? "C'est votre entreprise ?"
									: "You own this company ?")
							+ "</a></td></tr>"
							+ (url_sf != undefined && url_sf != null ? "<tr>"
									+ "<td colspan='2' style='text-align:center;border-top:solid 1px #ccc;'><a href='"
									+ url_sf
									+ "' target='_blank' style='text-decoration:none;'>"
									+ ($('lg').value.toLowerCase() == 'fr' ? "Commandez ou réservez en ligne sur "
											: "Command or reserve online with ")
									+ "<img src='http://www.searchfood.com/images/logo_wis.png' style='border:0;text-decoration:none;'/></a><br/>"
									+ ($('lg').value.toLowerCase() == 'fr' ? "partenaire de WhereInSophia"
											: "partner of WhereInSophia")
									+ "</td>" + "</tr>"
									: "") + "</table>"));

	if (document.getElementById("lg").value == "en"
			|| document.getElementById("lg").value == "EN"
			|| document.getElementById("lg").value == "En") {
		infotabs
				.push(new GInfoWindowTab(
						document.getElementById("versval").value,
						'<div id="versdiv" onclick="fermeWindows();"><div id="mesavancementvers">Type company\'s name</div><br/><span class="tofrominfo">Start <input type="radio" name="entoraddressvers" id="addressradiovers" value="address" onclick="javascript:addressSelected();"/><span class="tofrominfodis" id="addressChoiceVers"> address: </span><input type="radio" name="entoraddressvers" id="entrepriseradiovers" value="entreprise" onclick="javascript:entrepriseSelected();" checked><span class="tofrominfo" id="entrepriseChoiceVers"> company:</span></span><br/><input type="text" name="toaddress" class="adressinput" id="toaddress" onkeyup="javascript:addresstyped();"/><br/><div class="entSelectionHidden" id="entSelectionDivVers"></div><br/><span class="tofrominfo">End address: </span><br/><b>'
								+ address
								+ '</b><br/><br/><input type="button" name="GoTrace" onclick="javascript:traceRoute(\''
								+ address
								+ '\');return false;" value="Go!" id="Govers" disabled><input type="hidden" id="addressvalvers" name="addressvalvers"></div>'));
		infotabs
				.push(new GInfoWindowTab(
						document.getElementById("apartirval").value,
						'<div id="apartirdiv" onclick="fermeWindows();"><div id="mesavancementapartir">Type company\'s name</div><br/><span class="tofrominfo">Start address: </span><br/><b>'
								+ address
								+ '</b><br/><br/><span class="tofrominfo">End <input type="radio" name="entoraddress" id="addressradioapartir" onclick="javascript:addressSelected();"/><span class="tofrominfodis" id="addressChoiceApartir"> address: </span><input type="radio" name="entoraddress" id="entrepriseradioapartir" onclick="javascript:entrepriseSelected();" checked><span class="tofrominfo" id="entrepriseChoiceApartir"> company:</span></span><br/><input type="text" class="adressinput" name="fromaddress" id="fromaddress" onkeyup="javascript:addresstyped();"/><br/><div class="entSelectionHidden" id="entSelectionDivApartir"></div><br/><input type="button" name="GoTrace" onclick="javascript:traceRoute(\''
								+ address
								+ '\');return false;" value="Go!" id="Goapartir" disabled><input type="hidden" id="addressvalapartir" name="addressvalapartir"></div>'));
	} else {
		infotabs
				.push(new GInfoWindowTab(
						document.getElementById("versval").value,
						'<div id="versdiv" onclick="fermeWindows();"><div id="mesavancementvers">Tapez le nom de l\'entreprise</div><br/><span class="tofrominfo"><input type="radio" name="entoraddressvers" id="addressradiovers" value="address" onclick="javascript:addressSelected();"/><span class="tofrominfodis" id="addressChoiceVers">Adresse </span><input type="radio" name="entoraddressvers" id="entrepriseradiovers" value="entreprise" onclick="javascript:entrepriseSelected();" checked><span class="tofrominfo" id="entrepriseChoiceVers"> Entreprise</span> de départ : </span><br/><input type="text" name="toaddress" class="adressinput" id="toaddress" onkeyup="javascript:addresstyped();"/><br/><div class="entSelectionHidden" id="entSelectionDivVers"></div><br/><span class="tofrominfo">Adresse d\'arrivée : </span><br/><b>'
								+ address
								+ '</b><br/><br/><input type="button" name="GoTrace" onclick="javascript:traceRoute(\''
								+ address
								+ '\');return false;" value="Go!" id="Govers" disabled><input type="hidden" id="addressvalvers" name="addressvalvers"></div>'));
		infotabs
				.push(new GInfoWindowTab(
						document.getElementById("apartirval").value,
						'<div id="apartirdiv" onclick="fermeWindows();"><div id="mesavancementapartir">Tapez le nom de l\'entreprise</div><br/><span class="tofrominfo">Adresse de départ : </span><br/><b>'
								+ address
								+ '</b><br/><br/><span class="tofrominfo"><input type="radio" name="entoraddress" id="addressradioapartir" onclick="javascript:addressSelected();"/><span class="tofrominfodis" id="addressChoiceApartir"> Adresse </span><input type="radio" name="entoraddress" id="entrepriseradioapartir" onclick="javascript:entrepriseSelected();" checked><span class="tofrominfo" id="entrepriseChoiceApartir"> Entreprise </span>d\'arrivée : </span><br/><input type="text" class="adressinput" name="fromaddress" id="fromaddress" onkeyup="javascript:addresstyped();"/><br/><div class="entSelectionHidden" id="entSelectionDivApartir"></div><br/><input type="button" name="GoTrace" onclick="javascript:traceRoute(\''
								+ address
								+ '\');return false;" value="Go!" id="Goapartir" disabled><input type="hidden" id="addressvalapartir" name="addressvalapartir"></div>'));
	}

	marker.bindInfoWindowTabsHtml(infotabs, {
		maxWidth : 350
	});

	map.addOverlay(marker);
	marker.openInfoWindowTabsHtml(infotabs, {
		maxWidth : 350
	});
	markers_on_map[id] = marker;
	nbrequest = 0;
}

// showLocation() is called when you click on the Search button
// in the form. It geocodes the address entered into the form
// and adds a marker to the map at that location.
function showLocation(id) {
	var address = document.getElementById("q").value;
	address = address.replace('Laffitte', 'Laffite');
	address = address.replace('laffitte', 'Laffite');
	address = address.replace('LAFFITTE', 'Laffite');
	address = address.replace('Charles Victor Naudin', 'Charles Naudin');
	address = address.replace('Charles Victor Naudin'.toLowerCase(),
			'Charles Naudin');
	address = address.replace('Charles Victor Naudin'.toUpperCase(),
			'Charles Naudin');
	geocoder.getLocations(address, function(response) {
		addAddressToMap(id, response);
	});
}

// findLocation() is used to enter the sample addresses into the form.
function findLocation(id, address) {
	document.getElementById("q").value = address;
	showLocation(id);
}

function showLettre(lettre) {

	if (document.getElementById("activiteselect"))
		myactivity = document.getElementById("activiteselect").value;
	var xhr_object = null;
	if (window.XMLHttpRequest) // FIREFOX
		xhr_object = new XMLHttpRequest();
	else if (window.ActiveXObject) // IE
		xhr_object = new ActiveXObject('Microsoft.XMLHTTP');
	else
		return (false);
	if (lastlettre != "" && $('link_' + lastlettre))
		document.getElementById('link_' + lastlettre).className = 'passiflink';
	document.getElementById('link_' + lettre).className = 'activelink';
	if (lettre != null)
		lastlettre = lettre;
	xhr_object.onreadystatechange = function() {
		afficheResultats(xhr_object);
	}

	// on affiche le message d'acceuil
	// document.getElementById("message").className="tumevois";
	// document.getElementById('result').style.visibility='hidden';
	document.getElementById('result').innerHTML = '';
	document.getElementById('ajax_content').className = 'visible';

	if (document.getElementById("searchbartab").className == "searchbarTabActive")
		xhr_object.open('GET', 'getDivContext.php?act=' + myactivity + '&key='
				+ lastlettre + '&searchkey='
				+ sansaccent(document.getElementById("searchinput").value)
				+ '&lg=' + document.getElementById("lg").value, true);
	else
		xhr_object.open('GET', 'getDivContext.php?act=' + myactivity + '&key='
				+ lastlettre + '&lg=' + document.getElementById("lg").value,
				true);
	xhr_object.send(null);

}

function addressSelected() {
	fermeWindows();
	if (document.getElementById("addressradiovers").checked) {
		document.getElementById("addressChoiceVers").className = "tofrominfo";
		document.getElementById("entrepriseChoiceVers").className = "tofrominfodis";
		document.getElementById("toaddress").value = "";
		document.getElementById("addressvalvers").value = "";
		document.getElementById("Govers").disabled = "";
		if (document.getElementById("lg").value == "en"
				|| document.getElementById("lg").value == "EN"
				|| document.getElementById("lg").value == "En") {
			document.getElementById("mesavancementvers").innerHTML = "Type the address";
		} else {
			document.getElementById("mesavancementvers").innerHTML = "Tapez l'adresse";
		}
	}
	if (document.getElementById("addressradioapartir").checked) {
		document.getElementById("addressChoiceApartir").className = "tofrominfo";
		document.getElementById("entrepriseChoiceApartir").className = "tofrominfodis";
		document.getElementById("fromaddress").value = "";
		document.getElementById("addressvalapartir").value = "";
		document.getElementById("Goapartir").disabled = "";
		if (document.getElementById("lg").value == "en"
				|| document.getElementById("lg").value == "EN"
				|| document.getElementById("lg").value == "En") {
			document.getElementById("mesavancementapartir").innerHTML = "Type the address";
		} else {
			document.getElementById("mesavancementapartir").innerHTML = "Tapez l'adresse";
		}
	}

}

function entrepriseSelected() {
	fermeWindows();
	if (document.getElementById("entrepriseradiovers").checked) {
		document.getElementById("addressChoiceVers").className = "tofrominfodis";
		document.getElementById("entrepriseChoiceVers").className = "tofrominfo";
		document.getElementById("toaddress").value = "";
		document.getElementById("addressvalvers").value = "";
		document.getElementById("Govers").disabled = "disabled";
		if (document.getElementById("lg").value == "en"
				|| document.getElementById("lg").value == "EN"
				|| document.getElementById("lg").value == "En") {
			document.getElementById("mesavancementvers").innerHTML = "Type company's name";
		} else {
			document.getElementById("mesavancementvers").innerHTML = "Tapez le nom de l'entreprise";
		}
	}
	if (document.getElementById("entrepriseradioapartir").checked) {
		document.getElementById("addressChoiceApartir").className = "tofrominfodis";
		document.getElementById("entrepriseChoiceApartir").className = "tofrominfo";
		document.getElementById("fromaddress").value = "";
		document.getElementById("addressvalapartir").value = "";
		document.getElementById("Goapartir").disabled = "disabled";
		if (document.getElementById("lg").value == "en"
				|| document.getElementById("lg").value == "EN"
				|| document.getElementById("lg").value == "En") {
			document.getElementById("mesavancementapartir").innerHTML = "Type company's name";
		} else {
			document.getElementById("mesavancementapartir").innerHTML = "Tapez le nom de l'entreprise";
		}
	}
}

function fermeWindows() {
	if (document.getElementById("entSelectionDivVers")
			|| document.getElementById("entSelectionDivApartir")) {
		document.getElementById("entSelectionDivApartir").className = "entSelectionHidden";
		document.getElementById("entSelectionDivVers").className = "entSelectionHidden";
	}

}

function selectEnt(entname, addressvalue) {
	if (map.getInfoWindow().getSelectedTab() == 1) {
		document.getElementById("toaddress").value = entname;
		document.getElementById("addressvalvers").value = addressvalue;
		document.getElementById("entSelectionDivVers").className = "entSelectionHidden";
		document.getElementById("Govers").disabled = "";
		document.getElementById("mesavancementvers").innerHTML = "";
	} else if (map.getInfoWindow().getSelectedTab() == 2) {
		document.getElementById("fromaddress").value = entname;
		document.getElementById("addressvalapartir").value = addressvalue;
		document.getElementById("entSelectionDivApartir").className = "entSelectionHidden";
		document.getElementById("Goapartir").disabled = "";
		document.getElementById("mesavancementapartir").innerHTML = "";

	}
}

function addresstyped() {
	var sens = "";
	if (map.getInfoWindow().getSelectedTab() == 1)
		sens = "vers";
	else
		sens = "apartir";
	if (document.getElementById("entrepriseradio" + sens).checked) {
		var xhr_object = null;
		if (window.XMLHttpRequest) // FIREFOX
			xhr_object = new XMLHttpRequest();
		else if (window.ActiveXObject) // IE
			xhr_object = new ActiveXObject('Microsoft.XMLHTTP');
		else
			return (false);

		var loadingval = '<br/><img src="images/ajax-loader.gif" id="ajax_content_image" align="center"/>';

		if (sens == "apartir") {
			document.getElementById("entSelectionDivApartir").innerHTML = loadingval;
			document.getElementById("entSelectionDivApartir").className = "entSelectionVisible";

		} else {
			document.getElementById("entSelectionDivVers").innerHTML = loadingval;
			document.getElementById("entSelectionDivVers").className = "entSelectionVisible";
		}

		xhr_object.onreadystatechange = function() {
			if (xhr_object.readyState == 4)
				afficheEntSelection(xhr_object, sens);

		}

		if (sens == "vers")
			var entdebut = document.getElementById("toaddress").value;
		else
			var entdebut = document.getElementById("fromaddress").value;
		xhr_object.open('GET', 'getMachingEnts.php?debut=' + entdebut, true);
		xhr_object.send(null);

	} else {
		if (sens == "vers")
			document.getElementById("addressval" + sens).value = document
					.getElementById("toaddress").value;
		else
			document.getElementById("addressval" + sens).value = document
					.getElementById("fromaddress").value;

	}

}

function afficheHelp() {
	new Ajax.Updater('helpmescontent', 'getHelpContent.php?lg=' + getLangue(),
			{
				onComplete : function() {
					showWeb2();
				},
				evalScripts : true
			});
}
function closeHelp() {
	closeWeb2();
}

function getLangue() {
	return $('lg').value;
}

function showEntOfWeekFrame(id) {
	new Ajax.Updater('helpmescontent', 'getEntOfWeek.php?lg=' + getLangue()
			+ '&id=' + id, {
		onComplete : function() {
			showWeb2();
		}
	});
}

function showWeb2() {
	document.getElementById("disablediv").className = "on";
	document.getElementById("helpmesdiv").className = "helpmesdiv";
}

function closeWeb2() {
	document.getElementById("disablediv").className = "off";
	document.getElementById("helpmesdiv").className = "off";
}

function afficheEntSelection(xhr_object, sens) {

	if (sens == "apartir") {
		document.getElementById("entSelectionDivApartir").innerHTML = xhr_object.responseText;
		document.getElementById("entSelectionDivApartir").className = "entSelectionVisible";
		document.getElementById("Goapartir").disabled = "disabled";
		if (document.getElementById("lg").value == "en"
				|| document.getElementById("lg").value == "EN"
				|| document.getElementById("lg").value == "En") {
			document.getElementById("mesavancementapartir").innerHTML = "Select company";
		} else {
			document.getElementById("mesavancementapartir").innerHTML = "Sélectionnez l'entreprise";
		}
	} else {
		document.getElementById("entSelectionDivVers").innerHTML = xhr_object.responseText;
		document.getElementById("entSelectionDivVers").className = "entSelectionVisible";
		document.getElementById("Govers").disabled = "disabled";
		if (document.getElementById("lg").value == "en"
				|| document.getElementById("lg").value == "EN"
				|| document.getElementById("lg").value == "En") {
			document.getElementById("mesavancementvers").innerHTML = "Select company";
		} else {
			document.getElementById("mesavancementvers").innerHTML = "Sélectionnez l'entreprise";
		}
	}
}

function traceRoute(address) {
	var sens = "";
	if (map.getInfoWindow().getSelectedTab() == 1)
		sens = "vers";
	else
		sens = "apartir";

	var fromaddress;
	var toaddress;
	var ok = true;
	if (map.getInfoWindow().getSelectedTab() == 1) {
		fromaddress = document.getElementById("addressvalvers").value;
		if (fromaddress == "") {
			ok = false;
		}
		toaddress = address;
	} else if (document.getElementById("fromaddress")) {
		toaddress = document.getElementById("addressvalapartir").value;
		if (toaddress == "") {
			ok = false;
		}
		fromaddress = address;

	}
	if (ok) {
		showRelated("route");
		gdir = new GDirections(map, document.getElementById('routeresults'));
		GEvent.addListener(gdir, "load", onRouteLoad);
		GEvent.addListener(gdir, "error", handleRouteErrors);
		var query = 'from: ' + fromaddress + ' to: ' + toaddress;
		map.clearOverlays();
		gdir.load(query, {
			locale : document.getElementById("lg").value,
			getPolyline : true,
			getSteps : true
		});
	} else {
		if (document.getElementById("lg").value == "en"
				|| document.getElementById("lg").value == "EN"
				|| document.getElementById("lg").value == "En")
			alert('Please type an address or company name');
		else
			alert("Veuillez taper une adresse ou un nom d'entreprise");
		if (map.getInfoWindow().getSelectedTab() == 1)
			document.getElementById('toaddress').focus();
		else if (document.getElementById('fromaddress'))
			document.getElementById('fromaddress').focus();
	}
}

function showRelated(name) {
	if (name == "search") {
		document.getElementById('lettreliste').style.display = "block";
		document.getElementById('lettreliste').style.visibility = "visible";
		document.getElementById('activitybar').style.display = "block";
		document.getElementById('result').style.display = "block";
		document.getElementById('routeresults').className = "routehidden";
		document.getElementById('routeresinfo').className = "routehidden";
	} else if (name == "control") {
		document.getElementById('activitybar').style.display = "block";
		document.getElementById('result').style.display = "block";
		document.getElementById('lettreliste').style.display = "block";
		document.getElementById('lettreliste').style.visibility = "visible";
		document.getElementById('routeresults').className = "routehidden";
		document.getElementById('routeresinfo').className = "routehidden";

	} else if (name == "route") {
		document.getElementById('activitybar').style.display = "none";
		document.getElementById('result').style.display = "none";
		document.getElementById('lettreliste').style.display = "none";
		document.getElementById('controlbartab').className = 'controlbarTab';
		document.getElementById('searchbartab').className = 'searchbarTab';
		document.getElementById('itinerairetab').className = 'itineraireTabActive';
		document.getElementById('routeresults').className = "route";

	}

}

function onRouteLoad() {
	document.getElementById("routeresinfo").className = "routeinfo";
}

function handleRouteErrors() {
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
		alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: "
				+ gdir.getStatus().code);
	else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
		alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: "
				+ gdir.getStatus().code);

	else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
		alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: "
				+ gdir.getStatus().code);

	else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS) // <--- Doc
		// bug... this
		// is either not
		// defined, or
		// Doc is wrong
		alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: "
				+ gdir.getStatus().code);

	else if (gdir.getStatus().code == G_GEO_BAD_KEY)
		alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: "
				+ gdir.getStatus().code);

	else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
		alert("A directions request could not be successfully parsed.\n Error code: "
				+ gdir.getStatus().code);

	else
		alert("An unknown error occurred.");

}

function afficheResultats(xhr_object) {
	if (xhr_object.readyState == 4) {

		document.getElementById('ajax_content').className = 'invisible';
		document.getElementById('result').innerHTML = xhr_object.responseText;
		// document.getElementById('result').style.visibility='visible';
		showRelated("control");
	}
}

function showEntOfWeekInEntList() {
	new Ajax.Updater('result', "getEntOfWeek.php?lg=" + $('lg').value, {
		onLoading : function() {
			$('ajax_content').className = 'visible';
		},
		onComplete : function() {
			$('ajax_content').className = 'invisible';
		},
		evalScripts : true
	});
	return false;
}

function afficheLettreBar(xhr_object1, search) {
	if (search == undefined)
		search = false;
	if (xhr_object1.readyState == 4) {
		document.getElementById('ajax_lettre').className = 'invisible';

		document.getElementById('lettreliste').innerHTML = xhr_object1.responseText;
		lastlettre = document.getElementById("actualkey").value;
		if ($('versionuserid').value != 0 && lastlettre == "") {

			lastlettre = "0-9";

		}

		if (search
				|| document.getElementById("searchbartab").className == "searchbarTabActive")
			lastlettre = "";

		if (lastlettre != "")
			showLettre(document.getElementById("actualkey").value);
		else {
			if ($('searchinput').value != ""
					&& (search || document.getElementById("searchbartab").className == "searchbarTabActive"))
				afficheSearchResult();
			else
				showEntOfWeekInEntList();
		}
		if (gdir != null) {
			gdir.clear();
			initialize();
			gdir = null;
		}
		showRelated("search");

	}
}

function controlBar() {

	document.getElementById('controlbartab').className = 'controlbarTabActive';
	document.getElementById('searchbartab').className = 'searchbarTab';
	document.getElementById('itinerairetab').className = 'itineraireTab';
	document.getElementById('activitybar').style.visibility = "visible";
	document.getElementById('result').style.visibility = "visible";
	document.getElementById('lettreliste').style.visibility = "visible";
	document.getElementById('routeresults').className = "routehidden";
	document.getElementById('routeresinfo').className = "routehidden";
	changeSector();
}

function search() {
	if (gdir != null) {
		gdir.clear();
		initialize();
		gdir = null;
	}
	if (document.getElementById("activiteselect"))
		myactivity = document.getElementById("activiteselect").value;
	var xhr_object1 = null;
	if (window.XMLHttpRequest) // FIREFOX
		xhr_object1 = new XMLHttpRequest();
	else if (window.ActiveXObject) // IE
		xhr_object1 = new ActiveXObject("Microsoft.XMLHTTP");
	else
		return (false);

	document.getElementById("searchbartab").className = "searchbarTabActive";
	document.getElementById('controlbartab').className = 'controlbarTab';
	document.getElementById('itinerairetab').className = 'itineraireTab';

	xhr_object1.onreadystatechange = function() {
		afficheLettreBar(xhr_object1, true);

	}
	// alert('valeur =
	// '+sansaccent(document.getElementById("searchinput").value));
	showRelated("search");
	document.getElementById('lettreliste').style.visibility = 'hidden';
	document.getElementById("ajax_lettre").className = 'visible';

	xhr_object1.open('GET', 'getLettreBar.php?act=' + myactivity + '&key='
			+ lastlettre + '&searchkey='
			+ sansaccent(document.getElementById("searchinput").value) + '&lg='
			+ document.getElementById("lg").value, true);
	xhr_object1.send(null);
}

function sansaccent(mot) {
	var out = mot;
	re = /[àáâãäå]/g;
	out = out.replace(re, 'a');
	re = /[èéêë]/g;
	out = out.replace(re, 'e');
	re = /[ç]/g;
	out = out.replace(re, 'c');
	re = /[ìíîï]/g;
	out = out.replace(re, 'i');
	re = /[ðòóôõö]/g;
	out = out.replace(re, 'o');
	re = /[ùúûü]/g;
	out = out.replace(re, 'u');
	re = /[ýýÿ]/g;
	out = out.replace(re, 'y');

	return out;
}

function changeSector() {

	if (document.getElementById("activiteselect"))
		myactivity = document.getElementById("activiteselect").value;
	if (gdir != null) {
		gdir.clear();
		initialize();
		gdir = null;
	}
	var xhr_object1 = null;
	if (window.XMLHttpRequest) // FIREFOX
		xhr_object1 = new XMLHttpRequest();
	else if (window.ActiveXObject) // IE
		xhr_object1 = new ActiveXObject("Microsoft.XMLHTTP");
	else
		return (false);

	xhr_object1.onreadystatechange = function() {

		afficheLettreBar(
				xhr_object1,
				document.getElementById("searchbartab").className == "searchbarTabActive");

	}

	document.getElementById('lettreliste').style.visibility = 'hidden';
	document.getElementById("ajax_lettre").className = 'visible';

	if (document.getElementById("searchbartab").className == "searchbarTabActive") {
		xhr_object1.open('GET', 'getLettreBar.php?act=' + myactivity + '&key='
				+ lastlettre + '&searchkey='
				+ sansaccent(document.getElementById("searchinput").value)
				+ '&lg=' + document.getElementById("lg").value, true);
	} else
		xhr_object1.open('GET', 'getLettreBar.php?act=' + myactivity + '&key='
				+ lastlettre + '&lg=' + document.getElementById("lg").value,
				true);
	xhr_object1.send(null);

}

var url_sf = null;

function showEntreprise(id, raison, batiment, adresse, bp, CP, commune, tel,
		fax, site, description, activite, la, lo, url_sfood) {
	url_sf = url_sfood;
	var activitylg = "Activit&eacute;";
	var adexactelg = "Adresse exacte";

	if (document.getElementById("lg").value == "en") {
		activitylg = "Activity";
		adexactelg = "Detailed address";
	}

	ent_description = '<b>' + raison + '</b><br/><b>' + activitylg + ' : </b>'
			+ activite + '</b><br/>' + description + '<br/><b>' + adexactelg
			+ ' : </b> ' + adresse + ' ' + batiment + '<br/>' + commune + ' '
			+ CP + ' ' + bp + '<br/>' + '<b>Tel : </b>' + tel
			+ '<br/><b>Fax : </b>' + fax
			+ '<br/><b>Web : </b><A target="_blank" href="http://' + site
			+ '">' + site + '</A>';
	lastaddress = adresse;
	lastCP = CP;
	lastCommune = commune;
	nbrequest = 0;
	if (la == undefined || la == '' || la == null || lo == undefined
			|| lo == '' || lo == null) {
		findLocation(id, adresse + ' ' + commune + ' ' + CP);
	} else {
		addPinOnMap(id, la, lo, adresse + ' ' + commune + ' ' + CP);
	}
}

function showNewEditEnt(id, lo, la) {
	new Ajax.Updater('helpmescontent', 'getNewEditEnt.php?lg='
			+ getLangue()
			+ (id != undefined && id != null ? "&id="
					+ id
					+ (lo != undefined && lo != null ? "&lo=" + lo + "&la="
							+ la : "") : ""), {
		onComplete : function() {
			showWeb2();
		},
		evalScripts : true
	});
}

function checkRequiredAndSend(id) {
	reqs = new Array();
	$$('#' + id + ' .required').each( function(el) {
		if (el.value == "") {
			reqs.push(el);
			el.style.background = "#ffb8b8";
		} else {
			el.style.background = "#fff";
		}
	});
	if (reqs.length > 0) {
		alert("Veuillez remplir tous les champs ayant un *");
	} else {
		$(id).onsubmit();
	}
}

function sendAjaxForm(id, url, update) {
	new Ajax.Updater(update, url, {
		parameters : Form.serialize($(id)) + "&lg=" + getLangue(),
		evalScripts : true
	});
}

function hasStreetView(latlng, nomarker) {
	streetworking = true;
	streetviewclient
			.getNearestPanoramaLatLng(
					latlng,
					function(lat) {
						if (lat != null) {
							if ($('street_link_' + nomarker))
								$('street_link_' + nomarker).innerHTML = "<a href='#' onclick='showStreetView("
										+ latlng.lat()
										+ ","
										+ latlng.lng()
										+ ");'>StreetView</a>"
						} else {
							if ($('street_link_' + nomarker))
								$('street_link_' + nomarker).innerHTML = (getLangue() == "fr" ? "StreetView non disponible"
										: "StreetView isn't available");
						}
						streetworking = false;
					});
}

function showStreetView(lat, lng) {
	$('street_canvas').className = "street_visible";
	$('street_close').className = "street_visible";

	var ll = new GLatLng(lat, lng);
	streetviewpano = new GStreetviewPanorama($('street_canvas'), {
		latlng : ll
	});
	GEvent.addListener(streetviewpano, "error", handleStreetErr);
	$('map_canvas').hide();
}

function handleStreetErr(errorCode) {
	if (errorCode == FLASH_UNAVAILABLE) {
		alert("Error: Flash doesn't appear to be supported by your browser");
		return;
	} else {
		alert("Error while loading StreetView : " + errorCode);
	}

}

function hideSView() {
	streetviewpano.hide();
	$('map_canvas').show();
	$('street_canvas').className = 'street_hidden';
	$('street_close').className = 'street_hidden';

}

function afficheSearchResult() {
	new Ajax.Updater('result', "getSearchRes.php?searchkey="
			+ sansaccent($('searchinput').value) + "&act="
			+ document.getElementById("activiteselect").value + "&lg="
			+ getLangue(), {
		onLoading : function() {
			$('ajax_content').className = 'visible';
		},
		onComplete : function() {
			$('ajax_content').className = 'invisible';
		},
		evalScripts : true
	});
	return false;
}

function showLinkCreator(lg) {
	new Ajax.Updater('helpmescontent', 'linkCreator.php?lg=' + lg, {
		onComplete : function() {
			showWeb2();
		},
		evalScripts : true
	});
}

function putSelectedEntForLink(text, li) {
	new Ajax.Updater('selected_ent_info', 'getEntInfo.php', {
		parameters : "entname=" + text.value,
		onComplete : function() {
			$('select_lang_and_create').show();
		}
	});
}

function createLink() {
	new Ajax.Updater('helpmescontent', 'createEntLink.php', {
		parameters : "lg=" + getLangue() + "&entname=" + $('ent_name').value
	});
}

function regenCode() {
	var linkval = "<a href=\"http://www.whereinsophia.com?lg="
			+ getSelectedLangue()
			+ "&company="
			+ $('entnameval').value
			+ "\" target=\"_blank\"><img src=\"http://www.whereinsophia.com/images/"
			+ getSelectedLogo()
			+ ".png\" alt='Visualiser sur Where In Sophia' title='Visualiser sur Where In Sophia'/></a>";
	$('code_for_link').value = linkval;
	$('preview').innerHTML = "<p>" + linkval + "</p>";
}

function getSelectedLangue() {
	if ($('lg_fr').checked)
		return "fr";
	else
		return "en";
}

function getSelectedLogo() {
	if ($('logo_sel').checked)
		return $('logo_sel').value;
	else if ($('logo_sel_med').checked)
		return $('logo_sel_med').value;
	else
		return $('logo_sel_ts').value;
}

function showEntOnMap() {
	new Ajax.Request('getEntShowFunc.php', {
		parameters : "lg=" + getLangue() + "&entname=" + $('company').value,
		onSuccess : function(transport) {
			eval(transport.responseText);
		},
		evalScripts : true
	});
}

function setRightBounds(map, pts) {
	var bounds = map.getBounds();
	for (i = 0; i < pts.length; i++) {
		if (!bounds.containsLatLng(pts[i]))
			bounds.extend(pts[i]);
	}
	map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds) - 1);
}
