function getMap()
{
    return window.map;
}

function getFrame(name)
{
    return parent.frames[name];
}

function openWindow(page,name,options)
{
	// open the window
	newPopUp=window.open(page,name,options); 	
	// add the window to the array, so we can close it later if we need too.
	newPopUp.focus();
	var frame = getFrame("hidden");
	if( frame == null )
	    frame = opener.getFrame("hidden");
	frame.addPopup(newPopUp); 	
	return newPopUp;
}

function populateFormsFrame(url)
{
    if( url != null && url != "")
    {
		//alert(url);
        getFrame("forms").document.location.replace(url);
        populateDetailsFrame("./blank.htm");
    }
    else
        alert("Invalid URL.");
}

function populateDetailsFrame(url)
{
    if( url != null && url != "" )
        getFrame("details").document.location.replace(url);
    else
        alert("Invalid URL.");
}

function logout()
{
    //populateFormsFrame("./blank.htm");
    //populateDetailsFrame("./information.htm");
    //getFrame("menu").document.location.replace("./security/Login.aspx");
    parent.location.replace("./default.aspx?action=logout");
}

function highlightFeature(featureId)
{
    var map = getMap();
    if( map != null )
    {
        var layer = map.getMapLayer("Parcels");
        if( layer != null )
        {
            var func = "highlightFeature(featureId)";
            if( map.isBusy() ) 
                setTimeout(func, 500);
            else
            {
                var objs = layer.getMapObjectsEx();
                var foundIt = false;
                for(i = 0; i < objs.size(); i++)
                {
                    var obj = objs.item(i);
                    if( obj.getName() == facilityId)
                    {
                        foundIt = true;
                        var selection = map.getSelection();
                        selection.clear();
                        selection.addObject(obj,false);
                        if( map.isBusy() )
                            setTimeout(func, 500);
                        else
                        {
                            map.zoomSelected();
                            adjustScale()                        
                        }
                    }
                }
                if( !foundIt )
                {
                    alert("Could not find feature");
                }                
            }
        }
    }
}

function isAlpha(c) 
{
	return (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')));
} 

function isDigit(c) 
{
	return ((c >= '0') && (c <= '9'));
} 

function isAlnum(c) 
{
	return (isAlpha(c) || isDigit(c));
}

function adjustScale()
{
    var map = getMap();
	if( map.isBusy() )
		setTimeout("adjustScale()", 1000);
	else
	{
		//increase the scale by percentage
		var scale = map.getScale();	
		scale = scale + (scale * .75);
		
		map.zoomScale(map.getLat(), map.getLon(), scale);	
	}
}

function loadMap(role)
{
    if (role != null)
    {
		getMap().setUrl("./Maps/"+role+"-glhomes.mwf");
    }
	else
	{
		getMap().setUrl("./Maps/glhomes.mwf");
	}
}

function zoomToFeature(id, layerName)
{
    var map = getMap();
    if( map != null )
    {
        var layer = null;
        var layers = map.getMapLayers();
        if(layers == null)
			return;
		for(x = 0; x < layers.size(); x++){
			var l = layers.item(x);
			if(l.getLegendLabel() == layerName){
				layer = l;
				break;
			}
		}
        
        if( layer != null )
        {
            var func = "zoomToFeature('"+id+"', '"+layerName+"')";
            if( map.isBusy() ) 
                setTimeout(func, 500);
            else
            {
                var objs = layer.getMapObjectsEx();
                var foundIt = false;
                for(i = 0; i < objs.size(); i++)
                {
                    var obj = objs.item(i);
                    if( obj.getKey() == id)
                    {
                        foundIt = true;
                        var selection = map.getSelection();
                        selection.clear();
                        selection.addObject(obj,false);
                        if( map.isBusy() )
                            setTimeout(func, 500);
                        else
                        {
                            map.zoomSelected();
                            adjustScale()                        
                        }
                    }
                }
                if( !foundIt )
                    alert("Could not find feature: " + id +" on layer: " + layerName);
            }
        }
    }
}
