// aimsCustom.js
/*
*  JavaScript template file for ArcIMS HTML Viewer
*		dependent on aimsXML.js, ArcIMSparam.js, aimsCommon.js, aimsMap.js,
*		aimsLayers.js, aimsDHTML.js
*		aimsClick.js, aimsNavigation.js,
*/


// http://mmdev/website/esri/viewer.htm?fire=01&pinx=2556767&piny=388025&taxkey=3921262120
//jimw - whole file is "custom"

// global variables
aimsCustomPresent=true;
// change these to send XML response to custom function.
// use numbers >= 1000 and match in useCustomFunction()
// defaults are defined in aimsXML.js and use standard functions
	
// xml response mode for selection
selectXMLMode = 6;
// xml response mode for identify
identifyXMLMode = 7;
// xml response mode for query
queryXMLMode = 8;
// xml response mode for find
findXMLMode = 14;
// xml response mode hyperlink
hyperlinkXMLMode = 15;

//jimw
// define MKE custom variables
var mkePinX = "";         //&pinx
var mkePinY = "";         //&piny
var mkeTaxKey = "";       //&taxkey
var mkeFireStation = "";  //&fire
var mkePoliceStation = "";  //&police
var mkeElementary = "";   //&elem
var mkeMiddle = "";      //&midd
var mkeHigh = "";        //&high
var mkeAddress = "";     // &addr
var mkeSchool = "";     // &school

// these control the layers used for custom arguments
var mkeTaxKey_ID = 39;  // the LAYER id from the map config file
var mkeTaxKey_Field = "TAXKEY";
var mkeTaxKey_IsChar = true;

var mkeFireStation_ID = 52;  // the LAYER id from the map config file
var mkeFireStation_Field = "STATION";
var mkeFireStation_IsChar = true;

var mkePoliceStation_ID = 51;  // the LAYER id from the map config file
var mkePoliceStation_Field = "STATION";
var mkePoliceStation_IsChar = true;

var mkeSchool_ID = 35;  // the LAYER id from the map config file
var mkeSchool_Field = "taxkey";
var mkeSchool_IsChar = true;

var mkeMapJustStarting = true;  // used in aimsXML.writeXML()

// used for obtaining the geometry of the start feature (fire, school, etc)
var needStartFeatureGeometry = false;
var startFeatureLayerID = "";
var startFeatureQueryString = "";
var startFeatureLBRT = null;
var startExtentRatio = 0.25; // expand bounding box by this amount

function getMKEArguments(args, ARGS) {
  // this function gets called from aimsMap.getCommandLineParams()
  // this function happens at load time
  var startpos = 0;
  var endpos = 0;
  var pat = "";
  var pos = -1;
  
  pat = "PINX=";
  pos = ARGS.indexOf(pat);
  if (pos != -1) {
    startpos = pos + pat.length;
    endpos = args.indexOf("&",startpos);
    if (endpos == -1) { 
      endpos = args.length;
	}
	  mkePinX = args.substring(startpos,endpos);
  }

  pat = "PINY=";
  pos = ARGS.indexOf(pat);
  if (pos != -1) {
    startpos = pos + pat.length;
    endpos = args.indexOf("&",startpos);
    if (endpos == -1) {
	  endpos = args.length;
    }
	mkePinY = args.substring(startpos,endpos);
   }
 
  pat = "TAXKEY=";
  pos = ARGS.indexOf(pat);
  if (pos != -1) {
    startpos = pos + pat.length;
    endpos = args.indexOf("&",startpos);
    if (endpos == -1) {
	  endpos = args.length;
    }
	mkeTaxKey = args.substring(startpos,endpos);
  }

  pat = "ADDR=";
  pos = ARGS.indexOf(pat);
  if (pos != -1) {
    startpos = pos + pat.length;
    endpos = args.indexOf("&",startpos);
    if (endpos == -1) {
	  endpos = args.length;
    }
	mkeAddress = args.substring(startpos,endpos);
	mkeAddress = unescape(mkeAddress);
  }

  pat = "FIRE=";
  pos = ARGS.indexOf(pat);
  if (pos != -1) {
    startpos = pos + pat.length;
    endpos = args.indexOf("&",startpos);
    if (endpos == -1) {
	  endpos = args.length;
    }
	mkeFireStation = args.substring(startpos,endpos);
	needStartFeatureGeometry = true;  // used in aimsXML.js  mode 999
	startFeatureLayerID = mkeFireStation_ID;
	startFeatureQueryString = mkeFireStation_Field + "='" + mkeFireStation + "'"; 
  }
  
  pat = "POLICE=";
  pos = ARGS.indexOf(pat);
  if (pos != -1) {
    startpos = pos + pat.length;
    endpos = args.indexOf("&",startpos);
    if (endpos == -1) {
	  endpos = args.length;
    }
	mkePoliceStation = args.substring(startpos,endpos);
	needStartFeatureGeometry = true;  // used in aimsXML.js  mode 999
	startFeatureLayerID = mkePoliceStation_ID;
	startFeatureQueryString = mkePoliceStation_Field + "='" + mkePoliceStation + "'"; 
  }

  pat = "SCHOOL=";
  pos = ARGS.indexOf(pat);
  if (pos != -1) {
    startpos = pos + pat.length;
    endpos = args.indexOf("&",startpos);
    if (endpos == -1) {
	  endpos = args.length;
    }
	mkeSchool = args.substring(startpos,endpos);
	needStartFeatureGeometry = true;  // used in aimsXML.js  mode 999
	startFeatureLayerID = mkeSchool_ID;
	startFeatureQueryString = mkeSchool_Field + "='" + mkeSchool + "'"; 
  }		
}

function mkeClearPin() {
  mkePinX = "";
  mkePinY = "";
}

function sendStartFeatureQuery() {
	var theString = '<ARCXML VERSION="1.1">\n<REQUEST>\n<GET_FEATURES outputmode="xml" geometry="false" envelope="true"';
	theString += ' compact="true" featurelimit="1" beginrecord="0">\n';
	theString += '<LAYER id="' +  startFeatureLayerID + '" />';
	theString += '<QUERY subfields="#SHAPE# #id#" where="' + startFeatureQueryString + '" />';
	theString += '</GET_FEATURES></REQUEST></ARCXML>';
    sendToServer(imsQueryURL, theString, 1001); // process the response below
}

function overrideVisibility() {
  // called from aimsXML.writeXML, only once at startup
  if (mkeTaxKey != "") {
    setVisibleByID(mkeTaxKey_ID);
  }
  if (mkeFireStation != "") {
    setVisibleByID(mkeFireStation_ID);
  }
  if (mkePoliceStation != "") {
    setVisibleByID(mkePoliceStation_ID);
  }
  if (mkeSchool != "") {
    setVisibleByID(mkeSchool_ID);
  }
} 
	 
function setVisibleByID(theid) {
  // loop through the layerID and look for the specific ID and then
  // set the LayerVisible array for this memeber to "1"
  for (i=0; i<layerCount; i++) {
    if (LayerID[i] == theid) {
	  LayerVisible[i] = 1;
	  break;
	}
  }
}

// custom function for handling clicks 
// 		flow redirected here when
//		toolMode set to >=1000
function customMapTool(e) {
  if (toolMode == 1001) {
    return false; 
  }
  if (toolMode == 1002) {
    // insert code here
  }
}

// send  XML response to custom function
//		flow  redirected here when
//		XMLMode >=1000
function useCustomFunction(theReply) {
	if (XMLMode==1001) {
		// get startFeature's Geometry and save in variables
		var theXYs = getEnvelopeXYs(theReply, 0);
		// theXYs is bounding box of feature
		// make LRTB be union of PIN and fetaure's coords
	    eLeft = Math.min(theXYs[0], mkePinX);
		eBottom = Math.min(theXYs[1], mkePinY);
		eRight = Math.max(theXYs[2], mkePinX);
		eTop = Math.max(theXYs[3], mkePinY);
		// expand the rectangle
		eLeft = eLeft - (startExtentRatio * (eRight - eLeft));
		eRight = eRight + (startExtentRatio * (eRight - eLeft));
		eTop = eTop + (startExtentRatio * (eTop - eBottom));
		eBottom = eBottom - (startExtentRatio * (eTop - eBottom));
		sendMapXML();
		
	} else if (XMLMode==1002) {
		// insert code here
	} else {
		alert(msgList[55] + XMLMode + msgList[56]);
	}
	hideLayer("LoadData");
}

// add custom stuff to Map XML request. . . between selection and geocode
function addCustomToMap1(){
	var customString = "";
	/*
		customString += '<LAYER type="ACETATE" name="theMode">\n';
		customString += '<OBJECT units="PIXEL">\n<TEXT coord="5,' + (iHeight-10) + '" label="This is a test">\n';
		customString += '<TEXTMARKERSYMBOL fontstyle="BOLD" fontsize="12" font="ARIAL" fontcolor="' + modeMapColor + '" ';
		customString += 'threed="TRUE" glowing="' + modeMapGlow + '" />\n</TEXT>\n</OBJECT>';
		customString += '\n</LAYER>\n';
	*/
	return customString;
}

// add custom stuff to Map XML request. . . between clickpoints and copyright
function addCustomToMap2(){
	var customString = "";
	
	return customString;
}

// add custom stuff to Map XML request. . . under modeOnMap
function addCustomToMap3(){
  // burn the address from url args into the map
	var s = "";
	if (mkeAddress != "") {
	  s += '<LAYER type="ACETATE" name="theAddress">\n';
	  s += '<OBJECT units="database">\n<TEXT coords="' + (parseInt(mkePinX)+100) + coordsDelimiter + (parseInt(mkePinY)+100) + 
	       '" label="' + mkeAddress + '">\n';
	  s += '<TEXTMARKERSYMBOL fontstyle="BOLD" fontsize="12" font="ARIAL" fontcolor="255,0,0" ';
	  s += 'antialiasing="true" glowing="255,255,0"/></TEXT></OBJECT></LAYER>\n';
	}
	return s;
}

// add custom stuff to Map XML request. . . on top of everything
// called from aimsXML.writeXML() near the bottom of the function
function addCustomToMap4(){
  var s = "";
  if ( (mkePinX != "") && (mkePinY != "") ) {
    s += '<LAYER type="ACETATE" name="thePin">';
    s += '<OBJECT units="DATABASE"><POINT coords="' + mkePinX + coordsDelimiter + mkePinY +'">';
    s += '<SIMPLEMARKERSYMBOL color="255,0,0" type="circle" width="16"/>\n</POINT></OBJECT></LAYER>';
  }
  
  if (mkeTaxKey != "") {
    s += mkeMakeHightlightPolygon("Parcel", mkeTaxKey_ID, mkeTaxKey_Field, mkeTaxKey, mkeTaxKey_IsChar);
    alert(s);
  }

  if (mkeFireStation != "") {
    s += mkeMakeHightlightPoint("Fire Station", mkeFireStation_ID, mkeFireStation_Field, mkeFireStation, mkeFireStation_IsChar);
  }
  
  if (mkePoliceStation != "") {
    s += mkeMakeHightlightPoint("Police Station", mkePoliceStation_ID, mkePoliceStation_Field, mkePoliceStation, mkePoliceStation_IsChar);
  }

  if (mkeSchool != "") {
    s += mkeMakeHightlightPoint("School", mkeSchool_ID, mkeSchool_Field, mkeSchool, mkeSchool_IsChar);
  }
 
   //if (mkeElementary != "") {
   // s += mkeMakeHightlightPoint("Elementary", mkeElementary_ID, mkeElementary_Field, mkeElementary, mkeElementary_IsChar);
  //} 
  return s;
}

function mkeMakeHightlightPoint(layerName, layerID, layerField, layerValue, layerIsChar) {
  var s = "";
  var apos = "";
  if (layerIsChar == true) {
    apos = "&apos;";
  }
  s += '<LAYER type="featureclass" name="' + layerName + ' (' + layerValue + ')" id="' + layerID + '_H">';
  s += '<DATASET fromlayer="' + layerID + '"/>';
  s += '<SPATIALQUERY where="' + layerField + '=' + apos + layerValue + apos + '"/>';
  s += '<SIMPLERENDERER><TRUETYPEMARKERSYMBOL character="115" font="ESRI Shields" fontcolor="255,0,0" fontsize="25"/>';
  s += '</SIMPLERENDERER></LAYER>'; 
  return s; 
}


function mkeMakeHightlightPolygon(layerName, layerID, layerField, layerValue, layerIsChar) {
  var s = "";
  var apos = "";
  if (layerIsChar == true) {
    apos = "&apos;";
  }
  s += '<LAYER type="featureclass" name="' + layerName + ' (' + layerValue + ')" id="' + layerID + '_H">';
  s += '<DATASET fromlayer="' + layerID + '"/>';
  s += '<SPATIALQUERY where="' + layerField + '=' + apos + layerValue + apos + '"/>';
  s += '<SIMPLERENDERER><SIMPLEPOLYGONSYMBOL fillcolor="255,0,255" filltransparency="0.25" boundarytransparency="0.0"/>';
  s += '</SIMPLERENDERER></LAYER>'; 
  return s; 
}

// extract layers to download
function extractIt() {
	hideLayer("measureBox");
	alert(msgList[51]);
}


