// sjcDatalink.js
// Copyright (C) 2005 San Joaquin County Community Development Geographic Information Systems, ALL RIGHTS RESERVED
// initial creation: 01/14/2004 David Bollinger
//


// db Identify - external db access
//
function requestIdentify(e) {
	select.clear();
	getMapXY(mouse.x, mouse.y);
	var pixelTolerance = 2;
	var searchTolerance = (extent.width/iWidth) * pixelTolerance;
	var minx = mapX - searchTolerance;
	var miny = mapY - searchTolerance;
	var maxx = mapX + searchTolerance;
	var maxy = mapY + searchTolerance;
	var sXML =
		'<ARCXML version="1.1">\n' +
		' <REQUEST>\n' +
		'  <GET_FEATURES outputmode="xml" envelope="false" checkesc ="true" geometry="false" featurelimit="25">\n' +
		'   <LAYER id="' + layers.active.id + '" />\n' +
		'   <SPATIALQUERY subfields="#ALL#">\n' +
		'    <SPATIALFILTER relation="area_intersection" >\n' +
		'     <ENVELOPE minx="' + n2s(minx) + '" miny="' + n2s(miny) + '" maxx="' + n2s(maxx) + '" maxy="' + n2s(maxy) + '" />\n' +
		'    </SPATIALFILTER>\n' +
		'   </SPATIALQUERY>\n' +
		'  </GET_FEATURES>\n' +
		' </REQUEST>\n' +
		'</ARCXML>';
	showRetrieveData();
	com.send(imsQueryURL, sXML, receiveIdentify);
}


// get the attribute values from the identify XML response
// 	then send request to external db access server
// 	and request for map highlighting feature
//
function receiveIdentify(sXML) {
	var featureCount = parseInt(getTagAttValue(sXML,'FEATURECOUNT','count','0'));
	if (featureCount > 0) {
		// retrieve key and #id#
		var keyvalue = getTagAttValue(sXML, 'FIELDS', 'APN_CHR', '');
		var idvalue = getTagAttValue(sXML, 'FIELDS', '#ID#', '');
		// query database for key
	  doQueryExecuteDetails(keyvalue);
		// highlight map for #id#
		select.whereclause = layers.active.idfield + " = " + idvalue;
		map.request();
	} else {
		var Win1;
		Win1 = parent.TextFrame.document;
		Win1.open();
		Win1.writeln('<html><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><head>');
		Win1.writeln('	<title>Select/Identify Results</title>');
		Win1.writeln('</head>');
		Win1.writeln('<body BGCOLOR="White" TEXT="Black" LEFTMARGIN=0 TOPMARGIN=0>');
		Win1.writeln('<FONT FACE="Arial"><B>' + layers.active.name + '</B></font><FONT FACE="Arial" size="-2">');
		Win1.writeln('<br>Unable to locate feature.  Try zooming in to pinpoint feature.</FONT>');
		Win1.writeln('</body></html>');
		Win1.close();
	}
	hideRetrieveData();
}

function doSelectResult(value) {
	showRetrieveData();
	doQueryExecuteDetails(value);
	requestEnvelopeByApn(value);
}

function doQueryExecuteDetails(value) {
	var fra = parent.TextFrame;
  fra.document.location = appDir + "QueryExecuteDetails.asp?apn=" + value;
}

function requestEnvelopeByApn(value) {
	select.clear();
	select.whereclause = 'APN = ' + value;
	hideRetrieveData();
	var sXML =
		'<ARCXML VERSION="1.1">\n' +
		' <REQUEST>\n' +
		'  <GET_FEATURES outputmode="xml" geometry="false" envelope="true" compact="true" featurelimit="25" beginrecord="1">\n' +
		'   <LAYER id="' + layers.active.id + '" ftype="' + layers.active.type + '" />\n' +
		'   <QUERY subfields="#ALL#" where="' + select.whereclause + '" />\n' +
		'  </GET_FEATURES>\n' +
		' </REQUEST>\n' +
		'</ARCXML>';
  showRetrieveData();
	com.send(imsQueryURL, sXML, receiveEnvelopeByApn);
}

function receiveEnvelopeByApn(sXML) {
	hideRetrieveData();
	//var theError = getXMLErrorMessage(sXML);
	var featureCount = parseInt(getTagAttValue(sXML,'FEATURECOUNT','count','0'));

  if (featureCount < 1) {
    hideRetrieveData();
    hideRetrieveMap();
    alert("Could not locate specified parcel within map data, unable to zoom to map.\r\n\r\n" +
          "This may represent an error in either the map data or the textual data.\r\n\r\n" +
          "Please contact GIS staff so that this parcel can be researched further." );
    return;
  }

	// note that we assume there is only a single feature returned
  var idValue = getTagAttValue(sXML,'FIELDS','#ID#','');
	select.whereclause = layers.active.idfield + " = " + idValue;

	saveLastExtent();
	extent.loadXML(sXML);
	var mWMargin = 0;
	var mHMargin = 0;
	if (layers.active.type == "point") {
		mWMargin = fullextent.width * (25/1000);
		mHMargin = fullextent.height * (25/1000);
	} else {
		mWMargin = extent.width * (150/100);
		mHMargin = extent.height * (150/100);
	}
	extent.inset(-mWMargin, -mHMargin);
	map.request();
}



