// JavaScript Document

function IAttributeQuery() {
	
	//Properties
	this.featureLimit = 25;		//integer
	this.beginrecord = 1;		//integer
	this.subFields;				//string (seperate by space)
	this.layerId;				//integer
	this.where;					//string
	//this.selectFeatures; 		// boolean
	
	//use results2 obecjt for reply
	this.useResults2 = false;
	
	//Methods
	this.getQueryAXL = getAttributeQueryAXL;
	this.sendQuery = sendQuery;
	this.getQuery = getQuery;
	
	this.map;
}



function IPointQuery() {
	
	//Properties
	this.featureLimit = 25;		//integer
	this.beginrecord = 1;		//integer
	this.subFields;				//string (seperate by space)
	this.layerId;				//integer
	this.point;					//IPoint
	this.envelope;				//IEnvelope;
	this.percision = 6; 		// integer	
	this.selectFeatures; 		// boolean
	
	this.map; // IMap (needs to know what Map to ID on)
	
	
	//use results2 obecjt for reply
	this.useResults2 = false;
	
	//Methods
	this.getQueryAXL = getPointQueryAXL;
	this.sendQuery = sendQuery;
	this.getQuery = getQuery;
}


function IBufferQuery() {
	
	//Properties
	this.featureLimit = 25;		//integer
	this.beginrecord = 1;		//integer

	this.layerId;				//integer
	this.targetLayerId;
	this.subFields;				//string (seperate by space)
	this.where;
	this.distance;
	this.units = 'feet';
	
	//use results2 obecjt for reply
	this.useResults2 = false;
	
	this.getBufferAXL =getBufferAXL;
	this.getBufferAttributes = getBufferAttributes;
	this.sendQuery = sendQuery;
	this.map;
}

function getBufferAXL() {
	var bufferAXL ='';
		bufferAXL +='<?xml version="1.0" encoding="UTF-8" ?>\n';
		bufferAXL +='<ARCXML version="1.1">\n';
		bufferAXL +='<REQUEST>\n';
		bufferAXL +='<GET_FEATURES featurelimit="'+this.featureLimit+'" beginrecord="'+this.beginrecord+'" outputmode="xml" geometry="false" envelope="false" compact="true">\n';
		
		
		bufferAXL +='<LAYER id="'+this.layerId+'" />\n';
		bufferAXL +='<SPATIALQUERY where="'+this.where+'">\n';
		bufferAXL +='<BUFFER distance="'+this.distance+'" bufferunits="'+this.units+'" >\n';
		bufferAXL +='<TARGETLAYER id="'+this.targetLayerId+'" />\n';
		bufferAXL +='<SPATIALQUERY subfields="'+this.subFields+'" />\n';
		bufferAXL +='</BUFFER>\n';
		
		bufferAXL +='</SPATIALQUERY>\n';
		bufferAXL +='</GET_FEATURES>\n';
		bufferAXL +='</REQUEST>\n';
		bufferAXL +='</ARCXML>\n';
		return bufferAXL;
}





function getPointQueryAXL() {
	
	var searchTolerance =(this.map.xDiff/this.map.width) * this.percision;
	
	//use only when doing a Point.
	if(!this.envelope) 
	{
		
	var pointEnvelope = new tempEnvelope()
		pointEnvelope.minx = parseFloat(this.point.x) - searchTolerance;
		pointEnvelope.miny = parseFloat(this.point.y) + searchTolerance;
		pointEnvelope.maxx = parseFloat(this.point.x) + searchTolerance;
		pointEnvelope.maxy = parseFloat(this.point.y) - searchTolerance;
	}
	
	else
	{	
		pointEnvelope = this.envelope;
	}
	
	var queryAXL  = '<?xml version="1.0" encoding="UTF-8"?>\n';
		queryAXL += '<ARCXML version="1.1">\n';
		queryAXL += '<REQUEST>\n';
		queryAXL += '<GET_FEATURES outputmode="xml" geometry="false" envelope="true" checkesc ="true" featurelimit="' + this.featureLimit + '" beginrecord="' + this.beginrecord + '">\n';
		queryAXL += '<LAYER id="'+ this.layerId +'" />\n';
		queryAXL += '<SPATIALQUERY subfields="' + this.subFields + '">\n';
		queryAXL += '<SPATIALFILTER relation="area_intersection">\n';
		queryAXL += '<ENVELOPE minx="'+ pointEnvelope.minx +'" miny="'+ pointEnvelope.miny +'" maxx="'+ pointEnvelope.maxx +'" maxy="'+ pointEnvelope.maxy +'" />\n';
		queryAXL += '</SPATIALFILTER>\n';
		queryAXL += '</SPATIALQUERY>\n';
		queryAXL += '</GET_FEATURES>\n';
		queryAXL += '</REQUEST>\n';
		queryAXL += '</ARCXML>\n';
		return queryAXL;
}



function getAttributeQueryAXL() {
	
	var queryAXL =  '<?xml version="1.0" encoding="UTF-8"?>\n<ARCXML version="1.1">\n'
		queryAXL += '<REQUEST>\n';
		queryAXL += '<GET_FEATURES outputmode="xml" geometry="false" envelope="true" checkesc ="true" featurelimit="' + this.featureLimit + '" beginrecord="' + this.beginrecord + '">\n';
		queryAXL += '<LAYER id="' + this.layerId + '" />\n';
		queryAXL += '<SPATIALQUERY subfields="' + this.subFields + '" where="' + this.where + '" />\n';
		queryAXL += '</GET_FEATURES>\n';
		queryAXL += '</REQUEST>\n';
		queryAXL += '</ARCXML>\n';
		return queryAXL;
}






function sendQuery(axl) {
	
	//showLayer("loadingLayer");
	
	doDebug(axl,null)
	var resultsName = "RESULTS";
	if (this.useResults2) { resultsName = "RESULTS2"; }
	
	  
	
	var url = this.map.service.url + "&CustomService=Query";
    var xml = new JKL.ParseXML(url,axl);
    qdata = xml.parse();	

	RESPONSE.COUNT = qdata.ARCXML.RESPONSE.FEATURES.FEATURECOUNT.count;
	
	//Append the results into the objects.
	//If only one found. add to single array for standards.
	if (qdata.ARCXML.RESPONSE.FEATURES.FEATURE) {
		
	RESPONSE[resultsName] = qdata.ARCXML.RESPONSE.FEATURES.FEATURE;
	}
	//alert(data.ARCXML.RESPONSE.FEATURES.FEATURE[0]);
	if (qdata.ARCXML.RESPONSE.FEATURES.FEATURECOUNT.count =="1") {
		RESPONSE[resultsName] = new Array();
		RESPONSE[resultsName][0]  = qdata.ARCXML.RESPONSE.FEATURES.FEATURE;
	
	}
	if (!qdata.ARCXML.RESPONSE.FEATURES.FEATURE)
	{
		RESPONSE[resultsName] = new Array();
		RESPONSE[resultsName].length = 0;
	}
	//hideLayer("loadingLayer");
	
}

function getQuery(map) {
	
	if (!map)
	{ 
		this.map = myMap;
	}
	else {
		
	this.map = map;
	}
	var queryAXL = this.getQueryAXL();
	this.sendQuery(queryAXL);
}


function getBufferAttributes(map) {
	if (!map)
	{ 
		this.map = myMap;
	}
	else {
		
	this.map = map;
	}
	var bufaxl = this.getBufferAXL()
	this.sendQuery(bufaxl);
	
	
}




