var pixelTolerance= 4;
var idMinX, idMinY, idMaxX, idMaxY;
var idCount = 0;
var selectedId = 1;
var juris;
var municipality;
var prop_street;
var multiResults = new Array();
var cvtResults = new Array();
var cvtResults2 = new Array();
var cvtResults3 = new Array();
var cvtResults6 = new Array();
var cvtResults7 = new Array();
var cvtResults8 = new Array();
var cvtResults9 = new Array();
var cvtResults10 = new Array();
var endpos = 1;
var lookUp = new Array();

function identify(x1, y1){
	var idMins = convertPixelToMap(x1-(pixelTolerance/2), y1+(pixelTolerance/2));
	var idMaxs = convertPixelToMap(x1+(pixelTolerance/2), y1-(pixelTolerance/2));
	idMinX = idMins[0];
	idMinY = idMins[1];
	idMaxX = idMaxs[0];
	idMaxY = idMaxs[1];
	selectedId = 1;
	sendIdRequest(true); 
}

function convertPixelToMap(px, py){
	var dx = (maxx - minx)/mwidth;
	var mx = minx + (dx * px);
	var my = miny + (dx * (mheight - py));
	var newpoint = new Array();
	newpoint[0] = mx;
	newpoint[1] = my;
	return newpoint;

}

function sendIdRequest(countOnly, beginRecord){
	var axl = getIdRequest(countOnly, beginRecord);
	var queryUrl = url + "&CustomService=Query";
	http = getHTTPObject();
	if ((http != null)) {
	    http.open("POST", queryUrl, true);
	    if (countOnly) http.onreadystatechange = parseResultForCount;
	    else http.onreadystatechange = parseResult;
	    isWorking = true;
	    showLayer('loading');
	    //alert(axl);
	    http.send(axl);
  	}

}

function getIdRequest(countOnly, beginRecord){
	var axl = '<?xml version="1.0" encoding="UTF-8"?>';

	axl += '<ARCXML version="1.1">\n<REQUEST>\n<GET_FEATURES geometry="false" outputmode="xml" checkesc ="true" ';
	if(countOnly) axl += 'envelope="false" skipfeatures="true">\n';
	else axl += 'envelope="true" skipfeatures="false" beginrecord="' +  beginRecord + '" featurelimit="1">\n'; 
	axl += '<LAYER id="' + ActiveLayer + '" />';
	axl += '<SPATIALQUERY>';
	axl += '<SPATIALFILTER relation="area_intersection" >';
	axl += '<ENVELOPE maxy="' + idMaxY + '" maxx="' + idMaxX + '" miny="' + idMinY + '" minx="' + idMinX + '" />';
	axl += '</SPATIALFILTER>';
	axl += '</SPATIALQUERY>';
	axl += '</GET_FEATURES>';
	axl += '</REQUEST>';
	axl += '</ARCXML>';
	return axl;

}

function parseResultForCount(){
	if (http.readyState == 4) {
	    if (http.status == 200) {
	      var result = http.responseText;
	      //alert(result);
	      idCount = getCount(result); 
	      if(idCount != 0) sendIdRequest(false, 1);
	      else alert("No features found");
	     } else alert("Error retreiving data");
	     	          
       }  
}
function parseResult(){
	if (http.readyState == 4) {
	    if (http.status == 200) {
	      var result = http.responseText;
	      //alert(result);
	      processResult(result,100); 
	      
	     } else alert("Error retreiving data");
		          
       }  
}
function getCount(theReply){
	var theCount;
	var startpos = 0;
	var endpos = 0;
	var pos = theReply.indexOf("FEATURECOUNT");
	var pos1 = theReply.indexOf("count", pos + 12);
	if(pos1 != -1){
		pos1 += 7;
		var pos2 = theReply.indexOf("\"", pos1);
		theCount = parseFloat(theReply.substring(pos1,pos2));
	}
	else theCount=0;
	//alert(theCount);
	//hideLayer('loading');
	return theCount;
}
var s = 0;
function processResult2(theReply,i,theLayerName){

theLayerName = "CNTY_BUILDINGS_POINT";


  var selectedData = "";
  //var 
  endpos = 1;
  
  var pos = theReply.indexOf("<FIELDS ",endpos);
  for (s;s < i ;s++){
	  if (s > 0){
		  startpos = endpos + 1;
		  endpos = theReply.indexOf('" />',startpos);
			selectedData = theReply.substring(startpos,endpos);
			//if (s==1)alert(selectedData);
	  }else{
  		if (pos!=-1) {
			var startpos = pos + 8;
			endpos = theReply.indexOf('" />',startpos);
			selectedData = theReply.substring(startpos,endpos);
			//alert(selectedData);
			
  		}
		
	  }
	  
	 if(selectedData != ""){
		var fValue1 = getFieldValues(selectedData);
    	var fName1 = getFieldNames(selectedData);
    	
		for (var j = 0;j < fName1.length - 1;j++)
		
			{
			
			if (fName1[j] == "WGIS."+theLayerName+".BUILDING")//"WEQL.EQLPARCELS.PIN")
			{
				multiResults[s] = fValue1[j] ;
				lookUp[s] = fValue1[j];
				//if (s < 3) alert(s + " " + multiResults[s+""+0]);
				
				
			}
			
			
			}
 	 }
  }
   //hideLayer('loading');
   s = 0;
   
}
var c = 0;
function processResult3(theReply,i){
	//alert("in processResult3");
  var selectedData = "";
   endpos = 1;
  var pos = theReply.indexOf("<FIELDS ",endpos);
  for (c;c < i ;c++){
	  if (c > 0){
		  startpos = endpos + 1;
		  endpos = theReply.indexOf('" />',startpos);
			selectedData = theReply.substring(startpos,endpos);
			
	  }else{
  		if (pos!=-1) {
			var startpos = pos + 8;
			endpos = theReply.indexOf('" />',startpos);
			selectedData = theReply.substring(startpos,endpos);
  		}
		
	  }
	  
	 if(selectedData != ""){
		var fValue1 = getFieldValues(selectedData);
    	var fName1 = getFieldNames(selectedData);
		//alert(fName1.length);
		//return;
		for (var j = 0;j < fName1.length ;j++){
			
			if (fName1[j] == "WGIS.CNTY_BUILDINGS_POINT.ADDRESS")
			{
				cvtResults[c] = fValue1[j];
			}
			//alert(cvtResults[c+""+0] + " - " + cvtResults[c+""+1]);

		}
 	 }
  }
   c = 0;
}
						

var c1 = 0;
function processResult4(theReply,i){
	//alert("in processResult4");
  var selectedData = "";
   endpos = 1;
  var pos = theReply.indexOf("<FIELDS ",endpos);
  for (c1;c1 < i ;c1++){
	  if (c1 > 0){
		  startpos = endpos + 1;
		  endpos = theReply.indexOf('" />',startpos);
			selectedData = theReply.substring(startpos,endpos);
			
	  }else{
  		if (pos!=-1) {
			var startpos = pos + 8;
			endpos = theReply.indexOf('" />',startpos);
			selectedData = theReply.substring(startpos,endpos);
  		}
		
	  }
	  
	 if(selectedData != ""){
		var fValue1 = getFieldValues(selectedData);
    	var fName1 = getFieldNames(selectedData);
		//alert(fName1.length);
		//return;
		for (var j = 0;j < fName1.length ;j++){
			//alert(fName1[j]);
			if (fName1[j] == "WGIS.CNTY_BUILDINGS_POINT.LINK")
			{
				//if (fValue1[j] != "0"){
				cvtResults2[c1] = fValue1[j];
				//}
			}
			//alert(cvtResults[c+""+0] + " - " + cvtResults[c+""+1]);
			//alert(cvtResults2[c1]);

		}
 	 }
  }
   c1 = 0;
}

var c2 = 0;
function processResult5(theReply,i){
	//alert("in processResult4");
  var selectedData = "";
   endpos = 1;
  var pos = theReply.indexOf("<FIELDS ",endpos);
  for (c2;c2 < i ;c2++){
	  if (c2 > 0){
		  startpos = endpos + 1;
		  endpos = theReply.indexOf('" />',startpos);
			selectedData = theReply.substring(startpos,endpos);
			
	  }else{
  		if (pos!=-1) {
			var startpos = pos + 8;
			endpos = theReply.indexOf('" />',startpos);
			selectedData = theReply.substring(startpos,endpos);
  		}
		
	  }
	  
	 if(selectedData != ""){
		var fValue1 = getFieldValues(selectedData);
    	var fName1 = getFieldNames(selectedData);
		//alert(fName1.length);
		//return;
		for (var j = 0;j < fName1.length ;j++){
			//alert(fName1[j]);
			if (fName1[j] == "WGIS.CNTY_BUILDINGS_POINT.WIRELESS")
			{
				//if (fValue1[j] != "0"){
				cvtResults3[c2] = fValue1[j];
				//}
			}
			//alert(cvtResults[c+""+0] + " - " + cvtResults[c+""+1]);
			//alert(cvtResults2[c1]);

		}
 	 }
  }
   c2 = 0;
}

var c6 = 0;
function processResult6(theReply,i){
	//alert("in processResult4");
  var selectedData = "";
   endpos = 1;
  var pos = theReply.indexOf("<FIELDS ",endpos);
  for (c6;c6 < i ;c6++){
	  if (c6 > 0){
		  startpos = endpos + 1;
		  endpos = theReply.indexOf('" />',startpos);
			selectedData = theReply.substring(startpos,endpos);
			
	  }else{
  		if (pos!=-1) {
			var startpos = pos + 8;
			endpos = theReply.indexOf('" />',startpos);
			selectedData = theReply.substring(startpos,endpos);
  		}
		
	  }
	  
	 if(selectedData != ""){
		var fValue1 = getFieldValues(selectedData);
    	var fName1 = getFieldNames(selectedData);
		//alert(fName1.length);
		//return;
		for (var j = 0;j < fName1.length ;j++){
			//alert(fName1[j]);
			if (fName1[j] == "WGIS.CNTY_BUILDINGS_POINT.WIRELESSLOCATION1")
			{
				//if (fValue1[j] != "0"){
				cvtResults6[c6] = fValue1[j];
				//}
			}
			//alert(cvtResults[c+""+0] + " - " + cvtResults[c+""+1]);
			//alert(cvtResults2[c1]);

		}
 	 }
  }
   c6 = 0;
}

var c7 = 0;
function processResult7(theReply,i){
	//alert("in processResult4");
  var selectedData = "";
   endpos = 1;
  var pos = theReply.indexOf("<FIELDS ",endpos);
  for (c7;c7 < i ;c7++){
	  if (c7 > 0){
		  startpos = endpos + 1;
		  endpos = theReply.indexOf('" />',startpos);
			selectedData = theReply.substring(startpos,endpos);
			
	  }else{
  		if (pos!=-1) {
			var startpos = pos + 8;
			endpos = theReply.indexOf('" />',startpos);
			selectedData = theReply.substring(startpos,endpos);
  		}
		
	  }
	  
	 if(selectedData != ""){
		var fValue1 = getFieldValues(selectedData);
    	var fName1 = getFieldNames(selectedData);
		//alert(fName1.length);
		//return;
		for (var j = 0;j < fName1.length ;j++){
			//alert(fName1[j]);
			if (fName1[j] == "WGIS.CNTY_BUILDINGS_POINT.WIRELESSLOCATION2")
			{
				//if (fValue1[j] != "0"){
				cvtResults7[c7] = fValue1[j];
				//}
			}
			//alert(cvtResults[c+""+0] + " - " + cvtResults[c+""+1]);
			//alert(cvtResults2[c1]);

		}
 	 }
  }
   c7 = 0;
}

var c8 = 0;
function processResult8(theReply,i){
	//alert("in processResult4");
  var selectedData = "";
   endpos = 1;
  var pos = theReply.indexOf("<FIELDS ",endpos);
  for (c8;c8 < i ;c8++){
	  if (c8 > 0){
		  startpos = endpos + 1;
		  endpos = theReply.indexOf('" />',startpos);
			selectedData = theReply.substring(startpos,endpos);
			
	  }else{
  		if (pos!=-1) {
			var startpos = pos + 8;
			endpos = theReply.indexOf('" />',startpos);
			selectedData = theReply.substring(startpos,endpos);
  		}
		
	  }
	  
	 if(selectedData != ""){
		var fValue1 = getFieldValues(selectedData);
    	var fName1 = getFieldNames(selectedData);
		//alert(fName1.length);
		//return;
		for (var j = 0;j < fName1.length ;j++){
			//alert(fName1[j]);
			if (fName1[j] == "WGIS.CNTY_BUILDINGS_POINT.WIRELESSLOCATION3")
			//if (fName1[j] == "WGIS.CNTY_BUILDINGS_POINT.WIRELESSL0CATION3")
			{
				//if (fValue1[j] != "0"){
					//alert(fValue1[j]);
				cvtResults8[c8] = fValue1[j];
				//}
			}
			//alert(cvtResults[c+""+0] + " - " + cvtResults[c+""+1]);
			//alert(cvtResults2[c1]);

		}
 	 }
  }
   c8 = 0;
}

var c9 = 0;
function processResult9(theReply,i){
	//alert("in processResult4");
  var selectedData = "";
   endpos = 1;
  var pos = theReply.indexOf("<FIELDS ",endpos);
  for (c9;c9 < i ;c9++){
	  if (c9 > 0){
		  startpos = endpos + 1;
		  endpos = theReply.indexOf('" />',startpos);
			selectedData = theReply.substring(startpos,endpos);
			
	  }else{
  		if (pos!=-1) {
			var startpos = pos + 8;
			endpos = theReply.indexOf('" />',startpos);
			selectedData = theReply.substring(startpos,endpos);
  		}
		
	  }
	  
	 if(selectedData != ""){
		var fValue1 = getFieldValues(selectedData);
    	var fName1 = getFieldNames(selectedData);
		//alert(fName1.length);
		//return;
		for (var j = 0;j < fName1.length ;j++){
			//alert(fName1[j]);
			if (fName1[j] == "WGIS.CNTY_BUILDINGS_POINT.WIRELESSLOCATION4")
			{
				//if (fValue1[j] != "0"){
				cvtResults9[c9] = fValue1[j];
				//}
			}
			//alert(cvtResults[c+""+0] + " - " + cvtResults[c+""+1]);
			//alert(cvtResults2[c1]);

		}
 	 }
  }
   c9 = 0;
}

var c10 = 0;
function processResult10(theReply,i){
	//alert("in processResult4");
  var selectedData = "";
   endpos = 1;
  var pos = theReply.indexOf("<FIELDS ",endpos);
  for (c10;c10 < i ;c10++){
	  if (c10 > 0){
		  startpos = endpos + 1;
		  endpos = theReply.indexOf('" />',startpos);
			selectedData = theReply.substring(startpos,endpos);
			
	  }else{
  		if (pos!=-1) {
			var startpos = pos + 8;
			endpos = theReply.indexOf('" />',startpos);
			selectedData = theReply.substring(startpos,endpos);
  		}
		
	  }
	  
	 if(selectedData != ""){
		var fValue1 = getFieldValues(selectedData);
    	var fName1 = getFieldNames(selectedData);
		//alert(fName1.length);
		//return;
		for (var j = 0;j < fName1.length ;j++){
			//alert(fName1[j]);
			if (fName1[j] == "WGIS.CNTY_BUILDINGS_POINT.WIRELESSLOCATION5")
			{
				//if (fValue1[j] != "0"){
				cvtResults10[c10] = fValue1[j];
				//}
			}
			//alert(cvtResults[c+""+0] + " - " + cvtResults[c+""+1]);
			//alert(cvtResults2[c1]);

		}
 	 }
  }
   c10 = 0;
}

// get a list of field names from the returned record
function getFieldNames(recordString) {
	var theStuff = new String(recordString);				
	var theList = theStuff.split('" ');
	var fName1 = new Array();
	for (var f=0;f<theList.length;f++) {
		var v = theList[f].split('="');
		fName1[f] = v[0];
		
	}
	return fName1;

}

// get a list field values from the returned record
function getFieldValues(recordString) {
	var theStuff = new String(recordString);
				
	var theList = theStuff.split('" ');
	
	var fValue1 = new Array();
	for (var f=0;f<theList.length;f++) {
		var v = theList[f].split('="');
		if ((v[1]=="") || (v[1]==null)) v[1] = "&nbsp;";
		fValue1[f] = v[1];
		
	}
	return fValue1;
}

function displayIdResult(fName1, fValue1){

  var idContent =  "<table class='idTitleBarClass'>";
  idContent += "<tr onmousedown='startMove(event, \"IdResult\")'>";
 idContent += "<td width='95%'><font class='idTitleClass'>Identify Results</font></td>";
 idContent += "<td align='right' width='5%'> <img onmousedown='hideLayer(\"IdResult\");' src='images/close.gif'></td>";
 idContent += "</tr>";
 idContent += "</table>";
 
 idContent += "<table width='100%' cellspacing='5' cellpadding='5'>";
 idContent += "<tr>";
 //idContent += "<table width='100%' cellspacing='5' cellpadding='5'>";
 idContent += "<td class='idNonTitleClass'>Identify Layer : " + LayerName[ActiveLayerIndex] + "<br> Total Count : " + idCount;
 idContent += "</td>";
 idContent += "</tr>";
 
 idContent += "<tr> <td>";
 idContent += "<table border='1' cellpadding='0' cellspacing='2' width='100%'>";
 idContent += "<tr valign='top'>";
 idContent += "<td width='20%'>";
 idContent += "<table border='0' width='100%'>";
 idContent += "<tr class='headerRow'>";
 idContent += "<td>Record Num</td>";
 idContent += "</tr>";
 idContent += "</table>";
 idContent += "<div style='overflow:auto;height:295px;border-width:1px;'>";
 idContent += "<table width='100%' cellpadding='0' cellspacing='0'>";
 for (var i=1;i<=idCount;i++){
 	idContent += " <tr>";
 	idContent += "<td align='middle'>";
 	if(i==selectedId) idContent += "<span class='selectedIdClass'> " + i + "</span>";
 	else {
 		idContent += "<a href='#' onclick='setSelectedId(" + i + ");sendIdRequest(false," + i + " );return false;' style='color:black;font-size:12px;font-family:Arial, Verdana, Helvetica, sans-serif;'>";
 		idContent += "<font class='unselectedIdClass'>" + i + "</font></a>";
 	}
 idContent += "</td></tr>";
 }
 idContent += "</table>"; 
 idContent += "</div>";

 idContent += "</td>";
 idContent += "<td>";
 
 idContent += "<div style='overflow:auto;height:310px;border-width:1px;'>";
 idContent += "<table width='100%' cellpadding='0' cellspacing='0'>";
 idContent += "<tr>";
 idContent += "<td>";
 idContent += "<table cellspadding='0' cellspacing='2' width='100%'>";
 idContent += "<tr>";
 idContent += "<td class='headerRow'><b>Field</b></td><td class='headerRow'><b>Value</b></td>";
 idContent += "</tr>";
 for (var ii=0;ii<fValue1.length;ii++){

 	var evenOdd;
 	if((ii%2) == 0) evenOdd = "evenRow";

 	else evenOdd = "oddRow";

 	idContent += "<tr valign='top'>";
 	idContent += "<td class='" + evenOdd + "'><b>" + fName1[ii] + "</b></td><td class='" + evenOdd + "'>" + fValue1[ii] + "</td>";
 	idContent += "</tr>";
 }
 idContent += "</table>";
 idContent += "</td>";
 idContent += "</tr>";
 idContent += "</table>";
 idContent += "</div>";
 idContent += "</td>";
 idContent += "</tr>";
 idContent += "</table>";
 idContent +="</td>";
 idContent += "</tr>";
 idContent += "<tr>";
 idContent += "<td align='right'><input value='Close' onmousedown='hideLayer(\"IdResult\");' type='button' id='CloseButton' name='CloseButton'></td>";
 idContent += "</tr>";
 idContent += "</table>";
 updateContent('IdResult', idContent);
 showLayer('IdResult');
}

function setSelectedId(indx){
	selectedId = indx;
}