/** //////////////////////////////////////////////////////////////////////////////////////
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
 File : Javascript / client.js

 Cross-Browser X DHTML Library v3.15.1, from Cross-Browser.com
 Copyright © 2002,2003,2004 Michael Foster (mike@cross-browser.com)
 
 Using iFrame :

- iframes are not supported by Netscape 4.
- iframes inside layers are buggy in Opera 6 (solved in 7). 
Even outside layers, they can be very buggy in Opera 5 on Mac.
- WebTV reloads the entire page when reloading an iframe.
- Using location.href in an iframe to change the page in the iframe does not work 
in Explorer 4 and 5.0 on Windows and Opera 5 and 6.

  XHTML DOM elements :

- Object Map. Must be an <input type="image" />
- Object ZoomBox. 
  Must be an <div id="zoomrect"></div> 
  with CSS definition : #zoomrect { position: absolute; top: 0; left: 0; width: 1; height: 1; z-index: 105; border: 1px solid blue; visibility: visible; }

_________________________________________________________________________________________
//////////////////////////////////////////////////////////*/

var _mapclient = null;

//---------------------------------------------------------------------------------------
// Stack up wondow.onload event using Simon Willison solution 
// loadEvent( function() { ... } );
//
function loadEvent(func) 
{
  var oldonload = window.onload;
  if (typeof window.onload != 'function') { 
    window.onload = func; 
  } 
  else {
    window.onload = function() { 
      oldonload(); 
      func();
    }
  }
  
  // Hide spinner when page is loaded
  xHide('workprogress');  
}


/** ---------------------------------------------------------------------------------------
*
* ClientMapServer class (compat. IE5+)
*
*/
function ClientMapServer(id)
{
	//// Private Objects [MODIFY TARGETS IF REQUIRED]
	this.oMap     = xGetElementById('mainmap');
	this.oZoomBox = xGetElementById('zoomrect');
	this.oSpinner = xGetElementById('workprogess');
	this.oDist = xGetParentFrameElementById('divdist');// Location: parent.document
	this.currentTool = 'ZOOM_IN';
	this.cnv_clicks = 0;
	this.draw_x = new Array();
	this.draw_y = new Array();
	this.Xpoints = new Array();
	this.Ypoints = new Array();
	this.canvas = xGetElementById("myCanvasDiv");
	this.canvas2 = xGetElementById("myCanvas2Div");
	this.jg = new jsGraphics(this.canvas.id); // a drawing canvas for the lines and points
	this.jg2 = new jsGraphics(this.canvas2.id); // a drawing canvas for the last moving vertex
	this.d2pts = 6; // the distance between to points (measure tools);
	this.nbPts = 20; // number of points for the last vertex

	//// Public Methods
	this.setMapSize = function(w, h) 
	{ 
	this.nMapWidth = w; this.nMapHeight = h; 
	// Restore current tool
	var oTargetCmdBar = parent._cmdBar; 
	if (typeof oTargetCmdBar == 'object') { oTargetCmdBar.restoreCurrentTool(this); }  
	}

	this.setMapGeoExtent = function(minx, miny, maxx, maxy) { this.fGeoMinX = minx; this.fGeoMinY = miny; this.fGeoMaxX = maxx; this.fGeoMaxY = maxy; }      
	this.setCurrentTool = function(t, oSrc)
	{	if (xStr(t))
	{ this.currentTool = t; xGetElementById('cmd').value = t; this.setCursor();
			if (this.currentTool == 'INFO')
			{	xGetElementById('mapzoom').style.visibility="visible";
		}
			else
			{	xGetElementById('mapzoom').style.visibility="hidden";
		}
			if ((this.currentTool == 'SURFACE') || (this.currentTool == 'DIST'))
			{	this.oMap.ondblclick = function(e) {o._handleDblClick(e) };
	//				this.canvas.ondblclick = function(e) {o._handleDblClick(e) };
			}
			else
			{	this.oMap.ondblclick = null;
	//				this.canvas.ondblclick = null;
			}
	}
	if ((this.currentTool != "SURFACE") || (this.currentTool != "DIST"))
	{	this.jg.clear();
		this.jg2.clear();
	}
	else
	{	this.cnv_clicks = 0;
			this.draw_x = new Array();
		  this.draw_y = new Array();
		  this.Xpoints = new Array();
		  this.Ypoints = new Array();
			this.jg.clear();
			this.jg2.clear();
	}
	}

	this.setCursor       = function()
	{
	// Note: [Gecko] take effect only on first pixel move of mouse
	if (this.currentTool == 'ZOOM_IN' || this.currentTool == 'ZOOM_OUT' || this.currentTool == 'DIST') { 
	  if (this.oMap.style) this.oMap.style.cursor = 'crosshair'; 
	  return;
	}
	else if (this.currentTool == 'MOVE') { 
	  if (this.oMap.style) this.oMap.style.cursor = 'move';
	}
	else if (this.currentTool.search(/INFO/g) != -1) { 
	  if (this.oMap.style) this.oMap.style.cursor = 'help'; 
	}
	}

	//// Private Properties
	this.fGeoMinX = this.fGeoMinY = this.fGeoMaxX = this.fGeoMaxY = 0; //this.fGeoMinW = xGetElementById('MAP_EXTENT_MINX').value;   

	// Pt screen onMouseDown (x1/y1), onMouseMove/Up (x2/y2)
	this.x1 = this.y1 = this.x2 = this.y2 = -1;
	this.drag = false;

	// Map position
	this.nMapPosLeft = xPageX(this.oMap); // xLeft
	this.nMapPosTop  = xPageY(this.oMap); // xTop
	this.nMapWidth   = 0;
	this.nMapHeight  = 0;

	this.oMap.style.position = 'absolute';
	this.oMap.style.top  = this.nMapPosTop;
	this.oMap.style.left = this.nMapPosLeft;

	// Initialize wait spinner...
	xHide(this.oSpinner);
	this.oSpinner.style.position = 'absolute';

	//// Private Events
	// Reference 'this' by 'o' since inside function 'this' refers oMap
	var o = this;

	this.oMap.onmouseover = function(e) {o._handleMouseEnter(e)};
	this.oMap.onmousedown = function(e) {o._handleMouseDown(e); /* Turn off default mouse selection */ return false; };
	this.oMap.onmousemove = function(e) {o._handleMouseMove(e)};
	this.oMap.onmouseup   = function(e) {o._handleMouseUp(e)};
	this.oMap.onmouseout  = function(e) {o._handleMouseOut(e) };
	this.oMap.onkeydown    = function(e) {o._handleKeyDown(e) }; 

	this.oZoomBox.onmousemove = function(e) {o._handleMouseMove(e)};
	this.oZoomBox.onmouseup = function(e) {o._handleMouseUp(e)};

	this.canvas.onmousemove = function(e) {o._handleMouseMove(e)};
	this.canvas.onmouseup = function(e) {o._handleMouseUp(e)};
	this.canvas.onkeydown = function(e) {o._handleKeyDown(e) }; 
}

var p = ClientMapServer.prototype;

p.calcDist = function(tabX, tabY)
{	if (tabX.length < 1) return 0;

	/* deb ortho
	var dblRayonTerre=6378.13;
	var disttot = 0;
	xpred = parseFloat(this.transformPix2Geo(tabX[0], 0, this.nMapWidth, this.fGeoMinX, this.fGeoMaxX, 0));
	ypred = parseFloat(this.transformPix2Geo(tabY[0], 0, this.nMapHeight, this.fGeoMinY, this.fGeoMaxY, 1));
	a1=xpred*Math.PI/180;
	b1=ypred*Math.PI/180;
	for (i=1; i<tabX.length; i++)
	{	x = parseFloat(this.transformPix2Geo(tabX[i], 0, this.nMapWidth, this.fGeoMinX, this.fGeoMaxX, 0));
		y = parseFloat(this.transformPix2Geo(tabY[i], 0, this.nMapHeight, this.fGeoMinY, this.fGeoMaxY, 1));

		// calcul de la distance
		var landaDeg=(y+ypred)/2;
		var landaRad=landaDeg*Math.PI/180;
		var r = dblRayonTerre*Math.cos(landaRad);
		var dist = r*2*Math.PI*(x - xpred)/360;

		// REVOIR ALGO ORTHODROMIE !!
		r = dblRayonTerre;
		a2=x*Math.PI/180;
		b2=y*Math.PI/180;
		dist = Math.acos(Math.cos(a1)*Math.cos(b1)*Math.cos(a2)*Math.cos(b2) + Math.cos(a1)*Math.sin(b1)*Math.cos(a2)*Math.sin(b2) + Math.sin(a1)*Math.sin(a2)) * r;

		disttot+=Math.abs(dist);

		a1=a2;
		b1=b2;
		xpred=x;
		ypred=y;
	}
	//fin ortho
	*/ 

	var disttot = 0;
	xpred = parseFloat(this.transformPix2Geo(tabX[0], 0, this.nMapWidth, this.fGeoMinX, this.fGeoMaxX, 0));
	ypred = parseFloat(this.transformPix2Geo(tabY[0], 0, this.nMapHeight, this.fGeoMinY, this.fGeoMaxY, 1));
	for (i=1; i<tabX.length; i++)
	{	x = parseFloat(this.transformPix2Geo(tabX[i], 0, this.nMapWidth, this.fGeoMinX, this.fGeoMaxX, 0));
		y = parseFloat(this.transformPix2Geo(tabY[i], 0, this.nMapHeight, this.fGeoMinY, this.fGeoMaxY, 1));

		// calcul de la distance
		var x2 = x-xpred;
		var y2 = y-ypred;
		var dist = Math.sqrt(x2*x2+y2*y2);
		disttot+=Math.abs(dist);

		xpred=x;
		ypred=y;
	}

	return Math.round(100*disttot/1000)/100;
}

p.calcSurface = function(tabX, tabY)
{	if (tabX.length < 3) return 0;
	
	/*var disttot = 0;
	xpred = parseFloat(this.transformPix2Geo(tabX[0], 0, this.nMapWidth, this.fGeoMinX, this.fGeoMaxX, 0));
	ypred = parseFloat(this.transformPix2Geo(tabY[0], 0, this.nMapHeight, this.fGeoMinY, this.fGeoMaxY, 1));
	for (i=1; i<tabX.length; i++)
	{	x = parseFloat(this.transformPix2Geo(tabX[i], 0, this.nMapWidth, this.fGeoMinX, this.fGeoMaxX, 0));
		y = parseFloat(this.transformPix2Geo(tabY[i], 0, this.nMapHeight, this.fGeoMinY, this.fGeoMaxY, 1));
		
		// calcul de la distance
		var x2 = x-xpred;
		var y2 = y-ypred;
		var dist = Math.sqrt(x2*x2+y2*y2);
		disttot+=Math.abs(dist);
		
		xpred=x;
		ypred=y;
	}*/
	
	var surftot = 0;
	//var yBase = 1000000;
	xpred = parseFloat(this.transformPix2Geo(tabX[tabX.length-1], 0, this.nMapWidth, this.fGeoMinX, this.fGeoMaxX, 0));
	ypred = parseFloat(this.transformPix2Geo(tabY[tabX.length-1], 0, this.nMapHeight, this.fGeoMinY, this.fGeoMaxY, 1));
	for (i=0; i<tabX.length; i++)
	{	x = parseFloat(this.transformPix2Geo(tabX[i], 0, this.nMapWidth, this.fGeoMinX, this.fGeoMaxX, 0));
		y = parseFloat(this.transformPix2Geo(tabY[i], 0, this.nMapHeight, this.fGeoMinY, this.fGeoMaxY, 1));
		
		// calcul de la surface
		if (xpred == x)
		{	continue;
		}
		//var lngY = ypred - yBase;
		//var lngY2 = y - yBase;
		var lngX = Math.abs(x - xpred);
		var surf = (y + ypred) * lngX / 2;
		if (x > xpred)
		{	surftot+=surf;
		}
		else
		{	surftot-=surf;
		}
		
		xpred=x;
		ypred=y;
	}

	return Math.round(100*Math.abs(surftot)/10000)/100;
}

p.annulerDist = function()
{	this.cnv_clicks = 0;
	this.draw_x = new Array();
	this.draw_y = new Array();
	this.jg.clear();
	this.oDist.innerHTML = '';
}

p.annulerZone = function()
{	this.cnv_clicks = 0;
	this.draw_x = new Array();
	this.draw_y = new Array();
	this.Xpoints = new Array();
	this.Ypoints = new Array();
	this.jg.clear();
	this.oDist.innerHTML = '';
}

p._handleKeyDown = function(evt) {
  evt = (evt) ? evt : ((event) ? event : null);

	// ECHAP
	if (evt.keyCode == '27' && (this.currentTool == 'SURFACE'))
	{	//if (confirm("Voulez-vous annuler la création de cette zone ?"))
		{	this.annulerZone();
		}
	}
	// else if (evt.keyCode == '67' && (this.currentTool == 'SURFACE'))
	// {	if (confirm("Voulez-vous sauvegarder cette zone ?"))
		// {	this.validerZone(1);
		// }
	// }
	// SUPPR
	else if (evt.keyCode == '46' && (this.currentTool == 'SURFACE'))
	{	alert(evt.keyCode);
	}
	// ECHAP
	if (evt.keyCode == '27' && (this.currentTool == 'DIST'))
	{	this.annulerDist();
	}
}


p._handleMouseEnter = function(e)
{	if (this.currentTool == 'PAN')
  {	this.oMap.style.cursor = 'move';
	}
	this.setCursor();
}

p._handleDblClick = function(e)
{	if (this.currentTool == "SURFACE")
	{	var pts = "";
		for (i=0; i<this.cnv_clicks; i++)
		{	pts+=this.Xpoints[i]+","+this.Ypoints[i]+";";
		}
	}
}

//--------------------------------------------------------------
// Purpose  : Directly submit an Action to server
// Return   : None.
//
// Used by actions :
// - ZOOM_FULL, 
// - NAV_PREV, 
// - NAV_NEXT

p.submitAction = function(sender) 
{
   xGetElementById('cmd').value = sender.getAttribute('id');
   xShow(this.oSpinner);
   document.formMap.submit();
}
  

p.transformPix2Geo = function(nPixPos, dfPixMin, dfPixMax, dfGeoMin, dfGeoMax, nInversePix) 
{
    dfWidthGeo = dfGeoMax - dfGeoMin;
    dfWidthPix = dfPixMax - dfPixMin;

    dfPixToGeo = dfWidthGeo / dfWidthPix;

    if (Math.abs(dfPixToGeo) < 1)
        dNbDecimal = Math.abs(Math.log(dfPixToGeo))/Math.log(10)+1;
    else
        dNbDecimal = 0;

    if (nInversePix == 0) {
        dfDeltaPix = nPixPos - dfPixMin;
    }
    else {
        dfDeltaPix = dfPixMax - nPixPos;
    }

    dfDeltaGeo = dfDeltaPix * dfPixToGeo;

    dfPosGeo = new String(new Number(dfGeoMin) + new Number(dfDeltaGeo));
    aPosGeo = dfPosGeo.split(".");

    if (dNbDecimal > 0 && aPosGeo.length == 2) {
        dfPosGeo = aPosGeo[0]+"."+aPosGeo[1].substr(0, dNbDecimal);
    }
    else {
        dfPosGeo = aPosGeo[0];
    }

    szttt =  ""+"pixelpos = "+nPixPos+" result ="+dfPosGeo;
    //alert(szttt);
    return dfPosGeo;
}



/**
 * This event occurs when user moves the mouse over the map
 **/

p._handleMouseMove = function(e)
{
  if (typeof xEvent != 'function')
    return;
    
  var evt = new xEvent(e);
  this.x2 = evt.pageX; // cross-browser X-screen coordinate
  this.y2 = evt.pageY; // cross-browser Y-screen coordinate

  // Set cursor according current tool
  this.setCursor();
  
  // Set X, Y
  var fGeoPosX = this.transformPix2Geo((this.x2 - this.nMapPosLeft), 0, this.nMapWidth, this.fGeoMinX, this.fGeoMaxX, 0);
  var fGeoPosY = this.transformPix2Geo((this.y2 - this.nMapPosTop),  0, this.nMapHeight, this.fGeoMinY, this.fGeoMaxY, 1);
//  this.oCoord.innerHTML =  'Lat : '+ fGeoPosX + '&nbsp;&nbsp;Lon : ' + fGeoPosY; // + ' / '  '<br />Lambert II étendu' ; //this.x2;
//  this.oCoordLat.innerHTML =  'X : '+ fGeoPosX;
//  this.oCoordLong.innerHTML =  'Y : ' + fGeoPosY;

	//// MODE PAN
	if (this.currentTool == 'PAN' && this.drag)
	{
		var left = this.x2 - this.x1 + this.nMapPosLeft;
		var top  = this.y2 - this.y1  + this.nMapPosTop;
		xMoveTo(this.oMap, left, top);

		//window.status = left + ' -/- ' + top; 

		var t = this.y1 - this.y2;
		var r = this.x1 - this.x2 + this.nMapWidth;
		var b = this.y1 - this.y2 + this.nMapHeight;
		var l = this.x1 - this.x2;

		xClip(this.oMap, t, r, b, l);

		return false; // Fix for IE5
	}

	//// MODE ZOOM_IN
	else if (this.currentTool == 'ZOOM_IN' && this.drag)
	{
		if(this.x1==this.x2 && this.y1==this.y2) {    
			xHide(this.oZoomBox); // defer to the browser cursor  
		}
		else 
		{
			var x = Math.min(this.x1,  this.x2);// - this.offsetx; // UL corner of box
			var y = Math.min(this.y1,  this.y2);// - this.offsety;  
			var w = Math.abs(this.x1 - this.x2) + 1; // + 2; //this.thickness;
			var h = Math.abs(this.y1 - this.y2) + 1; // + 2; //this.thickness;

			xMoveTo(this.oZoomBox, x, y);
			xResizeTo(this.oZoomBox, w, h);
			xShow(this.oZoomBox);

			// Only for debug
			//window.status = 'h:' + h + ' w:' + w + ' x:' + x + ' y:' + y + ' {' + this.x1 + '/' + this.y1 + ',' + this.x2 + '/' + this.y2 + '}';

			return false; // Fix for IE5
		}
	}
	else if (this.currentTool == "SURFACE")
	{	this.draw_x[this.cnv_clicks] = this.x2;
		this.draw_y[this.cnv_clicks] = this.y2;

		// this.jg2.clear();
		this.jg.clear();
		this.jg.drawPolylinePts(this.Xpoints,this.Ypoints, this.d2pts);

		if (this.draw_x.length > 1)
		{	this.jg.drawLinePts(this.draw_x[this.cnv_clicks],this.draw_y[this.cnv_clicks],this.draw_x[this.cnv_clicks - 1],this.draw_y[this.cnv_clicks - 1],this.d2pts);
		//    {	this.jg2.drawLinePts(this.draw_x[this.cnv_clicks],this.draw_y[this.cnv_clicks],this.draw_x[this.cnv_clicks - 1],this.draw_y[this.cnv_clicks - 1],this.d2pts);
		}
		if (this.draw_x.length > 2)
		{	this.jg.drawLinePts(this.draw_x[this.cnv_clicks],this.draw_y[this.cnv_clicks],this.draw_x[0],this.draw_y[0],this.d2pts);
		//    {	this.jg2.drawLinePts(this.draw_x[this.cnv_clicks],this.draw_y[this.cnv_clicks],this.draw_x[0],this.draw_y[0],this.d2pts);
		}
		//    this.jg2.paint();
		this.jg.paint();
		this.oDist.innerHTML =  'Surface : ' + this.calcSurface(this.draw_x,this.draw_y) + ' ha&nbsp;';
	}
	else if (this.currentTool == "DIST")
	{	this.draw_x[this.cnv_clicks] = this.x2;
		this.draw_y[this.cnv_clicks] = this.y2;
		this.jg.clear();
		this.jg.drawPolylinePts(this.draw_x,this.draw_y, this.d2pts);
		this.jg.paint();
		this.oDist.innerHTML =  'Distance : ' + this.calcDist(this.draw_x,this.draw_y) + ' km&nbsp;';
	}
}


/**
 * This event occurs when the mouse leaves the map
 **/

p._handleMouseOut = function(e) 
{
//  this.oCoord.innerHTML  = '';
  this.oMap.style.cursor = this.oZoomBox.style.cursor = "default";
}

p._handleMouseDown = function(e) 
{
  var evt = new xEvent(e);

  this.drag = true;
  this.x1 = this.x2 = evt.pageX;
  this.y1 = this.y2 = evt.pageY;
  
  if (this.currentTool == 'ZOOM_IN')
  {
    //jg.paint();
  }
}

p._handleMouseUp = function(e) 
{	var evt = new xEvent(e);
	
	this.drag = false;
	
	// Skip right mouse down button
	var rightclick = false;
	if (!e) var e = window.event;
	if (e.which) rightclick = (e.which == 3 || evt.keyCode == 0);
	else if (e.button) rightclick = (e.button == 2);
	if (rightclick) return true;
	
	// submit the form with the values
	
//  xGetElementById('ACTION').value = this.currentTool;
	xGetElementById('cmd').value = this.currentTool;
	
	this.x1 -= this.nMapPosLeft;
	this.x2 -= this.nMapPosLeft;
	this.y1 -= this.nMapPosTop;
	this.y2 -= this.nMapPosTop;
	
	if (this.currentTool == 'ZOOM_IN'  || 
	this.currentTool == 'ZOOM_OUT' || 
	this.currentTool == 'INFO')
	{	//xHide(this.oZoomBox);
		xGetElementById('NAV_INPUT_TYPE').value = 'auto_rect';
		xGetElementById('NAV_INPUT_COORDINATES').value = this.x1+','+this.y1+';'+this.x2+','+this.y2;
		
		if (this.currentTool == 'ZOOM_OUT')
		{ xGetElementById('NAV_INPUT_COORDINATES').value = this.x2+','+this.y2;
		xGetElementById('NAV_INPUT_TYPE').value = 'POINT';
		}
		//xHide(this.oMap);
		xShow(this.oSpinner);
		document.formMap.submit();
	}
	else if (this.currentTool == 'PAN')
	{	xGetElementById('NAV_INPUT_COORDINATES').value = this.x1+','+this.y1+';'+this.x2+','+this.y2;
		//xHide(this.oMap);
		xShow(this.oSpinner);
		document.formMap.submit();
	}
	else if (this.currentTool.search(/INFO/g) != -1)
	{	if (this.currentTool.search(/INFO/g) != -1)
		{	// We want redirect query result to a popup window
			// (only parent iframe can open window) parent.xWinOpen
			window.open(xGetElementById(this.currentTool).value + '?X=' + this.x2 + '&Y=' + this.y2, "Résultats", "left=300,top=200,width=800,height=400,location=1,menubar=1,resizable=1,scrollbars=1,status=1,toolbar=1"); 
			return false;
		}
		else
		{	xGetElementById('NAV_INPUT_COORDINATES').value = this.x2+','+this.y2;
			xShow(this.oSpinner);
			document.formMap.submit();    
		}
	}
	else if (this.currentTool == "SURFACE")
	{	if (this.cnv_clicks == 0)
		{	this.draw_x[this.cnv_clicks]=this.x2;
			this.draw_y[this.cnv_clicks]=this.y2;
		}
		++this.cnv_clicks;
		
		this.draw_x[this.cnv_clicks]=this.x2;
		this.draw_y[this.cnv_clicks]=this.y2;
		
		this.Xpoints[this.cnv_clicks-1]=this.draw_x[this.cnv_clicks];
		this.Ypoints[this.cnv_clicks-1]=this.draw_y[this.cnv_clicks];
		
		this.jg.clear();
		this.jg.drawPolylinePts(this.Xpoints,this.Ypoints, this.d2pts);
		//    this.jg.paint();
		
		if (this.draw_x.length > 2)
		{	//this.jg2.clear();
			//this.jg2.drawLinePts(this.draw_x[this.cnv_clicks],this.draw_y[this.cnv_clicks],this.draw_x[0],this.draw_y[0],this.d2pts);
			//this.jg2.paint();
			this.jg.drawLinePts(this.draw_x[this.cnv_clicks],this.draw_y[this.cnv_clicks],this.draw_x[0],this.draw_y[0],this.d2pts);
		}
		this.jg.paint();
		this.oDist.innerHTML =  'Surface : ' + this.calcSurface(this.draw_x,this.draw_y) + ' ha&nbsp;';
	}
	else if (this.currentTool == "DIST")
	{	if (this.cnv_clicks == 0)
		{	this.draw_x[this.cnv_clicks]=this.x2;
			this.draw_y[this.cnv_clicks]=this.y2;
		}
		++this.cnv_clicks;
		
		this.draw_x[this.cnv_clicks]=this.x2;
		this.draw_y[this.cnv_clicks]=this.y2;
		
		this.jg.clear();
		this.jg.drawPolylinePts(this.draw_x,this.draw_y, this.d2pts);
		this.jg.paint();
		
		this.oDist.innerHTML =  'Distance : ' + this.calcDist(this.draw_x,this.draw_y) + ' km&nbsp;';
	}
}

// Called by the result popup window
function submitAction(szAction, szParams)
{	xShow('workprogess');
	xGetElementById('ACTION').value = szAction;
	xGetElementById('PARAMS').value = szParams;
	document.formMap.submit();
}

// This function is can be called by parent frame 
// while reloading map

function Wait() { // Show spinner
	xShow('workprogess');
}

