// 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;
	// Hannah Added - xml response mode for point selection
	selectptXMLMode = 7;
	// 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 == 1001) {
						// insert code here
						return false;
				}
				if (toolMode == 1002) {
						// insert code here

				}
				if (toolMode == 1234) {
				// hannah added: XY
					unprojectPoint(e);
					return false;
				}
}

// send  XML response to custom function
//		flow  redirected here when
//		XMLMode >=1000
function useCustomFunction(theReply) {
	if (XMLMode==1001) {
		// hannah inserted code here
		// get queryzoom response
			//alert(theReply);
			displayAttributeData1(theReply);
	} else if (XMLMode==1002) {
		displayAttributeData2(theReply);
	} else if (XMLMode==1003) {
		//hannah added: XY
			extractPtVal(theReply);
	} else if (XMLMode==1098) {
		// Hannah Added: get legend from response for printing
		printLegURL = getLegendURL(theReply);
		writePrintPage();
	} else {
		alert(msgList[55] + XMLMode + msgList[56]);
	}
	hideLayer("LoadData");
}

// add custom stuff to Map XML request. . . between selection and geocode
function addCustomToMap1(mySQL){
	var customString = "";
	customString = customString += '<LAYER type="featureclass" name="Starcom Crimes" visible="true">';
	customString = customString += '<DATASET type="point" fromlayer="11" />';
	customString = customString += '<SPATIALQUERY subfields="#ALL#" where="';
	customString = customString += mySQL;
	customString = customString += '" />';
	customString = customString += '<GROUPRENDERER>';
	customString = customString += '<VALUEMAPRENDERER lookupfield="MAPCRIME.CRIMETYPE">';
	customString = customString += '<EXACT value="ALARM - BUSINESS" label="Alarm-Business">';
	customString = customString += '<TRUETYPEMARKERSYMBOL font="ESRI Crime Analysis" outline="0,0,0" fontcolor="0,0,255" character="69" fontsize="20" />';
	customString = customString += '</EXACT>';
	customString = customString += '<EXACT value="ALARM - RESIDENCE" label="Alarm-Residence">';
	customString = customString += '<TRUETYPEMARKERSYMBOL font="ESRI Crime Analysis" outline="0,0,0" fontcolor="0,0,255" character="69" fontsize="20" />';
	customString = customString += '</EXACT>';
	customString = customString += '<EXACT value="ARMED ROBBERY" label="Armed Robbery">';
	customString = customString += '<TRUETYPEMARKERSYMBOL font="ESRI Crime Analysis" outline="0,0,0" fontcolor="255,0,0" character="46" fontsize="20" />';
	customString = customString += '</EXACT>';
	customString = customString += '<EXACT value="BURGLARY ATTEMPT" label="Burglary Attempt">';
	customString = customString += '<TRUETYPEMARKERSYMBOL font="ESRI Crime Analysis" outline="0,0,0" fontcolor="23,175,189" character="40" fontsize="20" />';
	customString = customString += '</EXACT>';
	customString = customString += '<EXACT value="BURGLARY/BUSINESS" label="Burglary/Business">';
	customString = customString += '<TRUETYPEMARKERSYMBOL font="ESRI Crime Analysis" outline="0,0,0" fontcolor="0,255,0" character="109" fontsize="20" />';
	customString = customString += '</EXACT>';
	customString = customString += '<EXACT value="BURGLARY/CONVEYANCE" label="Burglary/Conveyance">';
	customString = customString += '<TRUETYPEMARKERSYMBOL font="ESRI Crime Analysis" outline="0,0,0" fontcolor="247,71,17" character="61" fontsize="20" />';
	customString = customString += '</EXACT>';
	customString = customString += '<EXACT value="BURGLARY/RESIDENCE" label="Burglary/Residence">';
	customString = customString += '<TRUETYPEMARKERSYMBOL font="ESRI Crime Analysis" outline="0,0,0" fontcolor="0,0,255" character="105" fontsize="20" />';
	customString = customString += '</EXACT>';
	customString = customString += '<EXACT value="GRAND THEFT" label="Grand Theft">';
	customString = customString += '<TRUETYPEMARKERSYMBOL font="ESRI Crime Analysis" outline="0,0,0" fontcolor="23,175,189" character="41" fontsize="20" />';
	customString = customString += '</EXACT>';
	customString = customString += '<EXACT value="KIDNAPPING/ABDUCTION" label="Kidnapping/Abduction">';
	customString = customString += '<TRUETYPEMARKERSYMBOL font="ESRI Crime Analysis" outline="0,0,0" fontcolor="255,0,255" character="86" fontsize="20" />';
	customString = customString += '</EXACT>';
	customString = customString += '<EXACT value="MURDER" label="Murder">';
	customString = customString += '<TRUETYPEMARKERSYMBOL font="ESRI Crime Analysis" outline="0,0,0" fontcolor="255,0,255" character="34" fontsize="20" />';
	customString = customString += '</EXACT>';
	customString = customString += '<EXACT value="RAPE" label="Rape">'
	customString = customString += '<TRUETYPEMARKERSYMBOL font="ESRI Crime Analysis" outline="0,0,0" fontcolor="0,255,0" character="36" fontsize="20" />';
	customString = customString += '</EXACT>';
	customString = customString += '<EXACT value="STOLEN AUTO" label="Stolen Auto">';
	customString = customString += '<TRUETYPEMARKERSYMBOL font="ESRI Crime Analysis" outline="0,0,0" fontcolor="255,0,0" character="80" fontsize="20" />';
	customString = customString += '</EXACT>';
	customString = customString += '<EXACT value="STRONG ARM ROBBERY" label="Strong Arm Robbery">';
	customString = customString += '<TRUETYPEMARKERSYMBOL font="ESRI Crime Analysis" outline="0,0,0" fontcolor="0,0,255" character="89" fontsize="20" />';
	customString = customString += '</EXACT>';
	customString = customString += '<EXACT value="VANDALISM" label="Vandalism">';
	customString = customString += '<TRUETYPEMARKERSYMBOL font="ESRI Crime Analysis" outline="0,0,0" fontcolor="255,0,0" character="55" fontsize="20" />';
	customString = customString += '</EXACT>';
	customString = customString += '<EXACT value="FIELD INTERVIEW" label="Field Interview">';
	customString = customString += '<TRUETYPEMARKERSYMBOL font="ESRI Crime Analysis" outline="0,0,0" fontcolor="169,255,192" character="73" fontsize="20" />';
	customString = customString += '</EXACT>';
	customString = customString += '</VALUEMAPRENDERER>';
	customString = customString += '<SIMPLELABELRENDERER field="MAPCRIME.ADDCOM">';
	customString = customString += '<TEXTSYMBOL antialiasing="true" fontstyle="bold" fontsize="10" />';
	customString = customString += '</SIMPLELABELRENDERER>';
	customString = customString += '</GROUPRENDERER>';
	customString = customString += '</LAYER>';
	//alert(customString);
	return customString;
}

function addCustomToMap1a(mySQL){
	var customString = "";
	customString = customString += '<LAYER type="featureclass" name="Offenders" visible="true">';
	customString = customString += '<DATASET fromlayer="22" />';
	customString = customString += '<SPATIALQUERY subfields="#ALL#" where="';
	customString = customString += mySQL;
	customString = customString += '" />';
	customString = customString += '<GROUPRENDERER>';
	customString = customString += '<SIMPLERENDERER>';
    customString = customString += '<SIMPLEMARKERSYMBOL color="255,0,0" outline="0,0,0" type="circle" width="15" />';
	customString = customString += '</SIMPLERENDERER>';
	customString = customString += '<SIMPLELABELRENDERER field="LASTNAME">';
	customString = customString += '<TEXTSYMBOL font="Arial" fontsize="10" fontcolor="255,0,0" linelabelposition="PlaceAboveBelow" />';
	customString = customString += '</SIMPLELABELRENDERER>';
	customString = customString += '</GROUPRENDERER>';
	customString = customString += '</LAYER>';
	//alert(customString);
	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() {
	hideLayer("measureBox");
	alert(msgList[51]);
}

// Hannad added: XY
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.  NAD 1983 State Plane Florida East FIPS 0901 Feet
       // FEATURECOORDSYS is the id of the desired coordinate system of the resulting coordinates.  GCS North American 1983
	theString += '<FILTERCOORDSYS id="102658" />\n<FEATURECOORDSYS id="4269" />\n';
	theString += '</PROPERTIES>\n</GET_IMAGE>\n</REQUEST>\n</ARCXML>';
	//alert(theString);
	sendToServer(imsURL,theString,1003);	//XMLmode for unproject xy is 1003

	return false;	
}


//hannah added: XY
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 + "'' " );
}
