// 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;


// custom function for handling clicks 
// 		flow redirected here when
//		toolMode set to >=1000
function customMapTool(e) {
				if (toolMode == 1001) {
					getInfo(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 = "";
	customString += '<LAYER type="ACETATE" name="theMode">\n';
	customString += '<OBJECT units="PIXEL">\n';
	customString += '<LINE coords="0 20;' + iWidth + ' 20">\n<SIMPLELINESYMBOL color="0,0,0" width="2" />';
	customString += '\n</LINE>\n</OBJECT>';
	customString += '<OBJECT units="PIXEL">\n';
	customString += '<POLYGON coords="0 0;0 20;' + iWidth + ' 20;' + iWidth + ' 0;0 0">\n';
	customString += '<SIMPLEPOLYGONSYMBOL fillcolor="255,255,255" boundarycolor="255,255,255" />';
	customString += '\n</POLYGON>\n</OBJECT>';
	customString += '\n</LAYER>\n';
	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:	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();
	
	//alert("Back in the FindASPData function");
	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
	//alert("sLayer: " + sLayer);
	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);
	//alert(findXMLMode);
	sendToServer(imsQueryURL,theString,findXMLMode);
	
}

/********************
	Function:	checkMaxAllowedScale
	Desc:		Checks to see if the desired extents exceed the allowable minimum
	Written:	BML
	Date:		18-Apr-2007
********************/
function exceedsMaxAllowedScale()
{
	//debugger;
	var xDistance = Math.abs(eRight-eLeft);
	var sFactor = ((xDistance / iWidth) * 96 * 12);
	

	//if the scale factor is too small adjust to minimum zoom level
	//add the 1.05 mulitplier to help with panning the map slightly adjusting
	//the scale factor and re-enabling the zoom button when the user can only
	//zoom in a very small amount.
	if (sFactor < (g_lMaxZoomScale * 1.01))
	{
		return true;
	}
	else
	{
		return false;
	}
}

/********************
	Function:	resetToAllowedScale
	Desc:		resets the extents to the allowable scale
	Written:	BML
	Date:		18-Apr-2007
********************/
function resetToAllowedScale()
{
	var xDistance = Math.abs(eRight-eLeft);
	var newLeft = eLeft - (((g_lMaxZoomScale / 2) - xDistance) / 2);
	var newRight = eRight + (((g_lMaxZoomScale / 2) - xDistance) / 2);
	eRight = newRight;
	eLeft = newLeft;
}


//BML - 6 July 2004
function doZoomScale(zval)
{
	saveLastExtent();
	var msize;
	if (iHeight < iWidth)
	{
			msize = iHeight;
	}
	else
	{
			msize = iWidth;
	} 
	var midX = eRight - (xDistance / 2);
	var midY = eTop - (yDistance / 2);
	//mapscale is half the distance for the ratio scale 
	//take the input ratio factor (zval)
	//take the size of the longest side of the map/97.6925 to get the size in inches (97.. pix per inch)
	//then divide the size of the map from above by 12 to get feet since that's what our mapunits are in
	//multiply the ratio by the size of the map (length or witdh) in feet
	// then multiply that by .5 to get half the distance the extent needs to be
	//add or subtract that half map extent to the center point cooridnates to build an extent
	//var mscale= (zval * ((msize/97.6925)/12)) * .5;
	var mscale= (zval * ((msize/96)/12)) * .5;
	eLeft = ((midX) - (mscale));
	eRight =  ((midX) + (mscale));
	eBottom = ( (midY) - (mscale));
	eTop =  ((midY) + (mscale));
	sendMapXML(); 
}

