// logic=AppFunctions.js

// This variable determines the current tool - default is ZoomIn (toolMode = "Zoom In")
var toolMode = "Zoom In"

// This variable is used to display the tool name in modeFrame
var activeToolName

// This variable is used to display the active layer name in modeFrame
var activeLayerName

// Variables for VML / Measure & Identify by User Poly tools
var vectorX = new Array();
var vectorY = new Array();
var vectorPoints = 0;
var vectorString = "";
var measureClickCount = 0;

// **************************************************************
// TOOL CODE

// This function highlights the selected tool

function setToolIcon(){

 var a = parent.toolFrame.document

 if (toolMode == "Zoom In"){
  if (a.ZoomIn) a.ZoomIn.src="imsImages/zoomin_2.gif"
 } else {
  if (a.ZoomIn) a.ZoomIn.src="imsImages/zoomin_1.gif"
 }

 if (toolMode == "Zoom Out"){
  if (a.ZoomOut) a.ZoomOut.src="imsImages/zoomout_2.gif"
 } else {
  if (a.ZoomOut) a.ZoomOut.src="imsImages/zoomout_1.gif"
 }

 if (toolMode == "Pan"){
  if (a.Pan) a.Pan.src = "imsImages/pan_2.gif"
 } else {
  if (a.Pan) a.Pan.src = "imsImages/pan_1.gif"
 }

 if (toolMode == "Identify"){
  if (a.Identify) a.Identify.src = "imsImages/identify_2.gif"
 } else {
  if (a.Identify) a.Identify.src = "imsImages/identify_1.gif"
 }

 if (toolMode == "Drill-down Identify"){
  if (a.DrillDownIdentify) a.DrillDownIdentify.src = "imsImages/drilldownid_2.gif"
 } else {
  if (a.DrillDownIdentify) a.DrillDownIdentify.src = "imsImages/drilldownid_1.gif"
 }

 if (toolMode == "Markup"){
  if (a.Markup) a.Markup.src = "imsImages/markup_2.gif"
 } else {
  if (a.Markup) a.Markup.src = "imsImages/markup_1.gif"
 }

 if (toolMode == "MeasureLine"){  //6
  if (a.MeasureLine) a.MeasureLine.src = "imsImages/Measure_2.gif"
 } else {
  if (a.MeasureLine) a.MeasureLine.src = "imsImages/Measure_1.gif"
 }

 if (toolMode == "MeasureArea"){  //6
  if (a.MeasureArea) a.MeasureArea.src = "imsImages/measureArea_2.gif"
 } else {
  if (a.MeasureArea) a.MeasureArea.src = "imsImages/measureArea_1.gif"
 }


 if (toolMode == "MapNotes"){ // 10
  if (a.MapNotes) a.MapNotes.src = "imsImages/select_2.gif"
 } else {
  if (a.MapNotes) a.MapNotes.src = "imsImages/select_1.gif"
 }

 if (toolMode == "Identify by Polygon"){ // 11
  if (a.IdUserPoly) a.IdUserPoly.src = "imsImages/select_poly_2.gif"
 } else {
  if (a.IdUserPoly) a.IdUserPoly.src = "imsImages/select_poly_1.gif"
 }

}

// This function gets the tool image for display in modeFrame

function getActiveToolInfo(){

 var a = parent.modeFrame.document

 switch(toolMode) {
  case "Zoom In":
   a.activeToolImage.src="imsImages/zoomin_1.gif"
   activeToolName = "Zoom In"
  break
  case "Zoom Out":
   a.activeToolImage.src="imsImages/zoomout_1.gif"
   activeToolName = "Zoom Out"
  break
  case "Pan":
   a.activeToolImage.src="imsImages/pan_1.gif"
   activeToolName = "Pan"
  break
  case "Identify":
   a.activeToolImage.src="imsImages/identify_1.gif"
   activeToolName = "Identify"
  break
  case "Drill-down Identify":
   a.activeToolImage.src="imsImages/drilldownid_1.gif"
   activeToolName = "Drill-Down Identify"
  break
  case "Markup":
   a.activeToolImage.src="imsImages/markup_1.gif"
   activeToolName = "Redline Markup"
  break
  case "MeasureLine":
   a.activeToolImage.src="imsImages/measure_1.gif"
   activeToolName = "MeasureLine"
  break
  case "MeasureArea":
   a.activeToolImage.src="imsImages/measureArea_1.gif"
   activeToolName = "MeasureArea"
  break  
  case "MapNotes":
   a.activeToolImage.src="imsImages/markup_1.gif"
   activeToolName = "Add Map Note"
  break
  case "Identify by Polygon":
   a.activeToolImage.src="imsImages/select_poly_1.gif"
   activeToolName = "Identify by Polygon"
  break

 }

}

function setToolCursor(){
 if (isIE){
  if (mapFrame.document.all.theTop) {
   mapFrame.document.body.style.cursor = "crosshair";
   switch(toolMode){
    case "Pan":
     mapFrame.document.body.style.cursor = "move";
    break
   }
  }
 }
}

// ******************************************************
// This function responds to a click on map buttons and tools

function foo() {
   alert("foo");
}

function toolClick(toolName) {

 switch(toolName) {
  case "ZoomFull":
   mapZoomFullExtent();
  break

  case "ZoomPrevious":
   mapZoomPreviousExtent();
  break

  case "ZoomIn":
   toolMode = "Zoom In";
  break
  case "ZoomOut":
   toolMode = "Zoom Out";
  break

  case "Pan":
   toolMode = "Pan";
  break

  case "Identify":
   toolMode = "Identify";
  break

  case "DrillDownIdentify":
   toolMode = "Drill-down Identify";
  break

  case "Markup":
   toolMode = "Markup";
  break

  case "MeasureLine":
   toolMode = "MeasureLine";
  break

  case "MeasureArea":
   toolMode = "MeasureArea";
  break

  case "MapNotes":
   toolMode = "MapNotes";
  break

  case "IdUserPoly":
   toolMode = "Identify by Polygon";
  break

  case "ShowAttributes":
   showAttributes();
  break

  case "Clear":
   clearSelection();
  break

  case "ClearMarkup":
   clearMapMarkup();
  break

  case "TextMarkup":
   toolMode = "Text Markup"
//   toolMode = "MapNotes";
   showTextMarkupForm();
  break

  case "PrintPage":
   createPrintPage();
  break

  case "SaveUserExtent":
   saveUserMapExtent();
  break

  case "ZoomUserExtent":
   zoomUserMapExtent();
  break

  case "AdHocSearch":
   searchAdHoc();
  break

 }

 parent.modeFrame.location = "mode.asp"

 // Set the tool cursor
 setToolCursor();

}


// **************************************************************
// MAP REQUEST CODE

// This function submits the main map form

function submitForm(target){
 if (parent.mapFrame.showLayer) parent.mapFrame.showLayer("loadData")

 checkMapSize();

 parent.formFrame.document.mainForm.target = target;
 parent.formFrame.document.mainForm.submit();
}

// This function gets the size of the mapFrame and populates form width and height items

function checkMapSize(){
 var mapFrameWidth = mapFrame.innerWidth; // Netscape
 if (mapFrameWidth == null){
  mapFrameWidth = mapFrame.document.body.clientWidth; // IE
 }
 var mapFrameHeight = mapFrame.innerHeight; // Netscape
 if (mapFrameHeight == null){
  mapFrameHeight = mapFrame.document.body.clientHeight; // IE
 }
 parent.formFrame.document.mainForm.mapWidth.value = mapFrameWidth
 parent.formFrame.document.mainForm.mapHeight.value = mapFrameHeight
}

// This function responds to a map zoom to full extent request

function mapZoomFullExtent(){
 parent.formFrame.document.mainForm.source.value = "Map"
 parent.formFrame.document.mainForm.cmd.value = "ZoomFull"
 submitForm("mapFrame");
}

// This function responds to a map zoom to previous extent request

function mapZoomPreviousExtent(){
 parent.formFrame.document.mainForm.source.value = "Map"
 parent.formFrame.document.mainForm.cmd.value = "ZoomPrevious"
 submitForm("mapFrame");
}

// This function responds to a map zoom to extent request (any extent)

function mapZoomToExtent(x1,y1,x2,y2){
 parent.formFrame.document.mainForm.source.value = "Map"
 parent.formFrame.document.mainForm.cmd.value = "ZoomToExtent"
 parent.formFrame.document.mainForm.mapX1.value = x1
 parent.formFrame.document.mainForm.mapY1.value = y1
 parent.formFrame.document.mainForm.mapX2.value = x2
 parent.formFrame.document.mainForm.mapY2.value = y2
 submitForm("mapFrame");
}

// This function responds to a map zoomin in request

function mapZoomIn(x1,y1,x2,y2){
 parent.formFrame.document.mainForm.source.value = "Map"
 parent.formFrame.document.mainForm.cmd.value = "ZoomIn"
 parent.formFrame.document.mainForm.mapX1.value = x1
 parent.formFrame.document.mainForm.mapY1.value = y1
 parent.formFrame.document.mainForm.mapX2.value = x2
 parent.formFrame.document.mainForm.mapY2.value = y2
 submitForm("mapFrame");
}

// This function responds to a map zoom out request

function mapZoomOut(x1,y1,x2,y2){
 parent.formFrame.document.mainForm.source.value = "Map"
 parent.formFrame.document.mainForm.cmd.value = "ZoomOut"
 parent.formFrame.document.mainForm.mapX1.value = x1
 parent.formFrame.document.mainForm.mapY1.value = y1
 parent.formFrame.document.mainForm.mapX2.value = x2
 parent.formFrame.document.mainForm.mapY2.value = y2
 submitForm("mapFrame");
}

// This function responds to a map pan request

function mapPan(x1,y1,x2,y2){
 parent.formFrame.document.mainForm.source.value = "Map"
 parent.formFrame.document.mainForm.cmd.value = "Pan"
 parent.formFrame.document.mainForm.mapX1.value = x1
 parent.formFrame.document.mainForm.mapY1.value = y1
 parent.formFrame.document.mainForm.mapX2.value = x2
 parent.formFrame.document.mainForm.mapY2.value = y2
 submitForm("mapFrame");
}

// This function responds to a map identify request

function mapIdentify(x1,y1,x2,y2){
 parent.formFrame.document.mainForm.source.value = "Map"
 parent.formFrame.document.mainForm.cmd.value = "Identify"
 parent.formFrame.document.mainForm.mapX1.value = x1
 parent.formFrame.document.mainForm.mapY1.value = y1
 parent.formFrame.document.mainForm.mapX2.value = x2
 parent.formFrame.document.mainForm.mapY2.value = y2
 submitForm("mapFrame");
}

// This function responds to a map DRILLDOWN identify request

function mapDrillDownIdentify(x1,y1){
 parent.textFrame.location = "drilldown.asp?mapX1=" + x1 + "&mapY1=" + y1
}

// This function responds to a map refresh request

function mapRefresh(){
 parent.formFrame.document.mainForm.source.value = "Map"
 parent.formFrame.document.mainForm.cmd.value = "Refresh"
 submitForm("mapFrame");
}

// This function clears the map selection

function clearSelection(){
 parent.formFrame.document.mainForm.source.value = "Map"
 parent.formFrame.document.mainForm.cmd.value = "clearSelection"
 submitForm("mapFrame");
}

// This function clears the map markup graphics

function clearMapMarkup(){
 parent.formFrame.document.mainForm.source.value = "Map"
 parent.formFrame.document.mainForm.cmd.value = "clearMapMarkup"
 submitForm("mapFrame");
}

function showTextMarkupForm(){
 parent.textFrame.document.location = "textmarkup.asp"
}

function textMarkup(x1,y1,sText){
 parent.formFrame.document.mainForm.source.value = "Map"
 parent.formFrame.document.mainForm.cmd.value = "textMarkup"
 parent.formFrame.document.mainForm.mapX1.value = x1
 parent.formFrame.document.mainForm.mapY1.value = y1
 parent.formFrame.document.mainForm.text.value = sText
 submitForm("mapFrame");
}

// This function displays attribute information for any selected features

function showAttributes(){
 textFrame.location = "attributes.asp";
}


// This function displays the Ad-Hoc Search pane for the current active layer

function searchAdHoc(){
 textFrame.location = "search-adhoc.asp";
}


// This function creates a print page

function createPrintPage(){
 //var printWin = window.open("printpage.asp","print","menubar=yes,toolbar=yes,resizable,location=no")
 //printWin.focus()

 parent.textFrame.location = "print-Options.asp";

}

// This function is a catch-all attribute search based on a single field value e.g. #ID#

function searchMapByAttributes(searchLayer,searchQuery){
 parent.formFrame.document.mainForm.source.value = "Map"
 parent.formFrame.document.mainForm.cmd.value    = "Search"
 parent.formFrame.document.mainForm.searchLayer.value = searchLayer;
 parent.formFrame.document.mainForm.searchQuery.value = searchQuery;
 submitForm("mapFrame");
}


// This function is used to open the buffer dialog window

//function openBufferWindow(){
// var bufferWin = window.open("buffer.asp","buffer","height=120,width=275,top=100,left=100,status=no,toolbar=no,menubar=no,location=no")
// bufferWin.focus()
//}

// This function handles creatign a buffer of the selected feature(s)

function searchMapByBuffer(searchLayer,bufferWidth){
 parent.formFrame.document.mainForm.source.value = "Map"
 parent.formFrame.document.mainForm.cmd.value = "Buffer"
 parent.formFrame.document.mainForm.searchLayer.value = searchLayer;
 parent.formFrame.document.mainForm.bufferWidth.value = bufferWidth;
 submitForm("mapFrame");
}

// This function is a search based on a buffer surrounding an address or address/cross street

function searchMapByAddress(searchLayer,bufferWidth,searchAddress,searchCrossSt){
 parent.formFrame.document.mainForm.source.value = "Map"
 parent.formFrame.document.mainForm.cmd.value    = "Address"
 parent.formFrame.document.mainForm.searchLayer.value = searchLayer;
 parent.formFrame.document.mainForm.bufferWidth.value = bufferWidth;
 parent.formFrame.document.mainForm.searchAddress.value = searchAddress;
 parent.formFrame.document.mainForm.searchCrossSt.value = searchCrossSt;
 submitForm("mapFrame");
}

// This function plots a point at a matched address or address/cross street

function FindAddress(searchAddress,searchCrossSt){
 parent.formFrame.document.mainForm.source.value = "Map"
 parent.formFrame.document.mainForm.cmd.value    = "Address"
 parent.formFrame.document.mainForm.searchAddress.value = searchAddress;
 parent.formFrame.document.mainForm.searchCrossSt.value = searchCrossSt;
 submitForm("mapFrame");
}

// This function responds to a map coordinate request Add Map Note

function mapNoteLocation(x1,y1){
 parent.textFrame.document.MapNoteForm.noteX.value = x1
 parent.textFrame.document.MapNoteForm.noteY.value = y1
}

function MapNotesOnMap(MapNotesVis){
 parent.formFrame.document.mainForm.source.value = "Map"
 parent.formFrame.document.mainForm.cmd.value    = MapNotesVis;
 submitForm("mapFrame");
}


// **************************************************************
// USER MAP EXTENTS

// This function saves the current map extent in a cookie for retrieval later

function saveUserMapExtent(){
 var userExtentString = parent.mapFrame.iMapXmin +","+ parent.mapFrame.iMapYmin + ","+ parent.mapFrame.iMapXmax + "," + parent.mapFrame.iMapYmax
 setCookie("userExtent",userExtentString)
 alert("The current map extent has been saved.")
}

// This function zooms to the user specified extent saved in userExtent cookie

function zoomUserMapExtent(){
 cookieUserExtent = getCookie("userExtent")
 if (cookieUserExtent){
  var userExtent = cookieUserExtent.split(',')
  xmin   = parseInt(userExtent[0])
  ymin  = parseInt(userExtent[1])
  xmax    = parseInt(userExtent[2])
  ymax = parseInt(userExtent[3])
  mapZoomToExtent(xmin,ymin,xmax,ymax)
 } else {
  alert("A user extent was not previously saved.")
 }
}


/*
***************************************************************************************
Functions for VML Markup support
- Used by Measure and Identify By User Poly
***************************************************************************************
*/

var vectorX = new Array();
var vectorY = new Array();
var vectorPoints = 0;
var vectorString = "";


// This function responds to a map identify request
function mapIdentifyUserPoly(xystr){
 if (xystr.charAt(0)=="m" && xystr.charAt(1)==" ") {
   xystr = xystr.substring(2);
 }
 parent.formFrame.document.mainForm.source.value = "Map"
 parent.formFrame.document.mainForm.cmd.value = "IdUserPoly"
 parent.formFrame.document.mainForm.XYstring.value = xystr;
 parent.formFrame.document.mainForm.searchQuery.value = ""

 clearVectorPoints();
 submitForm("mapFrame");
}

// This function responds to a map markup request
function mapMarkup(xystr){
  parent.formFrame.document.mainForm.source.value = "Map"
  parent.formFrame.document.mainForm.cmd.value = "Markup"
  parent.formFrame.document.mainForm.XYstring.value = xystr;
  submitForm("mapFrame");
}

function clearVectorPoints() {
  vectorPoints = 0;
  vectorString = "";
}

function addVectorPoint(mouseX,mouseY,worldX,worldY) {
  vectorX[vectorPoints] = worldX;
  vectorY[vectorPoints] = worldY;
  vectorPoints++;
  if (vectorPoints == 1) vectorString = "m " + mouseX + "," + mouseY;
  if (vectorPoints >= 2) vectorString += "l " + mouseX + "," + mouseY;
}