package {
    import com.adobe.viewsource.ViewSource;
    import com.cb.utils.captcha.Captcha;
    
    import flash.display.Bitmap;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.events.MouseEvent;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.text.TextField;
    import flash.text.TextFieldType;

    [SWF(pageTitle="Server Side Captcha in Flash", backgroundColor="#FFFFFF", width="300", height="200", frameRate="30")]
    public class ServerSideCaptcha extends Sprite
    {
        [Embed(source="/assets/images/submit.jpg")]
        private var SubmitImage: Class;
        private var _submitBmp:Bitmap = new SubmitImage();

        private var _captcha:Captcha;
        private var _loader:URLLoader = new URLLoader();
        
        private var _text:TextField = new TextField();
        private var _result:TextField = new TextField();
        
        public function ServerSideCaptcha()
        {
            super();

            ViewSource.addMenuItem(this, "http://www.blackcj.com/blog/wp-content/swfs/ServerSideCaptcha/srcview/index.html");  // Used to allow ViewSource
            
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
        
            // Create a Captcha object
            _captcha = new Captcha( );

            _captcha.addEventListener(MouseEvent.CLICK, refreshCaptcha, false, 0, true);
            
            // Create a unique ID so the server can identify the Flash
            var date:Date = new Date();
            var id:String = "" + Math.floor(Math.random()*1000) + date.time; 
            
            // Pass in the url along with the ID so the Captcha can load
            _captcha.loadCaptcha("http://www.blackcj.com/utils/securimage_show.php?flash_id=" + id, id);
            
            this.addChild(_captcha);
            
            _text.type = TextFieldType.INPUT;
            _text.border = true;
            _text.borderColor = 0x000000;
            _text.height = 20;
            _text.y = 65;
            this.addChild(_text);

            var submitButton:Sprite = new Sprite();
            submitButton.addChild(_submitBmp);
            submitButton.addEventListener(MouseEvent.CLICK, submitForm, false, 0, true);
            submitButton.y = 95;
            submitButton.useHandCursor = true;
            submitButton.buttonMode = true;
            this.addChild(submitButton);
            
            // Displays success (true) or failure (false)        
            _result.y = 130;
            this.addChild(_result);
        }

        public function submitForm(event:MouseEvent):void
        {
            var loader:URLLoader = new URLLoader();
            loader.addEventListener(Event.COMPLETE, formSuccess, false, 0, true);
            var request:URLRequest = new URLRequest("http://www.blackcj.com/utils/check_captcha.php?flash_id=" + _captcha.id + "&captcha_text=" + _text.text);
            loader.load(request);
        }
        
        private function formSuccess(event:Event):void
        {
            refreshCaptcha();
            var result:String = event.currentTarget.data;
            _result.text = result;
        }

        /**
         * Handle IOError gracefully 
         * @param event
         * 
         */
        private function errorHandler(event:IOErrorEvent):void
        {
            trace("ERROR");
        }
        
        /**
         * Used to request a new Captcha from the server 
         * @param event
         * 
         */
        public function refreshCaptcha(event:MouseEvent = null):void
        {
            _captcha.fadeOut();
            var date:Date = new Date();
            var id:String = "" + Math.floor(Math.random()*1000) + date.time; 
            _captcha.loadCaptcha("http://www.blackcj.com/utils/securimage_show.php?flash_id=" + id, id);
        }
        
    }
}