
//Layer function in the topFrame
//The dropdown menu in the topFrame list the layers available
//The submit button in the topFrame triggers this function
function viewLayers(layer)
{
 var map = top.mainFrame.getMap();
 
 if (map.isBusy() == false) {

   //var CurrentScale = map.getScale();
   var mapLayer = map.getMapLayer(layer);
  if (layer == 'EAGLE_BUFF_1') {
   var mapLayer2 = map.getMapLayer('EAGLE_BUFF_2');
  }
   //var mapGroup = mapLayer.getMapLayerGroup();
   
  // if (CurrentScale < 15000) 
 //{   
  if (mapLayer.isVisible() == false) 
   {
   // if(mapGroup) { mapGroup.setShowInLegend(true); }  	
	mapLayer.setShowInLegend(true);
	mapLayer.setVisibility(true);
	mapLayer.setRebuild(true);
	top.mainFrame.listSel = layer;
     if (mapLayer2){
      mapLayer2.setShowInLegend(true);
	  mapLayer2.setVisibility(true);
	  mapLayer2.setRebuild(true);
     }
	map.refresh();
   }
 else
	mapLayer.setVisibility(false);
	top.mainFrame.listSel = layer;
 if (mapLayer2){
	  mapLayer2.setVisibility(false);
     }
	map.refresh(); 
// }
 //else
  // alert("Please zoom into scale less than 15,000 feet. Thanks.")
 }
 else 
   alert("Please Wait - map not fully loaded");
}

// Deleting of Redline Objects
function delObjs(){
    myMap = parent.mainFrame.getMap();
 	redlineLayer = myMap.getMapLayer("Redlines");
	if(redlineLayer != null){
 	selection = myMap.getSelection();
 	objects = selection.getMapObjectsEx("Redlines");
 	if (objects.size() > 0){
 		redlineLayer.removeObjects(objects);
 	}
 	else
 		alert("Select redlining objects first.");
  	}
  	else
		alert("There are no redlining objects.");  	 
 }
//* End of deleting Redline objects

// Global Select Object function - pass in key and layername of object

function select_Object(KEY,layerName)
{
   // Get MG Map Object
   var map = top.mainFrame.getMap();

   var mapSel = map.getSelection();
				
   var mapObjects = map.createObject("MGCollection");

   var mapLayer = map.getMapLayer(layerName);

	if (mapLayer != null)
	{
		if (KEY != "NULL")
	   	{
			   var mapObj = mapLayer.getMapObject(KEY);
				
			   if (mapObj != null)
				{  
			   mapObjects.add(mapObj);
				}
			   mapSel.clear();
			   mapSel.addObjectsEx(mapObjects, true);
			   top.mainFrame.zoomObject = false;
			   if (top.mainFrame.buffBool == true) {
			     top.mainFrame.notify();
			   }
		}
	}
	else
				alert('Map Layer: ' + layerName + '  ... not found.');

}

//* End of Select Object function

// Toggle Legend On
function LegendOn() {
 
 var myMap = parent.mainFrame.getMap();
 if (myMap.isBusy() == false)
   {
      myMap.LayersViewWidth = 225;
      myMap.refresh();	
   }
        else
		   alert("Wait - map not fully loaded"); 
 }

//*End of Toggle Legend On 
 
// Toggle Legend Off
 function LegendOff() {
 
 var myMap = parent.mainFrame.getMap();
 if (myMap.isBusy() == false)
   {
      myMap.LayersViewWidth = 1;
      myMap.refresh();	
   }
        else
		   alert("Wait - map not fully loaded");  
 }
 
 //*End of Toggle Legend Off
 
 
 // Incremental Panning function
function Pan(direction,pctchange)
{
   var map = top.mainFrame.getMap();
   
   // Get Width or Height in meters
   var delta;
   if (direction == "Left" | direction == "Right")
       delta = map.getWidth("FT");
   if (direction == "Up" | direction == "Down")
       delta = pctchange * map.getWidth("FT");
   delta = pctchange * delta;

   // Compute center point of map in Mapping Coordinate System (MCS)
   var xyPt = map.lonLatToMcs(map.getLon(), map.getLat());

   // Convert delta from Meters to MCS units.
   var MCStoFeet = map.getMCSScaleFactor();
   delta = delta / MCStoFeet;

   // Adjust by width / height of the map
    if (direction == "Left") 
    {
        xyPt.setX(xyPt.getX() - delta)
    }

    if (direction == "Right") 
    {
        xyPt.setX(xyPt.getX() + delta)
    }

    if (direction == "Up") 
    {
        xyPt.setY(xyPt.getY() + delta)
    }

    if (direction == "Down") 
    {
        xyPt.setY(xyPt.getY() - delta)
    }

   // Convert back to Lat/lon Coordinate System
   xyPt = map.mcsToLonLat(xyPt.getX(), xyPt.getY());

   // Zoom to the new location
   map.zoomScale(xyPt.getY(), xyPt.getX(), map.getScale());
}

//* End of Incremental Panning function


// Distance tool
function measureDistance()
{
	var map = parent.mainFrame.getMap();
    	if (!map.isBusy()) {
		map.viewDistance("");
	} else {
		alert("Wait - map not fully loaded");
	}
}
//* End of Distance tool
/////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
// Get Calculated Area
function getArea()
{
var map = parent.mainFrame.getMap();
var selected = map.getSelection().getMapObjectsEx(null);

	  	// If collection is empty, display alert...
		   if (selected.size() == 0)
		   {
		   alert("You must select a feature to report on.");
		   return;
		   }  
		   if (selected.size() > 1)
		   {
		   alert("Please select one feature to report on.");
		   return;
		   }  	  
var cntObjects = selected.size();		    
var i;
for (i = 0; i < cntObjects; i++)
{
   var obj = selected.item(i);
   var polySqft2 = obj.getArea();
   var polyArea = polySqft2/43560; 
}
alert("The calulated area of the polygon is: "+ polyArea+"\n\nThe calculated square footage is: " + polySqft2);
}
//* End of Get Calculated Area
///////////////////////////////////////////////////////////////////////////////
// Copy Map to Clipboard function
function copyMap()
{
 var map = parent.mainFrame.getMap();
 map.copyMap();
}
//* End of Copy Map to Clipboard function

// View Mapguide Preferences
function viewPrefs()
{
parent.mainFrame.getMap().preferencesDlg();
}
//* End of View Mapguide Preferences

// Clear selections
function clearSel()
{
//  Called to unselect all objects on the map.  Also Unselects the
//  items in the list box
   var map = parent.mainFrame.getMap();
   var mapSel = map.getSelection();
   // Clear selections on the map
   mapSel.clear();  
}
//* End of Clear selections 

// Change to select mode
function sMode()
{   
    var map = parent.mainFrame.getMap();
	map.selectMode()
}
//* End of Change to select mode

// Reload Map
 function reloadMap() {
   parent.mainFrame.firstView = true;
   var map = parent.mainFrame.getMap();
   var lat = map.getLat();
   var lon = map.getLon();
   var width = map.getWidth("M");
   var scale = map.getScale();
   
   map.setUrl("./mwf/hurricane.mwf?LAT="+lat+"&LON="+lon+"&MAPWIDTH="+width+"&MAPSCALE="+scale);
 }

// *End of Reload Map

// Toggle Legend Function
function setLegend(turnOn) {
	    parent.mainFrame.getMap().LayersViewWidth = turnOn;
 }

function toggleLegend() {
 
 var myMap = parent.mainFrame.getMap();
 
   if (myMap.isBusy() == false)
   {
		var showinmap = myMap.LayersViewWidth;
		if (showinmap == 1)
		    turnOn = 225;
		else 
		    turnOn = 1;
		setLegend(turnOn);			
		myMap.refresh();	
    }
        else
		   alert("The viewer is busy.  Please try again in a few seconds");
   }
//* End of Toggle Legend Function

// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
// Function: stopMap()
// Description: Stops Map form downloading or redrawing
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
function mapState()
{
  var map = parent.mainFrame.getMap();
  
	if (map.isBusy())
		{ 
		  map.stop(); 
	}
	 else
		{ 
		  map.refresh(); 
	}
 }

// Zoom by scale function
function mnuzoomScale() {
  parent.mainFrame.getMap().zoomScaleDlg();
}


        // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
        // Function: add_point()
        // Description: Generic script to add new point data
        // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
          function add_point(msg,loc,funcid) 
          {   
            var map = top.mainFrame.getMap();
            var CurrentScale = map.getScale();
            if (CurrentScale < 2001) 
            { 
              if (msg != null)  
              {
               var line0=msg;
               if (window.confirm(line0) ) 
               {
                top.mainFrame.ptType = "P";
                //top.mainFrame.redlineloc = loc+"?TAB="+tab;
                top.mainFrame.redlineID = funcid;
                top.mainFrame.redlineloc = loc;
                map.digitizePoint();
               }
              }
            }
            else
                alert("Please zoom to scale 2,000 feet or less to ensure accuracy of point.");
         }
          // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
        // Function: move_point()
        // Description: Generic script to move point data
        // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
          function move_point(msg,loc,funcid) 
          {   
            var map = top.mainFrame.getMap();
            var CurrentScale = map.getScale();
            var selected = map.getSelection().getMapObjectsEx(null);
	      // If collection is empty, display alert...
		   if (selected.size() == 0)
		   {
		   alert("You must first select an object.");
		   return;
		   }  
		 // If collection contains more than one record, display alert...
		   if (selected.size() > 1)
		   {
		   alert("Please select one object at a time.");
		   return;
		   }  	 
            if (CurrentScale < 2001) 
            { 
              if (msg != null)  
              {
               var line0=msg;
               if (window.confirm(line0) ) 
               {
                top.mainFrame.ptType = "MV";
                //top.mainFrame.redlineloc = loc+"?TAB="+tab;
                top.mainFrame.redlineID = funcid;
                top.mainFrame.redlineloc = loc;
                map.digitizePoint();
               }
              }
            }
            else
                alert("Please zoom to scale 2,000 feet or less to ensure accuracy of point.");
         }
    // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
        // Function: update_point()
        // Description: Generic script to update new point data
        // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
         function update_point(msg,loc,tab) {  
          var map = top.mainFrame.getMap();
          var selected = map.getSelection().getMapObjectsEx(null);
	      // If collection is empty, display alert...
		   if (selected.size() == 0)
		   {
		   alert("You must first select an object.");
		   return;
		   }  
		 // If collection contains more than one record, display alert...
		   if (selected.size() > 1)
		   {
		   alert("Please select one object at a time.");
		   return;
		   }  	  
         var obj = selected.item(0);
         var key = obj.getKey();		
         
         openWin(loc+"?ID="+key,540,680,"scrollbars,resizable");
         //window.location="../web/blank.cfm";
      }    
      
      // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
        // Function: add_polygon()
        // Description: Generic script to add new polygon data
        // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
          function add_polygon(msg,loc,funcid) 
          {   
            // alert(loc);
           var map = top.mainFrame.getMap();
           var CurrentScale = map.getScale();
           if (CurrentScale < 10001) 
           {      
            if (msg != null)  
            {  
               if (funcid == 33) 
               {
                 var rasterLayer = map.getMapLayer("COLOR_AERIALS");
                 //var boolRaster = rasterLayer.getVisibilty();
                 if (rasterLayer.isVisible()) 
                 {  
                   var line0=msg;
                   if (window.confirm(line0) ) 
                   {
                    top.mainFrame.boolNewPoly = true;
                    top.mainFrame.redlineID = funcid;
                    top.mainFrame.redlineloc = loc;
                    map.digitizePolygon();
                   }
                 }
                 else 
                   alert("Please turn on aerial photography when adding polygon.");
               }
               else {
                    var line0=msg;
                    if (window.confirm(line0) ) 
                    {
                     top.mainFrame.boolNewPoly = true;
                     top.mainFrame.redlineID = funcid;
                     top.mainFrame.redlineloc = loc;
                     map.digitizePolygon();
                    }
               }         
             }
           }
           else
                alert("Please zoom to scale 10,000 feet or less to ensure accuracy of polygon.");
         }
         
         // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
        // Function: add_polyline()
        // Description: Generic script to add new polyline data
        // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
          function add_polyline(msg,loc,funcid) 
          {   
            var map = top.mainFrame.getMap();
            var CurrentScale = map.getScale();
            if (CurrentScale < 10001) 
            {
             if (msg != null)  
             {
              var line0=msg;
              if (window.confirm(line0) ) 
              { 
                //top.mainFrame.ptType = "P";
                //top.mainFrame.redlineloc = loc+"?TAB="+tab;
                //top.mainFrame.boolNewPoly = true;
                top.mainFrame.redlineID = funcid;
                top.mainFrame.redlineloc = loc;
                map.digitizePolyline();
              }
             }
            }
            else
                alert("Please zoom to scale 10,000 feet or less to ensure accuracy of line.");
         }
           
function drawLayer(layer) {
 var map = top.mainFrame.getMap();
 var mapLayer = map.getMapLayer(layer);
 if (mapLayer.isVisible() == false) {
   mapLayer.setShowInLegend(true);
   mapLayer.setVisibility(true);
   mapLayer.setRebuild(true);
   top.mainFrame.listSel = layer;
 }
 else {
   //mapLayer.setShowInLegend(false);
   mapLayer.setVisibility(false);
   //mapLayer.setRebuild(false);
   top.mainFrame.listSel = layer;
 }
 map.refresh();
}


function viewBuff(){
	var map = top.mainFrame.getMap();
	//var mMode = map.getSelectionMode();
	map.setSelectionMode("Intersection");
	// retrieves the selected objects
	var MySelection = map.getSelection();
	var NumObjects;
	// retrieves the number of objects that were selected
	NumObjects = MySelection.getNumObjects();
	if (NumObjects > 0){
	  var BuffLayer = map.viewBufferDlg();
	}
	else{
		alert("Must select a feature to buffer first.");
  }
    //map.setSelectionMode(mMode);
	map.refresh();
}

//This function makes sure that the map will fit in the entire viewing area
//Called by the toggleLegend() function as well.
function setMapWidth() {
   
     var legendStat = top.mainFrame.getMap().LayersViewWidth;
	 var currentScale = top.mainFrame.getMap().getScale();
	 
	 if (legendStat == 200) {
	     top.mainFrame.getMap().setWidth(45.9,"MI");
		 top.mainFrame.getMap().refresh();
	 }
	 else 
	     top.mainFrame.getMap().zoomOut();
}

  function mapResults() {
	     var map = opener.opener.top.mainFrame.getMap();
	     map.setAutoRefresh(false);
	     //map.removeMapLayer(layer);
	     
	     //var url="mgDAT.aspx?LAYER="+layer+"&SQL="+sql+"&LABEL="+label;
	      
	     var url="mgDAT.aspx";
	     
	     map.addMapLayer(url);
	     map.refresh();
	     map.setAutoRefresh(true);  
	  }
	 
 // Aerials layer viewing function
function viewAerials(layer) {
var map = top.mainFrame.getMap();
if (map.isBusy() == false) {
   var CurrentScale = map.getScale();
   var rLayer = map.getMapLayer(layer);
   //var rLayer2 = getMap().getMapLayer('2000 Color Aerials - Full');
   var rGroup = rLayer.getMapLayerGroup();
   
 if (CurrentScale < 8001) 
 {
   if (rLayer.isVisible() == false)
   {
    if(rGroup) { rGroup.setShowInLegend(true); }
	//rLayer.setShowInLegend(false);
	rLayer.setVisibility(true);
	rLayer.setRebuild(true);
	//rLayer2.setVisibility(true);
	//rLayer2.setRebuild(true);
	map.refresh();
   }
	else 
	//rLayer.setShowInLegend(false);
	rLayer.setVisibility(false);
	//rLayer2.setVisibility(false);
	map.refresh();
 }
 else
   alert("Please zoom to scale to 8,000 feet or less to view aerials. Thanks.")
 }
 else 
   alert("Wait - map not fully loaded");
}
// End of Aerial function


//Function that erases layers
function erase_Redline(layerType)
{  
    var map = top.mainFrame.getMap();
    var mapLayer = map.getMapLayer(layerType);
    if (mapLayer) {
	   map.removeMapLayer(layerType);
	}
	//map.refresh();
}


//Notification function
function notify(){
	//alert("Juan
	var map = top.mainFrame.getMap();
	var dist = top.mainFrame.buffDist;
	var MySelection = map.getSelection();
	map.setAutoRefresh(false);
	erase_Redline("Analysis Buffer");
	var NumObjects;
	// retrieves the number of objects that were selected
	NumObjects = MySelection.getNumObjects();
	if (NumObjects > 0){
	    var buffSet = map.getBufferSetup();
	    buffSet.setUnit("ft");
	    buffSet.setDistance(dist);
	    buffSet.setLayerName("Analysis Buffer");
	    buffSet.setOneObject(true);
	    var edge = buffSet.getEdgeAttr();
	    edge.setColor(5);
	    edge.setStyle("Solid");
	    edge.setThickness(3);
	    edge.setVisible(true);
	    map.viewBuffer();
	    top.mainFrame.buffDist = 0;
	    top.mainFrame.buffBool = false;
	    var MyBuffLayer = map.getMapLayer("Analysis Buffer");
	    MySelection.clear();
		var BuffObjs = MyBuffLayer.getMapObjectsEx();
		MySelection.addObjectsEx(BuffObjs, true);
		map.zoomSelected();
	}
	else {
		alert("Must select a feature to buffer first.");
    }
    map.setAutoRefresh(true);
    MySelection.clear();
	map.refresh();
}
//End of notify

//load Non-Mapped Offender List
function loadList() {
    var map = top.mainFrame.getMap();
	var newscale = map.GetScale();
	    
	top.reportFrame.location="offenderList.aspx?TYPE=NOMAP";

    top.mainFrame.newScale = newscale
	  if (top.frames.MyFrames2.cols != "60%,40%") { 
	     top.mainFrame.expandbool = true;
		 top.frames.SetContentFrameToHalf();
	    }
parent.frames.SetContentFrameToHalf();
}


function zoom2MH(id) {
			var map = top.mainFrame.getMap();
			var mapLayer = map.getMapLayer("MOBILEHOME");
			if (mapLayer != null)
			{
		       var sqlstr = "FACILITY_ID = " + id; 
			   mapLayer.setSQLWhere(sqlstr);
			   mapLayer.setVisibility(true);
			   map.zoomGotoLocation("MOBILEHOME",id,3500);
			}
			else 
			{
				alert('Map Layer: MOBILEHOME ... not found.');
			}
}

function zoomShelter(id) {
   var map = opener.top.mainFrame.getMap();
   map.zoomGotoLocation("SHELTER",id,3500);
}

function zoom2Parcel(key,lat,lon,pid) 
{
	var map = opener.top.mainFrame.getMap();
	var sqlstr = "PRCL_GID =" + key;
	var mapLayer = map.getMapLayer("PARCEL_CENTS");
	mapLayer.setSQLWhere(sqlstr);
	mapLayer.setRebuild(true);
	mapLayer.setShowInLegend(false);
	mapLayer.setVisibility(true);
	map.zoomGotoLocation('Parcels',key,2500);
	window.location="parcel_report.aspx?id="+key+"&pid="+pid;
}


