var clickX			= new Array();
var clickY			= new Array();
var idx				= 0;
var sVMLPath		= new String();	
var blnMouseUp		= false;
var isClosed		= false;
var pixels			= 96;  //pixels on screen
var blnMouseUp		= true;
var blnMouseDn		= false;
var i				= 0;
var okToDraw		= false;
var eventSrcElement = "";
var unit			= 1; //default unit = feet.  change to 3.2808 if map units are meters
var vmlLine_yadj	= 0; // see createVMLLine()

function setMeasureUnits(unitParm) { if (unitParm == 'Meter') unit = 3.2808; }
function getMeasureUnits() { return unit; }

function makeVMLLine(startx,starty,endx,endy){
	var v = document.getElementById("vline")
	v.to= "'" + endx + ", " + endy + "'";  //  .from and .visibility is in dragimg.js
	v.from = "'" + startx + "," + starty + "'";
	//v.coordsize="21600,21600" //"580,520" 
	//v.coordorigin="14,119"
	//v.strokeweight=".75pt";
	//v.style.position="absolute";
	//v.strokecolor = "#ff0000";
	//v.style.zIndex="160";
	v.style.visibility="visible";
}

function createVMLLine(startx,starty,endx,endy){
	var v = document.createElement("v:line")	
	v.coordsize="21600,21600" //"580,520"
	v.coordorigin="0,0" //"14,119"
	v.to= "'" + endx + "," + (endy+vmlLine_yadj) + "'";  //  .from and .visibility is in dragimg.js
	v.from = "'" + startx + "," + (starty+vmlLine_yadj) + "'";
	v.strokeweight="0.75pt";
	v.style.position="absolute";
	v.style.top="0px";
	v.style.left="0px";
	v.strokecolor = "#ff0000";
	v.style.zIndex="160"
	//v.style.width="" + mapWidth + "px";
	//v.style.height="" + mapHeight + "px";
	document.body.appendChild(v);	
}

//this is not called from drawLine (above) any longer


/*
function getFeetDistance(x1,x2,y1,y2){

	// calc distance, get map points 
	x1 = Get_Map_PointX(x1)
	x2 = Get_Map_PointX(x2)
	y1 = Get_Map_PointY(y1)
	y2 = Get_Map_PointY(y2)
	
	//alert(x1 + " ; " + x2 + " ; " + y1 + " ; " + y2)
	
	var Dx, Dy
	
	Dx = Math.abs(x2-x1)
	Dy = Math.abs(y2-y1)
	
	//Distance formula
	lenMapUnits = Math.sqrt(Math.pow(Dx,2) + Math.pow(Dy,2))
	
	//alert(lenMapUnits)
	
	return lenMapUnits 
	
}
*/

function Get_Map_PointX(Xmouse){
	var ext_xmin
	var ext_xmax
	var x_point 
	Xmin = document.forms[0].txtXMin.value
	Xmax = document.forms[0].txtXMax.value
	ext_xmin = parseFloat(getLastExtent(Xmin))
	ext_xmax = parseFloat(getLastExtent(Xmax))

	x_point = parseFloat(ext_xmin) + parseFloat(((Math.abs(ext_xmax - ext_xmin) * Xmouse) / mapWidth))
	return x_point
}

function Get_Map_PointY(Ymouse){

    var ext_ymin
    var ext_ymax
    var y_point
	
	var Ymin = document.forms[0].txtYMin.value
	var Ymax = document.forms[0].txtYMax.value
	ext_ymin = getLastExtent(Ymin)
	ext_ymax = getLastExtent(Ymax)
	y_point = parseFloat(ext_ymin) + parseFloat(((Math.abs(ext_ymax - ext_ymin) * Ymouse) /  mapHeight))

	return y_point
}

function flipYMouseCoord(yMouse){
	var newY, newYCoord;	
	newY = mapHeight - yMouse;	
	newYCoord = Get_Map_PointY(newY)	
	return newYCoord	
}

function getLastExtent(ext){
	var word,newWord,wordLen,lastPos;
	newWord = ext.substring(0,ext.lastIndexOf("|"))
	lastPos = newWord.lastIndexOf("|") + 1
	lastChar = newWord.length
	chars = newWord.length - lastPos
	word = ext.substring(lastPos,lastChar)
	return word;
}


//function for defining a point	
function point(x,y){
	this.x=x;
	this.y=y;
}

//function for adding points to lines
function addToLine(line,item,x,y){
	line[item] = new point(x,y);
	return line;
}
//functioin for adding points to polygons
function addToPoly(polygon,item,x,y){
	polygon[item]=new point(x,y);
	return polygon;
}

//function for finding the area of a polygon, providing the polygon array(made of points)
//and the number of points. This is NOT persisted on the map.
function polygonArea(polygon,N){
	var i=0;
	var j;
	var area=0;
	for(i=0;i<N;i++){
		j = (i+1) % N;
		area += polygon[i].x * polygon[j].y * unit * unit;
		area -= polygon[i].y * polygon[j].x * unit * unit;
	}
	area/=2;
	return (area<0?-area:area);
}

//function for finding the perimeter of a polygon, providing the polygon array(made of points)
//and the number of points
function getPolyPerimeter(polygon,N){
	var i=0;
	var j;
	var length=0;
	if(N==2){
		length += Math.sqrt(((polygon[i].x-polygon[0].x) * (polygon[1].x-polygon[0].x)) + ((polygon[1].y-polygon[0].y) * (polygon[1].y-polygon[0].y))) * uni
	}else{
		for(i=0;i<N;i++){
			j = (i+1) % N;
			length += Math.sqrt(((polygon[i].x-polygon[j].x)*(polygon[i].x-polygon[j].x)) + ((polygon[i].y-polygon[j].y)*(polygon[i].y-polygon[j].y)));
		}
	}
	return length;
}

//function for finding th elength of any segment of a line, including the entire line.
//pass in th eline(array of points), the number of points in the array, 
//and which point to start on.
function getPolylineLength(line,N,start){
	var i=0;
	var j;
	var length=0;
	for(i=start;i<N;i++){
		j = (i+1);
		length += Math.sqrt(((line[i].x-line[j].x)*(line[i].x-line[j].x))+((line[i].y-line[j].y)*(line[i].y-line[j].y))) * unit
	}
	return length;
}

function getLatLon(uX,uY){

	uX = uX * getMeasureUnits();
	uY = uY * getMeasureUnits();
	
	pi4=Math.PI/4;
	p0=LatOrigin*angRad;
	p1=Lat1stParallel*angRad;
	p2=Lat2ndParallel*angRad;
	m0=CentralMeridian*angRad;
	x0=CentralMeridianEasting * getMeasureUnits(); // 12-5-08 boh - modified formula to accomodate coordinates that are not based in feet
	y0=FalseNorthing * getMeasureUnits(); // 12-5-08 boh - modified formula to accomodate coordinates that are not based in feet
	
	with(Math){
		var m1=cos(p1)/sqrt(1-(pow(ec,2))*pow(sin(p1),2))
		m2=cos(p2)/sqrt(1-(pow(ec,2))*pow(sin(p2),2))
		t0=tan(pi4-(p0/2))
		t1=tan(pi4-(p1/2))
		t2=tan(pi4-(p2/2))
		t0=t0/pow(((1-(ec*(sin(p0))))/(1+(ec*(sin(p0))))),ec/2)
		t1=t1/pow(((1-(ec*(sin(p1))))/(1+(ec*(sin(p1))))),ec/2)
		t2=t2/pow(((1-(ec*(sin(p2))))/(1+(ec*(sin(p2))))),ec/2)
		var n=log(m1/m2)/log(t1/t2)
		f=m1/(n*pow(t1,n))
		rho0=a*f*pow(t0,n)
		uX=uX-x0
		uY=uY-y0
		pi2=pi4*2
		rho=sqrt(pow(uX,2)+pow((rho0-uY),2))
		theta=atan(uX/(rho0-uY))
		txy=pow((rho/(a*f)),(1/n))
		lon=(theta/n)+m0
		uX=uX+x0
		var lat0=pi2-(2*atan(txy))
		part1=(1-(ec*sin(lat0)))/(1+(ec*sin(lat0)))
		lat1=pi2-(2*atan(txy*pow(part1,(ec/2))))
		while((abs(lat1-lat0))>0.000000002){
			lat0=lat1
			part1=(1-(ec*sin(lat0)))/(1+(ec*sin(lat0)))
			lat1=pi2-(2*atan(txy*pow(part1,(ec/2))));
		}
	}
	var lat=lat1/angRad;
	lon=lon/angRad;
	LatDMS=DDToDMS(lat,false,true);
	LonDMS=DDToDMS(-lon,false,true);

	var u=100000;lat=parseInt(lat*u+(5/10))/u;
	lon=parseInt(lon*u+(5/10))/u;
	return"Lat/Lon DD: "+lat+"\xB0 N,  "+-lon+"\xB0 W"+"<br>DMS: "+LatDMS+" N,  "+LonDMS+" W";
	//return"Lat/Lon DD: "+lat+" N,  "+-lon+" W"+"<br>DMS: "+LatDMS+" N,  "+LonDMS+" W";
}

function DDToDMS(dd,addZero,addDecimal){
	var d=parseInt(dd)
 dF=Math.abs(dd-d)
 m=parseInt(dF*60)
 s=dF*3600-m*60;
 if(addDecimal){
  var u=10;
  s=parseInt(s*u+(5/10))/u;
 }else{
  s=Math.round(s);
 };
 
 // boh 03-20-06 - fix minutes and seconds out of range
 if (s >= 60) {
   s = s - 60;
   m = m + 1;
 }
 if (m >= 60) {
   m = m - 60;
   d = d + 1;
 }
 // boh 03-20-06 - end
 
 if(addZero){
  if(d<10){
   d="0"+d;
  };
  if(m<10){
   m="0"+m;
  };
  if(s<10){
   s="0"+s;
  };
 };
 var dms=(d+'\xB0 '+m+'\' '+s+'\"');
 //var dms=d+" "+m+"' "+s+'"';
 return dms;

	/*var d=parseInt(dd)
	dF=Math.abs(dd-d)
	m=parseInt(dF*60)
	s=dF*3600-m*60;
	if(addDecimal){
		var u=10;
		s=parseInt(s*u+(5/10))/u;
	}else{
		s=Math.round(s);
	};if(addZero){
		if(d<10){
			d="0"+d;
		};
		if(m<10){
			m="0"+m;
		};
		if(s<10){
			s="0"+s;
		};
	};
	var dms=(d+'\xB0 '+m+'\' '+s+'\"');
	return dms;
*/
}


