var sQuote = "'";
var dQuote = '"';
var feat = "";
var fillcolor="0,255,0";
var boundarycolor="0,255,0";
var layID = 0;
var layLabel = 1;

function zoom2feat(feature,mn,layerIdent){
	showLayer('loadingMap');
	theMenu = mn;
	feat = "('" + feature + "')";
	sendZoomRequest(mn,layerIdent);
}

function sendZoomRequest(mn,layerIdent){
	var axl = getZoomRequest(mn,layerIdent);
	var queryUrl = url + "&CustomService=Query";
	http = getHTTPObject();
	if ((http != null)) {
	    http.open("POST", queryUrl, true);
	    http.onreadystatechange = parseZoomResult;
	    http.send(axl);
  	}
}

function getZoomRequest(mn,layerIdent){
	var axl = '<?xml version="1.0" encoding="UTF-8"?>\n';
	axl += '<ARCXML version="1.1">\n<REQUEST>\n';
	axl += '<GET_FEATURES outputmode="xml" geometry="false" envelope="true" checkesc ="true" ';
	axl += ' featurelimit="'+maxFeaturesReturned+'" beginrecord="1">\n'; 
	axl += '<LAYER id="'+layerIdent+'">\n';
	axl += '</LAYER>\n';
	axl += '<SPATIALQUERY subfields="#ALL#" where="'+mn+' IN ' + feat + '">\n';
	axl += '</SPATIALQUERY>\n';
	axl += '</GET_FEATURES>\n';
	axl += '</REQUEST>\n';
	axl += '</ARCXML>';

	return axl;
}

function parseZoomResult(){
	if (http.readyState == 4) {
		if (http.status == 200) {
			var result = http.responseText;
			checkCount(result); 
		} else alert("Error retreiving data");		          
	} 
}

function checkCount(result) {
	var featureCount = justGetTheFeatureCount(result);
	if (featureCount < maxFeaturesReturned) {
		parseEnvelopeZoom(result);
	} else {
		alert("Cannot zoom - too many features - maximum 5000 locations can be returned.");
	}
}


function parseEnvelopeZoom(theString) {
  processIdResult(theString);
  theString = theString.toUpperCase();
  var startpos=1;
  var tempString = "";
  var dQuote="\"";
  var featureCount = justGetTheFeatureCount(theString);
  var pos = theString.indexOf("ENVELOPE",startpos);
  if (pos!=-1) {
	var endpos;
	var startpos;
	var newpos;
	minx = 9999999;
	miny = 9999999;
	maxx = -9999999;
	maxy = -9999999;
	for (var j=0;j<featureCount;j++) {
		pos = pos + 8;
		newpos = pos;
		startpos = theString.indexOf("MINX=",pos);
		startpos += 6;
		endpos = theString.indexOf(dQuote,startpos);
		tempString = theString.substring(startpos,endpos);
		if (parseFloat(tempString) < minx) {
			minx = parseFloat(tempString);
		}
		startpos = theString.indexOf("MINY=",pos);
		startpos += 6;
		endpos = theString.indexOf(dQuote,startpos);
		tempString = theString.substring(startpos,endpos);
		if (parseFloat(tempString) < miny) {
			miny = parseFloat(tempString);
		}
		startpos = theString.indexOf("MAXX=",pos);
		startpos += 6;
		endpos = theString.indexOf(dQuote,startpos);
		tempString = theString.substring(startpos,endpos);
		if (parseFloat(tempString) > maxx) {
			maxx = parseFloat(tempString);
		}
		startpos = theString.indexOf("MAXY=",pos);
		startpos += 6;
		endpos = theString.indexOf(dQuote,startpos);
		tempString = theString.substring(startpos,endpos);
		if (parseFloat(tempString) > maxy) {
			maxy = parseFloat(tempString);
		}
		pos = theString.indexOf("ENVELOPE",newpos);
		if (pos==-1) {
			j = featureCount;
		}
	}

	//offset 200 feet from selected polygon
	minx = minx - 200.0;
	miny = miny - 200.0;
	maxx = maxx + 200.0;
	maxy = maxy + 200.0;
	var env = '<ENVELOPE minx="' + minx + '" miny="' + miny +'" maxx="' + maxx +'" maxy="' + maxy + '" />';
	getMap(env);
  } else {
	hideLayer('loadingMap');
  }

}


//----------------

function sendSubdQuery(){
	var axl = getTheQueryString();
	http = getHTTPObject();
	if ((http != null)) {
            http.open("POST", imsQueryURL, true);
	    http.onreadystatechange = parseTheString;
	    http.send(axl);
	}
}


function getTheQueryString() {
	layID=14;
	var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_FEATURES outputmode="xml" geometry="false" envelope="true" checkesc ="true"';
	theString += ' featurelimit="'+maxFeaturesReturned+'" beginrecord="1">\n';
	theString += '<LAYER id="'+layID+'" />\n';
	theString += '<SPATIALQUERY subfields="APARTMENT" />\n';
	theString += '</GET_FEATURES>\n';
	theString += '</REQUEST>\n';
	theString += '</ARCXML>';
	return theString;
}

var env2="";
function parseTheString(){
 	if (http.readyState == 4) {
		if (http.status == 200) {
			var result = http.responseText;
			parseShapeSubd(result);
			getMap("");
		} else alert("Error retreiving menu names");     
	}  
}


// parse layer field value samples
function parseShapeSubd(theReply) {
	var pos = 0;
	var startpos = 0;
	var endpos = 0;
	var subdivs = new Array();
	var ct="";
	ct +="<FORM>";
	ct += "<select size='31' id='parks' name='parks' onClick='zoom2feat(this.form.parks.options[this.form.parks.selectedIndex].value,\"APARTMENT\","+layID+");'>";
	var featureCount = justGetTheFeatureCount(theReply);
	var tempString="";
	if (featureCount > 0) {
		for (var i=0;i<featureCount;i++) {
			pos = theReply.indexOf("<FIELDS APARTMENT",endpos);
			startpos = pos + 19;
			endpos =theReply.indexOf('"',startpos);
			subdivs[i] = theReply.substring(startpos,endpos);
		}
		subdivs.sort();
		for (var j=0;j<featureCount;j++) {
			ct += "<option value='"+subdivs[j]+"' class='lst'>"+subdivs[j];
		}
		ct += "</select>";
		ct += "</form>";
		updateContent('parksform', ct);
	}
}

// get the number of features returned in xml response
function justGetTheFeatureCount(theReply) {
	var theCount = 0;
	var pos = theReply.indexOf("<FEATURECOUNT");
	if (pos!=-1) {
		var theValue = justGetTheValue(theReply,'count="',dQuote,pos);
		theCount = parseInt(theValue);	
	}
	return theCount;
}

function justGetTheValue(theReply,preString,postString,startpos) {
	var theValue = "";
	var pos = theReply.indexOf(preString,startpos);
	if (pos!=-1) {
		pos = pos + preString.length;
		var endpos = theReply.indexOf(postString,(pos));
		if (endpos!=-1) {
			theValue = theReply.substring(pos,endpos);
			xmlEndPos = endpos;
		}
	} else {
		var pos = theReply.indexOf(preString.toUpperCase(),startpos);
		if (pos!=-1) {
			pos = pos + preString.length;
			var endpos = theReply.indexOf(postString,(pos));
			if (endpos!=-1) {
				theValue = theReply.substring(pos,endpos);
				xmlEndPos = endpos;
			}
		}
	}
	return theValue;	
}
