// 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;

//global variable used to determine when an element should only
//be highlighted and not zoomed to (i.e. the user does and Identify
//on a Control Station that is already on the screen.
var gbHighlightOnly = false;

//the current control station in the text frame
var gsCurrentControlStation = "";


// custom function for handling clicks 
// 		flow redirected here when
//		toolMode set to >=1000
function customMapTool(e) {
				if (toolMode == 1001) {
					identifyStation(e);
				}
				if (toolMode == 1002) {
						// insert code here

				}

}

// send  XML response to custom function
//		flow  redirected here when
//		XMLMode >=1000
function useCustomFunction(theReply) {
	if (XMLMode==1001) {
		stationIdentifyReturn(theReply);
	} 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() {
	hideLayer("measureBox");
	alert(msgList[51]);
}

/********************
	Function:	stationIdentifyReturn
	Desc:		Process the response from a identifyStation
				event.  The Control Station ID is then
				sent to ControlStation.asp.
	Written:	BML
	Date:		17-Jun-2002
********************/
function stationIdentifyReturn(theReply)
{
	var theValue = "";
	var theField = "STATION_ID";
	
	if(typeof theField == "undefined")
	{
		//debug
		alert("Warning! Field is " + theField);
		return;
	}
	
	var pos = theReply.indexOf("<FIELDS ",startpos);
	
	if (pos!=-1)
	{
		var startpos = pos + 8;
		//alert(theValue);
		//alert(theReply);
		theValue = justGetFieldValue(theReply,theField,startpos);
		if (useTextFrame)
		{
			/*******
			Although Zoom is set to true the gbHighlightOnly variable will make
			the map only highlight the selected control station - it will not zoom the map.
			BML added 18-Jun-02
			*******/
			gbHighlightOnly = true;
			var theSite = baseURL + webSite + "ControlPoint.asp?StationID=" + theValue + "&Zoom=True";
			parent.TextFrame.document.location= theSite;
			gsCurrentControlStation = theValue;
		}
	}
	else
	{
		if (useTextFrame)
		{
			parent.TextFrame.document.open();
			parent.TextFrame.document.writeln('<meta http-equiv="Content-Type" content="text/html; charset=' + charSet + '"><html>');
			parent.TextFrame.document.writeln('<head>');
			parent.TextFrame.document.writeln('</head>');
			parent.TextFrame.document.writeln('<body>');
			parent.TextFrame.document.writeln('<br><blockquote>');
			parent.TextFrame.document.writeln('<span class="SearchText"><b><u>Error: Identify Station</u></b></span>');
			parent.TextFrame.document.writeln('<br><br>');
			parent.TextFrame.document.writeln('<span class="SearchText">No Control Station found at the location you selected.</span><br>');
			parent.TextFrame.document.writeln('</blockquote></body></html>');
			parent.TextFrame.document.close()
		}		
	}
}

/********************
	Function:	identifyStation
	Desc:		Takes and incoming mouse click event (e)
				and determines the control station the
				user clicked.  The response is processed
				by stationIdentifyReturn (XMLMode - 1001).
	Written:	BML
	Date:		17-Jun-2002
********************/
function identifyStation(e)
{
	//alert("Begin identifyStation");
	setActiveLayerByName("Control Points");
	
	if (checkIfActiveLayerAvailable()) {
		//alert("ActiveLayerAvailable");
		highlightedOne="";
		var theX = mouseX;
		var theY = mouseY;
		getMapXY(theX,theY);
		searchTolerance = (xDistance/iWidth) * pixelTolerance;
		var tempWest = mapX - searchTolerance;
		var tempNorth = mapY + searchTolerance;
		var tempEast = mapX + searchTolerance;
		var tempSouth = mapY - searchTolerance;
		var theString = writeGetFeatures(tempWest,tempSouth,tempEast,tempNorth);
		
		//alert("The String: " + theString);
		//if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
		showRetrieveData();
		sendToServer(imsQueryURL,theString,1001);
	}
	else
	{
		alert("Active Layer NOT available");
	}
}

/********************
	Function:	findASPData
	Desc:		Locates a feature on the map given the following parameters:
				sLayer - ArcIMS Layer name
				sField - Database fieldname to query
				sValue - Value to search for
	Written:	BML
	Date:		17-Jun-2002
********************/
function findASPData(sParams)
{
	//alert("sParams: " + sParams);
	var sQueryString = "";
	var sLayer = "";
	var sField = "";
	var sValue = "";
	var paramArray = new Array();
	

	paramArray = sParams.split("|");

	//the array must have three elements
	if (paramArray.length != 3)
	{
		alert("Invalid String Sent to findASPData \n" + sParams);
	}
	else
	{
		sLayer = paramArray[0];
		sField = paramArray[1];
		sValue = paramArray[2];
	}

	//set the active layer
	setActiveLayerByName(sLayer);
	
	//build the query string
	if (sField != "")
		sQueryString += sField.toUpperCase() + " = " + sValue.toUpperCase();

	//make sure we have a query field and value
	if (sField == "")
	{
		alert("Error processing string data in 'findASPdata()'");
		return;
	}

	//format the query string
	sQueryString = swapQuotes(sQueryString);
	sQueryString = makeXMLsafe(sQueryString);
	
	//set the ArcIMS query parameters
	setQueryString=sQueryString;
	selectionMode=1;
	selectData.length=0;
	LayerFields.length=0;
	LayerFieldType.length=0;
	LayerFieldCount=0;
	highlightedOne="";
	showBuffer=false;
	showRetrieveData();

	//write the XML statement and send it off
	var theString = writeQueryXML(sQueryString);
	//alert("findASPData - theString: " + theString);
	sendToServer(imsQueryURL,theString,findXMLMode);
}

