// 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 

***************************************************************************************
*/


// process query
function sendQueryString(newString) {
	//if (LayerIDField[ActiveLayerIndex]!="#ID#") {
		newString = fixSingleQuotes(newString);
		newString = swapQuotes(newString);
	//}
	//alert(newString);
	newString = makeXMLsafe(newString);
	setQueryString=newString;
	selectionMode=1;
	selectData.length=0;
	LayerFields.length=0;
	LayerFieldType.length=0;
	LayerFieldCount=0;
	//highlightedOne="";
	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 += ' 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="' + forceComma(limitRight) + '" maxy="' + forceComma(limitTop) + '" minx="' + forceComma(limitLeft) + '" miny="' + forceComma(limitBottom) + '" />\n';
		theString += '</SPATIALFILTER>\n';
		theString += '</SPATIALQUERY>\n';
	} else {
	//theString += '<QUERY subfields="' + selectFields + '" where="' + queryString + '" />';
	theString += '<SPATIALQUERY subfields="' + selectFields + '" where="' + queryString + '" />';
	}
	theString += '</GET_FEATURES>';
	theString += '</REQUEST>';
	theString += '</ARCXML>';
	//alert("writeQueryXML()\nQuery XML Request:\n" + theString);
	selectLayer=ActiveLayer;
	selectType=ActiveLayerType;
	selectCount=0;
	hightlightedOne="";
	selectPoints.length=1;
	selectLeft.length=1;
	selectRight.length=1;
	selectTop.length=1;
	selectBottom.length=1;
	return theString;
}

function writeStartQueryXML(queryString, idAndShapeOnly) {
	
	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="0">\n';
	theString += '<LAYER id="' + ActiveLayerIndex + '" />';
	if (idAndShapeOnly) {
		theString += '<QUERY subfields="#ID# #SHAPE#" where="' + queryString + '" />';
	} else {
		theString += '<QUERY subfields="' + selectFields + '" where="' + queryString + '" />';
	}
	theString += '</GET_FEATURES>';
	theString += '</REQUEST>';
	theString += '</ARCXML>';
	//alert("K2P Query\n\n" + theString);
	return theString;
	
}

function setStartQuery() {

	var theString = writeStartQueryXML(highlightedOne,true);
	sendToServer(imsQueryURL, theString, 20);
}

// get min and max x,y's from xml stream . . . return an array with values
function getK2PEnvelopeXYs(theString, startpos) {
	//alert("Starting to read \n\n" + theString + "\n\n starting at position: " + startpos)

	var theEnvelope = new Array();
	theString = theString.toUpperCase();
	var tempString = "";
	var pos = theString.indexOf("ENVELOPE",startpos);
	if (pos!=-1) {

		//alert("Match found at position " + pos)
		pos = pos + 8;
		
		startpos = theString.indexOf("MINX=",pos);
		startpos += 6;
		var endpos = theString.indexOf(dQuote,startpos);
		tempString = theString.substring(startpos,endpos);
		theEnvelope[0] = parseFloat(setDecimalString(tempString)); 
		startpos = theString.indexOf("MINY=",pos);
		startpos += 6;
		endpos = theString.indexOf(dQuote,startpos);
		tempString = theString.substring(startpos,endpos);
		theEnvelope[1] = parseFloat(setDecimalString(tempString)); 
		startpos = theString.indexOf("MAXX=",pos);
		startpos += 6;
		endpos = theString.indexOf(dQuote,startpos);
		tempString = theString.substring(startpos,endpos);
		theEnvelope[2] = parseFloat(setDecimalString(tempString)); 
		startpos = theString.indexOf("MAXY=",pos);
		startpos += 6;
		endpos = theString.indexOf(dQuote,startpos);
		tempString = theString.substring(startpos,endpos);
		theEnvelope[3] = parseFloat(setDecimalString(tempString)); 
		xmlEndPos = endpos;
	}

	//alert("At close, xmlEndPos is " + xmlEndPos)
	return theEnvelope;
}

function parseStartQuery(theReply) 
{	

	//If nothing was found
	var featureCount = justGetFeatureCount(theReply);
	if (featureCount == 0)
	{
		sendMapXML();
		alert("Nothing has been found")
		return;
	}
	
	self.focus()

	//alert("There are " + featureCount + " features")

	//var xmlEndPos = 0;
	var endpos = 1;

	for (var i=0;i<featureCount;i++)
	{			
		//alert("passing start positon " + endpos)
		var theXYs = getK2PEnvelopeXYs(theReply, endpos)
		selectLeft[selectCount] = theXYs[0];
		selectBottom[selectCount] = theXYs[1];
		selectRight[selectCount] = theXYs[2];
		selectTop[selectCount] = theXYs[3];
		endpos = xmlEndPos;

		selectCount ++;
	}
	zoomToReturnedRecords()
}

