﻿function ActiveXNotInstalled(isInternalMap)
{
    //alert(isInternalMap);
	if (isInternalMap==true)
	{
	    alert("To use the GIS Viewer, you must download an ActiveX Control.\n\nClick 'OK' to view the download instructions.\n\nNOTE: Once you've downloaded the plug-in, you'll have to refresh your browser to view the map.");
	    open("http://map1/Online_Help/Online_viewer.htm?#Active_X.htm");
	}
	else
	{
	    alert("To use the GPGIS Viewer, you must download an ActiveX Control.\n\nClick 'OK' to view the download instructions.\n\nNOTE: Once you've downloaded the plug-in, you'll have to refresh your browser to view the map.");
	    open("http://www.gpgis.com/OnlineHelp_Secure/Online_viewer.htm?#Active_X.htm");
	}
}

function setAllGroupedLayersCollapseStatusToTrue(map,GroupedLayersCollapseStatus)
{
// set all grouped layers to be collapsed
	var groupedLayers = map.getMapLayerGroups();
	var numberOfGroupedLayers = groupedLayers.size();
	for (var i = 0; i < numberOfGroupedLayers; i++)
	{
		GroupedLayersCollapseStatus[i] = 0;
	}
	return GroupedLayersCollapseStatus;	
}

function setAllGroupedLayersLegendStatusToFalse(map,GroupedLayersLegendStatus)
{
// set variable indicating that all grouped layers are 'not visible' in the legend (scale is too high)
	var groupedLayers = map.getMapLayerGroups();
	var numberOfGroupedLayers = groupedLayers.size();
	for (var i = 0; i < numberOfGroupedLayers; i++)
	{
		GroupedLayersLegendStatus[i] = 1;
	}
	return GroupedLayersLegendStatus;	
}

function setIsInitialMapLoadToFalse(map)
{
	if (map.isBusy() || map == null)
	{
		setTimeout("setIsInitialMapLoadToFalse();", 500);
		return;
	}
	else
	{
		isInitialMapLoad = false;
		return isInitialMapLoad;
	}
}

function setGroupedLayersCollapseStatus(map,GroupedLayersCollapseStatus,GroupedLayersLegendStatus)
{
// set the collapse status taking into account which layer groups the user has collapsed and expanded
// if the layer group hasn't shown up in the legend yet, the legend status array makes sure it will be collapsed when it does show up
	var groupedLayers = map.getMapLayerGroups();
	var mapScale = map.getScale();
	var numberOfGroupedLayers = groupedLayers.size();
	
	// loop through each of the Grouped Layers
	for (var i = 0; i < numberOfGroupedLayers; i++)
	{
		var groupedLayer = groupedLayers.item(i);
		var groupedLayerName = groupedLayer.name;
		var layersInGroupedLayer = groupedLayer.getMapLayers();
		var numberOfLayersInGroupedLayer = layersInGroupedLayer.size();
		// find the Maximum Scale of each Grouped Layer
		var maxScaleInGroupedLayer = 0;
		// loop through each of the Layers in the Grouped Layer
		for (var j = 0; j < numberOfLayersInGroupedLayer; j++)
		{
			var layer = layersInGroupedLayer.item(j);
			maxScaleInGroupedLayer = getMaxScaleForLayer(layer,maxScaleInGroupedLayer);
		}
		// determine if the Layer Group shows up in the Legend at this map Scale
		if (mapScale <= maxScaleInGroupedLayer)
		{
			if (GroupedLayersLegendStatus[i] == 1)
			{
				// if the Layer Group does show up in the Legend but this is the first time it has shown up,
				// set the grouped layer to be collapsed
				GroupedLayersCollapseStatus[i] = 0;		
				GroupedLayersLegendStatus[i] = 0;
			}
			else
			{
				// if it's not the first time a Grouped Layer has shown up in the Legend
				// set the Grouped Layer collapse status according to what the user has set it to
				if (groupedLayer.isCollapsed() == true)
				{
					GroupedLayersCollapseStatus[i] = 0;		
				}
				else
				{
					GroupedLayersCollapseStatus[i] = 1;
				}
			}
		}
	}
	return [GroupedLayersCollapseStatus,GroupedLayersLegendStatus];
}

function getMaxScaleForLayer(layer,maxScaleInGroupedLayer)
{
	var layerStyles = layer.getMapLayerStyles();
	var numberOfLayerStylesInLayer = layerStyles.size();
	var maxScaleOfLayer = 0;
	// loop through each of the Layer Styles in the Layer
	for (var k = 0; k < numberOfLayerStylesInLayer; k++)
	{
		var maxScaleOfLayerStyle = layerStyles.item(k).getMaxDisplayRange();
		// determine the max scale in the layer
		if (maxScaleOfLayerStyle > maxScaleOfLayer)
		{
			maxScaleOfLayer = maxScaleOfLayerStyle;
		}
	}
	// determine if the max layer scale is greater than the max grouped layer scale
	if (maxScaleOfLayer > maxScaleInGroupedLayer)
	{
		maxScaleInGroupedLayer = maxScaleOfLayer;
	}
	return maxScaleInGroupedLayer;				
}

function collapseLayerGroup(map, layerGroupName)
{
    var layerGroup = map.getMapLayerGroup(layerGroupName);
    if(layerGroup != null)
    {
	    if(layerGroup.isCollapsed() != true)
		{
		    layerGroup.collapse();
		}
	}
}

function collapseGroupedLayers(map,GroupedLayersCollapseStatus)
{
	var groupedLayers = map.getMapLayerGroups();
	var mapScale = map.getScale();
	var numberOfGroupedLayers = groupedLayers.size();
	// loop through each of the Grouped Layers
	for (var i = 0; i < numberOfGroupedLayers; i++)
	{
		var groupedLayer = groupedLayers.item(i);
		var groupedLayerName = groupedLayer.name;		
		if (GroupedLayersCollapseStatus[i] == 0)
		{
			groupedLayer.collapse();
		}
	}
}

function setSecurity()
{            
    var theMap = getMap();
    if(theMap.isBusy() || theMap == null)
    {
        setTimeout("setSecurity()", 500);
    }
    else
    {	
        var layer = theMap.getMapLayer(countyLanduseLayerName);
        if (layer == null)
        {
            return myMap;
        }
        else
	    {
            layer.setShowInlegend(!layer.setShowInlegend(true));
            theMap.refresh();
        }
    }
}

function setPremiseNumberLayerSecurity() {
    var theMap = getMap();
    if (theMap.isBusy() || theMap == null) {
        setTimeout("setPremiseNumberLayerSecurity()", 500);
    }
    else {
        var layer = theMap.getMapLayer(PremiseNumberLayerName);
        if (layer == null) {
            return myMap;
        }
        else {
            layer.setShowInlegend(!layer.setShowInlegend(true));
            theMap.refresh();
        }
    }
}           

//function setASTSecurity()
//{            
//    var theMap = getMap();
//    if(theMap.isBusy() || theMap == null)
//    {
//        setTimeout("setSecurity()", 200);
//    }
//    else
//    {	
//        var layer = theMap.getMapLayer(ASTMissingImagesLayerName);
//        if (layer == null)
//        {
//            return myMap;
//        }
//        else
//	    {
//            layer.setShowInlegend(!layer.setShowInlegend(true));
//            theMap.refresh();
//        }
//    } 
//}           

function getMap()
{
    if (navigator.appName == "Netscape")
    {
        return document.myMap;
    }
    else
    {
        return myMap;
    }
}

function openFileOptionsWindow()
{
    if(fileOptionsWindow == null || fileOptionsWindow.closed == true)
    {
        var fileOptionsWindowProperties = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=400,height=400";
	    fileOptionsWindow = open("FileOptions.aspx", "fileOptions", fileOptionsWindowProperties);
	}
	else
	{
	    fileOptionsWindow.focus();
	}
}

function save_map(theMapName)
{
    var fName = theMapName;
    var password = "";
    if(getMap().saveMWF(fName, password))
    {
        document.cookie = "saveLocal=True; path=/; expires=Tuesday 23-Sep-10 23:00:00 GMT";
        alert("Map has been saved to " + theMapName);
    }
    else
    {
        alert("Unable to save map to '" + theMapName + "'");
    }
}

function openReport(theURL)
{
    //this is used by the 'double-click' reports in the MWFs
    var reportsWindowProperties = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,dependent=no,alwaysraised";
    var reportWindow = open(theURL, "report", reportsWindowProperties); 

    if (reportWindow != null || reportWindow.closed == false)
    {
        reportWindow.focus(theURL);
    }
}

function openRoadNetworkReport(theURL) {
    //this is used by the 'double-click' reports in the MWFs
    var reportsWindowProperties = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,dependent=no,alwaysraised";
    var reportWindow = open(theURL, "report", reportsWindowProperties);
        reportWindow.focus();   
   
}

//function openTaggingReport(theURL)
//{
//    //this is used by the 'double-click' reports with tagging control in the MWFs
//    var reportsWindowProperties = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,dependent=no,alwaysraised";
//    var reportWindow = open(theURL, "report", reportsWindowProperties); 

//    if (reportWindow != null || reportWindow.closed == false)
//    {
//        reportWindow.focus(theURL);
//    }
//}

function pageFocus()
{
    window.focus();
}

function openDigitalLibrary(theURL)
{
    theURL = theURL.replace(/\+/g,'%2B')
    openReport(theURL);
}

function openCustomReportWindow()
{
    if(customReportWindow == null || customReportWindow.closed == true)
    {
        var customReportWindowProperties = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=400,height=400";
	    customReportWindow = open("custom_reports.aspx", "customReport", customReportWindowProperties);
	}
	else
	{
	    customReportWindow.focus();
	}
}

function openLabelWindow()
{
    if(labelWindow == null || labelWindow.closed == true)
    {
        var labelWindowProperties = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=350,height=500";
        labelWindow = open("LabelTK.aspx", "labels", labelWindowProperties); 
    }
    else
    {
        labelWindow.focus();
    }
}

function openAquaMapLabelWindow()
{
    if(aquaMapLabelWindow == null || aquaMapLabelWindow.closed == true)
    {
        var aquaMapLabelWindowProperties = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=350,height=500";
        aquaMapLabelWindow = open("../../AquaMap/web/CB_Labels.aspx", "aquamapLabels", aquaMapLabelWindowProperties); 
    }
    else
    {
        aquaMapLabelWindow.focus();
    }
}

function openAquaMapUnmatchedAddressWindow()
{
    if(aquaMapUnmatchedAddressWindow == null || aquaMapUnmatchedAddressWindow.closed == true)
    {
        var aquaMapUnmatchedAddressWindowProperties = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=750,height=500";
        aquaMapUnmatchedAddressWindow = open("../../AquaMap/web/reports/CB_UnmatchedAddresses.aspx", "CustomAquaMapTools", aquaMapUnmatchedAddressWindowProperties); 
    }
    else
    {
        aquaMapUnmatchedAddressWindow.focus();
    }
}

function openKeyViewMapWindow()
{
    if(keyViewMapWindow == null || keyViewMapWindow.closed == true)
    {
        var keyViewMapWindowProperties = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=200,height=180";
        keyViewMapWindow = open("indexMap/mapsize3.htm", "keyViewMap", keyViewMapWindowProperties); 
    }
    else
    {
        keyViewMapWindow.focus();
    }
}

function onSelectionChanged(map)
{
    if(areaCollection != null)
    {			
        map.removeMapLayer("Area");
        areaCollection=null;
    }

    try
    {
        if(theBusWindow != null && theBusWindow.closed == false)
        {
            theBusWindow.focus();
        }
        else
        {
            focus();
        }
    }
    catch(Error)
    {
        focus();
    }
}

function onDigitizedPolyline(map, numPoints, points)
{
    switch(lnType)
	{
		case "tkLine":
            addLineFromDrawingToolkit(map, numPoints, points);
			break;
		case "moveStop":
		    determineDistanceToMoveBusStop(points);
			break;
    }
}

function onViewedDistance(map, totalDistance, distances, units)
{
    // measure
}

function onDigitizedPolygon(map, numPoints, points)
{
    // Checks to see if the polygon was created using the drawing toolkit
	if(areaTrigger == 0)
	{
	    drawPolygonFromDrawingToolkit(map, numPoints, points);
    }
	else
	{
	    calculateAreaFromPolygon(map, numPoints, points);
	}		
}

function onDigitizedPoint(map, point)
{
    //Look to see what kind of point symbol is being added and draw the symbol accordingly
    switch(ptType)
    {
		case "tkSymbol":
            addSymbolFromDrawingToolkit(map, point)
			break;
        case "tkText":
            addTextFromDrawingToolkit(map, point);
			break;
        //This code is for a bus stop symbol being added from the Transit Toolkit	
		case "busStp":
			addBusStopSymbol(map, point);
			break;
		//This code is for a custom utility symbol added
        case "utlSymbol":
			//Get the symbol attributes from the values selected
//			var theSymbolOpt = drawingToolkitWindow.document.draw_form.draw_symbol_name.options;
//			var	theSymName = optSel(theSymbolOpt);
//			var theRotation = drawingToolkitWindow.document.draw_form.draw_symbol_rotate.value;
//			var theWidth = drawingToolkitWindow.document.draw_form.draw_symbol_width.value;
//			var theHeight = drawingToolkitWindow.document.draw_form.draw_symbol_height.value;
//			//<!--- Call the function to set the new redline symbol attributes --->
//			setSymAttt(theSymName, theRotation, theWidth, theHeight);
//			//<!--- Create the utility symbols on the layer called Utility Temp --->
//			var myLayer = rlLyrSetup("Utility Temp");
//			//<!--- Set the utility object's key value.  Right now it is set to a blank value--->
//			var keyID = "";
//			//<!--- Call the function to create the new redline object --->
//			addSymObj(point, myLayer, keyID);
//			//<!--- Get the values to be added to the database --->
//			//<!--- Get the x and y values of the point that was just added --->
//			var X = point.getX();
//  			var Y = point.getY();
//			//<!--- Note, the following four attributes have already been defined.  They don't have to be redefined this way --->
//			var SYM = theSymName;
//			var ROT = theRotation;
//  			var WID = theWidth;
//			var HEIGHT = theHeight;
//			var utilFeatures = getTheUtilKeys();
//			var insertPage = 'insert.aspx?x=' + X + '&y=' + Y + '&symbol=' + SYM + '&rotation=' + ROT + '&width=' + WID + '&height=' + HEIGHT + '&util_ID=' + utilFeatures;
//			parent.insertFrame.location.replace(insertPage);
			break;
    }
}

//function getTheUtilKeys()
//{
//    var map = getMap();
//    var msg = "";  
//    // Get map, layer, and features
//    var features = getMap().getMapLayer('Utility Temp').getMapObjectsEx();
//    for(var i = 0; i < features.size(); i++)
//    {
//        // Get each item in collection...
//        var utilObj = features.item(i); 
//        // ...then get key name and add it to msg  
//        msg = msg + utilObj.getKey() + "\n";
//    }   
//}