<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" viewSourceURL="srcview/index.html">

<mx:Script>
    <![CDATA[
        import mx.rpc.events.FaultEvent;
        import mx.rpc.events.ResultEvent;
        import mx.rpc.http.HTTPService;
        import mx.controls.Alert;
        import com.markbarton.CurrentUser;
        import mx.utils.ObjectUtil;
        
        //Base URL normally computed based on the current environment
        private static const BASE_URL:String="http://dev01/";
        
        //Global current user - stored at the Application.application level
        public var currentUser:CurrentUser;
        
        //Called from the submit button
        private function submitCredentials():void{
            callDomino();
        }
        
        private function callDomino():void{
            var httpObj:HTTPService=new HTTPService();
            //prevent caching    
            var params:Object=new Object;
            params.nocache=Math.round(Math.random()*1000);
            
            //Call the NAB first
            httpObj.url=BASE_URL + "ibill/ibill.nsf?login";
            //These will be posted    
            params.username=username.text;
            params.password=password.text;
            //Get the user.xml file from your Domino application
            params.RedirectTo=BASE_URL + "ibill/ibill.nsf/user.xml?openpage";
            httpObj.request="POST";
            //Use e4x as the return format    
            httpObj.resultFormat="e4x";
            //Add the event handlers
            httpObj.addEventListener(ResultEvent.RESULT,resultsHandler);
            httpObj.addEventListener(FaultEvent.FAULT,handleLoginFault);    
              httpObj.send(params);
        }
        
            private function resultsHandler(event:ResultEvent):void{
                //If there is a value in the XML then continue we have logged in
                if(event.result.person.name!=""){
                //Create the currentUser Object passing the results
                currentUser=new CurrentUser(event.result);
                //Show the current user name
                Alert.show(currentUser.name);
                }
                else{
                //There was a problem with the login
                    Alert.show("Error cannot login: ");
                }    
        }
        
        /*Special case for handling problems with logging in - this is because we are expecting XML but Domino 
        returns a HTML error for session based authentication */
        
        public static function handleLoginFault(event:FaultEvent):void{
            Alert.show("Error cannot login");
        }
        
        
    ]]>
</mx:Script>

    <mx:Form x="0" y="50">
        <mx:FormItem label="Username">
            <mx:TextInput id="username"/>
        </mx:FormItem>        
        <mx:FormItem label="Password">
            <mx:TextInput id="password"/>
        </mx:FormItem>        
    <mx:Button label="Submit" click="submitCredentials()"/>
        
    </mx:Form>
</mx:Application>