// aimsCustom.js
/*
*  JavaScript template file for ArcIMS HTML Viewer
*		dependent on aimsXML.js, ArcIMSparam.js, aimsCommon.js, aimsMap.js,
*		aimsLayers.js, aimsDHTML.js
*		aimsClick.js, aimsNavigation.js,
*/

// global variables
	aimsCustomPresent=true;
	// change these to send XML response to custom function.
	// use numbers >= 1000 and match in useCustomFunction()
	// defaults are defined in aimsXML.js and use standard functions
	
	// xml response mode for selection
	selectXMLMode = 6;
	// xml response mode for identify
	XMLMode = 7;
	// xml response mode for query
	queryXMLMode = 8;
	// xml response mode for find
	findXMLMode = 14;
	// xml response mode hyperlink
	hyperlinkXMLMode = 15;




// custom function for handling clicks 
// 		flow redirected here when
//		toolMode set to >=1000
//function customMapTool(e) {
				//if (toolMode == 1001) {
						// insert code here
				//		return false;
					
		//		}
		//		if (toolMode == 1002) {
						// insert code here
					//coordinateTool(e)
	//			}

//}

// send  XML response to custom function
//		flow  redirected here when
//		XMLMode >=1000
function useCustomFunction(theReply) {
	if (XMLMode==1001) {
		// insert code here
	} else if (XMLMode==1002) {
		// insert code here
	} else {
		alert(msgList[55] + XMLMode + msgList[56]);
	}
	hideLayer("LoadData");
}

// add custom stuff to Map XML request. . . between selection and geocode
function addCustomToMap1(){
	var customString = "";
	/*
		customString += '<LAYER type="ACETATE" name="theMode">\n';
		customString += '<OBJECT units="PIXEL">\n<TEXT coord="5 ' + (iHeight-10) + '" label="This is a test">\n';
		customString += '<TEXTMARKERSYMBOL fontstyle="BOLD" fontsize="12" font="ARIAL" fontcolor="' + modeMapColor + '" ';
		customString += 'threed="TRUE" glowing="' + modeMapGlow + '" />\n</TEXT>\n</OBJECT>';
		customString += '\n</LAYER>\n';
	*/
	return customString;
}

// add custom stuff to Map XML request. . . between clickpoints and copyright
function addCustomToMap2(){
	var customString = "";
	
	return customString;
}

// add custom stuff to Map XML request. . . under modeOnMap
function addCustomToMap3(){
	var customString = "";
		/*
		customString += '<LAYER type="ACETATE" name="theMode">\n';
		customString += '<OBJECT units="PIXEL">\n<TEXT coord="5 ' + (iHeight-10) + '" label="This is a test">\n';
		customString += '<TEXTMARKERSYMBOL fontstyle="BOLD" fontsize="12" font="ARIAL" fontcolor="' + modeMapColor + '" ';
		customString += 'threed="TRUE" glowing="' + modeMapGlow + '" />\n</TEXT>\n</OBJECT>';
		customString += '\n</LAYER>\n';
		alert(customString);
		*/
	return customString;
}

// add custom stuff to Map XML request. . . on top of everything
function addCustomToMap4(){
	var customString = "";
	
	return customString;
}

// extract layers to download
function extractIt() {
	alert(msgList[51]);
}


//***************************************************************************************
//Coordinate tool to get geographic and UTM coordinates.
//***************************************************************************************
function coordinateTool(e){
	var theX = mouseX;
	var theY = mouseY;	
	getMapXY(theX,theY);
	alert(mouseX)
	if (document.all) drawPntGraphic(theX, theY);
	
	var latlon = UTMToGeo_Main(mapX, mapY,15);
	var utm = GeoToUTM_Main(latlon[0], latlon[1]);
	var utmX = numRound(utm[0],3);
	var utmY = numRound(utm[1],3);
	var utmZone = utm[2];
	
	var latDD = numRound(latlon[0], 4);
	var lonDD = numRound(latlon[1], 4);

	var latDeg = numTruncate(latlon[0]);
	var dmsTmp = numDecDigits(latlon[0]) * 60;
	var latMin = numTruncate(dmsTmp);
	var latSec = numRound(numDecDigits(dmsTmp) * 60,1);
	var latDMS = Math.abs(latDeg) + "° " + latMin + "' " + latSec + "\""
	latDMS = (latlon[0] > -1)? latDMS + " N": latDMS + " S";

	var lonDeg = numTruncate(latlon[1]);
	var dmsTmp = numDecDigits(latlon[1]) * 60;
	var lonMin = numTruncate(dmsTmp);
	var lonSec = numRound(numDecDigits(dmsTmp) * 60,1);
	var lonDMS = Math.abs(lonDeg) + "° " + lonMin + "' " + lonSec + "\"" 
	lonDMS = (latlon[1] > -1)? lonDMS + " E": lonDMS + " W";
	
	var theForm = parent.TOCFrame.document.forms[0];
	theForm.latdd.value = latDD;
	theForm.latdms.value = unescape(latDMS);
	theForm.londd.value = lonDD;
	theForm.londms.value = lonDMS;
	
	if (latlon[1] > -90){
		var xyz = GeoToUTM_Main(latlon[0],latlon[1]);
		theForm.x.value = numRound(xyz[0], 3);
		theForm.y.value = numRound(xyz[1], 3);
		theForm.zone.value = xyz[2];
	}else{
		theForm.x.value = numRound(mapX, 3);
		theForm.y.value = numRound(mapY, 3);
		theForm.zone.value = 15;
	}
}

function numDecDigits(Num){
	var absNum = Math.abs(Num);
	return (absNum - Math.floor(absNum));	
}

function numTruncate(Num){
	var absNum = Math.floor(Math.abs(Num));
	return absNum * (Num / Math.abs(Num));
}
function numRound(Num,prec){
	var multiplier=Math.pow(10,prec);
	var theNum=Math.floor(Num*multiplier + 0.5) / multiplier;
	return theNum;
}

var pntStr = "";
var mouseClickXY = new Array();

function clearRubberBand(toolName){
	
	//toolName = modeBlurb//added by Resz
	
	
	if (!document.all) return;
	if ((arguments.length > 0) && (toolName == "print") ) return;

	clickCount = 0;
	mouseClickXY.length=0;
	pntStr = "";
	document.all['rubberBand'].path = "m 0 0";
	
}
function drawRubberBand(){
	if(clickCount == 1)
		pntStr = "m " + mouseClickXY[0]  + " l ";
		
	if(clickCount > 2) 
		pntStr += ", "; 
	if(clickCount >=2)
		pntStr += mouseClickXY[clickCount-1];

	//Do not close the polygon if measure distance
	pathStr = (toolMode==20)? pntStr + " e": pntStr + " x e";

	document.all['rubberBand'].style.top = document.all['theImage'].offsetTop;
	document.all['rubberBand'].style.left = document.all['theImage'].offsetLeft;
	document.all['rubberBand'].path = pathStr;
}
function drawPntGraphic(theX, theY){
	clearRubberBand();
	var x = theX + 1;
	var y = theY - 1;
	pntStr = "m " + (x-1)  + ", "  + (y-1) + " l " + (x+1)  + ", "  + (y-1)  +  ", "  + 
			(x+1)  + ", "  + (y+1) +  ", "  + (x-1)  + ", "  + (y+1) +  ", "  + (x-1)  + ", "  + (y-1);	
	drawRubberBand();
	
}
///////////////////////////////////////////////////////////
function coordinate_processLatLon(theReply){
//alert(theReply)
//draw the temp graphic
	var theX = mouseX;
	var theY = mouseY;	

	getMapXY(theX,theY);
	if (document.all) drawPntGraphic(theX, theY);
//Parse out the lat/lon values
	var theString = theReply.toUpperCase();
	var pos = theString.indexOf("POINT");
	if (pos!=-1) {
		var startpos = theString.indexOf("X=",pos) + 3;
		var endpos = theString.indexOf(dQuote,startpos);
		var lon = parseFloat(theString.substring(startpos,endpos));
			startpos = theString.indexOf("Y=",pos)+3;
			endpos = theString.indexOf(dQuote,startpos);
		var lat = parseFloat(theString.substring(startpos,endpos));
}
//alert(lat)
//Get UTM coordinates, requires coordConverter.js
	var utm = GeoToUTM_Main(lat,lon);
//alert(utm[0])
	var utmX = numRound(utm[0],3);
	var utmY = numRound(utm[1],3);
	var utmZone = utm[2];
//Convert lat/lon from decimal digits to degree, minute, second format
	var latDD = numRound(lat, 4);
	var lonDD = numRound(lon, 4);

	var latDeg = numTruncate(lat);
	var dmsTmp = numDecDigits(lat) * 60;
	var latMin = numTruncate(dmsTmp);
	var latSec = numRound(numDecDigits(dmsTmp) * 60,1);
	var latDMS = Math.abs(latDeg) + "° " + latMin + "' " + latSec + "\""
		latDMS = (lat > -1)? latDMS + " N": latDMS + " S";

	var lonDeg = numTruncate(lon);
	var dmsTmp = numDecDigits(lon) * 60;
	var lonMin = numTruncate(dmsTmp);
	var lonSec = numRound(numDecDigits(dmsTmp) * 60,1);
	var lonDMS = Math.abs(lonDeg) + "° " + lonMin + "' " + lonSec + "\"" 
		lonDMS = (lon > -1)? lonDMS + " E": lonDMS + " W";

//Display output in decimal digits, decree/minute/second, and UTM
//coordinateForm(latDD, latDMS, lonDD, lonDMS, utmX, utmY, utmZone); 
	var theForm = parent.TOCFrame.document.forms[0];
		theForm.latdd.value = latDD;
		theForm.latdms.value = unescape(latDMS);
		theForm.londd.value = lonDD;
		theForm.londms.value = lonDMS;
		theForm.x.value = utmX;
		theForm.y.value = utmY;
		theForm.zone.value = utmZone;
	
}







