// aimsQuery.js
/*
*  JavaScript template file for ArcIMS HTML Viewer
*		dependent on aimsXML.js, ArcIMSparam.js, aimsCommon.js, aimsMap.js, aimsIdentify.js, aimsSelect.js, and aimsLayers.js
*		To be interactive, dependent also on aimsDHTML.js, aimsClick.js, and aimsNavigation.js
*/

aimsQueryPresent=true;

var showSampleValues=false;

var storedQueryCount = 0;

var storedQueryName = new Array();
var storedQueryString = new Array();
var storedQueryVariable = new Array();
var storedQueryVarCount = new Array();
var storedQueryFieldList = new Array();
var storedQueryIndex = 0;


/*
***************************************************************************************

Querying functions

***************************************************************************************
*/


// query form
function queryForm() {
	fieldIndex=0;
	showSampleValues=false;

		setLayerFields(ActiveLayerIndex);
		if (showSampleValues) {
			var theText = writeFieldSample(LayerFields[fieldIndex]);
			sendToServer(imsQueryURL,theText,40);
		} else {
			writeQueryForm();
		}

}

// find Form
function findForm() {
	setLayerFields(ActiveLayerIndex);
		if (useTextFrame) {
			parent.TextFrame.document.location= appDir + "findForm.htm";
		} else {
			Win1 = open(appDir + "findForm.htm","QueryWindow","width=575,height=150,scrollbars=yes,resizable=yes");
		}
}



// process query
function sendQueryString(newString) {
	//if (LayerIDField[ActiveLayerIndex]!="#ID#") {
		newString = fixSingleQuotes(newString);
		newString = swapQuotes(newString);
	//}
	newString = makeXMLsafe(newString);
	setQueryString=newString;
	selectionMode=1;
	selectData.length=1;
	LayerFields.length=1;
	LayerFieldType.length=1;
	LayerFieldCount=0;
	showBuffer=false;
	showRetrieveData();
	var theString = writeQueryXML(newString);
	sendToServer(imsQueryURL,theString,queryXMLMode);
}

// write out XML request to query attributes
function writeQueryXML(queryString) {
	if (swapSelectFields) {
		selectFields=selFieldList[ActiveLayerIndex];
	}
	var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_FEATURES outputmode="xml" geometry="false" envelope="true"';
	theString += ' compact="true" featurelimit="' + maxFeaturesReturned + '" beginrecord="' + queryStartRecord + '">\n';
	theString += '<LAYER id="' + ActiveLayer + '" />';
	if (useLimitExtent) {
		// keep this within the limitExtent
		theString += '<SPATIALQUERY subfields="' + selectFields + '" where="' + queryString + '">';
		theString += '<SPATIALFILTER relation="area_intersection">\n';
		theString += '<ENVELOPE maxx="' + limitRight + '" maxy="' + limitTop + '" minx="' + limitLeft + '" miny="' + limitBottom + '" />\n';
		theString += '</SPATIALFILTER>\n';
		theString += '</SPATIALQUERY>\n';
	} else {
	theString += '<SPATIALQUERY subfields="' + selectFields + '" where="' + queryString + '" />';
	}
	theString += '</GET_FEATURES>';
	theString += '</REQUEST>';
	theString += '</ARCXML>';
	selectLayer=ActiveLayer;
	selectType=ActiveLayerType;
	selectCount=0;
	hightlightedOne="";
	selectPoints.length=1;
	selectLeft.length=1;
	selectRight.length=1;
	selectTop.length=1;
	selectBottom.length=1;
	return theString;

}

// write out XML request to get field value samples
function writeFieldSample(theField) {
	var theFields = theField;
	var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_FEATURES outputmode="xml" geometry="false" envelope="false"';
	theString += ' compact="true" featurelimit="' + numberDataSamples + '">\n';
	theString += '<LAYER id="' + ActiveLayer + '" />';
	theString += '<QUERY subfields="' + theFields + '" />';
	theString += '</GET_FEATURES>';
	theString += '</REQUEST>';
	theString += '</ARCXML>';
	selectLayer=ActiveLayer;
	selectType=ActiveLayerType;
	selectCount=0;
	hightlightedOne="";
	selectPoints.length=1;
	selectLeft.length=1;
	selectRight.length=1;
	selectTop.length=1;
	selectBottom.length=1;
	return theString;
}
// send find request
function getFind(theValue) {
	showRetrieveData();
	var theNewQueryString = "";
	var theTempString = "";
	var fieldListString = "#ID# #SHAPE#";
	var elemCount = 0;
	for (var i=0;i<LayerFields.length;i++) {
		if (LayerFieldType[i].indexOf("12",0)!=-1) {
			theTempString = "(" + LayerFields[i] + " LIKE '%" + theValue + "%')";
			if (elemCount>0) theTempString = " OR " + theTempString;
			if (theNewQueryString.length + theTempString.length < 1024) {
				theNewQueryString = theNewQueryString + theTempString;
				fieldListString = fieldListString + " " + LayerFields[i];
				elemCount += 1;
			}

		}
	}
	if (theNewQueryString!="") {
		showBuffer=false;
		theNewQueryString=makeXMLsafe(theNewQueryString);
		setQueryString = theNewQueryString;
		var theString = writeFindRequest(theNewQueryString,fieldListString);
		sendToServer(imsQueryURL,theString,findXMLMode);
	} else {
		alert("Unable to build Query String.");
	}
}

// find and zoom to search item - variables passed from top.htm (added May 14,2002 by jko)
function findSearchItem(theValue,ALI) {
    // we need to set the toolMode and selectionMode globals since we are simulating an identify
	toolMode = 1;
	selectionMode = 1;
	// ALI is the ActiveLayerIndex which is set by the corresponding code
	switch(ALI){
		  case 'Parks':
			ALI = ParksLayerIndex;
			break;
		  case 'PSA':
			ALI = PSALayerIndex;
			break;
		  case 'Places':
			ALI = PlacesLayerIndex;
			break;
		  case 'Transit':
			ALI = TransitLayerIndex;
		    break;
          case 'SHIM':
			ALI = WaterwaysSHIMLayerIndex;
		    break;
		  case 'CommunityAssetMapping':
			ALI = AssetMappingLayerIndex;
		    break;
	}
	// we need to set the ActiveLayer to Lots (or whichever layer we're zooming to)
	setActiveLayer(ALI);
	showRetrieveData();
	var theNewQueryString = "";
	//var fieldListString = LayerIDField[ActiveLayerIndex] + " " + LayerShapeField[ActiveLayerIndex];
	var fieldListString = "#ID# #SHAPE#";
	
	// set the string to LOT_ID (or the appropriate field for the layer)
	if (ALI == ParksLayerIndex){
		theNewQueryString = "COMPKEY = '"+theValue+"'";
	}
	if (ALI == PSALayerIndex){
		theNewQueryString = "NEIGHBOURHOOD = '"+theValue+"'";
	}
	if (ALI == PlacesLayerIndex){
		theNewQueryString = "DESCRIPTION = '"+theValue+"'";
		LayerVisible[PlacesLayerIndex]=1;
	}
	if (ALI == TransitLayerIndex){
		theNewQueryString = "NAME = '"+theValue+"'";
	}
	if (ALI == WaterwaysSHIMLayerIndex){
		theNewQueryString = "CREEK_NAME = '"+theValue+"'";
	}
	if (ALI == AssetMappingLayerIndex){
		theNewQueryString = "DISTRICT = '"+theValue+"'";
	}

	if (theNewQueryString!="") {
		showBuffer=false;
		//theNewQueryString=makeXMLsafe(theNewQueryString);

		setQueryString = theNewQueryString;
		var theString = writeFindRequest(theNewQueryString,fieldListString);
		sendToServer(imsQueryURL,theString,findXMLMode);
	} else {
		alert("Unable to build Query String.");
	}
}

// find and zoom to infrastructure - variables passed from top.htm (added Jan 25th by jko)
function getFindInfrastructure(theValue,ALI) {
    // we need to set the toolMode and selectionMode globals since we are simulating an identify
	toolMode = 1;
	selectionMode = 1;
	
	// ALI is the ActiveLayerIndex which is set by the corresponding infrastructure asp code
	switch(ALI){
		  case 'DraMains':
			ALI = DraLayerIndex;
			break;
		  case 'SanMains':
			ALI = SanLayerIndex;
			break;
		  case 'CmbMains':
			ALI = CmbLayerIndex;
			break;
		  case 'WtrMains':
			ALI = WtrLayerIndex;
			break;
		  case 'WtrFittings':
			ALI = WtrFitLayerIndex;
			break;
	}

	// we need to set the ActiveLayer to Lots (or whichever layer we're zooming to)
	setActiveLayer(ALI);
	var theNewQueryString = "";
	var theTempString = "";
	var fieldListString = "#ID# #SHAPE#";
	var elemCount = 0;
	
	// set the string to LOT_ID (or the appropriate field for the layer)
	theNewQueryString = "COMPKEY = '"+theValue+"'";
	if (theNewQueryString!="") {
		showBuffer=false;
		setQueryString = theNewQueryString;
		var theString = writeFindRequest(theNewQueryString,fieldListString);
		sendToServer(imsQueryURL,theString,findXMLMode);
	} else {
		alert("Unable to build Query String.");
	}

}


// send find request
// added custom version of getFind to zoom to parcel after ASP page returns (Chris Macleod INFORM Nov 22)
function getFindCustom(theValue,ALI) {
    // we need to set the toolMode and selectionMode globals since we are simulating an identify
	toolMode = 1;
	selectionMode = 1;
	
	// we need to set the ActiveLayer to Lots (or whichever layer we're zooming to)
	switch(ALI){
		  case 'P':
			ALI = LotLayerIndex;
			break;
		  case 'S':
			ALI = StrataLayerIndex;
			break;
		  case 'A':
			ALI = AirSpaceLayerIndex;
			LayerVisible[AirSpaceLayerIndex]=1;
			break;
/*		  case 'L':
			ALI = LeaseLayerIndex;
			break;*/
	}
	setActiveLayer(ALI);
	var theNewQueryString = "";
	var theTempString = "";
	var fieldListString = "#ID# #SHAPE#";
	var elemCount = 0;
	
	// set the string to LOT_ID (or the appropriate field for the layer)
	theNewQueryString = "LOT_ID = '"+theValue+"'";
	if (theNewQueryString!="") {
		showBuffer=false;
		//theNewQueryString=makeXMLsafe(theNewQueryString);

		setQueryString = theNewQueryString;
		var theString = writeFindRequest(theNewQueryString,fieldListString);
		sendToServer(imsQueryURL,theString,DEFfindXMLMode);
	} else {
		alert("Unable to build Query String.");
	}

}


// Custom Find for zooming to the selected intersection (Chris Macleod INFORM)
// ALSO used by svyqryintersection.asp to zoom to the selected Street Intersections 
 function getFindIntersection(theValue) {
    // we need to set the toolMode and selectionMode globals since we are simulating an identify
	toolMode = 1;
	selectionMode = 1;
	setActiveLayer(StreetIntersectionLayerIndex);
	var theNewQueryString = "";
	var theTempString = "";
	//var fieldListString = LayerIDField[ActiveLayerIndex] + " " + LayerShapeField[ActiveLayerIndex];
	var fieldListString = "#ID# #SHAPE#";
	var elemCount = 0;

	theNewQueryString = "INTERSECTIONCODE = '"+theValue+"'";
	if (theNewQueryString!="") {
		showBuffer=false;

		setQueryString = theNewQueryString;
		var theString = writeFindRequest(theNewQueryString,fieldListString);
		sendToServer(imsQueryURL,theString,findXMLMode);
	} else {
		alert("Unable to build Query String.");
	}

}

// Custom Find for zooming to the selected MONUMENT
 function getFindMonument(theValue) {
    // we need to set the toolMode and selectionMode globals since we are simulating an identify
    // A new toolMode has been introduced for this application (DEFfindXMLMode).
	toolMode = 1;
	selectionMode = 1;
	setActiveLayer(MonumentLayerIndex);
	LayerVisible[MonumentLayerIndex]=1;
	var theNewQueryString = "";
	var theTempString = "";
	//var fieldListString = LayerIDField[ActiveLayerIndex] + " " + LayerShapeField[ActiveLayerIndex];
	var fieldListString = "#ID# #SHAPE#";
	var elemCount = 0;

	theNewQueryString = "GCM_NO = '"+theValue+"'";
	if (theNewQueryString!="") {
		showBuffer=false;

		setQueryString = theNewQueryString;
		var theString = writeFindRequest(theNewQueryString,fieldListString);
		sendToServer(imsQueryURL,theString,DEFfindXMLMode);
	} else {
		alert("Unable to build Query String.");
	}

}

// Custom Find for zooming to the selected BPBM - vertical benchmark
 function getBPBM(theValue) {
    // we need to set the toolMode and selectionMode globals since we are simulating an identify
    // A new toolMode has been introduced for this application (DEFfindXMLMode).
	toolMode = 1;
	selectionMode = 1;
	setActiveLayer(BPBMLayerIndex);
	LayerVisible[BPBMLayerIndex]=1;
	var theNewQueryString = "";
	var theTempString = "";
	var fieldListString = "#ID# #SHAPE#";
	var elemCount = 0;

	theNewQueryString = "MARKER_ID = '"+theValue+"'";
	if (theNewQueryString!="") {
		showBuffer=false;

		setQueryString = theNewQueryString;
		var theString = writeFindRequest(theNewQueryString,fieldListString);
		sendToServer(imsQueryURL,theString,DEFfindXMLMode);
	} else {
		alert("Unable to build Query String.");
	}

}

function findStreet(theValue) {
    // we need to set the toolMode and selectionMode globals since we are simulating an identify
    // A new toolMode has been introduced for this application (DEFfindXMLMode).
	toolMode = 1;
	selectionMode = 1;
	setActiveLayer(RoadsLayerIndex);
	var theNewQueryString = "";
	var theTempString = "";
	//var fieldListString = LayerIDField[ActiveLayerIndex] + " " + LayerShapeField[ActiveLayerIndex];
	var fieldListString = "#ID# #SHAPE#";
	// find and display multiple street segments

	theNewQueryString = "STREETNAMEC = '"+theValue+"'";
	
	if (theNewQueryString != "") {
		showBuffer=false;
		setQueryString = theNewQueryString;
		var theString = writeFindRequest(theNewQueryString,fieldListString);
		sendToServer(imsQueryURL,theString,DEFfindXMLMode);
	} else {
		alert("Unable to build Query String.");
	}
}

// write out find form
function writeFindRequest(findQuery,fieldList) {
	var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_FEATURES outputmode="xml" geometry="false" envelope="true"';
	theString += ' compact="true" featurelimit="' + maxFeaturesReturned + '" beginrecord="' + queryStartRecord + '">\n';
	theString += '<LAYER id="' + ActiveLayer + '" />\n';
	if (useLimitExtent) {
		// keep this within the limitExtent
		alert("inside writeFindRequest, adding SPATIALFILTER stuff");
		theString += '<SPATIALQUERY subfields="' + fieldList + '" where="' + findQuery + '" />';
		theString += '<SPATIALFILTER relation="area_intersection">\n';
		theString += '<ENVELOPE maxx="' + limitRight + '" maxy="' + limitTop + '" minx="' + limitLeft + '" miny="' + limitBottom + '" />\n';
		theString += '</SPATIALFILTER>\n';
		theString += '</SPATIALQUERY>\n';
	} else {
	theString += '<QUERY subfields="' + fieldList + '" where="' + findQuery + '" />';
	}
	theString += '</GET_FEATURES>\n';
	theString += '</REQUEST>\n';
	theString += '</ARCXML>';
	selectLayer=ActiveLayer;
	selectType=ActiveLayerType;
	selectCount=0;
	hightlightedOne="";
	selectPoints.length=1;
	selectLeft.length=1;
	selectRight.length=1;
	selectTop.length=1;
	selectBottom.length=1;
	return theString;

}
