﻿var ActiveSelectTool;

//Select click.
function SelectToolClick(e) {

   //Disable active select tool.
   if (ActiveSelectTool) {
      if (ActiveSelectTool.id == e.id) {
         gMap.cancelGetGeometry();
         enableMainTool();
         return;
      }
   }
   //Deactivate main toolbar item.
   disableMainTool();

   //Deactivate previous tool.
   disableCustomTools();

   //Activate tool.
   ActiveSelectTool = e;
   ActiveSelectTool.style.backgroundColor = ActiveBackgroundColor;
   ActiveSelectTool.style.borderColor = ActiveBorderColor;

   //Get user mouse clicks.
   getSelectUserInput();
}

//Process select.
function getSelectUserInput() {
   if (ActiveSelectTool.id == "SelectPoint") {
      gMap.getGeometry(ESRI.ADF.Graphics.ShapeType.Point, submitSelectOnMap, null, "Black", "Red", "Crosshair", true);

   } else if (ActiveSelectTool.id == "SelectLine") {
      gMap.getGeometry(ESRI.ADF.Graphics.ShapeType.Path, submitSelectOnMap, null, "Black", null, "Crosshair", true);

   } else if (ActiveSelectTool.id == "SelectRectangle") {
      gMap.getGeometry(ESRI.ADF.Graphics.ShapeType.Envelope, submitSelectOnMap, null, "Black", "Red", "Crosshair", true);

   } else if (ActiveSelectTool.id == "SelectPolygon") {
      gMap.getGeometry(ESRI.ADF.Graphics.ShapeType.Ring, submitSelectOnMap, null, "Black", "Red", "Crosshair", true);

   } else if (ActiveSelectTool.id == "SelectCircle") {
      gMap.getGeometry(ESRI.ADF.Graphics.ShapeType.Circle, submitSelectOnMap, null, "Black", "Red", "Crosshair", true);
   }
}

//User completed select clicks.
function submitSelectOnMap(geomObj) {
   var srCtrl = "SelectResults1_";

   var obj,hid;
   var geomStr = "";

   if (!ActiveSelectTool) {
      return;
   }
   //Geometry string.
   if (ESRI.ADF.Geometries.Point.isInstanceOfType(geomObj)) {
      geomStr = geomObj.toString(',');

   } else if (ESRI.ADF.Geometries.Envelope.isInstanceOfType(geomObj)) {
      geomStr = geomObj.get_xmin() + "," + geomObj.get_ymin() + "," + geomObj.get_xmax() + "," + geomObj.get_ymax();

   } else if (ESRI.ADF.Geometries.Polyline.isInstanceOfType(geomObj)) {
      geomStr = geomObj.getPath(0).toString('|', ',');

   } else if (ESRI.ADF.Geometries.Polygon.isInstanceOfType(geomObj)) {
      geomStr = geomObj.getRing(0).toString('|', ',');

   } else if (ESRI.ADF.Geometries.Oval.isInstanceOfType(geomObj)) {
      geomStr = geomObj.get_center().toString("|") + "|" + geomObj.get_width() + "|" + geomObj.get_height();
   }
   //Load hidden fields.
   $get(srCtrl + "TaskName_hid").value = "SelectOnMap";
   $get(srCtrl + "Command_hid").value = ActiveSelectTool.id;
   $get(srCtrl + "GeomStr_hid").value = geomStr;

   obj = $get(srCtrl + "SelectableLayers_DropDownList");
   hid = $get(srCtrl + "SelectLayer_hid");
   hid.value = obj.value;

   obj = $get(srCtrl + "SelectMode_DropDownList");
   hid = $get(srCtrl + "SelectMode_hid");
   hid.value = obj.value;

   obj = $get("Buffer_Checkbox");
   hid = $get(srCtrl + "SearchValues_hid");
   hid.value = obj.checked + "";
   var bufDis = GetBufDis(obj.checked);
   hid.value += "," + bufDis;
   if (obj.checked) {
      if (bufDis == 0) {
         return;
      }
   }
   //Submit.
   showBusy("SelectOnMap");
   obj = $get(srCtrl + "CallbackButton1");
   obj.click();
}

//Deactivate select tool.
function disableActiveSelectTool() {
   if (ActiveSelectTool) {
      ActiveSelectTool.style.backgroundColor = "";
      ActiveSelectTool.style.borderColor = "";
      ActiveSelectTool = null;
   }
}
//Tool over.
function SelectToolOver(e) {
   if (e.style.backgroundColor != ActiveBackgroundColor) {
      e.style.backgroundColor = HoverColor;
      e.style.borderColor = ActiveBorderColor;
   }
}
//Tool out.
function SelectToolOut(e) {
   if (e.style.backgroundColor != ActiveBackgroundColor) {
      e.style.backgroundColor = "";
      e.style.borderColor = "";
   }
}

