 		return super.contains(child);
 	}
 
+	//--------------------------------------------------------------------------
+	//
+	//  Sandbox Event handlers for messages from children
+	//
+	//--------------------------------------------------------------------------
+
+	/**
+	 * @private
+	 * 
+	 * Add a popup request handler for domain local request and 
+	 * remote domain requests.
+	 */
+	private function addPopupRequestHandler(event:Event):void
+	{
+		if (event.target != this && event is PopUpRequest)
+			return;
+
+		var popUpRequest:PopUpRequest = PopUpRequest.marshal(event);
+
+		// If the is not for mutual trust between us an the child that wants the 
+		// popup, then don't host the pop up.
+		if (!SandboxUtil.hasMutualTrustWithChild(this, popUpRequest.bridge))
+		{
+			return;
+		}
+					
+		var topMost:Boolean;
+
+		// Need to have mutual trust between two application in order
+		// for an application to host another application's popup.
+		if (SandboxUtil.hasMutualTrustWithParent(this))
+		{
+			// ask the parent to host the popup
+			popUpRequest.bridge = sandboxBridgeGroup.parentBridge;
+			sandboxBridgeGroup.parentBridge.dispatchEvent(popUpRequest);
+			return;
+		}
+		
+		// add popup as a child of this system manager
+        if (!popUpRequest.childList || popUpRequest.childList == PopUpManagerChildList.PARENT)
+            topMost = popUpRequest.parent && popUpChildren.contains(popUpRequest.parent);
+        else
+            topMost = (popUpRequest.childList == PopUpManagerChildList.POPUP);
+
+        var children:IChildList;
+        children = topMost ? popUpChildren : this;
+        children.addChild(DisplayObject(popUpRequest.window));
+        
+        if (popUpRequest.modal)    
+	        numModalWindows++;
+        
+		// add popup to the list of managed forms
+		var remoteForm:RemotePopUp = new RemotePopUp(popUpRequest.window, popUpRequest.bridge);
+		forms.push(remoteForm);
+		
+		if (!isTopLevelRoot() && sandboxBridgeGroup)
+		{
+			// We've added the popup as far as it can go.
+			// Add a placeholder to the top level root application
+			var request:PopUpRequest = new PopUpRequest(PopUpRequest.ADD_PLACEHOLDER, 
+			                                            popUpRequest.window, 
+			                                            popUpRequest.bridge);
+			request.placeholderId = NameUtil.displayObjectToString(DisplayObject(popUpRequest.window));
+			dispatchEvent(request);
+		}
+	}
+	
+	/**
+	 * @private
+	 * 
+	 * Message from a child system manager to 
+	 * remove the popup that was added by using the
+	 * addPopupRequestHandler.
+	 */
+	private function removePopupRequestHandler(event:Event):void
+	{
+		var popUpRequest:PopUpRequest = PopUpRequest.marshal(event);
+
+		if (SandboxUtil.hasMutualTrustWithParent(this))
+		{
+			// since there is mutual trust the popup is hosted by the parent.
+			sandboxBridgeGroup.parentBridge.dispatchEvent(popUpRequest);
+			return;
+		}
+					
+        if (popUpChildren.contains(popUpRequest.window))
+            popUpChildren.removeChild(popUpRequest.window);
+        else
+            removeChild(DisplayObject(popUpRequest.window));
+        
+        if (popUpRequest.modal)    
+			numModalWindows--;
+
+		removeRemotePopUp(new RemotePopUp(popUpRequest.window, popUpRequest.bridge));
+		
+		if (!isTopLevelRoot() && sandboxBridgeGroup)
+		{
+			// if we got here we know the parent is untrusted, so remove placeholders
+			var request:PopUpRequest = new PopUpRequest(PopUpRequest.REMOVE_PLACEHOLDER, 
+														null,
+														sandboxBridgeGroup.parentBridge);
+			request.placeholderId = NameUtil.displayObjectToString(popUpRequest.window);
+			dispatchEvent(request);
+		}
+		            
+	}
+	
+	/**
+	 * @private
+	 * 
+	 * Handle request to add a popup placeholder.
+	 * The placeholder represents an untrusted form that is hosted 
+	 * elsewhere.
+	 */
+	 private function addPlaceholderPopupRequestHandler(event:Event):void
+	 {
+		var popUpRequest:PopUpRequest = PopUpRequest.marshal(event);
+
+		if (event.target != this && event is PopUpRequest)
+			return;
+	 	
+		if (!forwardPlaceholderRequest(popUpRequest, true))
+		{
+			// Create a RemotePopUp and add it.
+			var remoteForm:RemotePopUp = new RemotePopUp(popUpRequest.placeholderId, popUpRequest.bridge);
+			forms.push(remoteForm);
+		}
+
+	 }
+
+	/**
+	 * @private
+	 * 
+	 * Handle request to add a popup placeholder.
+	 * The placeholder represents an untrusted form that is hosted 
+	 * elsewhere.
+	 */
+	 private function removePlaceholderPopupRequestHandler(event:Event):void
+	 {
+		var popUpRequest:PopUpRequest = PopUpRequest.marshal(event);
+	 	
+		if (!forwardPlaceholderRequest(popUpRequest, false))
+		{
+	        // remove the placeholder from forms array
+			var n:int = forms.length;
+			for (var i:int = 0; i < n; i++)
+			{
+				if (isRemotePopUp(forms[i]))
+				{
+					if (forms[i].window == popUpRequest.placeholderId &&
+					    forms[i].bridge == popUpRequest.bridge)
+					{
+						forms.splice(i, 1);
+						break;
+					}
+				}
+			}
+		}			 	
+		
+	 }
+
+	/**
+	 * Forward a form event update the parent chain. 
+	 * Takes care of removing object references and substituting
+	 * ids when an untrusted boundry is crossed.
+	 */
+	private function forwardFormEvent(eObj:Object):Boolean
+	{
+		
+		if (isTopLevelRoot())
+			return false;			
+			
+		var bridge:IEventDispatcher = sandboxBridgeGroup.parentBridge; 
+		if (bridge)
+		{
+			var sbRoot:DisplayObject = getSandboxRoot();
+			eObj.sender =  bridge;
+			if (sbRoot == this)
+			{
+				if (!(eObj.data is String))
+					eObj.data = NameUtil.displayObjectToString(DisplayObject(eObj.data));
+				else
+					eObj.data = NameUtil.displayObjectToString(DisplayObject(this)) + "." + eObj.data;
+				
+				bridge.dispatchEvent(Event(eObj));
+			}
+			else
+			{
+				if (eObj.data is String)
+					eObj.data = NameUtil.displayObjectToString(DisplayObject(this)) + "." + eObj.data;
+ 
+				sbRoot.dispatchEvent(Event(eObj));
+			}
+		}
+
+		return true;
+	}
+	
+	/**
+	 * Forward an AddPlaceholder request up the parent chain, if needed.
+	 * 
+	 * @param eObj PopupRequest as and Object.
+	 * @param addPlaceholder true if adding a placeholder, false it removing a placeholder.
+	 * @return true if the request was forwared, false otherwise
+	 */
+	private function forwardPlaceholderRequest(eObj:Object, addPlaceholder:Boolean):Boolean
+	{
+	 	// Only the top level root tracks the placeholders.
+	 	// If we are not the top level root then keep passing
+	 	// the message up the parent chain.
+	 	if (isTopLevelRoot())
+	 		return false;
+	 		
+		// If the window object is passed, then this is the first
+		// stop on the way up the parent chain.
+		var refObj:Object = null;
+		var oldId:String = null;
+		if (eObj.window)
+		{
+			refObj = eObj.window;
+			
+			// null this ref out so untrusted parent cannot see
+			eObj.window = null;
+		}
+		else
+		{
+			refObj = eObj.bridge;
+			
+			// prefix the existing id with the id of this object
+			oldId = eObj.placeholderId;
+			eObj.placeholderId = NameUtil.displayObjectToString(this) + "." + eObj.placeholderId;
+		}
+
+		if (addPlaceholder)
+			addPlaceholderId(eObj.placeholderId, oldId, eObj.bridge, refObj);
+		else 
+			removePlaceholderId(eObj.placeholderId);
+				
+		
+		var sbRoot:DisplayObject = getSandboxRoot();
+		var bridge:IEventDispatcher = sandboxBridgeGroup.parentBridge; 
+		eObj.bridge =  bridge;
+		if (sbRoot == this)
+			bridge.dispatchEvent(Event(eObj));
+		else 
+			sbRoot.dispatchEvent(Event(eObj));
+			
+		return true;
+	}
+
+	/**
+	 * One of the system managers in another sandbox deactivated and sent a message
+	 * to the top level system manager. In response the top-level system manager
+	 * needs to find a new form to activate.
+	 */
+	private function deactivateFormSandboxEventHandler(event:Event):void
+	{
+		// trace("bridgeDeactivateFormEventHandler");
+
+		if (event is SandboxBridgeRequest)
+			return;
+
+		var eObj:Object = Object(event);
+
+		if (!forwardFormEvent(eObj))
+		{
+			// deactivate the form
+			if (isRemotePopUp(form) && 
+				RemotePopUp(form).window == eObj.data &&
+				RemotePopUp(form).bridge == eObj.sender)
+				deactivateForm(form);
+		}
+	}
+	
+	
+	/**
+	 * A form in one of the system managers in another sandbox has been activated. 
+	 * The form being activate is identified. 
+	 * In response the top-level system manager needs to activate the given form
+	 * and deactivate the currently active form, if any.
+	 */
+	private function activateFormSandboxEventHandler(event:Event):void
+	{
+		// trace("bridgeActivateFormEventHandler");
+		var eObj:Object = event;
+
+		if (!forwardFormEvent(eObj))
+			// just call activate on the remote form.
+			activateForm(new RemotePopUp(eObj.data, eObj.sender));			
+	}
+		
+	/**
+	 * One of the system managers in another sandbox activated and sent a message
+	 * to the top level system manager to deactivate this form. In response the top-level system manager
+	 * needs to deactivate all other forms except the top level system manager's.
+	 */
+	private function activateApplicationSandboxEventHandler(event:Event):void
+	{
+		// trace("bridgeActivateApplicationEventHandler");
+		if (!isTopLevelRoot())
+		{
+			sandboxBridgeGroup.parentBridge.dispatchEvent(event);
+			return;    	
+		}
+
+		// An application was activated, active the main document.
+		activateForm(document);
+	}
+
+
+    /**
+     *  @private
+     * 
+     *  Re-dispatch events sent over the bridge to listeners on this
+     *  system manager. PopUpManager is expected to listen to these
+     *  events.
+     */  
+    private function modalWindowRequestHandler(event:Event):void
+    {
+        if (event is ModalWindowRequest)
+            return;
+        
+        var request:ModalWindowRequest = ModalWindowRequest.marshal(event);
+            
+        if (!preProcessModalWindowRequest(request, this, getSandboxRoot()))
+            return;
+                        
+        // Ensure a PopUpManager exists and dispatch the request it is
+        // listening for.
+        Singleton.getInstance("mx.managers::IPopUpManager");
+        dispatchEvent(request);
+    }
+
+    /**
+     *  @private
+     * 
+     *  Calculate the visible rectangle of the requesting application in this
+     *  application. Forward the request to our parent to see this the rectangle
+     *  is further reduced. Continue up the parent chain until the top level
+     *  root parent is reached.
+     */  
+    private function getVisibleRectRequestHandler(event:Event):void
+    {
+        if (event is SandboxBridgeRequest)
+            return;
+        
+        var request:SandboxBridgeRequest = SandboxBridgeRequest.marshal(event);
+        var rect:Rectangle = Rectangle(request.data);
+        var owner:DisplayObject = sandboxBridgeGroup.getChildBridgeOwner(request.requestor);
+        var localRect:Rectangle;
+        
+        if (owner is IApplicationLoader)
+            localRect = IApplicationLoader(owner).getVisibleApplicationRect();
+        else
+        {
+            localRect = owner.getBounds(this);
+            var pt:Point = localToGlobal(localRect.topLeft);
+            localRect.x = pt.x;
+            localRect.y = pt.y;
+        }        
+           
+        rect = rect.intersection(localRect); // update rect
+        request.data = rect;
+        
+        // forward request 
+        if (useBridge())
+        { 
+        var bridge:IEventDispatcher = sandboxBridgeGroup.parentBridge;
+            request.requestor = bridge;
+            bridge.dispatchEvent(request);
+        }
+        
+        Object(event).data = request.data;           // update request
+    }
+
+	//--------------------------------------------------------------------------
+	//
+	//  Sandbox Event handlers for messages from parent
+	//
+	//--------------------------------------------------------------------------
+	
+	/**
+	 * @private
+	 * 
+	 * Sent by the SWFLoader to change the size of the application it loaded.
+	 */
+	private function setActualSizeRequestHandler(event:Event):void
+	{
+		// empty.  This should never be the root of a SWF
+	}
+	
+	/**
+	 * @private
+	 * 
+	 * Get the size of this System Manager.
+	 * Sent by a SWFLoader.
+	 */
+	private function getSizeRequestHandler(event:Event):void
+	{
+		// empty.  This should never be the root of a SWF
+	}
+	
+	/**
+	 * @private
+	 * 
+	 * Handle request to activate a particular form.
+	 * 
+	 */
+	private function activateRequestHandler(event:Event):void
+	{
+		var eObj:Object = Object(event);
+
+		// If data is a String, then we need to parse the id to find
+		// the form or the next bridge to pass the message to.
+		// If the data is a SystemMangerProxy we can just activate the
+		// form.
+		var child:Object = eObj.data; 
+		var nextId:String = null;
+		if (eObj.data is String)
+		{
+			var placeholder:PlaceholderData = idToPlaceholder[eObj.data];
+			child = placeholder.data;
+			nextId = placeholder.id;
+			
+			// check if the dialog is hosted on this system manager
+			if (nextId == null)
+			{
+				var popUp:RemotePopUp = findRemotePopUp(child, placeholder.bridge); 
+				
+				if (popUp)
+				{
+					activateRemotePopUp(popUp);
+					return;
+				}
+			}
+		}
+		
+		if (child is SystemManagerProxy)
+		{
+			// deactivate request from the top-level system manager.
+			var smp:SystemManagerProxy = SystemManagerProxy(eObj.data);
+			var f:IFocusManagerContainer = findFocusManagerContainer(smp);
+			if (smp && f)
+				smp.activateProxy(f);
+		}	
+		else if (child is IFocusManagerContainer)
+			IFocusManagerContainer(child).focusManager.activate();
+		else if (child is IEventDispatcher)
+		{
+				eObj.data = nextId;
+				eObj.requestor = child;
+				IEventDispatcher(child).dispatchEvent(event);
+		}
+		else 
+			throw new Error();	// should never get here
+	}
+
+	/**
+	 * @private
+	 * 
+	 * Handle request to deactivate a particular form.
+	 * 
+	 */
+	private function deactivateRequestHandler(event:Event):void
+	{
+		var eObj:Object = Object(event);
+
+		var child:Object = eObj.data; 
+		var nextId:String = null;
+		if (eObj.data is String)
+		{
+			var placeholder:PlaceholderData = idToPlaceholder[eObj.data];
+			child = placeholder.data;
+			nextId = placeholder.id;
+
+			// check if the dialog is hosted on this system manager
+			if (nextId == null)
+			{
+				var popUp:RemotePopUp = findRemotePopUp(child, placeholder.bridge); 
+				
+				if (popUp)
+				{
+					deactivateRemotePopUp(popUp);
+					return;
+				}
+			}
+		}
+		
+		if (child is SystemManagerProxy)
+		{
+			// deactivate request from the top-level system manager.
+			var smp:SystemManagerProxy = SystemManagerProxy(child);
+			var f:IFocusManagerContainer = findFocusManagerContainer(smp);
+			if (smp && f)
+				smp.deactivateProxy(f);
+		}
+		else if (child is IFocusManagerContainer)
+			IFocusManagerContainer(child).focusManager.deactivate();
+			
+		else if (child is IEventDispatcher)
+		{
+			eObj.data = nextId;
+			eObj.requestor = child;
+			IEventDispatcher(child).dispatchEvent(event);
+			return;
+		}
+		else
+			throw new Error();		
+	}
+
+	//--------------------------------------------------------------------------
+	//
+	//  Sandbox Event handlers for messages from either the
+	//  parent or child
+	//
+	//--------------------------------------------------------------------------
+
+	/**
+	 * Is the child in event.data this system manager or a child of this 
+	 * system manager?
+	 *
+	 * If the display object is a child event.preventDefault is called,
+	 * otherwise nothing is done. 
+	 */
+	private function isBridgeChildHandler(event:Event):void
+	{
+		// if we are broadcasting messages, ignore the messages
+		// we send to ourselves.
+		if (event is SandboxBridgeRequest)
+			return;
+
+		var eObj:Object = Object(event);
+
+		if (eObj.data && rawChildren.contains(eObj.data as DisplayObject))
+		{
+			event.preventDefault();
+		}
+	}
+	
+	/**
+	 * Can this form be activated. The current test is if the given pop up 
+	 * is visible and is enabled. 
+	 *
+	 * If the can be activated event.preventDefault is called,
+	 * otherwise nothing is done. 
+	 */
+	private function canActivateHandler(event:Event):void
+	{
+		var eObj:Object = Object(event);
+
+		// If data is a String, then we need to parse the id to find
+		// the form or the next bridge to pass the message to.
+		// If the data is a SystemMangerProxy we can just activate the
+		// form.
+		var request:SandboxBridgeRequest;
+		var child:Object = eObj.data; 
+		var nextId:String = null;
+		if (eObj.data is String)
+		{
+			var placeholder:PlaceholderData = idToPlaceholder[eObj.data];
+			child = placeholder.data;
+			nextId = placeholder.id;
+			
+			// check if the dialog is hosted on this system manager
+			if (nextId == null)
+			{
+				var popUp:RemotePopUp = findRemotePopUp(child, placeholder.bridge); 
+				
+				if (popUp)
+				{
+					request = new SandboxBridgeRequest(SandboxBridgeRequest.CAN_ACTIVATE,
+																false, true, 
+																IEventDispatcher(popUp.bridge), 
+																popUp.window);
+				 	if (popUp.bridge)
+				 	{
+				 		popUp.bridge.dispatchEvent(request);
+				 		if (request.isDefaultPrevented())
+				 			event.preventDefault();
+				 	}
+					return;
+				}
+			}
+		}
+		
+		if (child is SystemManagerProxy)
+		{
+			var smp:SystemManagerProxy = SystemManagerProxy(child);
+			var f:IFocusManagerContainer = findFocusManagerContainer(smp);
+			if (smp && f && canActivateLocalComponent(f))
+				event.preventDefault();
+		}	
+		else if (child is IFocusManagerContainer)
+		{
+			if (canActivateLocalComponent(child))
+				event.preventDefault();
+		}
+		else if (child is IEventDispatcher)
+		{
+			var bridge:IEventDispatcher = IEventDispatcher(child);
+		    request = new SandboxBridgeRequest(SandboxBridgeRequest.CAN_ACTIVATE,
+															false, true, 
+															bridge, 
+															nextId);
+			
+			if (bridge)
+			{
+				bridge.dispatchEvent(request);
+				if (request.isDefaultPrevented())
+					event.preventDefault();
+			}
+		}
+		else 
+			throw new Error();	// should never get here
+	}
+	
+
+	/**
+	 * @private
+	 * 
+	 * Test if a display object is in an applcation we want to communicate with over a bridge.
+	 * 
+	 */
+	public function isDisplayObjectInABridgedApplication(displayObject:DisplayObject):Boolean
+	{
+		if (sandboxBridgeGroup)
+		{
+			var request:SandboxBridgeRequest = new SandboxBridgeRequest(SandboxBridgeRequest.IS_BRIDGE_CHILD,
+																		false, true, null, displayObject);
+			var children:Array = sandboxBridgeGroup.getChildBridges();
+			var n:int = children.length;
+			for (var i:int = 0; i < n; i++)
+			{
+				var childBridge:IEventDispatcher = IEventDispatcher(children[i]);
+				
+				// No need to test a child if it does not trust us, we will never see
+				// their display objects.
+				// Also, if the we don't trust the child don't send them a display object.
+				if (sandboxBridgeGroup.canAccessChildBridge(childBridge) &&
+					sandboxBridgeGroup.accessibleFromChildBridge(childBridge) &&
+					!childBridge.dispatchEvent(request))
+					return true;
+			}
+		}
+			
+		return false;
+	}
+
+	/**
+	 * Create the requested manager
+	 */
+	private function initManagerHandler(event:Event):void
+	{
+		// if we are broadcasting messages, ignore the messages
+		// we send to ourselves.
+		if (event is MarshalEvent)
+			return;
+
+		// initialize the registered manager implementation
+		var name:String = event["name"];
+		Singleton.getInstance(name);
+	}
+
+	/**
+	 * Create the requested manager
+	 */
+	public function addChildToSandboxRoot(layer:String, child:DisplayObject):void
+	{
+		if (getSandboxRoot() == this)
+		{
+			this[layer].addChild(child);
+		}
+		else
+		{
+			addingChild(child);
+			var me:MarshalEvent = new MarshalEvent(MarshalEvent.SYSTEM_MANAGER);
+			me.name = layer + ".addChild";
+			me.value = child;
+			getSandboxRoot().dispatchEvent(me);
+			childAdded(child);
+		}
+	}
+
+	/**
+	 * Create the requested manager
+	 */
+	public function removeChildFromSandboxRoot(layer:String, child:DisplayObject):void
+	{
+		if (getSandboxRoot() == this)
+		{
+			this[layer].removeChild(child);
+		}
+		else
+		{
+			removingChild(child);
+			var me:MarshalEvent = new MarshalEvent(MarshalEvent.SYSTEM_MANAGER);
+			me.name = layer + ".removeChild";
+			me.value = child;
+			getSandboxRoot().dispatchEvent(me);
+			childRemoved(child);
+		}
+	}
+
+
+	/**
+	 * marshal some data
+	 */
+	private function marshalHandler(event:Event):void
+	{
+		// if we are broadcasting messages, ignore the messages
+		// we send to ourselves.
+		if (event is MarshalEvent)
+			return;
+
+		var eventObj:Object = event;
+		var value:Object = eventObj.value.value;
+		var type:Class = eventObj.value.type;
+
+		var info:Object = ObjectUtil.getClassInfo(value);
+
+		var alias:String = info.alias;
+
+		var currentType:Class = getClassByAlias(alias);
+		var ba:ByteArray = new ByteArray();
+		ba.writeObject(value);
+		registerClassAlias(alias, type);
+		ba.position = 0;
+		value = ba.readObject();
+		eventObj.value = value;
+		registerClassAlias(alias, currentType);
+
+	}
+
+	/**
+	 * perform the requested action from a trusted dispatcher
+	 */
+	private function systemManagerHandler(event:Event):void
+	{
+		if (event["name"] == "sameSandbox")
+		{
+			event["value"] = currentSandboxEvent == event["value"];
+			return;
+		}
+
+		// if we are broadcasting messages, ignore the messages
+		// we send to ourselves.
+		if (event is MarshalEvent)
+			return;
+
+		// initialize the registered manager implementation
+		var name:String = event["name"];
+
+		switch (name)
+		{
+		case "popUpChildren.addChild":
+			popUpChildren.addChild(event["value"]);
+			break;
+		case "popUpChildren.removeChild":
+			popUpChildren.removeChild(event["value"]);
+			break;
+		case "cursorChildren.addChild":
+			cursorChildren.addChild(event["value"]);
+			break;
+		case "cursorChildren.removeChild":
+			cursorChildren.removeChild(event["value"]);
+			break;
+		case "toolTipChildren.addChild":
+			toolTipChildren.addChild(event["value"]);
+			break;
+		case "toolTipChildren.removeChild":
+			toolTipChildren.removeChild(event["value"]);
+			break;
+		case "screen":
+			event["value"] = screen;
+			break;
+		case "application":
+		    event["value"] = document;
+		    break;
+		case "isTopLevelRoot":
+		    event["value"] = isTopLevelRoot();
+		    break;
+	    case "getVisibleApplicationRect":
+	        event["value"] = getVisibleApplicationRect(); 
+		}
+	}
+	
 	// fake out mouseX/mouseY
 	mx_internal var _mouseX:*;
 	mx_internal var _mouseY:*;

		myWindow.nativeWindow.addEventListener("close", cleanup);
	}
}
}
		{
			try
			{
			    // check if the loader info is valid.
			    root.loaderInfo.parentAllowsChild;
			    
				if (canAccessParent() && accessibleFromParent())
				{
					try
					{
