// 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
	identifyXMLMode = 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 == 1004) {
		drillIdentify(e);
	}
	if (toolMode == 1001) {
		// insert code here
		unprojectPoint(e);
		return false;
	}
	if (toolMode == 1002) {
		// insert code here
	}
}

// send  XML response to custom function
//		flow  redirected here when
//		XMLMode >=1000
function useCustomFunction(theReply) {
	if (XMLMode==1001) {
		extractPtVal(theReply);
	} else if (XMLMode==1002) {
		// insert code here
	} else if (XMLMode == 1004) {
		doDrill(theReply);
	} else if (XMLMode == 1005) {
		getAccounts(theReply);  // nancy.js
	} else if (XMLMode == 1006) {
		getAccountEnv(theReply);  // nancy.js
	} else if (XMLMode == 1007) {
		myDrillResponse(theReply);  // nancy.js
	} else if (XMLMode == 1008) {
		getAccounts(theReply);  // nancy.js
	} else {
		alert("Unknown Mode:" + XMLMode + "\nUnable to execute request.");
	}
	//hideLayer("LoadData");
}

// add custom stuff to Map XML request. . . between selection and geocode
function addCustomToMap1(){
	var customString = "";
	return customString;
}

// add custom stuff to Map XML request. . . between clickpoints and copyright
function addCustomToMap2(){
	var customString = "";
		customString += zoomToPoint_Add_XML();
	return customString;
}

// add custom stuff to Map XML request. . . under modeOnMap
function addCustomToMap3(){
	var 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() {
	hideLayer("measureBox");
	alert("Function not yet enabled.");
}
function unprojectPoint(e) {	
	//send a request that will return unprojected coordinates
	getImageXY(e);
	getMapXY(mouseX,mouseY);
	
	var theString = '<ARCXML VERSION="1.0.1">\n<REQUEST>\n<GET_IMAGE>\n<PROPERTIES>\n';
	theString += '<ENVELOPE minx="' + (mapX-5) + '" miny="' + (mapY-5) + '" maxx="' + (mapX+5) + '" maxy="' + (mapY+5) + '" />\n';
	theString += '<IMAGESIZE height="1" width="1" />\n';
	theString += '<LAYERLIST >\n';
	for (var i=0;i<layerCount;i++) {
		theString += '<LAYERDEF id="' + LayerID[i] + '" visible="false" />\n';
	}
	theString += '</LAYERLIST>\n';
        // FILTERCOORDSYS is the id of the ArcIMS service's output coordinate system.
       // FEATURECOORDSYS is the id of the desired coordinate system of the resulting coordinates.
	theString += '<FILTERCOORDSYS id="26959" />\n<FEATURECOORDSYS id="4269" />\n';
	theString += '</PROPERTIES>\n</GET_IMAGE>\n</REQUEST>\n</ARCXML>';
	//alert(theString);
	sendToServer(imsURL,theString,1001);	//XMLmode for unproject xy is 1001

	return false;	
}
function extractPtVal(theReply) {
		var theXYs =  getEnvelopeXYs(theReply, 0)
		limitLeft = theXYs[0];
		limitBottom = theXYs[1];
		limitRight = theXYs[2];
		limitTop = theXYs[3];
		var theX = Math.round(((limitLeft + limitRight)/2)*1000000)/1000000;
		var theY = Math.round(((limitBottom + limitTop)/2)*1000000)/1000000;
		var degX = (theX|0);
		var tempminX = (((theX-degX)*60)*(-1));
		var minX = (tempminX|0);
		var secX = Math.round((((tempminX-minX)*60))*10000)/10000;

		var degY = (theY|0);
		var tempminY = (((theY-degY)*60));
		var minY = (tempminY|0);
		var secY = Math.round((((tempminY-minY)*60))*10000)/10000;
		
		//alert("Longitude/Latitude(DD): " + theX + "/ " + theY + "\n" + "Longitude/Latitude(DMS): " + degX + "° " + minX + "' " + secX + "''/ " + degY + "° " + minY + "' " + secY + "'' " );
		alert("Longitude(DD): " + theX + "\n" + "Latitude(DD): " + theY + "\n" + "Longitude(DMS): " + degX + "° " + minX + "' " + secX + "''     \n" + "Latitude(DMS): " + degY + "° " + minY + "' " + secY + "'' " );
}

