//*****************************************************************************
// javascript for map zoom and pan functions
//*****************************************************************************

//*****************************************************************************
// Settings for map image size
// default map image size, in pixels
// dynamically overridden, not necessary to change this unless program behavior is revised

var iWidth = 740; 	// image width, MAP_WIDTH on server side
var iHeight = 445;	// image height, MAP_HEIGHT on server side
var minx=0.0; // these values will be reset each time a map returned.
var miny=0.0;
var maxx=0.0;
var maxy=0.0;
var unitsPerPixel=0.0;

//*****************************************************************************
// Global vars to save mouse position
var mouseX=0;
var mouseY=0;
var mapX=0.0;// real coord for mouse point;
var mapY=0.0; // used for draw lines, etc

var x1=0; // hold first mouse down x
var y1=0; // first mouse down y
var x2=0; // moving mouse x
var y2=0; // moving mouse y
var zleft=0;  // used for clip
var zright=0; 
var ztop=0;
var zbottom=0;

var zoomBoxColor = "#FF0000";	// color of zoombox

var hspc = 0; 		// horizontal image offset
var vspc = 0; 		// vertical image offset
var dragBoxLineSiz = 2; 	// Zoombox line width;
		
var zooming = false;
var panning = false;


//*****************************************************************************
//assign event handlers used by both Nav and IE
function init() {
	if (parent.isNav) {
		setNSEventCapture();
	}
	document.onmousedown = mapClickDn;
	document.onmousemove = getMouse;
	document.onmouseup = mapClickUp;
	document.ondblclick = mapDblClick;
	
	//window.onresize = resizeMap;
}

//set event capture for NAV
function setNSEventCapture() {
	if (parent.isNav) {
		document.captureEvents(Event.MOUSEDOWN | Event.MOUSEUP | Event.MOUSEMOVE | Event.RESIZE|Event.DBLCLICK);
	}
}
//*****************************************************************************
// map_dhtml.js
//
// library functions for layer manipulations
//
//*****************************************************************************
// Create a DHTML layer
function createLayer(name, inleft, intop, width, height, visible, content) {
	var layer;
	if (parent.isNav4) {
		document.writeln('<layer name="' + name + '" left=' + inleft + ' top=' + intop + ' width=' + width + ' height=' + height +  ' visibility=' + (visible ? '"show"' : '"hide"') +  '>');
		document.writeln(content);
		document.writeln('</layer>');
	} else {
		document.writeln('<div id="' + name + '" style="position:absolute; overflow:hidden; left:' + inleft + 'px; top:' + intop + 'px; width:' + width + 'px; height:' + height + 'px;' + '; z-index:1; visibility:' + (visible ? 'visible;' : 'hidden;') +  '">');
		document.writeln(content);
		document.writeln('</div>');
	}
}

// Create a default DHTML layer, only used in status bar
function createDefaultLayer(name, visible, content) {
	var layer;
	if (parent.isNav4) {
		document.writeln('<layer name="' + name + '" visibility=' + (visible ? '"show"' : '"hide"') +  '>');
		document.writeln(content);
		document.writeln('</layer>');
	} else {
		document.writeln('<div id="' + name + '" style="overflow:hidden; visibility:' + (visible ? 'visible;' : 'hidden;') +  '">');
		document.writeln(content);
		document.writeln('</div>');
	}
}

// check for existance of layer
function hasLayer(name) {
	return getLayer(name)!=null;
}

// get the layer object called "name"
function getLayer(name) {
	if (parent.isNav4) {
		return(document.layers[name]);
	} else if (parent.isIE4) {
		layer = eval('document.all.' + name + '.style');
		return(layer);
	} else if (parent.is5up) {
		var theObj = document.getElementById(name);
		if (theObj!=null) return theObj.style;
	} 
	return(null);
}

function isVisible(name) {
	var layer = getLayer(name);
	if (parent.isNav && layer.visibility == "show")
		return(true);
	if (parent.isIE && layer.visibility == "visible")
		return(true);
	return(false);
}
		

// move layer to x,y
function moveLayer(name, x, y) {		
	var layer = getLayer(name);		
	if (parent.isNav4)
		layer.moveTo(x, y);
		 //if (document.all) {
	else {
		layer.left = x + "px";
		layer.top  = y + "px";
	}
}

// set layer background color
function setLayerBackgroundColor(name, color) {		
	var layer = getLayer(name);		
	if (parent.isNav4)
		layer.bgColor = color;
		//else if (document.all)
	else
		layer.backgroundColor = color;
}

// toggle layer to invisible
function hideLayer(name) {		
	var layer = getLayer(name);		
	if (parent.isNav4)
		layer.visibility = "hide";
		//if (document.all)
	else
		layer.visibility = "hidden";
		//layer.display="none";
}

// toggle layer to visible
function showLayer(name) {		
	var layer = getLayer(name);		
	if (parent.isNav4)
		layer.visibility = "show";
		//if (document.all)
	else
		layer.visibility = "visible";
		//layer.display="block";
}

// clip layer display to clipleft, cliptip, clipright, clipbottom
// Not working with Mozilla Milestone 12 (Nav5)
function clipLayer2(name, clipleft, cliptop, clipright, clipbottom) {		
	var layer = getLayer(name);		
	if (parent.isNav4) {
		layer.clip.left   = clipleft;
		layer.clip.top    = cliptop;
		layer.clip.right  = clipright;
		layer.clip.bottom = clipbottom;
	}
	//if (document.all)
	else if (parent.isIE) {
		layer.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
	}
}

function clipLayer(name, clipleft, cliptop, clipright, clipbottom) {		
	var layer = getLayer(name);		
	if (parent.isNav4) {
		layer.clip.left   = clipleft;
		layer.clip.top    = cliptop;
		layer.clip.right  = clipright;
		layer.clip.bottom = clipbottom;
	} else {
		var newWidth = clipright - clipleft;
		var newHeight = clipbottom - cliptop;
		layer.height = newHeight;
		layer.width	= newWidth;
		layer.top	= cliptop + "px";
		layer.left	= clipleft + "px";
	}
}

function boxIt(theLeft,theTop,theRight,theBottom) {
	if (!parent.isNav4) {
		theTop = theTop + vspc;
		theBottom = theBottom + vspc;
		theLeft = theLeft + hspc;
		theRight = theRight + vspc;
	}
	clipLayer("zoomBoxTop",theLeft,theTop,theRight,theTop+dragBoxLineSiz);
	clipLayer("zoomBoxLeft",theLeft,theTop,theLeft+dragBoxLineSiz,theBottom);
	clipLayer("zoomBoxRight",theRight-dragBoxLineSiz,theTop,theRight,theBottom);
	clipLayer("zoomBoxBottom",theLeft,theBottom-dragBoxLineSiz,theRight,theBottom);	
	showLayer("zoomBoxTop");
	showLayer("zoomBoxLeft");
	showLayer("zoomBoxRight");
	showLayer("zoomBoxBottom");
}



/////THE FOLLOWING IS ORGINAL map_pan_zoom.js
//*****************************************************************************
// map_pan_zoom.js
//
// pan and zoom handlers
//*****************************************************************************

//*****************************************************************************
// doMouseClick() is used for all tools that don't drag boxes or move images
// handles mouseup and mousedn
// returns true if mouse click in map window, else returns false

function doMouseClick(e) {
	moveLayer("theMap",hspc,vspc);
	getImageXY(e);	//save image x&y in global mouseX and mouseY
	if (inMapWindow()) {
		document.frmMap.x1.value = mouseX;
		document.frmMap.y1.value = mouseY;
		document.frmMap.x2.value = mouseX;
		document.frmMap.y2.value = mouseY;
		return true;
	}
	return false;
}



//*****************************************************************************
// startZoomBox() 
// start the drag box

function startZoomBox(e) {
	moveLayer("theMap",hspc,vspc);
	getImageXY(e);	//save image x&y in global mouseX and mouseY
	// keep it within the MapImage or MapWindow
	if (inMapWindow()) {
		if (zooming) {
			stopZoomBox(e);
		} else {
			x1=mouseX;
			y1=mouseY;
			x2=x1+1;
			y2=y1+1;
			zleft=x1;
			ztop=y1;
			zbottom=y1;
			zright=x1
					
			boxIt(x1,y1,x2,y2);
			
			//store coords in form, with pt2 = pt1 for mouseDown
			document.frmMap.x1.value = x1;
			document.frmMap.y1.value = y1;
			document.frmMap.x2.value = x1;
			document.frmMap.y2.value = y1;
			
			zooming=true;
		}
	}
	return false;
}

//*****************************************************************************
// stopZoomBox() 
// stops the zoom box
// doesn't invoke other actions

function stopZoomBox(e) {
	zooming=false;
	hideLayer("zoomBoxTop");
	hideLayer("zoomBoxLeft");
	hideLayer("zoomBoxRight");
	hideLayer("zoomBoxBottom");
	//window.scrollTo(0,0);
	//return true;
}

//*****************************************************************************
// getMouse() 
// get the coords at mouse position
// continues the pan action, rubber-bands the zoom action

function getMouse(e) {
	window.status="";
	getImageXY(e);	//save image x&y in global mouseX and mouseY
	// keep it within the MapImage or MapWindow
	if (!inMapWindow()) {
		//this abandons the drag box without calling operation
		//useful mid-drag "escape route" for users
		//comment-out to keep dragging,
		//box will freeze at edge until mouse brought back over image or released
		killDrag(e);
		
	} else {
		x2=mouseX;
		y2=mouseY;
		//showStatus("");
		if (zooming) {
			setClip();
		} else if (panning) {
			panMouse();	
		} else if (circling){
			circleIt(x1,y1,x2,y2);
		} else if (drawing){
			showGraphics(x2,y2);
			if (parent.ToolFrame.tool=="measure"){
				showMeasure(x2,y2);
			}
		} else {
			getMapXY(mouseX, mouseY);
			showStatus("Coordinates:["+parseInt(mapX)+","+""+parseInt(mapY)+"]");
		}
		
		// save x2&y2 to form fields
		document.frmMap.x2.value = x2;
		document.frmMap.y2.value = y2;
   	}
	// next line needed for Mac
	return false;
}

function showStatus(statusString){
	if (parent.MapStatusBarFrame){
		//parent.MapStatusBarFrame.document.Show.MouseXY.value = statusString;
		parent.MapStatusBarFrame.document.forms[0].coords.value=statusString;
	}else {
		window.status = statusString; 
	}
}


// get cursor location, save image x&y in globals
function getImageXY(e) {
	if (parent.isNav) {
		mouseX=e.pageX;
		mouseY=e.pageY;
	} else {
		mouseX=event.clientX + document.body.scrollLeft;
		mouseY=event.clientY + document.body.scrollTop;
	}
	// subtract offsets from page left and top
	mouseX = mouseX-hspc;
	mouseY = mouseY-vspc;
}	


// clip zoom box layer to mouse coords
function setClip() {	
	var tempX=x1;
	var tempY=y1;
	if (x1>x2) {
		zright=x1;
		zleft=x2;
	} else {
		zleft=x1;
		zright=x2;
	}
	if (y1>y2) {
		zbottom=y1;
		ztop=y2;
	} else {
		ztop=y1;
		zbottom=y2;
	}
	
	if ((x1 != x2) && (y1 != y2)) {
		boxIt(zleft,ztop,zright,zbottom);
	}
	//return false;
}



function killDrag(e) {
	if (zooming) {
		stopZoomBox(e);
	}
	else if (panning) {
		moveLayer("theMap",0,0);
		clipLayer("theMap",0,0,iWidth,iHeight);
		stopPan(e);
	}else if (circling){
		stopCircle(e);
	} else if (drawing){
		// 2003-08-21: stopGraphics(e);
	}
}

//*****************************************************************************
// startPan() 
// starts the pan, movement of image

function startPan(e) {
	moveLayer("theMap",hspc,vspc);
	getImageXY(e);
	// keep it within the MapImage or MapWindow
	if (inMapWindow()) {
		if (panning) {
			//stopPan(e);
			killDrag(e);
		} else {
			x1=mouseX;
			y1=mouseY
			x2=x1+1;
			y2=y1+1;
			panning=true;
			//store coords in form, with pt2 = pt1 for mouseDown
			document.frmMap.x1.value = x1;
			document.frmMap.y1.value = y1;
			document.frmMap.x2.value = x1;
			document.frmMap.y2.value = y1;
		}
	}
	return false;

}

//*****************************************************************************
// stopPan() 
// stops the pan, movement of image

function stopPan(e) {
	//window.scrollTo(0,0);
	panning=false;
	//return true;
	
}

//*****************************************************************************
// panMouse() 
// continue the pan, movement of image

function panMouse() {
	var xMove = x2-x1;
	var yMove = y2-y1;
	moveLayer("theMap",xMove+hspc,yMove+vspc);
	//return false;
}




//*****************************************************************************
// setZoomColor() 
// set the drag box color

function setZoomColor() {
	setLayerBackgroundColor("zoomBoxTop", zoomBoxColor);
	setLayerBackgroundColor("zoomBoxLeft", zoomBoxColor);
	setLayerBackgroundColor("zoomBoxRight", zoomBoxColor);
	setLayerBackgroundColor("zoomBoxBottom", zoomBoxColor);

	setLayerBackgroundColor("zoomOVBoxTop", zoomBoxColor);
	setLayerBackgroundColor("zoomOVBoxLeft", zoomBoxColor);
	setLayerBackgroundColor("zoomOVBoxRight", zoomBoxColor);
	setLayerBackgroundColor("zoomOVBoxBottom", zoomBoxColor);
}

//*****************************************************************************
// inMapWindow() 
// returns true if current mouseX and mouseY are inside map window
// accounts for scrolling of document

function inMapWindow() {
	if (parent.isIE) {
		if ((mouseX>(document.body.scrollLeft)) && 
			(mouseX<(document.body.scrollLeft+document.body.clientWidth)) && 
			(mouseY>(document.body.scrollTop)) &&
			(mouseY<(document.body.scrollTop+document.body.clientHeight)))
			return true;
		else
			return false;
	} else {
		if ((mouseX>(window.pageXOffset)) && 
			(mouseX<(window.pageXOffset + window.innerWidth)) && 
			(mouseY>(window.pageYOffset)) &&
			(mouseY<(window.pageYOffset + window.innerHeight)))
			return true;
		else
			return false;
	}
}


//*****************************************************************************
// map_click.js
// map mouse event handlers
//
//
//*****************************************************************************

//mapClickDn() handler for frmMap onMouseDown event
//initial toolbar click invokes opCode integrity check
function mapClickDn(e) {
	if (!isLeftMouse(e)) return false;
	var opCode = document.frmMap.tool.value;
	if (opCode=="") opCode=parent.ToolFrame.tool;
  	//var opCode = parent.ToolFrame.tool;
	switch (opCode) {
		//possible future tools, unknown behaviors
		case "zoom_in":	
		case "zoom_out":
		case "sel_feature":// allow multiple 
			startZoomBox(e);
			break;
		case "pan":
			startPan(e);
			break;
      	case "graphics":
      		var gType="";
	  		try{gType=parent.AuxFrame.getGraphicsType();} catch (e){}
      		if (gType=="circle"){
        		startCircle(e);
      		} else{
        		startGraphics(e);
      		}
      		break;
		case "measure":
			startGraphics(e);
			break;
		case "identify":
		case "hot_link":
    	case "text":
   			if (doMouseClick(e))
			 parent.ToolFrame.doCommand(opCode);
			break;
   		default:
		//	alert("Unknown TOOL opcode(4): "+opCode);
	}
	return false;
}

// mapClickUp() handles mouseUp event
//
function mapClickUp(e) { 
	if (!isLeftMouse(e)) return false;
	getImageXY(e);
	var opCode = document.frmMap.tool.value;
	if (opCode=="") opCode=parent.ToolFrame.tool;
	//var opCode = parent.ToolFrame.tool;
	switch (opCode) {
		//these tools invoke the operation on mouse up
		case "zoom_in":	
		case "zoom_out":
		case "sel_feature":// allow miltiple:pvweb2
			if (zooming) {
				stopZoomBox(e);
				parent.ToolFrame.doCommand(opCode);
			}
			break;
		case "pan":
			if (panning) {
				stopPan(e);
				parent.ToolFrame.doCommand('pan');
			}
			break;
        case "graphics":
      		var gType="";
	  		try{gType=parent.AuxFrame.getGraphicsType();} catch (e){}
            if (gType=="circle"){
        		if (circling){
          			stopCircle(e);
                // NL: 2004-05-29: submit form in AuxForm instead of mapForm
                var f1=parent.AuxFrame.document.forms[0];
                var f2=parent.MapFrame.document.forms[0];
                f1.x1.value=f2.x1.value;
                f1.y1.value=f2.y1.value;
                f1.x2.value=f2.x2.value;
                f1.y2.value=f2.y2.value;
                
          			parent.ToolFrame.doCommand(opCode);
        		}
      		} else {
        		addPoint(mouseX, mouseY);
      		}
      		break;
        case "measure":
			addPoint(mouseX, mouseY);
			break;
		default:
			break;
	}
	// next line needed for Mac	
	return false;
}

//function used to determine whether left mouse button was clicked
function isLeftMouse(e) {
	if (parent.isNav) {
		if (e.which == 1) return true;
	} else if (parent.isIE) {
		if (event.button == 1) return true;
	}
	return false;
}

//*******************
// 2003-04-02: start draw circle
var circling=false;
function startCircle(e) {
	moveLayer("theMap",hspc,vspc);
	getImageXY(e);	//save image x&y in global mouseX and mouseY
	// keep it within the MapImage or MapWindow
	if (inMapWindow()) {
		if (circling) {
			stopCircle(e);
		} else {
			// x1, y1 is center, x2,y2 is a point on cricle
			x1=mouseX;
			y1=mouseY
			x2=x1+1;
			y2=y1+1;
			showLayer("oval");// "oval" is the DIV, theOval is VML shape
			
			circleIt(x1,y1,x2,y2);
			
			//store coords in form, with pt2 = pt1 for mouseDown
			document.frmMap.x1.value = x1;
			document.frmMap.y1.value = y1;
			document.frmMap.x2.value = x2;
			document.frmMap.y2.value = y2;
			circling=true;
		}
	}
	return false;
}

function circleIt(theCenterX, theCenterY, theArcX, theArcY){
	var t=parent.ToolFrame;
	var radius=parseInt(Math.sqrt(parseFloat(theCenterX-theArcX)*parseFloat(theCenterX-theArcX)+        	parseFloat(theCenterY-theArcY)*parseFloat(theCenterY-theArcY)));
    var r=radius *(maxx-minx)/iWidth;
	var area=3.1415926*r*r;
    r=convertUnits(r,t.mapUnits,t.distUnits);
	area=convertUnits(area,t.mapUnits,t.areaUnits);
	showStatus("[Radius="+formatNumber(r,t.numDecimals)+t.distUnits+" Area="+formatNumber(area,t.numDecimals)+t.areaUnits+"]");
   var shape=document.getElementById("theOval");
   if (shape!=null) setShapeProperties(shape);
  clipLayer("theOval",theCenterX-radius,theCenterY-radius,theCenterX+radius,theCenterY+radius);
}

function stopCircle(e){
	circling=false;
	hideLayer("oval");// oval is layer name, "theOval" is shape name;
}

// 2003-04-03
var drawing=false;
var mapXs=new Array();
var mapYs=new Array();
var points=0;
var graphicsString="";
var totalLength=0.0;
var segmentLength=0.0;

function getMapXY(px, py){
	mapX=(maxx-minx)*(px/iWidth)+minx;
	mapY=(maxy-miny)*((iHeight-py)/iHeight)+miny;
}
function startGraphics(e){
	moveLayer("theMap",hspc,vspc);
	getImageXY(e);	//save image x&y in global mouseX and mouseY
	// keep it within the MapImage or MapWindow
	if (inMapWindow()) {
		if (drawing) {
			//stopGraphics(e);
		} else {
			x1=mouseX;
			y1=mouseY;
    		points=0;
			totalLength=0.0;
			segmentLength=0.0;
			graphicsString="";
			mapXs.length=0;
			mapYs.length=0;
			showLayer("graphics");
			addPoint(x1,y1);
			//showGraphics(mouseX, mouseY);
			drawing=true;
		}
	}
	return false;
}
function showGraphics(px, py){
	var tv= document.frmMap.tool.value;//parent.ToolFrame.tool;//
	if (tv=="graphics"){
    	tv=parent.AuxFrame.getGraphicsType();
  	}
	var gPath=graphicsString;
	if (tv=="line"||tv=="polygon"||tv=="measure") {
		gPath=gPath+" l "+px+","+py; // l : line to 
  	} 
	var closeLine=" e";
	if (tv=="polygon") closeLine=" x e";
	var shape=document.getElementById("theGraphics");
  if (shape!=null){
    shape.path=gPath+closeLine;
    setShapeProperties(shape);
  }
}

function stopGraphics(e){
	drawing=false;
}
function addPoint(px, py){
  getMapXY(px,py);
  mapXs[points]=mapX;
  mapYs[points]=mapY;
  points++;  
  totalLength+=segmentLength
  var tv=document.frmMap.tool.value;//parent.ToolFrame.tool;//
  if (tv=="graphics"){
    tv=parent.AuxFrame.getGraphicsType();
  }
  if (tv=="point"){
  	graphicsString=graphicsString+" m "+px+","+py+" l "+(px+2)+","+py+" "+(px+2)+","+(py+2)+" "+px+","+(py+2)+" "+px+","+py;
  } else {
    if (points==1) {
  	 	graphicsString="m "+px+","+py; // m: move to 
  	}else{
	 	graphicsString=graphicsString+" l "+px+","+py;
  	}
  }
}

function showMeasure(px,py){
    var t=parent.ToolFrame;
	getMapXY (px,py);
	var preX=mapXs[points-1];
	var preY=mapYs[points-1];
    segmentLength=Math.sqrt((preX-mapX)*(preX-mapX)+(preY-mapY)*(preY-mapY));
	var tmpTotal=totalLength+segmentLength;
	var measureArea=calcArea(mapXs,mapYs, mapX, mapY);
	
	var sLength=convertUnits(segmentLength,t.mapUnits,t.distUnits);
   var tTotal=convertUnits(tmpTotal,t.mapUnits,t.distUnits);
	var mArea=convertUnits(measureArea,t.mapUnits,t.areaUnits);

	document.measureFrm.theMeasSegment.value = formatNumber(sLength,t.numDecimals)+ " "+t.distUnits;
	document.measureFrm.theMeasTotal.value = formatNumber(tTotal,t.numDecimals)+ " "+t.distUnits;
	document.measureFrm.theMeasArea.value = formatNumber(mArea,t.numDecimals)+ " "+t.areaUnits;
	
	var ml=getLayer("measureBox");
	//showStatus("h:"+parseInt(ml.height)+"w:"+);
	var mx=Math.min(parent.getMapWidth()-parseInt(ml.width), px);
	var my=Math.min(parent.getMapHeight()-parseInt(ml.height), py);
	moveLayer("measureBox",mx,my);
	//moveLayer("measureBox",px,py);
	showLayer("measureBox");
	
	//showStatus("Segment Length: "+parseInt(segmentLength)+" Total Length: "+parseInt(totalLength));
}

// reference http://astronomy.swin.edu.au/~pbourke/geometry/polyarea/
// xs, ys: array contains clicked points. x,y: moving points;
function calcArea (xs,ys, x,y) {
  if (xs.length<2) return 0;
  var i=0, j=0;
  var xxs=new Array();
  var yys=new Array();
  for (i=0; i<xs.length; i++){
    xxs[i]=xs[i];
	yys[i]=ys[i];
  }
  xxs[xs.length]=x;
  yys[ys.length]=y;
  var area = 0.0;
  for (i=0;i<xxs.length;i++) {
		j = (i + 1) % xxs.length;
		area += xxs[i] * yys[j]-xxs[j]*yys[i];
  }
  area /= 2.0;
  return(area < 0 ? -area : area);
}

function formatNumber(number, decno){
	var intpart=parseInt(number);
	if (decno==0) return intpart;
	var decnum=number-intpart;
	//var decp=parseInt(decnum*10^decno);
	var decpart=""+parseInt(Math.pow(10,decno)*decnum+0.5);
	// fill leading 0s.
	for (i=0; i<decno; i++){
		decpart="0"+decpart;
	}
	 decpart=decpart.substring(decpart.length-decno);
	return ""+intpart+"."+decpart;
}
// http://www.ex.ac.uk/cimt/dictunit/dictunit.htm
function convertUnits(oldTotal,oldUnits,newUnits) {
	var newTotal=0;
	if (oldUnits=="FEET") {
		if (newUnits=="FEET") {	newTotal = oldTotal ;}
		else if (newUnits=="MILES") {	newTotal = oldTotal / 5280;}
		else if (newUnits=="METERS") {	newTotal = oldTotal * (3048/10000);	}
		else if (newUnits=="KILOMETERS") {	newTotal = oldTotal * (3048/10000000);}
		// area:
		else if (newUnits=="SQFEET"){newTotal=oldTotal;}
		else if (newUnits=="ACRES"){newTotal=oldTotal /9/4840;}
		else if (newUnits=="SQMILES"){newTotal=oldTotal /9/4840/640;}
		else if (newUnits=="SQMETERS"){newTotal=oldTotal*0.0929 ;}
		else if (newUnits=="SQKILOMETERS"){newTotal=oldTotal*0.0929 /1000000;}
	}/* else if (oldUnits=="MILES") {
		if (newUnits=="FEET") {			newTotal = oldTotal * 5280;	} 
		else if (newUnits=="METERS") {	newTotal = oldTotal * (1609344/1000);					} 
		else if (newUnits=="KILOMETERS") {	newTotal = oldTotal * (1609344/1000000);}
	} else if (oldUnits=="METERS") {
		if (newUnits=="FEET") {		newTotal = oldTotal * (3280839895/1000000000);} 
		else if (newUnits=="MILES") {newTotal = oldTotal * (6213711922/10000000000000);	} 
		else if (newUnits=="KILOMETERS") {	newTotal = oldTotal / 1000;}
	} else if (oldUnits=="KILOMETERS"){
		if (newUnits=="FEET") {	newTotal = oldTotal * (3280839885/1000000);	}
		else if (newUnits=="METERS") {newTotal = oldTotal * 1000;} 
		else if (newUnits=="MILES") {newTotal = oldTotal * (6213711922/10000000000);}
	} */else{
		alert("non supported units");
	}
	
	return newTotal;
}


function mapDblClick(e){
	stopGraphics(e);
	var opCode = document.frmMap.tool.value;
	if (opCode=="") opCode=parent.ToolFrame.tool;
	//var opCode = parent.ToolFrame.tool;
	switch (opCode) {
    	case "graphics":
			var str="";
			for (var i=0; i<mapXs.length; i++){
				if (i>0) str+=";"
				str=str+mapXs[i]+" "+mapYs[i];
			}
			if (str!=""){
				document.frmMap.coords.value=str;
        parent.AuxFrame.document.forms[0].coords.value=str;
				parent.ToolFrame.doCommand(opCode);
			}
			break;
		default:
		 break;
	}
}
function clearGraphics(){
	hideLayer("measureBox");
	stopGraphics();
	points=0;
	totalLength=0.0;
	segmentLength=0.0;
	graphicsString="";
	mapXs.length=0;
	mapYs.length=0;
	hideLayer("graphics");
}

var hex = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F");
var RGB=new Array(256);
var k=0;
for (i = 0; i < 16; i++) {	
	for (j = 0; j < 16; j++) {		
		RGB[k] = hex[i] + hex[j];
		k++;
	}
}

function rgbToHex(theRGB){
  var vals=theRGB.split(",");
  var ret="#"+RGB[parseInt(vals[0])]+RGB[parseInt(vals[1])]+RGB[parseInt(vals[2])];
  return ret;
}

function setShapeProperties(shape){
  var f=parent.AuxFrame.document.graphicsForm;
  if (f!=null){
      shape.strokeColor=rgbToHex(f.color.options[f.color.selectedIndex].value);
      shape.strokeWeight=f.width.options[f.width.selectedIndex].text+" px";
  } else {
     shape.strokeColor="#FF0000";
     shape.strokeWeight="1 px";
  }

}



