
//Set Global Variables
var event_trigger = false;                 // Don't need to re-highlight
var activeLayer = "parcels";              // Set parcels as current reporting layer
var theKey = 'NULL';

var minLat;
var minLon;
var maxLat;
var maxLon;
var delta;

if (navigator.appName == "Netscape")
{
	document.write("<APPLET CODE=\"MapGuideObserver5.class\" WIDTH=1 HEIGHT=1 NAME=\"obs\" MAYSCRIPT>");
  document.write("</APPLET>");
}
	 

function tocItem (name)
{
	this.on = new Image();
	this.on.src = "./images/" + name + "on.gif"
	this.off = new Image();
	this.off.src = "./images/" + name + "off.gif"
}

function toc_new (name)
{
	tocItem[name] = new tocItem(name);
}

function img_act (imgName)
{
  if (document.images) document[imgName].src = tocItem[imgName].on.src;
}

function img_inact (imgName)
{
  if (document.images) document [imgName].src = tocItem[imgName].off.src;
}

function load_toc (name, num)
{
	for (i = 1; i <= num; i++) toc_new(name + i);
}

if (document.images != null) load_toc ('toc', 35);




function getMap()
/*
	Purpose:	Returns the map object
	Inputs:		  None
	Returns: 	Map object 
*/
{
  if (navigator.appName == "Netscape")
  	return parent.theMap.document.map;
  else
    return parent.theMap.map;
}


function launchWin(TYPE)
//
//	Launches the Help Window and the Search Window

{
  if (TYPE == "SEARCH")
 	{
    winFind=window.open("search_frame.cfm","searchWin","toolbar=no,location=no,dependent=yes,scrollbars=yes,alwaysRaised=yes,directories=no,height=325,width=550");
    winFind.focus();
    }
	
  if (TYPE == "HELP")
	{
	winHelp = window.open("help.cfm","helpWin","toolbar=no,location=no,dependent=yes,resizable=yes,alwaysRaised=yes,scrollbars=yes,directories=no,height=590,width=510");
	 winHelp.focus();
	}       
  if (TYPE == "PROJECT")
	{
	winProj = window.open("projects/project_frame.cfm","projWin","toolbar=no,location=no,dependent=yes,resizable=yes,alwaysRaised=yes,scrollbars=yes,directories=no,height=375,width=655");
	 winProj.focus();
	}   	
if (TYPE == "CUPSPRC")
	{
	winCUP = window.open("permits/case_frame.cfm","cupWin","toolbar=no,location=no,dependent=yes,resizable=yes,alwaysRaised=yes,scrollbars=yes,directories=no,height=375,width=655");
	 winCUP.focus();
	}   	
  if (TYPE == "Choose")
	{
	winCP = window.open("chooseProj.cfm","cpWin","toolbar=no,location=no,dependent=yes,resizable=yes,alwaysRaised=yes,scrollbars=yes,directories=no,height=375,width=655");
	 winCP.focus();
	}   	
  if (TYPE == "CPrint")
	{
	winCP = window.open("choosePrnt.cfm","cpWin","toolbar=no,location=no,dependent=yes,resizable=yes,alwaysRaised=yes,scrollbars=yes,directories=no,height=375,width=655");
	 winCP.focus();
	}   		
	
}     //  end function

 function onSelectionChanged(map)
{
	if(getMap().isBusy())
		{ var wait=1;
		}
	else {        

	var mapSel = map.getSelection();
	if (mapSel != null){       // prevent possible redlining's clear redline error
			var numObjs = mapSel.getNumObjects();
			map.setAutoRefresh(true);
															//	var myLayerName = ""     //  trying
			var theKey = ""
			var type = ""
    
		if (mapSel.getNumObjects() > 0)
		  {
		    var myObjectSelection = mapSel.getMapObjectsEx(null);
			var myLayerName = myObjectSelection.item(0).getMapLayer().getName();

		var SelKeys = mapSel.getAsString(myLayerName," ' ");
		  }	    //	   //  end if
	  	           
		
	  	           			 
				//Parcel Value
				if ((myLayerName == 'parcels') ){
					var layerParcelSHP = map.getMapLayer(myLayerName);
					var theKeyCollection = mapSel.getMapObjectsEx(layerParcelSHP);
					if (theKeyCollection.isEmpty() != true)
					  {
					    var theObj = theKeyCollection.item(0);
						var theKey = theObj.getKey();
						var type = "Parcel";
					  }				  
				 }     //  end if parcels
 }      //  end else 
}                   //  end if     
}			// end onSelectionChanged function  


/*  function onMapLoaded(map)
{
    var mapname = map.getName()
//	alert("Changing loaded map to " + mapname);
}                   // end onMapLoaded function      */


function displayMap()
{
	if(getMap().isBusy())
		{ var wait=1;
		}
		else {
				var openmode=1
				var targetframe="theMap"
				var destination=eval("window.parent."+targetframe)
				var thebox=document.mapsForm
		if (openmode==1)
			destination=top.theMap.document
			destination.location=thebox.framecombo2.options[thebox.framecombo2.selectedIndex].value
		if (navigator.appName == "Netscape")
  	 		 parent.REPORT.document.location = "report_frame.cfm";
  	    else
  			 parent.REPORT.location = "report_frame.cfm";
	}     //  end else
}         // end function


function goReport()
{
	getMap().viewReportsDlg();
}


function launch_report()
//
//	Launches a Report based on the layer selected and Zooms To the Selected Objects
//
{
  var map = getMap();
 
    var selected = map.getSelection().getMapObjectsEx(null);
	
	// If collection is not empty...
    if (selected.size() > 0)
	{
		if (selected.size() == 1)
		{	
			for (i = 0; i < selected.size(); i++)
			{
		    	var layer = selected.item(i).getMapLayer().getName();
				var key = selected.item(i).getKey();
				
				switch (layer)
			
				{	
				case "parcels":
				   	 map.setAutoRefresh(false); // Prevent busy state from happening
                                                        // when zoomSelected is called
		             map.zoomSelected();  						
    	             map.viewReport("Parcel Ownership"); // This report has been configured in the MWF  (busy state begins in zoomSelected)
		             map.setAutoRefresh(true);  // Reset the autoRefresh flag 
					break;
				case "Projects":
		//			map.setAutoRefresh(false); // Prevent busy state from happening  // when zoomSelected is called
                    map.zoomSelected();  	
		            map.viewReport("Capital Improvement Projects");   // This report has been configured in the MWF
		            map.setAutoRefresh(true);  // Reset the autoRefresh flag 
					break;
				case "cup_sprc":
		//			map.setAutoRefresh(false); // Prevent busy state from happening  // when zoomSelected is called
                    map.zoomSelected();  	
		            map.viewReport("CUP Report");   // This report has been configured in the MWF
		            map.setAutoRefresh(true);  // Reset the autoRefresh flag 
					break;

				case "Streets":
					map.viewReport('Street');
					break;

				default:
					alert("There is no report for that feature.");
				
				}     // end switch
			}         // end for
			}
		}
return;          
}  

function zoomToObject(Category,KEY)
//	Invokes Zoom Goto based on an object Key passed as an argument

{
 var myMap = getMap();
	if ( (KEY != "")  )
	   	{	   
		   theKey = KEY;
		   getMap().zoomGotoLocation(Category,KEY,2500);
		   event_trigger = true;
	}
	else
		{
			alert(" You have entered a null or illegal value! \n \n Please note: The format is 0000000000 ");
    	winFind.focus();
		}
}


function zoomSelected()
{
    var map = getMap();
    var selected = map.getSelection().getMapObjectsEx(null);
    if (selected.size()>0)
        map.zoomSelected();
    else
        alert("nothing selected!");
} 

function activeProjects()

//	Invokes the "Capital Improvement Projects" Zoom Goto in the MWF

{
	    var currentMap="";
        var msg = "";  
	    var exists = false;
		var layerName = "";
		
		currentMap = getMap().getName();
		
	     var layers = map.getMapLayersEx();    
       
    	for (var i = 0; i < layers.size(); i++)          // Use size() to step through collection   
  	   {
        // Get each item in collection...
        var layer = layers.item(i); 
		layerName = layer.getName();
	  
 if (layerName =="Projects")
	 {
	 	exists = true;
     }      

	  }      //  end for loop
	
	if  (exists == false)
	{
	alert("Please select CIP Projects map in order to view projects.");
	}
   else
   {
		getMap().setAutoRefresh(false); // Prevent busy state from happening
                                                        // when zoomSelected is called
		getMap().zoomGotoLocation("Capital Improvement Projects","pro",2500);

		getMap().setAutoRefresh(true);  // Reset the autoRefresh flag 
        getMap().refresh();             // Update the display  
	}
}     //  end ActiveProjects function

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function:	onViewChanged
returns:	report if objects are selected
args:		
Sets the North arrow, gets the selected objects and returns the report
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
function onViewChanged(map)
{
	    var KEY = theKey;
		var theSelection = map.getSelection();
		var theSelectedObjects = theSelection.getMapObjectsEx(null);

if(getMap().isBusy())
			{ var wait=1;
			}               //end if     
		else {      
		
	   	 var theMap=getMap();
		 theMap.setAutoRefresh(true);             // Prevent busy state from happening		
	     var theExtents=theMap.getMapExtent(true,true);
		 var myLayer = theMap.getMapLayer("N Arrow");
		 var myRedline = theMap.getRedlineSetup();
	     var mySymbol = myRedline.getSymbolAttr();
	     mySymbol.setSymbol("direction");
		 
           if (myLayer == null )
                 myLayer = theMap.createLayer("redline", "N Arrow");
	          	 myLayer.setPriority(99.9999);
		         myLayer.setSelectability(false);

	       // create logo point object, or get it if it already exists
	           myLayer.removeAllObjects();
	           myLayer.setShowInLegend(false);
               var obj = myLayer.getMapObject("temp");

               if (obj == null)
               var obj = myLayer.createMapObject("temp", "", "");

            // grab the map extents and set the point position	
	          var logoPoint = theMap.createObject("MGPoint");
	          var Difflat=(theExtents.getMaxX()-theExtents.getMinX());
	          var Difflon=(theExtents.getMaxY()-theExtents.getMinY());
	          var positionLat=theExtents.getMaxX()-(Difflat/15);
	          var positionLon=theExtents.getMinY()+(Difflon/8);	
			  
	         logoPoint.setX(positionLat);
	         logoPoint.setY(positionLon);
			 
	        mySymbol.setWidth(Difflat/35,"M");
	        mySymbol.setHeight(Difflat/35,"M");		
			
			// add the symbol object 
            obj.addSymbolPrimitive(logoPoint, true);
		    theMap.setAutoRefresh(true);                          //    needed to keep symbol properly placed
        }                 // end else			
            theSelection.addObjectsEx(theSelectedObjects, true);
		
			if (event_trigger == false)         // Don't need to re-highlight
		    {
			   return; 
		    }			
			else              // Do need to re-highlight
			{	 
			  if (KEY != "NULL")
               	{
				var MpLt = theMap.getLat()
				var MpLn = theMap.getLon()
				var MpScl = theMap.getScale()
				var mapSel = map.getSelection();	
				
				var currentMap = "";
				var msg = "";
	//			var exists = false;
				var activeLayer = "";
				
				currentMap = getMap().getName();
				
				var layers = map.getMapLayersEx();
				
				for (var i = 0; i < layers.size(); i++)          // Use size() to step through collection   
  	  			{
      					  // Get each item in collection...
      					  var layer = layers.item(i); 
						  activeLayer = layer.getName();
	  
	  			switch (activeLayer)
				 {
 				case "Projects":
					var mapLayer = map.getMapLayer(activeLayer);	
		//				 alert("Setting active layer to " + activeLayer); 
					var mapObjects = map.createObject("MGCollection");
		            var mapObj = mapLayer.getMapObject(KEY);
					
		 if (mapObj != null)  
		     	
			mapObjects.add(mapObj);
                   	   	  
		     mapSel.clear();
		     mapSel.addObjectsEx(mapObjects, true);
		 
		     event_trigger = false;                    // Don't need to re-highlight
			 KEY = null;            
		parent.theMap.launch_report();
					break; 
		    	case "cup_sprc":
					var mapLayer = map.getMapLayer(activeLayer);	
		//				 alert("Setting active layer to " + activeLayer); 
					var mapObjects = map.createObject("MGCollection");
		            var mapObj = mapLayer.getMapObject(KEY);
					
		 if (mapObj != null)  
		     	
			mapObjects.add(mapObj);
                   	   	  
		     mapSel.clear();
		     mapSel.addObjectsEx(mapObjects, true);
		 
		     event_trigger = false;                    // Don't need to re-highlight
			 KEY = null;            

			
	       parent.theMap.launch_report();
					break; 
				case "parcels":

					var mapLayer = map.getMapLayer(activeLayer);	
					var mapObjects = map.createObject("MGCollection");
	  
					var mapObj = mapLayer.getMapObject(KEY);
			
			 if (mapObj != null)  
		     	
			mapObjects.add(mapObj);
                   	   	  
		     mapSel.clear();
		     mapSel.addObjectsEx(mapObjects, true);
		 
		     event_trigger = false;                    // Don't need to re-highlight
			 KEY = null;            
			
	       parent.theMap.launch_report();
		   map.zoomWidth(MpLt,MpLn,(MpScl*2),'FT');	    //zooms out by a factor of 2     
		   			   }                      //  end for
		 }               //  end if  KEY !=NULL              
 }   //   end else        
     }     //  end else   
}      //   end onViewChanged    

  function onViewChanging(map)      // 'thisMap' is MGMap object provided by event
{

}        // end onViewChanging function  



/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	Initializes the application by setting the Plugin Observer
	and resets the report frame
++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
function initialize()

{
   if (navigator.appName == "Netscape")
   { 
   		var observer = document.obs;
   		getMap().setViewChangedObserver(observer);
//		getMap().setSelectionChangedObserver(observer);
		getMap().setBusyStateChangedObserver(observer);

   }
   if (navigator.appName == "Netscape")
  	  parent.REPORT.document.location = "report_frame.cfm";
   else
  	  parent.REPORT.location = "report_frame.cfm";     
}    
	
function onBusyStateChanged(map, busyState)
{
    alert('map.IsBusy:  ' + busyState);	
}    // End onBusyStateChanged function
  
  
function onDoubleClickObject(mapObj)
//
//	Invokes the Owner report

{
	var map = getMap();
   	var mapSel = map.getSelection();
	var layerName = mapObj.getMapLayer().getName(); 
	switch (layerName) 
	{
	    	case "parcels" : getMap().viewReport('Parcel Ownership');   break;
		case "Projects" : getMap().viewReport('Capital Improvement Projects');  break;
		case "Streets" :  getMap().viewReport('Street'); break;  
		case "tea_parcels" :  getMap().viewReport('Parcel Ownership'); break; 
		case "uninc_LAcnty_parcels" : getMap().viewReport('Parcel Ownership');   break;	//added by mzuber on 8/11/06	
		case "CCU Locations" :  getMap().viewReport('CCU Information'); break;
		case "Public_PC" :  getMap().viewReport('Public Sewer Info'); break;
		case "Sanitation_PC" :  getMap().viewReport('County Sewer Info'); break;
		case "Public_MH" :  getMap().viewReport('Public Manhole Info'); break;
		case "Sanitation_MH" :  getMap().viewReport('County Manhole Info'); break;
		case "lfdc_current" :  getMap().viewReport('LFDC Report'); break;  //added by mzuber on 06/09/08
		case "lfdc_expired" :  getMap().viewReport('LFDC Report'); break;  //added by mzuber on 06/09/08
		case "lfdc_status" :  getMap().viewReport('LFDC Report'); break;  //added by mzuber on 06/09/08
		case "Current_AA_Permits" :  getMap().viewReport('Additional Animal Permit Report'); break;  //added by mzuber on 05/21/07
		case "Expired_AA_Permits" :  getMap().viewReport('Additional Animal Permit Report'); break;  //added by mzuber on 05/21/07
		case "Denied_AA_Permits" :  getMap().viewReport('Additional Animal Permit Report'); break;  //added by mzuber on 05/21/07
		case "ADA_ramps" :  getMap().viewReport('ADA Ramp Info'); break;  //added by mzuber on 01/29/07	
		case "Section8" :  getMap().viewReport('Section 8 Info'); break;  //added by mzuber on 04/17/07
		case "Newsrack_Compliance" : getMap().viewReport('Compliant News Rack Cluster'); break;  //added by mzuber on 5/16/2007
		case "Newsrack_Violations" : getMap().viewReport('Violating News Rack Cluster'); break;  //added by mzuber on 5/16/2007	
		case "Banner Locations" : getMap().viewReport('Banner'); break;
		case "sign_poles" : getMap().viewReport('Sign Inventory'); break;  //added by mzuber on 8/23/2007
		case "CUP" :  getMap().viewReport('CUP Report'); break;
		case "SPR" :  getMap().viewReport('SPR Report'); break;
		case "ABC License"  :  getMap().viewReport('ABC License'); break;
		case "cop_hotels" : getMap().viewReport('Hotel Motel'); break;  //added by mzuber on 3/24/2008
		case "Water_Purveyor" : getMap().viewReport('Water Purveyor'); break;
		case "Public Facility" : getMap().viewReport('Public Building Plans'); break;
		case "Facility Area" : getMap().viewReport('Public Facility Site Plans'); break;
		case "line 1 stops" : getMap().viewReport('AVTA Bus Stop Amenities'); break;  //added by jdeyo on 8/26/2010
		case "line 2 stops" : getMap().viewReport('AVTA Bus Stop Amenities'); break;  //added by jdeyo on 8/26/2010
		case "line 3 stops" : getMap().viewReport('AVTA Bus Stop Amenities'); break;  //added by jdeyo on 8/26/2010
		case "line 4 stops" : getMap().viewReport('AVTA Bus Stop Amenities'); break;  //added by jdeyo on 8/26/2010
		case "line 5 stops" : getMap().viewReport('AVTA Bus Stop Amenities'); break;  //added by jdeyo on 8/26/2010
		case "line 6 stops" : getMap().viewReport('AVTA Bus Stop Amenities'); break;  //added by jdeyo on 8/26/2010
		case "line 7 stops" : getMap().viewReport('AVTA Bus Stop Amenities'); break;  //added by jdeyo on 8/26/2010
		case "line 8 stops" : getMap().viewReport('AVTA Bus Stop Amenities'); break;  //added by jdeyo on 8/26/2010
		case "line 9 stops" : getMap().viewReport('AVTA Bus Stop Amenities'); break;  //added by jdeyo on 8/26/2010
		case "line 11 stops" : getMap().viewReport('AVTA Bus Stop Amenities'); break;  //added by jdeyo on 8/26/2010
		case "line 12 stops" : getMap().viewReport('AVTA Bus Stop Amenities'); break;  //added by jdeyo on 8/26/2010
		case "line LLAExp stops" : getMap().viewReport('AVTA Bus Stop Amenities'); break;  //added by jdeyo on 8/26/2010
		
		default : 
		
	      	break; 
	}
}             //  end function

/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function:	checkSelection
returns:	none
args:		none
+++
Checks selected items in Map. If items are selected the 'de-select' button
on the toolbar is enabled, otherwise it is disabled.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

function checkSelection()
{
	var MyMapSel = getMap().getSelection();
	
	if (MyMapSel.getNumObjects() >= 1)
        return 0;     
	else
		return 1;
}


function staticBuffer(size)                              //  set buffer paramaters
{
	if (checkSelection() == 0)
	{
	  var MyBuffer = getMap().getBufferSetup();
	  MyBuffer.setDistance(size);
	  MyBuffer.setUnit("FT");
	  MyBuffer.setLayerName("Buffer");
	  var MyEdge = MyBuffer.getEdgeAttr();
	  var MyFill = MyBuffer.getFillAttr();
	  MyFill.setBackMode("Transparent");
	  MyFill.setHatchPattern("Diagonal135");
	  MyFill.setColor(10);
	  MyFill.setStyle("Hatch");
	  MyEdge.setColor(3);
	  MyEdge.setThickness(3);
	  getMap().viewBuffer();
	  }
}	
function userDefBuffer()
{
	var map = getMap(); 

	if (checkSelection() == 0)
	{
		// Prompt user for buffer distance
		var userBufSize = parseFloat(newPrompt("Enter Buffer distance in feet:",""));
		if (!(isNaN(userBufSize)))
		staticBuffer(userBufSize);
		
		var map = getMap();
		var mgSel = map.getSelection();
		setTimeout("buffselect()",1000)
		map.refresh();
	}
}
function buffselect()
{

 //layer to select from
   var layer_name = "parcels"
   var layer_to_select = map.getMapLayer(layer_name)
   //Get selected object to create buffer
 
   sel = map.getSelection();
   
       //Get the buffer objects 
    objs = map.getMapLayer("Buffer").getMapObjectsEx()
    //nBufs = objs.size();

    //select the "buffer" objects
    sel.addObjectsEx(objs,false)
   
    //we do this because viewBuffer() does not work in busy state
   map.setAutoRefresh(false);

    //Viewer shouldn't be busy any more. However, just check!
    if(map.isBusy())
		alert("Map busy. Wait!")
    
    //clear selected object
    sel.clear();    
    
    //Get the buffer objects 
    objs = map.getMapLayer("Buffer").getMapObjectsEx()
    //nBufs = objs.size();

    //select the "buffer" objects
    sel.addObjectsEx(objs,false)
    
    //select objects within the selection
     coll = map.createObject("MGCollection")
     coll.add(layer_to_select)  //we are interested here in only one layer (Parcels)

   //Choose the intersection selection mode for selectWithin()
    map.setSelectionMode("Intersection")

     map.selectWithin(coll)

     map.viewReport('Parcel Ownership');   

}             // end function


function clearBuffer()
{
			var map = getMap();
			var mgSel = map.getSelection();
		   map.setAutoRefresh(true);             	
	       map.removeMapLayer("Buffer"); 
		   if (mgSel != null)
		         mgSel.clear();
	//	   map.getSelection().clear();

			map.refresh();
			
  			if (navigator.appName == "Netscape")
  	  	    parent.REPORT.document.location = "report_frame.cfm";
   			else
  	  		parent.REPORT.location = "report_frame.cfm";

}     //  end clearBuffer function


/*
function:	clearSelected
returns:	none
args:		none
+++
Removes all of the map features from the selection and updates the display.
This method is the equivalent of the Select > Clear menu command in the user
interface.
*/

function clearselected()
{
	var map = getMap();
 	var sel = map.getSelection();
    sel.clear();

}

function print(title)
{
		var map = getMap();
		var setup = map.getPageSetup();
		setup.setTitle(title);
		map.pageSetupDlg();
		map.printDlg()
}	

function CopyMap()
{
	var theMap = getMap();
	if(!theMap.isBusy())
	theMap.copyMap();
	alert ("Current map view has been copied to your Clipboard");
}

function measureDistance()
 {
    theMap=getMap();
    myscale=getMap().getScale();
    getMap().viewDistance("M"); 

 }

function setMap(mapName)
{
	var myURL = "http://gis.cityofpalmdale.org/maps/" + mapName + ".mwf?Scale=" + showMapScale() + "&Lon=" + showMapLon() + "&Lat=" + showMapLat() + "&ToolBar=Off" + "&Ext=.mwf"
		
	var myMap = getMap()
	myMap.setURL(myURL);
	
}

function showMapWidth()
{
    var width = getMap().getWidth();
	return width;
}

function showMapScale()
{
    var scale = getMap().getScale();
	return scale;
}

function showMapLat()
{
    var lat = getMap().getLat();
	return lat;
}

function showMapLon()
{
    var lon = getMap().getLon();
	return lon;
}

function showZoomScale()
{
    getMap().zoomScaleDlg();
}


IE4 = document.all;

function newPrompt(title,mess,def) {
     retVal = (IE4) ? makeInputBox("Buffer Radius Request","Please enter a radius in feet:","") : prompt("Please enter a radius","");
   return retVal;
}

function IEBox(title,mess,icon,buts,defbut,mods) {
   retVal = (IE4) ? makeMsgBox("Buffer Radius Request","Please enter a radius in feet",2,1,1,1) : null;
   return retVal;
}   


