<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
                layout="absolute" width="465" height="298"
                creationComplete="init()" viewSourceURL="srcview/index.html">
    <mx:states>
        <mx:State name="LoggedIn">
            <mx:SetProperty target="{status}" name="text" value="Logged In"/>
            <mx:SetProperty target="{loginLogoutButton}" name="label" value="Log Out"/>
            <mx:SetEventHandler target="{loginLogoutButton}" name="click" handler="doLogout()"/>
        </mx:State>
    </mx:states>
    <mx:Script>
        <![CDATA[
            import mx.core.IFlexDisplayObject;
            import mx.managers.PopUpManager;
            import mx.controls.Alert;
            import flash.profiler.showRedrawRegions;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;
            import mx.rpc.remoting.RemoteObject;
            
            private var accountService:RemoteObject;            
            
            private function init():void
            {
                accountService = new RemoteObject( "AccountBalanceDestination" );
                accountService.CheckBalance.addEventListener( ResultEvent.RESULT, gotBalance );
                accountService.CheckBalance.addEventListener( FaultEvent.FAULT, gotError );
                accountService.logout(); 
            }
            
            public function setCredentials( username:String, password:String):void
            {
                accountService.setCredentials( username, password );
                currentState = "LoggedIn"
            }
            
            private function checkBalance():void
            {
                accountService.CheckBalance();
            }
            
            private function gotBalance( result:ResultEvent ):void
            {
                balance.text = result.result.toString();
            }
            
            private function gotError( fault:FaultEvent ):void
            {
                Alert.show( "Server reporter an error - " + fault.fault.faultString );
            }
            
            private function doLogin():void
            {
                var popup:IFlexDisplayObject = PopUpManager.createPopUp( this, LoginForm, true );
                PopUpManager.centerPopUp( popup );
            }
            
            private function doLogout():void
            {
                accountService.logout();
                currentState = "";
            }
        ]]>
    </mx:Script>
    <mx:Panel x="10" y="10" width="447" height="275" layout="absolute" title="Remoting Security Example">
        <mx:Label x="116" y="169" text="Current status:"/>
        <mx:Label x="213" y="167" text="Not Logged In" id="status" fontSize="12" fontWeight="bold" color="#ff0000"/>
        <mx:Canvas x="62.5" y="30" width="302" height="105" borderStyle="solid" cornerRadius="10">
            <mx:Button x="59" y="23" label="Check Account Balance" width="182" click="checkBalance()"/>
            <mx:Text x="59" y="61" text="Account Balance: $"/>
            <mx:TextInput x="168" y="59" width="73" editable="false" id="balance" borderStyle="solid" backgroundColor="#a9c0b7" borderColor="#408080"/>
        </mx:Canvas>
        <mx:Text x="82.5" y="21" text="Checking Account" opaqueBackground="0xffffff"/>
        <mx:Button id="loginLogoutButton" x="171" y="195" label="Log In" click="doLogin()"/>
    </mx:Panel>
    
</mx:Application>