var infoWindow;
var dataWindow;
var printWindow;
var exportWindow;
var validationWindow;

/**
		-------------------------------------------------------
		Classe acai_carto_dynamicMap
		-------------------------------------------------------
		Description : Objet representant la zone "carte"
									Gere l'ensemble des images WMS composant la carte, au sens de couches
									Inclut les actions disponibles sur la carte (zoom, pan, etc.)
									Communique avec les autres elements du "conteneur" global
											- mise a jour de la zone "informations"
*/
function acai_carto_dynamicMap(pNom,pMapImages,pLeft,pTop,pWidth,pHeight,pViewer,pMapTitle) {
	var fMapTitle = pMapTitle || '';
	var fMapImage = new acai_carto_dynamicMapURL(pNom, pMapImages, pLeft, pTop, pWidth, pHeight, pViewer + ".getInfos()",pMapTitle);
	var fCouche;
	var fMapURLs = pMapImages;
	var fCoucheImage;
	var fViewer = pViewer;
	var fInfos = pViewer + ".getInfos()";
	var fMiniMap = pViewer + ".getMiniMap()";
	var fLegend = pViewer + ".getLegend()";
	var fAuteur = "";
	var fDateValidite = "";
	var fDescription = "";
	var fCopyright = "";
	var fAppInfos = "";
	var fCreator = "";
	var fNumLogosFichiers = 0;
	var fNumLogosURLs = 0;
	var fListeLogosFichiers = new Array(1);
	var fListeLogosURLs = new Array(1);
	
	var draw_x = new Array();
	var draw_y = new Array();
	var nb_points = new Array();
	var nb_pts = 0;
	var nb_poly = 0;
	var type_geom = "";
	var fInsertService = "";
	var fFeatureFieldName = "";
	var fGeometryTypeFieldName = "";
	var fGeometryFieldName = "";
	var fSrsFieldName = "";

	this.setAuteur = function(pAuteur) {
		fAuteur = pAuteur;
	};
	this.setDateValidite = function(pDateValidite) {
		fDateValidite = pDateValidite;
	};
	this.setDescription = function(pDescription) {
		fDescription = pDescription;
	};
	this.setCopyright = function(pCopyright) {
		fCopyright = pCopyright;
	};
	this.setAppInfos = function(pAppInfos) {
		fAppInfos = pAppInfos;
	};
	this.setCreator = function(pCreator) {
		fCreator = pCreator;
	};
	this.addPrintLogoFile = function(pPrintLogoFile) {
		fNumLogosFichiers ++;
		fListeLogosFichiers[fNumLogosFichiers] = pPrintLogoFile;
	};
	this.addPrintLogoURL = function(pPrintLogoURL) {
		fNumLogosURLs ++;
		fListeLogosURLs[fNumLogosURLs] = pPrintLogoURL;
	};

	this.setInsertService = function(pInsertService,pFeatureFieldName,pGeometryTypeFieldName,pGeometryFieldName,pSrsFieldName) {
		fInsertService = pInsertService;
		fFeatureFieldName = pFeatureFieldName;
		fGeometryTypeFieldName = pGeometryTypeFieldName;
		fGeometryFieldName = pGeometryFieldName;
		fSrsFieldName = pSrsFieldName;
	};

	var fCurrentLeft = 0;
	var fCurrentTop = 0;
	var fCurrentWidth = 0;
	var fCurrentHeight = 0;
	var fMapLimitXmin;
	var fMapLimitXmax;
	var fMapLimitYmin;
	var fMapLimitYmax;
	var fMiniMapXmin;
	var fMiniMapXmax;
	var fMiniMapYmin;
	var fMiniMapYmax;
	var fLimitedMap = false;
	var fMiniMapOK = false;

	var fCurrentTool;
	var cadreZoom;
	var mouseX = 0;
	var mouseY = 0;
	var startX = 0;
	var startY = 0;
	var realX = 0;
	var realY = 0;
	var deltaX = 0;
	var deltaY = 0;
	var offsetX = 0;
	var offsetY = 0;
	var zooming = false;
	var panning = false;
	var started = false;
	var typeZoom;
	var jg;
	var jgOK = false;
	var distTotale = 0;
	var pointAvantX = - 1;
	var pointAvantY = - 1;
	var pointActuelX = - 1;
	var pointActuelY = - 1;
	var pointInitX = - 1;
	var pointInitY = - 1;
	
	var _fBgColor;
	var _fColor;
	var cadreZoom;


	this.setCurrentTool = function(pCurrentTool) {
		fCurrentTool = pCurrentTool;
	};

	this.refreshCurrentTool = function(pCurrentTool) {
		if (pCurrentTool == "ZOOMIN") {
			this.activeZoomIn();
		}
		if (pCurrentTool == "ZOOMIN_RECT") {
			this.activeZoomInRect();
		}
		if (pCurrentTool == "ZOOMOUT" ) {
			this.activeZoomOut();
		}
		if (pCurrentTool == "ZOOMOUT_RECT" ) {
			this.activeZoomOutRect();
		}
		if (pCurrentTool == "PAN" ) {
			this.activePan();
		}
		if (pCurrentTool == "RECENTER" ) {
			this.activeRecenter();
		}
		if (pCurrentTool == "QUERY" ) {
			this.activeMapQuery();
		}
		if (pCurrentTool == "DISTANCE" ) {
			this.activeDistance();
		}
		if (pCurrentTool == "POINT" ) {
			this.activeCreatePoint();
		}
		if (pCurrentTool == "POLYLINE" ) {
			this.activeCreatePolyline();
		}
		if (pCurrentTool == "POLYGON" ) {
			this.activeCreatePolygon();
		}
	};
	
	this.getCurrentTool = function() {
		return fCurrentTool;
	};
	
	this.getMapTitle = function() {
		return fMapTitle;
	};

	this.setMapImage = function(pMapImage) {
		if (fMapImage != pMapImage) {
			fMapImage = pMapImage;
			this.refresh();
		}
	};

	this.getMapImage = function() {
		return fMapImage;
	};

	this.setURLs = function(pMapURLs) {
		fMapURLs = pMapURLs;
		fMapImage.setURLs(pMapURLs);
	};

	this.setProjection = function(pProjection) {
		fMapImage.setProjection(pProjection);
	};
	
	this.setMapLimitBounds = function(pMapLimitXmin, pMapLimitXmax, pMapLimitYmin, pMapLimitYmax) {
		fMapLimitXmin = pMapLimitXmin;
		fMapLimitXmax = pMapLimitXmax;
		fMapLimitYmin = pMapLimitYmin;
		fMapLimitYmax = pMapLimitYmax;
		fLimitedMap = true;
	};

	this.setMiniMapBounds = function(pMiniMapXmin, pMiniMapXmax, pMiniMapYmin, pMiniMapYmax) {
		fMiniMapXmin = pMiniMapXmin;
		fMiniMapXmax = pMiniMapXmax;
		fMiniMapYmin = pMiniMapYmin;
		fMiniMapYmax = pMiniMapYmax;
		fMiniMapOK = true;
	};

	this.init = function() {
		document.getElementById(this.getNom()).onmousemove = getMouse;
		fCurrentLeft = this.getLeft();
		fCurrentTop = this.getTop();
		fCurrentWidth = this.getWidth();
		fCurrentHeight = this.getHeight();
		var contenuHTML = '<div id=\'' + this.getNom() + '_mapLayer\''
			+ ' style=\'position:absolute;overflow:hidden;left:0px;top:0px;'
			+ 'width:' + this.getWidth() + 'px;height:' + this.getHeight() + 'px\'>'
			+  this.getMapImage().getSrc()
			+ '</div>';
		this.setContenu(contenuHTML);
	};

	// ------------------------------------------------------------------------
	// Affichage de la carte selon l'etendue geographique initiale
	// ------------------------------------------------------------------------
	this.restoreBounds = function() {
		this.desactiveTools();
		executeNewBounds(fMapImage.getInitXmin(),fMapImage.getInitXmax(),fMapImage.getInitYmin(),fMapImage.getInitYmax());
	};

	// ------------------------------------------------------------------------
	// Affichage de la carte selon l'etendue geographique precedente
	// ------------------------------------------------------------------------
	this.cancelBounds = function() {
		this.desactiveTools();
		executeNewBounds(fMapImage.getBackXmin(),fMapImage.getBackXmax(),fMapImage.getBackYmin(),fMapImage.getBackYmax());
	};

	// ------------------------------------------------------------------------
	// Affichage de la carte selon une nouvelle etendue geographique
	// ------------------------------------------------------------------------
	this.gotoBounds = function(pNewXmin,pNewXmax,pNewYmin,pNewYmax,fromLocalize) {
		this.desactiveTools();
		executeNewBounds(pNewXmin,pNewXmax,pNewYmin,pNewYmax,fromLocalize);
	};

	function executeNewBounds(pNewXmin,pNewXmax,pNewYmin,pNewYmax,fromLocalize) {
		var invalidBounds = false;
		
		// zoom sur un objet ponctuel ???
		if ( pNewXmax == pNewXmin && pNewYmax == pNewYmin ) {
			pTempXmin = pNewXmin - (fMapImage.getXmax() - fMapImage.getXmin())/2;
			pTempXmax = pNewXmin + (fMapImage.getXmax() - fMapImage.getXmin())/2;
			pTempYmin = pNewYmin - (fMapImage.getYmax() - fMapImage.getYmin())/2;
			pTempYmax = pNewYmin + (fMapImage.getYmax() - fMapImage.getYmin())/2;
			pNewXmin = pTempXmin;
			pNewXmax = pTempXmax;
			pNewYmin = pTempYmin;
			pNewYmax = pTempYmax;
		}
		// controle vis a vis du zoom maximal (en fait largeur geographique minimale)
		var pZoomMax = eval(fViewer + ".getZoomMax()");
		if ( (pNewXmax-pNewXmin) < pZoomMax) {
				rapport = (pNewXmax-pNewXmin) / (pNewYmax-pNewYmin);
				// on etire la zone pour se caler sur le zoom maximal...
				pTempXmin = pNewXmin - (pZoomMax - (pNewXmax-pNewXmin))/2;
				pTempXmax = pNewXmax + (pZoomMax - (pNewXmax-pNewXmin))/2;
				pTempYmin = pNewYmin - (pZoomMax/rapport - (pNewYmax-pNewYmin))/2;
				pTempYmax = pNewYmax + (pZoomMax/rapport - (pNewYmax-pNewYmin))/2;
				if (!fromLocalize) {
					alert(ALERT_ZOOM_MAXI);
				}
				pNewXmin = pTempXmin;
				pNewXmax = pTempXmax;
				pNewYmin = pTempYmin;
				pNewYmax = pTempYmax;
		}
		// controle vis a vis de l'etendue maximale autorisee
		//		remarque : la nouvelle etendue est deja homothetique :)
		// controle 1 :nouvelle etendue trop grande ?
		if (pNewXmax - pNewXmin > (fMapLimitXmax - fMapLimitXmin)) {
			pNewXmin = fMapLimitXmin;
			pNewYmin = fMapLimitYmin;
			pNewXmax = fMapLimitXmax;
			pNewYmax = fMapLimitYmax;
			invalidBounds = true;
		}
		// controle 2 : debordements ?
		if (pNewXmin < fMapLimitXmin) {
			pNewXmax = pNewXmax + (fMapLimitXmin - pNewXmin);
			pNewXmin = fMapLimitXmin;
			invalidBounds = true;
		}
		if (pNewXmax > fMapLimitXmax) {
			pNewXmin = pNewXmin + (fMapLimitXmax - pNewXmax);
			pNewXmax = fMapLimitXmax;
			invalidBounds = true;
		}
		if (pNewYmin < fMapLimitYmin) {
			pNewYmax = pNewYmax + (fMapLimitYmin - pNewYmin);
			pNewYmin = fMapLimitYmin;
			invalidBounds = true;
		}
		if (pNewYmax > fMapLimitYmax) {
			pNewYmin = pNewYmin + (fMapLimitYmax - pNewYmax);
			pNewYmax = fMapLimitYmax;
			invalidBounds = true;
		}

		// mise a jour de la carte principale
		fMapImage.setXmin(pNewXmin);
		fMapImage.setXmax(pNewXmax);
		fMapImage.setYmin(pNewYmin);
		fMapImage.setYmax(pNewYmax);

		eval(fInfos + ".setGeoDim(" + fMapImage.getWidth() + ","
				+ fMapImage.getHeight() + ","
				+ fMapImage.getXmin() + ","
				+ fMapImage.getXmax() + ","
				+ fMapImage.getYmin() + ","
				+ fMapImage.getYmax() + ")"
				);
		fMapImage.refresh();
		eval(fViewer + ".getLayerControl().refresh()");

		// mise a jour de la carte de navigation (minimap)
		if (fMiniMapOK) {
				var scaleMapMiniMap = MINIMAP_WIDTH / fMapImage.getWidth();

				var scaleFromInitialZoom = (pNewXmax - pNewXmin) / (fMiniMapXmax - fMiniMapXmin);
				var initialScale = (fMiniMapXmax - fMiniMapXmin) / fMapImage.getWidth();

				var initialDistanceXgeo = fMiniMapXmax - fMiniMapXmin;
				var initialDistanceYgeo = fMiniMapYmax - fMiniMapYmin;
				var initialCenterXgeo = fMiniMapXmin + initialDistanceXgeo/2;
				var initialCenterYgeo = fMiniMapYmin + initialDistanceYgeo/2;

				var newDistanceXgeo = pNewXmax - pNewXmin;
				var newDistanceYgeo = pNewYmax - pNewYmin;
				var newCenterXgeo = pNewXmin + newDistanceXgeo/2;
				var newCenterYgeo = pNewYmin + newDistanceYgeo/2;

				var deltaXgeo = newCenterXgeo - initialCenterXgeo;
				var deltaYgeo = -(newCenterYgeo - initialCenterYgeo);

				var deltaX = deltaXgeo / initialScale * scaleMapMiniMap;
				var deltaY = deltaYgeo / initialScale * scaleMapMiniMap;

				eval(fMiniMap + ".refreshFromMap(" + scaleFromInitialZoom + "," + deltaX + "," + deltaY +")");
		}
		
		// mise a jour de la legende
		eval(fLegend + ".setDeprecated(true)");
		if (eval(fLegend + ".isVisible()")) {
			eval(fLegend + ".show()");
		}
	// Désactivé par BM le 8juin2010	
		// message en cas de provenance d'une localisation par selection
		//if (invalidBounds && fromLocalize) {
		//	alert(ALERT_NEW_BOUNDS);
		//}
	}

	// ------------------------------------------------------------------------
	// Gestion des outils "zoom"
	// ------------------------------------------------------------------------
	this.activeZoomIn = function() {
		fCouche = document.getElementById(this.getNom());
		fCoucheImage = document.getElementById(this.getNom() + '_mapLayer');
		this.desactiveTools();
		zooming = true;
		typeZoom=1;
		fCouche.style.cursor = "crosshair";
		this.setCurrentTool("ZOOMIN");
		setMapEvents("ZOOM");
	};
	
	this.activeZoomInRect = function() {
		fCouche = document.getElementById(this.getNom());
		fCoucheImage = document.getElementById(this.getNom() + '_mapLayer');
		this.desactiveTools();
		zooming = true;
		typeZoom=1;
		cadreZoom = new acai_commons_genericCadre("cadreZoom", false);
		cadreZoom.setBorderColor(COSMETIC_COLOR);
		cadreZoom.setBorderWidth(ZOOM_BORDER_WIDTH);
		fCouche.style.cursor = "crosshair";
		this.setCurrentTool("ZOOMIN_RECT");
		setMapEvents("ZOOM_RECT");
	};
	
	this.activeZoomOut = function() {
		fCouche = document.getElementById(this.getNom());
		fCoucheImage = document.getElementById(this.getNom() + '_mapLayer');
		this.desactiveTools();
		zooming = true;
		typeZoom=-1;
		fCouche.style.cursor = "crosshair";
		this.setCurrentTool("ZOOMOUT");
		setMapEvents("ZOOM");
	};

	this.activeZoomOutRect = function() {
		fCouche = document.getElementById(this.getNom());
		fCoucheImage = document.getElementById(this.getNom() + '_mapLayer');
		this.desactiveTools();
		zooming = true;
		typeZoom=-1;
		cadreZoom = new acai_commons_genericCadre("cadreZoom", false);
		cadreZoom.setBorderColor(COSMETIC_COLOR);
		cadreZoom.setBorderWidth(ZOOM_BORDER_WIDTH);
		fCouche.style.cursor = "crosshair";
		this.setCurrentTool("ZOOMOUT_RECT");
		setMapEvents("ZOOM_RECT");
	};

	function executeZoom(xmin, ymin, xmax, ymax, fromIncremental) {
		var centerXzoom;
		var centerYzoom;
		var zoomFactor = 0;
		var distanceXgeo;
		var distanceYgeo;
		var zoomAllowed = false;
		// rectangle suffisant ???
		if ( (xmax-xmin) > DELTA_MIN_ZOOM && (ymax-ymin) > DELTA_MIN_ZOOM) {
			// adaptation du zoom rectangle au rectangle de l'image (memes proportions...)
			var aNewBounds = acai_carto_utils.adaptRectangleToMap(fCurrentWidth, fCurrentHeight, xmin, xmax, ymin, ymax);
			xmin = aNewBounds[0];
			xmax = aNewBounds[1];
			ymin = aNewBounds[2];
			ymax = aNewBounds[3];
	
			centerXzoom = xmin + (xmax-xmin)/2;
			centerYzoom = ymin + (ymax-ymin)/2;
			zoomFactor = fCurrentWidth/(xmax-xmin);
			zoomAllowed = true;
		} else {
			// zoom incremental !!!
			// on prend en compte le point du d?but du zoom
			centerXzoom = startX-fCurrentLeft;
			centerYzoom = startY-fCurrentTop;
			zoomFactor = ZOOM_FACTOR;
		}
		var centerXgeo = fMapImage.getXmin() + (fMapImage.getXmax()-fMapImage.getXmin())*centerXzoom / fCurrentWidth;
		var centerYgeo = fMapImage.getYmax() - (fMapImage.getYmax()-fMapImage.getYmin())*centerYzoom / fCurrentHeight;
		
		if (typeZoom === -1) {
			distanceXgeo = (fMapImage.getXmax()-fMapImage.getXmin())*zoomFactor;
			distanceYgeo = (fMapImage.getYmax()-fMapImage.getYmin())*zoomFactor;
		} else {
			distanceXgeo = (fMapImage.getXmax()-fMapImage.getXmin())/zoomFactor;
			distanceYgeo = (fMapImage.getYmax()-fMapImage.getYmin())/zoomFactor;
		}

		var newXmin = (centerXgeo - distanceXgeo/2);
		var newXmax = (centerXgeo + distanceXgeo/2);
		var newYmin = (centerYgeo - distanceYgeo/2);
		var newYmax = (centerYgeo + distanceYgeo/2);

		var newBounds = fromIncremental || zoomAllowed;

		if (newBounds) {
			executeNewBounds(newXmin, newXmax, newYmin, newYmax);
		}
	}

	function doZoom(e) {
		if ( getImageXY(e) ) {
			startX = mouseX;
			startY = mouseY;
			executeZoom(startX-fCurrentLeft, startY-fCurrentTop, startX-fCurrentLeft, startY-fCurrentTop, true);
		} else {
			alert(ALERT_RIGHT_CLIC);
		}
		return false;
	}

	function startZoom(e) {
		if ( getImageXY(e) ) {
			if (started) {
				fCouche.onmouseup = null;
				stopZoom(e);
			} else {
				startX = mouseX;
				startY = mouseY;
				cadreZoom.setVisible(true);
				cadreZoom.setPosition(startX,startY,2,2);
				started = true;
				fCouche.onmouseup = stopZoom;
			}
		} else {
			alert(ALERT_RIGHT_CLIC);
		}
		return false;
	}

	function stopZoom(e) {
		if ( getImageXY(e) ) {
			cadreZoom.setVisible(false);
			started = false;
			if (deltaX!=0 || deltaY!=0) {
				executeZoom(realX-fCurrentLeft, realY-fCurrentTop, realX-fCurrentLeft+deltaX, realY-fCurrentTop+deltaY, false);
			}
		} else {
			alert(ALERT_RIGHT_CLIC);
		}
		return false;
	}

	// ------------------------------------------------------------------------
	// Gestion de l'outil "deplacement"
	// ------------------------------------------------------------------------
	this.activeRecenter = function() {
		fCouche = document.getElementById(this.getNom());
		fCoucheImage = document.getElementById(this.getNom() + '_mapLayer');
		this.desactiveTools();
		panning = true;
		fCouche.style.cursor = "move";
		this.setCurrentTool("RECENTER");
		setMapEvents("RECENTER");
	};

	this.activePan = function() {
		fCouche = document.getElementById(this.getNom());
		fCoucheImage = document.getElementById(this.getNom() + '_mapLayer');
		this.desactiveTools();
		panning = true;
		fCouche.style.cursor = "move";
		this.setCurrentTool("PAN");
		setMapEvents("PAN");
	};

	function executeRecenterOrPan(deltaX, deltaY, fromRecenter) {
		var centerXpan;
		var centerYpan;
		var panAllowed = false;
		// d?placement suffisant ?
		if ( Math.abs(deltaX) > DELTA_MIN_PAN || Math.abs(deltaY) > DELTA_MIN_PAN) {
			centerXpan = fCurrentWidth/2 - deltaX;
			centerYpan = fCurrentHeight/2 - deltaY;
			panAllowed = true;
		} else {
			// simple recentrage
			centerXpan = startX-fCurrentLeft;
			centerYpan = startY-fCurrentTop;
		}
		var centerXgeo = fMapImage.getXmin() + (fMapImage.getXmax()-fMapImage.getXmin())*centerXpan / fCurrentWidth;
		var centerYgeo = fMapImage.getYmax()- (fMapImage.getYmax()-fMapImage.getYmin())*centerYpan / fCurrentHeight;
		var distanceXgeo = (fMapImage.getXmax()-fMapImage.getXmin());
		var distanceYgeo = (fMapImage.getYmax()-fMapImage.getYmin());
		
		var newBounds = fromRecenter || panAllowed;

		if (newBounds) {
			executeNewBounds((centerXgeo - distanceXgeo/2), (centerXgeo + distanceXgeo/2), (centerYgeo - distanceYgeo/2), (centerYgeo + distanceYgeo/2));
		}
	}

	function doRecenter(e) {
		if ( getImageXY(e) ) {
			startX = mouseX;
			startY = mouseY;
			executeRecenterOrPan(0, 0, true);
		} else {
			alert(ALERT_RIGHT_CLIC);
		}
		return false;
	}

	function startPan(e) {
		if ( getImageXY(e) ) {
			startX = mouseX;
			startY = mouseY;
			started = true;
		} else {
			alert(ALERT_RIGHT_CLIC);
		}
		return false;
	}

	function stopPan(e) {
		if ( getImageXY(e) ) {
			started = false;
			if (offsetX!=0 || offsetY!=0) {
				executeRecenterOrPan(offsetX, offsetY, false);
			}
			fCoucheImage.style.left = "0px";
			fCoucheImage.style.top = "0px";
		} else {
			alert(ALERT_RIGHT_CLIC);
		}
		return false;
	}

	// ------------------------------------------------------------------------
	// Gestion de l'outil "fiche information"
	// ------------------------------------------------------------------------
	this.activeMapQuery = function() {
		eval(fViewer + ".getControlPanel().activeTab(VUE_LAYERS);");
		fCouche = document.getElementById(this.getNom());
		fCoucheImage = document.getElementById(this.getNom() + '_mapLayer');
		this.desactiveTools();
		fCouche.style.cursor = "crosshair";
		this.setCurrentTool("QUERY");
		setMapEvents("QUERY");
	};
	
	function executeMapQuery(e) {
		var tmpDeltaX;
		var tmpDeltaY;
		if ( getImageXY(e) ) {
			if (FEATUREINFO_SERVER == "") {
				alert(ERROR_FEATUREINFO_SERVER);
			} else {
				var aGetFeatureInfoURLs = new Array(1);
				var nbURL = 0;
//				var urlQuery = "<Infos><Viewer>" + fViewer + "</Viewer><MapWidth>" + fMapImage.getWidth() + "</MapWidth><MapHeight>" + fMapImage.getHeight() + "</MapHeight>";
				var urlQuery = "<Infos>";
				
				if ( (mouseX > fCurrentLeft) && (mouseX < fCurrentLeft + fCurrentWidth) & (mouseY > fCurrentTop) & (mouseY < fCurrentTop + fCurrentHeight) ) {
						tmpDeltaX = mouseX-fCurrentLeft;
						tmpDeltaY = mouseY-fCurrentTop;
						var mapXmin = fMapImage.getXmin();
						var mapXmax = fMapImage.getXmax();
						for (var i=(fMapURLs.length-1) ; i>=1 ; i--) {

								if ( (fMapURLs[i].getInfoCard() && fMapURLs[i].getQueryable() && fMapURLs[i].getVisible() == true ) && ( (mapXmax-mapXmin) < fMapURLs[i].getMinScale() ) && ( (mapXmax-mapXmin) > fMapURLs[i].getMaxScale() ) ) {
										nbURL++;
										urlQuery += "<Layer><Name>" + escape(fMapURLs[i].getTitle()) + "</Name>";
										urlQuery += "<Request>" + (getWMSFeatureInfoURL(fMapURLs[i],tmpDeltaX,tmpDeltaY)) + "</Request></Layer>";
								}
						}
						urlQuery += "</Infos>";
						if (nbURL>0) {
							if (window.infoWindow && !infoWindow.closed) {
								infoWindow.close();
							}
							
							infoWindow = window.open("","infoWindow","toolbar=no, menubar=yes,scrollbars=yes,width=994,height=250,resizable=yes");
							infoWindow.document.write('<html><head><meta http-equiv="Content-Type" Content="text/html; charset=ISO-8859-1"></head>');
							infoWindow.document.write('<body><form name=\'appel\' action=\'' + FEATUREINFO_SERVER + '\' method=\'POST\'>');
							infoWindow.document.write('<input type=\'hidden\' name=\'infos\' value=\'' + urlQuery + '\'>');
							infoWindow.document.write('</form>');
							infoWindow.document.write(acai_carto_utils.waitingMessage(WAITING_QUERY));
							infoWindow.document.write('<script>window.focus();document.forms["appel"].submit()</script>');
							infoWindow.document.write('</body>');
							infoWindow.document.write('</html>');
							infoWindow.document.close();
						} else {
							alert(ALERT_NO_LAYERS_FOR_QUERY);
						}
				}
			}
		} else {
			alert(ALERT_RIGHT_CLIC);
		}
		return false;
	}

	function getWMSFeatureInfoURL(pLayer,x,y) {
			return pLayer.getServeur()
				+ "SERVICE=WMS&VERSION=1.1.1&REQUEST=GetFeatureInfo&SRS=" + fMapImage.getProjection()
				+ "&WIDTH=" + fMapImage.getWidth()
				+ "&HEIGHT=" + fMapImage.getHeight()
				+ "&BBOX=" + fMapImage.getXmin() + "," + fMapImage.getYmin() + "," + fMapImage.getXmax() + "," + fMapImage.getYmax()
				+ "&LAYERS=" + pLayer.getLayerName()
				+ "&STYLES=" + pLayer.getStyleName()
				+ "&FORMAT=" + pLayer.getImageFormat()
				+ "&QUERY_LAYERS=" + pLayer.getLayerName()
				+ "&X=" + x.toString()
				+ "&Y=" + y.toString()
//				+ "&INFO_FORMAT=text/xml"
				+ "&INFO_FORMAT=application/vnd.ogc.gml"
				+ "&FEATURE_COUNT=999";

	}

	// ------------------------------------------------------------------------
	// Gestion de l'outil "tableau des donnees"
	// ------------------------------------------------------------------------
	this.executeDataSheet = function(pViewer) {
		eval(fViewer + ".getControlPanel().activeTab(VUE_LAYERS);");
		this.desactiveTools();
		if (FEATURE_SERVER == "") {
			alert(ERROR_FEATURE_SERVER);
		} else {
			var aGetFeatureURLs = new Array(1);
			numLayer = pViewer.getLayerControl().getDataSheetLayer();

			var urlQuery = "<Infos><Viewer>" + fViewer + "</Viewer><MapWidth>" + fMapImage.getWidth() + "</MapWidth><MapHeight>" + fMapImage.getHeight() + "</MapHeight>";

			if (numLayer !=-1) {
					sGetFeatureURL = fMapURLs[numLayer].getServeur();
					sGetFeatureURL += "SERVICE=WFS&VERSION=1.0.0&REQUEST=GetFeature";
					sGetFeatureURL += "&TYPENAME=" + fMapURLs[numLayer].getLayerName();
					aGetFeatureURLs[1] = [fMapURLs[numLayer].getTitle(),sGetFeatureURL];
					urlQuery += "<Layer><Name>" + escape(fMapURLs[numLayer].getTitle()) + "</Name>";
					urlQuery += "<Request>" + sGetFeatureURL + "</Request></Layer>";
					urlQuery += "</Infos>";
					if (window.dataWindow && !dataWindow.closed) {
						dataWindow.close();
					}
					dataWindow = window.open("","dataWindow","toolbar=no, menubar=no,scrollbars=yes,width=500,height=400,resizable=yes");
					dataWindow.document.write('<html><head><title>' + "" + '</title></head>');
					dataWindow.document.write('<body><form name=\'appel\' action=\'' + FEATURE_SERVER + '\' method=\'POST\'>');
					dataWindow.document.write('<input type=\'hidden\' name=\'infos\' value=\'' + urlQuery + '\'>');
					dataWindow.document.write('</form>');
					dataWindow.document.write(acai_carto_utils.waitingMessage(WAITING_QUERY));
					dataWindow.document.write('<script>window.focus();document.forms["appel"].submit()</script>');
					dataWindow.document.write('</body>');
					dataWindow.document.write('</html>');
					dataWindow.document.close();
			} else {
					alert(ALERT_NO_LAYERS_FOR_DATA);
			}
		}
	};

	// ------------------------------------------------------------------------
	// Demande d'impression de la carte
	// ------------------------------------------------------------------------
	this.printMap = function() {
		try {
			printWindow.close();
		} catch (exception) {
		}
		var pShortMapTitle = this.getMapTitle();
		var posCopyright = pShortMapTitle.indexOf(" - &copy;")
		if (posCopyright != -1) {
			pShortMapTitle = pShortMapTitle.substring(0,posCopyright);
		}
		
		printWindow = window.open("","printWindow","toolbar=no, menubar=yes,scrollbars=yes,width=600,height=400,resizable=yes");
		var printContent ='<html><head><title>' + PRINT_WINDOW_TITLE + ' "' + pShortMapTitle + '"</title></head>';
		printContent += '<body bgcolor="#FFFFFF" text="#000000">';
		printContent += '<p style="text-align:center;"><img src="' + VIEWER_LOCATION + PRINT_LOGO + '"></p>';
		printContent += '<p style="font-family:Arial, Helvetica, sans-serif;font-style:normal;font-size:20;font-weight:bold;text-align:center;">' + pShortMapTitle + '</p>';

		var mapXmin = fMapImage.getXmin();
		var mapXmax = fMapImage.getXmax();
		for (var i = 1 ; i<fMapURLs.length ; i++) {
			if (fMapURLs[i].getVisible() && ( (mapXmax-mapXmin) < fMapURLs[i].getMinScale() ) && ( (mapXmax-mapXmin) > fMapURLs[i].getMaxScale() ) ) {
				op = parseFloat(document.getElementById("theMap"+"_WMS"+i).style.opacity);
				if (op == null) {
					op=1;
				}
				pSrc = this.getMapImage().getWMSLayerURL(fMapURLs[i]);
				calque = new acai_commons_image(pSrc, this.getMapTitle(), this.getWidth(), this.getHeight(), this.getNom() + '_WMS' + i);
				calque.setOpacity(op*100);

				printContent += '<div style=\'border:1px solid #000000;position:absolute;top:150;left:5;width:' + this.getWidth() + ';height:' + this.getHeight() + '\'>';
				printContent += calque.toHTML();
				printContent += '</div>';
			}
		}
		var posLegend = 10 + this.getWidth();
		printContent += '<div style=\'position:absolute;top:150;left:' + posLegend + '\'>';
		for (i = 1 ; i<fMapURLs.length ; i++) {
			if (fMapURLs[i].getVisible() && fMapURLs[i].getLegendURL()!='NONE' && ( (mapXmax-mapXmin) < fMapURLs[i].getMinScale() ) && ( (mapXmax-mapXmin) > fMapURLs[i].getMaxScale() ) ) {
				printContent += '<img src=\'' + fMapURLs[i].getLegendURL() + '\'/><br/>';
			}
		}
		printContent += '</div>';

		var posScale = 130 + this.getHeight();
		printContent += '<div style=\'position:absolute;top:' + posScale + ';left:10;background-color:#FFFFFF;';
		printContent += 'font-family:Verdana, Geneva, Arial, Helvetica, sans-serif;font-style:normal;font-size:10;'
		printContent += '\'>';
		printContent += '&nbsp;' + eval(fInfos + ".getScaleInfo('#000000','#FFFFFF')") + '&nbsp;';
		printContent += '</div>';

		if (fCopyright !== "") {
			printContent += '<div style=\'position:absolute;top:160;left:10;background-color:#FFFFFF;';
			printContent += 'font-family:Verdana, Geneva, Arial, Helvetica, sans-serif;font-style:normal;font-size:10;'
			printContent += '\'>';
			printContent += '&nbsp;&copy; ' + fCopyright + '&nbsp;<br/>';
			printContent += '</div>';
		}

		var posInfos = 150 + this.getHeight();
		printContent += '<div style=\'position:absolute;top:' + posInfos + ';left:5;width:' + this.getWidth() + ';';
		printContent += 'font-family:Verdana, Geneva, Arial, Helvetica, sans-serif;font-style:normal;font-size:10;'
		printContent += '\'>';
		printContent += '<br/><span style=\'font-weight:bold;\'>' + PRINT_WINDOW_LABEL_INFOS + '</span><br/>';
		if (fAuteur !== "") {
			printContent += PRINT_WINDOW_LABEL_AUTHOR + fAuteur + '<br/>';
		}
		if (fDateValidite !== "") {
			printContent += PRINT_WINDOW_LABEL_MAP_DATE + fDateValidite + '<br/>';
		}
		
		printContent += PRINT_WINDOW_LABEL_PRINT_DATE + (new Date()).toLocaleString() + '<br/>';
		if (fDescription !== "") {
			printContent += '<br/><span style=\'font-weight:bold;\'>' + PRINT_WINDOW_LABEL_DESC + '</span><br/>';
			printContent += fDescription + '<br/>';
		}
		if (fAppInfos !== "") {
			printContent += '<br/><div style=\'font-family:Verdana, Geneva, Arial, Helvetica, sans-serif;font-style:normal;font-size:10;font-weight:bold;text-align:center;\'>' + fAppInfos + '</div><br/>&nbsp;';
		}
		printContent += '</div>';
		printContent += '<script>alert("' + WAITING_PRINT + '");</script>';
		printContent += '</body>';
		printContent += '</html>';
		printWindow.document.write(printContent);
		printWindow.document.close();
		printWindow.focus();
	};

	// ------------------------------------------------------------------------
	// Export PDF de la carte
	// ------------------------------------------------------------------------
	this.exportPDF = function() {
		if (EXPORT_PDF_SERVER == "") {
			alert(ERROR_EXPORT_PDF_SERVER);
		} else {
			exportMap(EXPORT_PDF_SERVER);
		}
	};
	
	// ------------------------------------------------------------------------
	// Enregistrement PNG de la carte
	// ------------------------------------------------------------------------
	this.exportPNG = function() {
		if (EXPORT_PNG_SERVER == "") {
			alert(ERROR_EXPORT_PNG_SERVER);
		} else {
			exportMap(EXPORT_PNG_SERVER);
		}
	};
	
	function exportMap (exportService) {
		var pShortMapTitle = fMapTitle;
		var posCopyright = pShortMapTitle.indexOf(" - &copy;")
		if (posCopyright != -1) {
			pShortMapTitle = pShortMapTitle.substring(0,posCopyright);
		}
		var urlExport = "<Export>\n"
			+ "<Titre>" + pShortMapTitle.replace(/\x27/gi,"&#39") + "</Titre>\n"
			+ "<Largeur>" + fMapImage.getWidth() + "</Largeur>\n"
			+ "<Hauteur>" + fMapImage.getHeight() + "</Hauteur>\n";
			
		var mapXmin = fMapImage.getXmin();
		var mapXmax = fMapImage.getXmax();
		for (var i = 1 ; i<fMapURLs.length ; i++) {
			if (fMapURLs[i].getVisible() && ( (mapXmax-mapXmin) < fMapURLs[i].getMinScale() ) && ( (mapXmax-mapXmin) > fMapURLs[i].getMaxScale() ) ) {
				urlExport += "<Couche>\n";
				urlExport += "<Url>" + fMapImage.getWMSLayerURL(fMapURLs[i]) + "</Url>\n";
				op = parseFloat(document.getElementById("theMap"+"_WMS"+i).style.opacity);
				if (op == null) {
					op=1;
				}
				urlExport += "<Opacite>" + op + "</Opacite>\n";
				urlExport += "</Couche>\n";
			}
		}
		for (var i = 1 ; i<fMapURLs.length ; i++) {
			if (fMapURLs[i].getVisible() && ( (mapXmax-mapXmin) < fMapURLs[i].getMinScale() ) && ( (mapXmax-mapXmin) > fMapURLs[i].getMaxScale() ) ) {
				if (fMapURLs[i].getLegendURL()!='NONE') {
					urlExport += "<UrlLegende>" + fMapURLs[i].getLegendURL() + "</UrlLegende>\n";
				}
			}
		}
		for (var i=1; i<fListeLogosURLs.length ; i++) {
			urlExport += "<UrlLogo>" + fListeLogosURLs[i] + "</UrlLogo>\n";
		}
		for (var i=1; i<fListeLogosFichiers.length ; i++) {
			urlExport += "<FichierLogo>" + fListeLogosFichiers[i] + "</FichierLogo>\n";
		}
		
		if (fAuteur !== "") {
			urlExport += "<Auteur>" + fAuteur.replace(/\x27/gi,"&#39") + "</Auteur>\n";
		}
		if (fCopyright !== "") {
			urlExport += "<Copyright>" + fCopyright.replace(/\x27/gi,"&#39") + "</Copyright>\n";
		}
		if (fDescription !== "") {
			urlExport += "<Description>" + fDescription.replace(/<br\x2F>/gi,"\n").replace(/\x27/gi,"&#39") + "</Description>\n";
		}
		if (fDateValidite !== "") {
			urlExport += "<DateValidite>" + fDateValidite + "</DateValidite>\n";
		}
		if (fAppInfos !== "") {
			urlExport += "<ApplicationInfos>" + fAppInfos.replace(/<br\x2F>/gi,"\n").replace(/\x27/gi,"&#39") + "</ApplicationInfos>\n";
		}
		if (fCreator !== "") {
			urlExport += "<Creator>" + fCreator.replace(/<br\x2F>/gi,"\n").replace(/\x27/gi,"&#39") + "</Creator>\n";
		}
		
		urlExport += "</Export>";
		
		if (window.exportWindow && !exportWindow.closed) {
			exportWindow.close();
		}
		exportWindow = window.open("","exportWindow","toolbar=no, menubar=no,scrollbars=no,width=500,height=100,resizable=no");
		exportWindow.document.write('<html><head><title>' + "" + '</title></head>');
		exportWindow.document.write('<body><form name=\'appel\' action=\'' + exportService + '\' method=\'POST\'>');
		exportWindow.document.write('<input type=\'hidden\' name=\'paramsExport\' value=\'' + urlExport + '\'>');
		exportWindow.document.write('</form>');
		exportWindow.document.write('<script>window.focus();document.forms["appel"].submit()</script>');
		exportWindow.document.write('</body>');
		exportWindow.document.write('</html>');
		exportWindow.document.close();
	}

	// ------------------------------------------------------------------------
	// Gestion de l'outil "mesure de distances"
	// ------------------------------------------------------------------------
	this.activeDistance = function() {
		fCouche = document.getElementById(this.getNom());
		fCoucheImage = document.getElementById(this.getNom() + '_mapLayer');		
		this.desactiveTools();
		fCouche.style.cursor = "crosshair";
		jg = new jsGraphics(this.getNom() + '_mapLayer');
		jg.setColor(COSMETIC_COLOR);
		jg.setStroke(DISTANCE_LINES_WIDTH);
		this.setCurrentTool("DISTANCE");
		setMapEvents("DISTANCE");
	};

	function newPointDistance(e) {
		if ( getImageXY(e) ) {
			if ( (mouseX > fCurrentLeft) && (mouseX < fCurrentLeft + fCurrentWidth) & (mouseY > fCurrentTop) & (mouseY < fCurrentTop + fCurrentHeight) ) {
					// sur l'image !!!
					var pointActuelX = mouseX-fCurrentLeft;
					var pointActuelY = mouseY-fCurrentTop;
					if(pointAvantX != - 1) {
							var dist_x = (pointAvantX - pointActuelX)*(fMapImage.getXmax() - fMapImage.getXmin())/fCurrentWidth;
							var dist_y = (pointAvantY - pointActuelY)*(fMapImage.getYmax() - fMapImage.getYmin())/fCurrentHeight;
							var distPartielle = Math.sqrt(dist_x * dist_x + dist_y * dist_y);
							if (distPartielle > 0.001) {
								distTotale = distTotale + distPartielle;
								eval(fInfos + ".setDistance(" + distPartielle + "," + distTotale + ")");

								jg.drawEllipse(pointActuelX-4,pointActuelY-4,4,4);
								jg.drawLine(pointAvantX - 4, pointAvantY - 4, pointActuelX - 4, pointActuelY - 4);
								// d?calage de 4 pixels ???
								jg.paint();

							}
					} else {
							distTotale = 0;
							eval(fInfos + ".setDistance(0,0)");

							jg.clear();
							jg.setColor(COSMETIC_COLOR);
							jg.setStroke(DISTANCE_LINES_WIDTH);
							jg.drawEllipse(pointActuelX-4,pointActuelY-4,4,4);
							jg.paint();

					}
					pointAvantX = pointActuelX;
					pointAvantY = pointActuelY;
			}
		} else {
			alert(ALERT_RIGHT_CLIC);
		}
		return false;
	}
	
	function stopDistance(e) {
		if ( getImageXY(e) ) {
			pointAvantX = - 1;
			pointAvantY = - 1;
		} else {
			alert(ALERT_RIGHT_CLIC);
		}
		return false;
	}
	
	// ------------------------------------------------------------------------
	// Initialisation d'un outil "creation de geometrie"
	// ------------------------------------------------------------------------
	function initCreateGeometry(pNomCoucheImage) {
		draw_x = new Array();
		draw_y = new Array();
		nb_points = new Array();
		nb_poly = 0;
		nb_pts = 0;
		jg = new jsGraphics(pNomCoucheImage);
		jg.setColor(COSMETIC_COLOR);
		jg.setStroke(DISTANCE_LINES_WIDTH);
	}

	// ------------------------------------------------------------------------
	// Gestion de l'outil "creation de point"
	// ------------------------------------------------------------------------
	this.activeCreatePoint = function() {
		fCouche = document.getElementById(this.getNom());
		fCoucheImage = document.getElementById(this.getNom() + '_mapLayer');		
		this.desactiveTools();
		fCouche.style.cursor = "crosshair";
		initCreateGeometry(this.getNom() + '_mapLayer');
		type_geom = "point";
		this.setCurrentTool("POINT");
		setMapEvents("POINT");
	};

	function newPointPoint(e) {
		if ( getImageXY(e) ) {
			if ( (mouseX > fCurrentLeft) && (mouseX < fCurrentLeft + fCurrentWidth) & (mouseY > fCurrentTop) & (mouseY < fCurrentTop + fCurrentHeight) ) {
				// sur l'image !!!
				var pointActuelX = mouseX-fCurrentLeft;
				var pointActuelY = mouseY-fCurrentTop;
				if ((pointActuelX != pointAvantX) && (pointActuelY != pointAvantY)) {
					jg.drawEllipse(pointActuelX-4,pointActuelY-4,4,4);
					jg.paint();
					draw_x[nb_poly] = new Array();
					draw_y[nb_poly] = new Array();
					draw_x[nb_poly][nb_pts] = Xcarte(pointActuelX);
					draw_y[nb_poly][nb_pts] = Ycarte(pointActuelY);
					nb_poly++;
				}
				pointAvantX = pointActuelX;
				pointAvantY = pointActuelY;
			}
		}
		return false;
	}

	// ------------------------------------------------------------------------
	// Gestion de l'outil "creation de polyligne"
	// ------------------------------------------------------------------------
	this.activeCreatePolyline = function() {
		fCouche = document.getElementById(this.getNom());
		fCoucheImage = document.getElementById(this.getNom() + '_mapLayer');		
		this.desactiveTools();
		fCouche.style.cursor = "crosshair";
		initCreateGeometry(this.getNom() + '_mapLayer');
		type_geom = "polyligne";
		this.setCurrentTool("POLYLINE");
		setMapEvents("POLYLINE");
	};

	// ------------------------------------------------------------------------
	// Gestion de l'outil "creation de polygone"
	// ------------------------------------------------------------------------
	this.activeCreatePolygon = function() {
		fCouche = document.getElementById(this.getNom());
		fCoucheImage = document.getElementById(this.getNom() + '_mapLayer');		
		this.desactiveTools();
		fCouche.style.cursor = "crosshair";
		initCreateGeometry(this.getNom() + '_mapLayer');
		type_geom = "polygone";
		this.setCurrentTool("POLYGON");
		setMapEvents("POLYGON");
	};

	function newPointPolyXXX(e) {
		if ( getImageXY(e) ) {
			if ( (mouseX > fCurrentLeft) && (mouseX < fCurrentLeft + fCurrentWidth) & (mouseY > fCurrentTop) & (mouseY < fCurrentTop + fCurrentHeight) ) {
				// sur l'image !!!
				pointActuelX = mouseX-fCurrentLeft;
				pointActuelY = mouseY-fCurrentTop;
				jg.drawEllipse(pointActuelX-4,pointActuelY-4,4,4);
				if ((pointActuelX != pointAvantX) && (pointActuelY != pointAvantY)) {
					if (nb_pts != 0) {
						// dessine ligne
						jg.drawLine(pointAvantX - 4, pointAvantY - 4, pointActuelX - 4, pointActuelY - 4);
					}
				}
				jg.paint();
				if (nb_pts == 0) {
					draw_x[nb_poly] = new Array();
					draw_y[nb_poly] = new Array();
					pointInitX = pointActuelX;
					pointInitY = pointActuelY;
				}
				if ((pointActuelX != pointAvantX) && (pointActuelY != pointAvantY)) {
					draw_x[nb_poly][nb_pts] = Xcarte(pointActuelX);
					draw_y[nb_poly][nb_pts] = Ycarte(pointActuelY);
					nb_pts++;
				}
				pointAvantX = pointActuelX;
				pointAvantY = pointActuelY;
			}
		}
		return false;
	}
	
	function stopPolyline(e) {
		if ( getImageXY(e) ) {
			if(nb_pts > 1) {
				jg.paint();
				nb_poly++;
				nb_pts = 0;
				pointAvantX = - 1;
				pointAvantY = - 1;
			}
		} else {
			alert(ALERT_RIGHT_CLIC);
		}
		return false;
	}
	
	function stopPolygon(e) {
		if ( getImageXY(e) ) {
			if(nb_pts > 1) {
				jg.drawLine(pointInitX - 4, pointInitY - 4, pointActuelX - 4, pointActuelY - 4);
				jg.paint();
				nb_poly++;
				nb_pts = 0;
				pointAvantX = - 1;
				pointAvantY = - 1;
			}
		} else {
			alert(ALERT_RIGHT_CLIC);
		}
		return false;
	}

	this.activeDebug = function() {
		var debug = "";
		for (var poly = 0 ; poly < nb_poly ; poly++) {
			debug += "Poly " + poly + "\n";
			for (var pt = 0 ; pt < draw_x[poly].length ; pt++) {
				debug += "Pt " + pt + "\n";
				debug += "x= " + draw_x[poly][pt] + " y= " + draw_y[poly][pt] + "\n";
			}
			debug += "\n";
		}
//		alert(debug);
	};
	
	this.validGeometry = function() {
		var isValid = true;
		var errorMsg = "";
		var debug = "";
		if ((type_geom != "polygone") && (type_geom != "polyligne") && (type_geom != "polygonetrou") &&(type_geom != "point")) {
			errorMsg = "Pas de choix d'outil de saisie";
			isValid = false;
		} else {
			if (draw_x.length == 0) {
				errorMsg = "Pas d'objet saisi";
				isValid = false;
			} else {
				if (type_geom == "polyligne") {
					for (var poly = 0 ; poly < draw_x.length ; poly++) {
						if (draw_x[poly].length < 2) {
							errorMsg = "Une polyligne ne comporte pas au moins 2 points";
							isValid = false;
						}
					}
				}
				if (type_geom == "polygone") {
					for (var poly = 0 ; poly < draw_x.length ; poly++) {
						if (draw_x[poly].length < 3) {
							errorMsg = "Un polygone ne comporte pas au moins 3 points";
							isValid = false;
						}
					}
				}
			}
		}
			
		if (isValid) {
			for (var poly = 0 ; poly < draw_x.length ; poly++) {
				debug += "Poly " + poly + "\n";
				for (var pt = 0 ; pt < draw_x[poly].length ; pt++) {
					debug += "Pt " + pt + "\n";
					debug += "x= " + draw_x[poly][pt] + " y= " + draw_y[poly][pt] + "\n";
				}
				debug += "\n";
			}
//			alert(debug);
			var geomAsString = "";
			for (var poly = 0 ; poly < draw_x.length ; poly++) {
				for (var pt = 0 ; pt < draw_x[poly].length ; pt++) {
					geomAsString += draw_x[poly][pt] + "," + draw_y[poly][pt];
					if (pt != (draw_x[poly].length-1) ) {
						geomAsString += " ";
					}
				}
				if (type_geom == "polygone") {
					geomAsString += " " + draw_x[poly][0] + "," + draw_y[poly][0];
				}
				if (poly != (draw_x.length-1) ) {
					geomAsString += ":";
				}
			}
//			alert(geomAsString);


			if (window.validationWindow && !validationWindow.closed) {
				validationWindow.close();
			}
			var objet = "objetMetier";
			var srsCode = fMapImage.getProjection().split(":");
			validationWindow = window.open("","validationWindow","toolbar=no, menubar=no,scrollbars=yes,width=500,height=200,resizable=yes");
			validationWindow.document.write('<html><head><meta http-equiv="Content-Type" Content="text/html; charset=ISO-8859-1"></head>');
			validationWindow.document.write('<body><form name=\'appel\' action=\'' + fInsertService + '\' method=\'POST\'>');
			validationWindow.document.write('<input type=\'text\' name=\'' + fFeatureFieldName + '\' value=\'' + objet + '\'>');
			validationWindow.document.write('<input type=\'text\' name=\'' + fGeometryTypeFieldName + '\' value=\'' + type_geom + '\'>');
			validationWindow.document.write('<input type=\'text\' name=\'' + fGeometryFieldName + '\' value=\'' + geomAsString + '\'>');
			validationWindow.document.write('<input type=\'text\' name=\'' + fSrsFieldName + '\' value=\'' + srsCode[1] + '\'>');
			validationWindow.document.write('</form>');
//			validationWindow.document.write(acai_carto_utils.waitingMessage(WAITING_QUERY));
			validationWindow.document.write('<script>window.focus();document.forms["appel"].submit()</script>');
			validationWindow.document.write('</body>');
			validationWindow.document.write('</html>');
			validationWindow.document.close();
		} else {
			alert(errorMsg);
		}
	};
	
	// ------------------------------------------------------------------------
	// Gestion commune (survol pointeur sur la carte notamment)
	// ------------------------------------------------------------------------
	this.desactiveTools = function() {
		zooming = false;
		panning = false;
		document.onmousedown = rien;
		fCouche.onmouseup = rien;
		fCouche.ondblclick = rien;
		fCouche.style.cursor = "default";
		try {
			jg.clear();
			jg = null;
		} catch (exception) {
		}
		pointAvantX = - 1; // noe
		pointAvantY = - 1;	
		eval(fInfos + ".setDistance(-1,0)");
		eval(fMiniMap + ".desactiveTools()");
	};

	function setMapEvents(activeMapTool) {
//		fCouche.onmousemove = getMouse;
		if (activeMapTool == "ZOOM_RECT") {
			fCouche.onmousedown = startZoom;
			fCouche.onmouseup = stopZoom;
		}
		if (activeMapTool == "ZOOM") {
			fCouche.onmousedown = null;
			fCouche.onmouseup = doZoom;
		}
		if (activeMapTool == "PAN") {
			fCouche.onmousedown = startPan;
			fCouche.onmouseup = stopPan;
		}
		if (activeMapTool == "RECENTER") {
			fCouche.style.cursor = "move";
			fCouche.onmousedown = null;
			fCouche.onmouseup = doRecenter;
		}
		if (activeMapTool == "DISTANCE") {
			fCouche.onmouseup = null;
			fCouche.onmousedown = newPointDistance;
			fCouche.ondblclick = stopDistance;
		}
		if (activeMapTool == "QUERY") {
			fCouche.onmouseup = null;
			fCouche.onmousedown = executeMapQuery;
		}
		if (activeMapTool == "POINT") {
			fCouche.onmouseup = null;
			fCouche.onmousedown = newPointPoint;
//			fCouche.ondblclick = stopPoint;
		}
		if (activeMapTool == "POLYLINE") {
			fCouche.onmouseup = null;
			fCouche.onmousedown = newPointPolyXXX;
			fCouche.ondblclick = stopPolyline;
		}
		if (activeMapTool == "POLYGON") {
			fCouche.onmouseup = null;
			fCouche.onmousedown = newPointPolyXXX;
			fCouche.ondblclick = stopPolygon;
		}
	}

	function rien(e) {
		if (!getImageXY(e) ) {
			alert(ALERT_RIGHT_CLIC);
		}
		return false;
	}

	function getMouse(e) {
		if ( getImageXY(e) ) {
			if ( (mouseX < fCurrentLeft) || (mouseX > fCurrentLeft + fCurrentWidth) || (mouseY < fCurrentTop) || (mouseY > fCurrentTop + fCurrentHeight) ) {
				fCouche.style.cursor = "default";
				eval(fInfos + ".setGeoPos(0,0)");
			} else {
				eval(fInfos + ".setGeoPos(" + (mouseX-fCurrentLeft) + "," + (mouseY-fCurrentTop) + ")");
				if (started) {
					fCouche.style.cursor = ( zooming ? "crosshair" : fCouche.style.cursor);
					fCouche.style.cursor = ( panning ? "move" : fCouche.style.cursor);
	
					mouseX = ( (mouseX < fCurrentLeft) ? fCurrentLeft : mouseX);
					mouseX = ( (mouseX > fCurrentLeft + fCurrentWidth) ? fCurrentLeft+ fCurrentWidth : mouseX);
					mouseY = ( (mouseY < fCurrentTop) ? fCurrentTop : mouseY );
					mouseY = ( (mouseY > fCurrentTop + fCurrentHeight) ? fCurrentTop + fCurrentHeight : mouseY );
	
					if (zooming) {
						if (mouseX<startX) {
							realX = mouseX;
							deltaX = startX - mouseX;
						} else {
							realX = startX;
							deltaX = mouseX - startX;
						}
	
						if (mouseY<startY) {
							realY = mouseY;
							deltaY = startY - mouseY;
						} else {
							realY = startY;
							deltaY = mouseY - startY;
						}
						cadreZoom.setPosition(realX,realY,deltaX,deltaY);
						}
					if (panning) {
						offsetX = mouseX - startX;
						offsetY = mouseY - startY;
						fCoucheImage.style.left = offsetX + "px";
						fCoucheImage.style.top = offsetY + "px";
					}
				}
			}
		}
		return false;
	}

	function getImageXY(e) {
		var clicOK = false;
		if (navigator.userAgent.indexOf("MSIE") != -1) {
			mouseX = event.clientX + ( (document.documentElement && document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft);
			mouseY = event.clientY + ( (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop);;
			if ( event.button <= 1) {
				clicOK = true;
			}
			if ( event.button == 2) {
				clicOK = false;
			}
		} else {
			mouseX = e.pageX;
			mouseY = e.pageY;
			if ( e.which == 1 || e.which == 19) {
				clicOK = true;
			}
			if ( e.which == 3) {
				clicOK = false;
			}
		}
		clicOK=true;
		return clicOK;
	}

	// Xcarte transforme xImg abscisse en pixels sur Ox en une abscisse réèlle
	function Xcarte(xImg) {
		return acai_carto_utils.decimalRound(fMapImage.getXmin() + xImg *(fMapImage.getXmax() - fMapImage.getXmin())/fCurrentWidth,2);
	}

	// Ycarte transforme yImg ordonnée en pixels sur Oy en une ordonnée réelle
	function Ycarte(yImg) {
		return acai_carto_utils.decimalRound(fMapImage.getYmax() - yImg *(fMapImage.getYmax() - fMapImage.getYmin())/fCurrentHeight,2);
	}

	// ------------------------------------------------------------------------
	// fin d'execution du constructeur
	// ------------------------------------------------------------------------
	this.setNom(pNom);
	this.setPosition(pLeft,pTop,pWidth,pHeight);
	fCouche = document.getElementById(this.getNom());
}

acai_carto_dynamicMap.prototype = new acai_commons_genericCouche;

