var numDecimals;
function calcDistance(mX0,mY0,mX,mY) {
	// Note: decimal are not hard coded to allow use with locales using commas instead of points.
	//if (clickCount>0) {
		var mUnits = mUnits //"METERS"; //MapUnits;
		var mDistance = 0;
		//var p = clickCount-1;
	//	var Lon1 = clickPointX[p] * Math.PI / 180;
	//	var Lon2 = mX * Math.PI / 180;
	//	var Lat1 = clickPointY[p] * Math.PI / 180;
	//	var Lat2 = mY * Math.PI / 180;
	//	var LonDist = Lon1-Lon2;
	//	var LatDist = Lat1-Lat2;
	//	if (MapUnits=="DEGREES") {
	//		var A = Math.pow(Math.sin(LatDist / 2),2) + Math.cos(Lat1) * Math.cos(Lat2) * Math.pow(Math.sin(LonDist /2),2);
	//		//var A = Math.cos(Lat1) * Math.cos(Lat2) * Math.pow(Math.sin(LonDist /2),2);
	//		var C = 2 * Math.asin(Math.min(1, Math.sqrt(A)));
	//		var D = (3963 - 13 * Math.sin((Lat1 + Lat2) / 2)) * C
	//		mDistance = D * 5280;
	//		mUnits = "FEET";
	//	} else {
			var xD = Math.abs(mX - mX0);
			var yD = Math.abs(mY - mY0);
			mDistance = Math.sqrt(Math.pow(xD,2) + Math.pow(yD,2));
	//	}
		
		//var theDist = convertUnits(mDistance,'METERS','METERS');
		var theDist = convertUnits(mDistance,mUnits,sUnits);
		var u = Math.pow(10,numDecimals);
		currentMeasure = parseInt(theDist*u+(5/10))/u;
		
	//	updateMeasureBox();
		
	//}
}

// convert the amounts to new units
function convertUnits(theDist1,mUnits,sUnits) {
	// Note: decimal are not hard coded to allow use with locales using commas instead of points.	
	var theDist = parseFloat(theDist1);
	var mDistance = theDist;
	//alert(theDist);
	if (mUnits == "FEET") {
		if (sUnits=="MILES") {
			mDistance = theDist / 5280;
		} else if (sUnits == "METERS") {
			mDistance = theDist * (3048/10000);
		} else if (sUnits == "KILOMETERS") {
			mDistance = theDist * (3048/10000000);
		}
	} else {
		if (sUnits=="MILES") {
			mDistance = theDist * (6213711922/10000000000000);
		} else if (sUnits == "FEET") {
			mDistance = theDist * (3280839895/1000000000);
		} else if (sUnits == "KILOMETERS") {
			mDistance = theDist / 1000;
		}
	}
	
	
	//////////////////////////////////
	sbDistance = mDistance/5;
	// /*
	var num1 = 0;
	var num2 = sbDistance;
	if (sbDistance>10000000) {
		num1 = parseInt(sbDistance/5000000);
		num2 = num1 * 5000000;
	} else if (sbDistance>1000000) {
		num1 = parseInt(sbDistance/500000);
		num2 = num1 * 500000;
	} else if (sbDistance>100000) {
		num1 = parseInt(sbDistance/50000);
		num2 = num1 * 50000;
	} else if (sbDistance>10000) {
		num1 = parseInt(sbDistance/5000);
		num2 = num1 * 5000;
	} else if (sbDistance>1000) {
		num1 = parseInt(sbDistance/500);
		num2 = num1 * 500;
	} else if (sbDistance>100) {
		num1 = parseInt(sbDistance/50);
		num2 = num1 * 50;
	} else if (sbDistance>10) {
		num1 = parseInt(sbDistance/5);
		num2 = num1 * 5;
	} else if (sbDistance>1) {
		num1 = parseInt(sbDistance/(25/100));
		num2 = num1 * (25/100);
	} else if (sbDistance>0.1) {
		num1 = parseInt(sbDistance/(25/1000));
		num2 = num1 * (25/1000);
	}
	sbDistance = num2;
	// */
	if (sbDistance > 2) {
		ScaleBarPrecision = "0";
	} else if (sbDistance > 1) {
		ScaleBarPrecision = "1";
	} else if (sbDistance > 1/10) {
		ScaleBarPrecision = "2";
	} else if (sbDistance > 1/100) {
		ScaleBarPrecision = "3";
	} else {
		ScaleBarPrecision = "4";
	}
	//////////////////////////////////
	 numDecimals = ScaleBarPrecision;
	
	
	var u = Math.pow(10,numDecimals);
	//alert(u);
	if (!isNav) mDistance = parseInt(mDistance * u + (5/10)) / u
	//alert(mDistance);
	return mDistance;
}

function startmeas()
{
	document.getElementById("zoomin").src = "images/ToolBox/zoom_off.gif"
	document.getElementById("zoomout").src = "images/ToolBox/zoomout_off.gif"
	document.getElementById("Identify").src = "images/ToolBox/identify_off.gif"
	document.getElementById("imgDiv").style.cursor = "crosshair";
	if(document.frmMap.hid_drawxstrmeas.value == "&")
	{
		document.frmMap.hid_drawxstrmeas.value = "";
		document.frmMap.hid_drawystrmeas.value = "";
	}		
	document.frmMap.hid_drawxstrmeas.value = document.frmMap.hid_drawxstrmeas.value + "&";
	document.frmMap.hid_drawystrmeas.value = document.frmMap.hid_drawystrmeas.value + "&";	
	document.getElementById("div1").style.visibility = 'visible';	
	document.frmMap.divvisibility.value = 'visible';
	document.frmMap.meastext2.value = 0;
	document.frmMap.hid_startmeasure.value = "stop";
	document.frmMap.strAction.value="measure";
	var inhtml;
	
		inhtml =" <table width=100%><tr><td><table width=100%><tr><td align=left><b> Measure </td><td align=right><img src='images/toolbox/measure.gif'></td></tr></table></td></tr>"
		inhtml = inhtml + "<tr height='2'><td bgcolor='#006dad' width='100%'></td></tr>"
		inhtml = inhtml + "<tr><td >Click on the Map</td></tr>"
		inhtml = inhtml + "<tr><td align=center><img src='images/measurehint.gif'></td></tr>"
		inhtml = inhtml + "<tr height='2'><td bgcolor='#006dad' width='100%'></td></tr>"
		inhtml = inhtml + "</table>";
	if(usehint == "true")
	{
	//document.getElementById("div1").style.visibility = 'visible';	
		document.getElementById("hint").innerHTML = inhtml;
	}
}
function clearmeas()
{
	document.frmMap.hid_drawxstrmeas.value = "";
	document.frmMap.hid_drawystrmeas.value = "";	
	document.frmMap.strAction.value="refresh";
	document.frmMap.meastext1.value = 0;
	document.frmMap.meastext2.value = 0;
	document.getElementById("div1").style.visibility = 'hidden'	
	document.frmMap.divvisibility.value = 'hidden';
	document.frmMap.hid_startmeasure.value = "stop";
	document.frmMap.submit();
}