
/*********************************************************
 Anno  (MarkUp Tab) code
********************************************************** */
var mouseAnnoLoaded=false;
var mouseAnnoLoadedType=false;
var annoTypeIndex = 0

function setAnnoType(idx){
	annoTypeIndex = idx;
	document.forms[0].txtAnnoIndex.value=idx;
	switch(idx){
		case "1": //point
			document.getElementById("trText1").style.display="block";
			document.getElementById("tblAnnoText").style.visibilty="visible"
			document.forms[0].ddlCoordType.style.visibility="visible";
			document.getElementById("spnAnnoInfo").innerHTML="Click on the map to place a point graphic to the map"
			//document.getElementById("spnSetAnno").style.visibility="hidden"
			break;
		case "2": //circle
			var tbl = document.getElementById("tblAnnoText")
			tbl.style.visibility="hidden";
			document.forms[0].ddlCoordType.style.visibility="hidden";
			document.getElementById("spnAnnoInfo").innerHTML="Click and drag on the map to draw a circle starting at the center point of the circle"
			document.getElementById("spnSetAnno").style.visibility="hidden"
			break;
		case "3": //rectangle
			var tbl = document.getElementById("tblAnnoText")
			tbl.style.visibility="hidden";
			document.forms[0].ddlCoordType.style.visibility="hidden";
			document.getElementById("spnAnnoInfo").innerHTML="Click and drag on the map to draw a rectangle"
			document.getElementById("spnSetAnno").style.visibility="hidden"
			break;
		case "4": // Line
			var tbl = document.getElementById("tblAnnoText")
			tbl.style.visibility="hidden";
			document.forms[0].ddlCoordType.style.visibility="hidden";
			document.getElementById("spnAnnoInfo").innerHTML="Click and drag on the map to draw a line"
			document.getElementById("spnSetAnno").style.visibility="hidden"
			break;
		case "5": // Text, so turn on the Text area box
			var tbl = document.getElementById("tblAnnoText")
			tbl.style.visibility="visible";
			document.forms[0].ddlCoordType.style.visibility="visible";
			document.getElementById("spnAnnoInfo").innerHTML="You can add text to the map by selecting the position and entering the appropriate information.\nThe upper left corner of the text box will be placed at the point";
			var ddl = document.forms[0].ddlCoordType
			document.getElementById("spnSetAnno").style.visibility="visible"
			toggleAnnoCoordsFields(ddl.selectedIndex)
			break;
		default: //default help info display
			document.getElementById("spnAnnoInfo").innerHTML="You can add text to the map by selecting the position and entering the appropriate information";
	}
}
		
function setAnno(){
	//txtHiddenAnno.value will store the specs for the 'on-going' anno
	//that is added to the map. 
	var annoString=""
	mouseAnnoLoaded = false;
	var blnGotIt = false;
	var rad = document.forms[0].radAnno;	
	try{	
	
		var serviceName,latitudeDegrees,latitudeMinutes,longitudeDegrees,longitudeMinutes	
		serviceName = document.forms[0].txtServiceName.value;
		latitudeDegrees = document.forms[0].ddlLatDegrees.options[document.forms[0].ddlLatDegrees.selectedIndex].value;
		latitudeMinutes = document.forms[0].ddlLatMinutes.options[document.forms[0].ddlLatMinutes.selectedIndex].value;
		latitudeSeconds = document.forms[0].txtLatSeconds.value;
		longitudeDegrees = document.forms[0].ddlLongDegrees.options[document.forms[0].ddlLongDegrees.selectedIndex].value;
		longitudeMinutes = document.forms[0].ddlLongMinutes.options[document.forms[0].ddlLongMinutes.selectedIndex].value;
		longitudeSeconds = document.forms[0].txtLongSeconds.value;
	
		var ddlCoType = document.forms[0].ddlCoordType;	
		for(i=0;i<rad.length;i++){
			if(rad[i].checked){
				annoTypeIndex = i;
				break;
			}
		}
		if(annoTypeIndex==0){  //Point
			switch(ddlCoType.selectedIndex){
				case 0:
					//do nothing, it's a mouse click
					break;
				case 1: //Lat/Long
					if(!validateLatLong(sMsg)){
						return
					}					
					showRetrieving();
					GetCoordsFromLatLong(serviceName,latitudeDegrees,latitudeMinutes,latitudeSeconds,longitudeDegrees,longitudeMinutes,longitudeSeconds,"West","North")
					break;
				case 2: //Map Coords
					setAnnoByMapCoords(document.forms[0].txtAnnoXCoord.value,document.forms[0].txtAnnoYCoord.value);					
					break;
			}
		}
		if(annoTypeIndex==4){ //text
			if(document.forms[0].txtUserTextAnno.value==""){
				alert("Text is required to add Text annotation.");
				return;
			}			
			switch(ddlCoType.selectedIndex){
				case 0: //at mouse click . The page will post from the 'release' event of the mouse
					document.getElementById("divMsg")="Text Anno"
					document.forms[0].txtToolState.value="Text Anno"
					break;
				case 1: //at Lat/Long
					var sMsg = ""
					if(!validateLatLong(sMsg)){
						return
					}					
					showRetrieving();
					GetCoordsFromLatLong(serviceName,latitudeDegrees,latitudeMinutes,latitudeSeconds,longitudeDegrees,longitudeMinutes,longitudeSeconds,"West","North")
					break;
				case 2: //at map coords (planar)
					showRetrieving();
					setAnnoByMapCoords(document.forms[0].txtAnnoXCoord.value,document.forms[0].txtAnnoYCoord.value);
					break;
			}
		}
		
		
	}catch(ex){
		document.getElementById("btnGetAnno").disabled = false;
		hideRetrieving();
		alert(ex.message);		
	}	
}

function validateLatLong(){	
	var sErrMsg = ""
	
	var latitudeSeconds = new Number()
	var longitudeSeconds = new Number()	
	//latS = latitudeSeconds/latitudeSeconds
	var serviceName,latitudeDegrees,latitudeMinutes,longitudeDegrees,longitudeMinutes	
	serviceName = document.forms[0].txtServiceName.value;
	latitudeDegrees = document.forms[0].ddlLatDegrees.options[document.forms[0].ddlLatDegrees.selectedIndex].value;
	latitudeMinutes = document.forms[0].ddlLatMinutes.options[document.forms[0].ddlLatMinutes.selectedIndex].value;
	latitudeSeconds = document.forms[0].txtLatSeconds.value;
	longitudeDegrees = document.forms[0].ddlLongDegrees.options[document.forms[0].ddlLongDegrees.selectedIndex].value;
	longitudeMinutes = document.forms[0].ddlLongMinutes.options[document.forms[0].ddlLongMinutes.selectedIndex].value;
	longitudeSeconds = document.forms[0].txtLongSeconds.value;
	
	if(latitudeSeconds.length==0){
		sErrMsg = "-Latitude Seconds requires a positive number starting at 0\n";	
	}else{
		if(isNaN(latitudeSeconds)){
			sErrMsg += "-Invalid Latitude Seconds value\n";						
		}
		else{
			if(latitudeSeconds <0 || latitudeSeconds>=60){
				sErrMsg += "-Latitude Seconds must be >=0 and <60\n"
			}
		}
	}
	if(longitudeSeconds.length==0){
		sErrMsg += "-Longitude Seconds requires a positive number starting at 0\n";	
	}else{
		if(isNaN(longitudeSeconds)){
			sErrMsg += "-Invalid Longitude Seconds value";						
		}else if(longitudeSeconds<0 || longitudeSeconds>=60){
			sErrMsg += "-Longitude Seconds must be >=0 and <60\n"
		}
	}
	if(sErrMsg.length>0){
		sErrMsg = "Entry Error(s):\n" + sErrMsg;
		alert(sErrMsg);
		return false;
	}
	return true				
}


function toggleAnnoCoordsFields(idx){
	switch(idx){
		case 0:
			document.getElementById("tblLatLongCoords").style.display="none"; //set anno at cursor
			document.getElementById("tblAnnoXYCoords").style.display="none"; //set anno at cursor
			document.getElementById("divTextAnno").style.visibility="visible";
			document.getElementById("spnSetAnno").style.visibility="hidden";
			break;
		case 1:
			document.getElementById("tblLatLongCoords").style.display="block"; //set anno at cursor
			document.getElementById("tblAnnoXYCoords").style.display="none"; //set anno at cursor
			document.getElementById("divTextAnno").style.visibility="hidden";
			document.getElementById("spnSetAnno").style.visibility="visible";
			break;
		case 2:
			document.getElementById("tblLatLongCoords").style.display="none"; //set anno at cursor
			document.getElementById("tblAnnoXYCoords").style.display="block"; //set anno at cursor
			document.getElementById("divTextAnno").style.visibility="hidden";
			document.getElementById("spnSetAnno").style.visibility="visible";
			break;
		default:
			document.getElementById("tblLatLongCoords").style.display="none"; //set anno at cursor
			document.getElementById("tblAnnoXYCoords").style.display="block"; //set anno at cursor
			document.getElementById("divTextAnno").style.visibility="hidden";
			document.getElementById("spnSetAnno").style.visibility="hidden";
			break;
	}
}

function deselectAnno(){
	var radAnno = document.forms[0].radAnno
	for(i=0;i<radAnno.length;i++){
		radAnno[i].checked=false;
	}
	var tbl = document.getElementById("tblAnnoText")
	tbl.style.visibility="hidden";
	document.forms[0].ddlCoordType.style.visibility="hidden";
	document.getElementById("spnAnnoInfo").innerHTML=""
	document.getElementById("spnSetAnno").style.visibility="hidden"
}

function mapCoords(x1,y1,x2,y2){
	this.x1=x1;
	this.y1=y1;
	this.x2=x2;
	this.y2=y2;
}

function setAnnoByMapCoords(x,y){
	//1. Get X min/max extents	
	var ddl = document.forms[0].ddlCoordType;
	var txt = document.forms[0].txtHiddenAnno;
	try{
		var mapX1,mapX2,mapY1,mapY2,extX1,extY1,extX2,extY2;		
		extX1 = getLastExtent(document.forms[0].txtXMin.value);
		extY1 = getLastExtent(document.forms[0].txtYMin.value);
		extX2 = getLastExtent(document.forms[0].txtXMax.value);
		extY2 = getLastExtent(document.forms[0].txtYMax.value);		
		var easting = x 
		var northing = y
		mapX1 = x;
		mapY1 = y;
		//2. get difference in x/y extents
		var xScale = Math.abs(extX2-extX1)/mapWidth;
		xPos = Math.abs((easting-extX1)/xScale);
		var yScale = Math.abs(extY2-extY1)/mapHeight;
		yPos = Math.abs((northing-extY1)/yScale);
		//flip Y
		yPos = mapHeight-Math.abs((mapHeight-yPos));
		///////// Move Text Anno to Map Coords //////////////////////////
		//alert('xPos=' + xPos + ', yPos=' + yPos + ', minX=' + minX + ', minY=' + minY)
		
		var toolState = document.forms[0].txtToolState.value
		if(toolState.toLowerCase()=="point anno"){
			drawDot(xPos,yPos);
			setAnnoString("Point","",ddl.options[ddl.selectedIndex].value,mapX1,mapY1,"","","","","","","",""); //dragimage.js
		}else if(toolState.toLowerCase()=="text anno"){
			var anno = document.createElement("div")
			anno.className="textanno"
			anno.style.position = "absolute";
			anno.style.left = Math.abs(parseInt(xPos) + parseInt(minX));
			anno.style.top = Math.abs(parseInt(yPos) + parseInt(minY));
			var sUserText =  document.forms[0].txtUserTextAnno.value;
			var sNewText = ""
			if(sUserText.indexOf("\n")>0){
				for(i=0;i<sUserText.length;i++){
					if(sUserText.charAt(i)=="\n"){
						sNewText += "</br>"
					}else{
						sNewText += sUserText.charAt(i);
					}
				}
			}else{
				sNewText=sUserText;
			}
			anno.innerHTML = sNewText //document.forms[0].txtUserTextAnno.value;
			anno.style.visibility = "visible";	
			document.body.appendChild(anno)
			setAnnoString("Text",document.forms[0].txtUserTextAnno.value,ddl.options[ddl.selectedIndex].value,mapX1,mapY1,"","","","","","","",""); //dragimage.js
		}
		showRetrieving()
		__doPostBack("mapImage","")
	
	}catch(ex){
		alert(ex.message);
	}
}

function setAnnoString(annoType,annoText,coordType,x1,y1,x2,y2){
	
	var theType = annoType.toLowerCase();
	if(x2==undefined || x2!=""){x2+=""}
	if(y2==undefined || y2==""){y2+=""};
	
	if(theType=="point"){
		switch(coordType){
			case "0":
				x1 = Get_Map_PointX(x1);
				if(x2!=""){x2 = Get_Map_PointX(x2)}
				y1 = flipYMouseCoord(y1);
				if(y2!=""){y2 = flipYMouseCoord(y2)};
				break;
			case "1": //Lat/Long
				var ymin = getLastExtent(document.forms[0].txtYMin.value);
				var ymax = getLastExtent(document.forms[0].txtYMax.value);
				var diff = ymax - parseFloat(y1);
				y1 = parseFloat(ymin) + parseFloat(diff);
				break;			
			case "2": //map Coords
				var ymin = getLastExtent(document.forms[0].txtYMin.value);
				var ymax = getLastExtent(document.forms[0].txtYMax.value);
				//var diff = Math.abs(parseFloat(document.forms[0].txtAnnoYCoord.value)-ymin);
				x1 = document.forms[0].txtAnnoXCoord.value;
				y1 = document.forms[0].txtAnnoYCoord.value //flipYMapCoord(document.forms[0].txtAnnoYCoord.value)  //parseFloat(ymax) - parseFloat(diff);
				break;
		}
		//assume mouse coords passed in
		/*
		*/
	}else if(theType=="circle" || theType=="rectangle" || theType=="line"){
		x1 = Get_Map_PointX(x1);
		if(x2!=""){x2 = Get_Map_PointX(x2)}
	    y1 = flipYMouseCoord(y1);
		if(y2!=""){y2 = flipYMouseCoord(y2)};
		
	}else if(theType=="text"){ //text anno
		var ddl = document.forms[0].ddlCoordType
		var coordType = ddl.selectedIndex
		if(coordType==0){   //mouse coords (click)
			x1 = Get_Map_PointX(x1);
			if(x2!=""){
				x2 = Get_Map_PointX(x2)
		    }
			y1 = flipYMouseCoord(y1);
			if(y2!=""){
				y2 = flipYMouseCoord(y2);
			}
		}else if(coordType==1){ //Lat/Long
			//need to flip Y map coords
			var ymin = getLastExtent(document.forms[0].txtYMin.value);
			var ymax = getLastExtent(document.forms[0].txtYMax.value);
			var diff = ymax - parseFloat(y1);
			y1 = parseFloat(ymin) + parseFloat(diff);
			
		}else if(coordType == 2){ //Map Coords
			//need to flip Y map coords
			var ymin = getLastExtent(document.forms[0].txtYMin.value);
			var ymax = getLastExtent(document.forms[0].txtYMax.value);
			var diff = parseFloat(document.forms[0].txtAnnoYCoord.value)-ymin;
			x1 = document.forms[0].txtAnnoXCoord.value;
			y1 = document.forms[0].txtAnnoYCoord.value //flipYMapCoord(document.forms[0].txtAnnoYCoord.value)  //y1 = parseFloat(ymin) + parseFloat(diff);
		}
	}else if(theType=="longlat"){
		x1 = Get_Map_PointX(x1);
		if(x2!=""){x2 = Get_Map_PointX(x2)}
		y1 = flipYMouseCoord(y1);
		if(y2!=""){y2 = flipYMouseCoord(y2)};
		var ymin = getLastExtent(document.forms[0].txtYMin.value);
		var ymax = getLastExtent(document.forms[0].txtYMax.value);
		var diff = ymax - parseFloat(y1);
		y1 = parseFloat(ymin) + parseFloat(diff);
	}
	
	setAnnoToHiddenField(annoType,annoText,coordType,x1,y1,x2,y2)	
	
}

function setAnnoToHiddenField(annoType,annoText,coordType,x1,y1,x2,y2){
	var txt = document.forms[0].txtHiddenAnno;
	var sNewText = ""
	txt.value += annoType + "|";
	txt.value += stripChar(stripChar(annoText,"|"),"~") + "|";
	txt.value += coordType + "|";
	txt.value += "x1=" + x1 + ":" + "y1=" + y1 + ":" + "x2=" + x2 + ":" + "y2=" + y2 + "|~";
}

function createDiv(x,y,text,cssClass){
	try{
		var anno = document.createElement("div")
		anno.className=cssClass
		anno.style.position = "absolute";
		anno.style.left = x;
		anno.style.top = y;
		anno.innerHTML = text;
		anno.style.visibility = "visible";				
		document.body.appendChild(anno)
	
	}catch(ex){
		alert(ex.message);
	}
}

