/**
 *     Generated by mxmlc 2.0
 *
 *    Package:    
 *    Class:         BasicExample
 *    Source:     C:\workspaces\flash\urlrequestbuilder\examples\basic_example\BasicExample.mxml
 *    Template:     flex2/compiler/mxml/gen/ClassDef.vm
 *    Time:         2009.03.13 19:58:18 GMT
 */

package 
{

import flash.accessibility.*;
import flash.debugger.*;
import flash.display.*;
import flash.errors.*;
import flash.events.*;
import flash.events.MouseEvent;
import flash.external.*;
import flash.filters.*;
import flash.geom.*;
import flash.media.*;
import flash.net.*;
import flash.printing.*;
import flash.profiler.*;
import flash.system.*;
import flash.text.*;
import flash.ui.*;
import flash.utils.*;
import flash.xml.*;
import mx.binding.*;
import mx.containers.Form;
import mx.containers.FormHeading;
import mx.containers.FormItem;
import mx.containers.HBox;
import mx.containers.VBox;
import mx.controls.Button;
import mx.controls.DataGrid;
import mx.controls.TextInput;
import mx.controls.dataGridClasses.DataGridColumn;
import mx.core.Application;
import mx.core.ClassFactory;
import mx.core.DeferredInstanceFromClass;
import mx.core.DeferredInstanceFromFunction;
import mx.core.IDeferredInstance;
import mx.core.IFactory;
import mx.core.IPropertyChangeNotifier;
import mx.core.UIComponentDescriptor;
import mx.core.mx_internal;
import mx.events.FlexEvent;
import mx.styles.*;


[Frame("_BasicExample_FlexInit")]

[Frame("_BasicExample_mx_managers_SystemManager")]



public class BasicExample
    extends mx.core.Application
{

    [Bindable]
/**
 * @private
 **/
    public var filesGrid : mx.controls.DataGrid;

    [Bindable]
/**
 * @private
 **/
    public var mainForm : mx.containers.Form;

    [Bindable]
/**
 * @private
 **/
    public var submitBtn : mx.controls.Button;

    [Bindable]
/**
 * @private
 **/
    public var userAge : mx.controls.TextInput;

    [Bindable]
/**
 * @private
 **/
    public var userName : mx.controls.TextInput;




private var _documentDescriptor_ : mx.core.UIComponentDescriptor = 
new mx.core.UIComponentDescriptor({
  type: mx.core.Application
  ,
  propertiesFactory: function():Object { return {
    childDescriptors: [
      new mx.core.UIComponentDescriptor({
        type: mx.containers.VBox
        ,
        stylesFactory: function():void {
          this.verticalAlign = "middle";
          this.horizontalAlign = "center";
        }
        ,
        propertiesFactory: function():Object { return {
          percentWidth: 100.0,
          percentHeight: 100.0,
          childDescriptors: [
            new mx.core.UIComponentDescriptor({
              type: mx.containers.FormHeading
              ,
              stylesFactory: function():void {
                this.color = 16777215;
              }
              ,
              propertiesFactory: function():Object { return {
                label: "Multipart Form Example"
              }}
            })
          ,
            new mx.core.UIComponentDescriptor({
              type: mx.containers.Form
              ,
              id: "mainForm"
              ,
              propertiesFactory: function():Object { return {
                percentWidth: 80.0,
                childDescriptors: [
                  new mx.core.UIComponentDescriptor({
                    type: mx.containers.FormItem
                    ,
                    propertiesFactory: function():Object { return {
                      label: "Name:",
                      percentWidth: 100.0,
                      childDescriptors: [
                        new mx.core.UIComponentDescriptor({
                          type: mx.controls.TextInput
                          ,
                          id: "userName"
                          ,
                          propertiesFactory: function():Object { return {
                            text: "John Roberts",
                            percentWidth: 100.0
                          }}
                        })
                      ]
                    }}
                  })
                ,
                  new mx.core.UIComponentDescriptor({
                    type: mx.containers.FormItem
                    ,
                    propertiesFactory: function():Object { return {
                      label: "Age:",
                      percentWidth: 100.0,
                      childDescriptors: [
                        new mx.core.UIComponentDescriptor({
                          type: mx.controls.TextInput
                          ,
                          id: "userAge"
                          ,
                          propertiesFactory: function():Object { return {
                            text: "26",
                            percentWidth: 100.0
                          }}
                        })
                      ]
                    }}
                  })
                ,
                  new mx.core.UIComponentDescriptor({
                    type: mx.containers.FormItem
                    ,
                    propertiesFactory: function():Object { return {
                      label: "Files:",
                      percentWidth: 100.0,
                      childDescriptors: [
                        new mx.core.UIComponentDescriptor({
                          type: mx.containers.HBox
                          ,
                          propertiesFactory: function():Object { return {
                            percentWidth: 100.0,
                            childDescriptors: [
                              new mx.core.UIComponentDescriptor({
                                type: mx.controls.DataGrid
                                ,
                                id: "filesGrid"
                                ,
                                propertiesFactory: function():Object { return {
                                  percentWidth: 100.0,
                                  columns: [_BasicExample_DataGridColumn1_c(), _BasicExample_DataGridColumn2_c()]
                                }}
                              })
                            ,
                              new mx.core.UIComponentDescriptor({
                                type: mx.controls.Button
                                ,
                                events: {
                                  click: "___BasicExample_Button1_click"
                                }
                                ,
                                propertiesFactory: function():Object { return {
                                  label: "Browse"
                                }}
                              })
                            ]
                          }}
                        })
                      ]
                    }}
                  })
                ]
              }}
            })
          ,
            new mx.core.UIComponentDescriptor({
              type: mx.controls.Button
              ,
              id: "submitBtn"
              ,
              events: {
                click: "__submitBtn_click"
              }
              ,
              propertiesFactory: function():Object { return {
                x: 99,
                y: 61,
                label: "Submit"
              }}
            })
          ]
        }}
      })
    ]
  }}
})

    /**
     * @private
     **/
    public function BasicExample()
    {
        super();

        mx_internal::_document = this;

        //    our style settings


        //    ambient styles
        mx_internal::_BasicExample_StylesInit();

        //    properties
        this.layout = "absolute";

        //    events
        this.addEventListener("applicationComplete", ___BasicExample_Application1_applicationComplete);

    }

    /**
     * @private
     **/
    override public function initialize():void
    {
         mx_internal::setDocumentDescriptor(_documentDescriptor_);



        super.initialize();
    }


            import co.uk.mikestead.net.URLRequestBuilder;
            import co.uk.mikestead.net.URLFileVariable;
            import mx.controls.Alert;
            import mx.collections.ArrayCollection;
            import flash.net.URLVariables;
            import flash.events.*;

            /**
             * Replace this with the location you've placed form_handler.php.
             * This should be under the same domain as the published swf or you'll need
             * a cross domain policy file.
             */
            public const WEB_SERVICE_URL:String = "http://localhost/multipartform/form_handler.php";

            private var filePicker:FileReferenceList;
            private var fileLoadCount:uint;
            private var variables:URLVariables;

            /**
             * Create file picker and set all fields to default values.
             */
            private function setup():void
            {
                filePicker = new FileReferenceList()
                filePicker.addEventListener(Event.SELECT, onFilesPicked);
                resetFields();
            }

            /**
             * Event handler for file picked event. This gives us the array of files
             * selected by the user.
             *
             * As soon as the files are selected we load them into memory. We do this
             * right away as this action (as of player 10) needs to be in response to
             * a user interaction.
             *
             * If we chose to load these when the user clicked "submit" then they would
             * have to click submit twice because, as of player 10, you can not send a URLRequest
             * containing mulitipart data unless it is also triggered by a user interaction event.
             */
            private function onFilesPicked(event:Event):void
            {
                var fileList:Array = filePicker.fileList;
                filesGrid.dataProvider = new ArrayCollection(fileList);
                loadFileVariables();
                submitBtn.enabled = false;
            }

            /**
             * Load the files the user has selected
             */
            private function loadFileVariables():void
            {
                fileLoadCount = 0;
                variables = new URLVariables();
                var fileList:Array = filePicker.fileList;
                for each(var file:FileReference in fileList)
                {
                    file.addEventListener(Event.COMPLETE, onFileInMemory)
                    file.addEventListener(IOErrorEvent.IO_ERROR, onError);
                    // could listen to progress events here too
                    file.load();
                }
            }

            /**
             * Event handler called each time an file loads into memory.
             *
             * Here we wrap the file data in a URLFileVariable and add it to our URLVariables.
             */
            private function onFileInMemory(event:Event):void
            {
                var file:FileReference = FileReference(event.target);
                variables["file" + fileLoadCount] = new URLFileVariable(file.data, file.name);

                if (++fileLoadCount >= filePicker.fileList.length)
                    submitBtn.enabled = true;
            }

            /**
             * Called when user clicks the submit button.
             *
             * Adds string variables to URLVaraibles instance and then asks URLRequestBuilder
             * to create a URLRequest with these. Then create a URLLoader and send the data
             * off to the server.
             */
            private function submit():void
            {
                variables.userName = userName.text;
                variables.userAge = userAge.text;

                var request:URLRequest = new URLRequestBuilder(variables).build();
                request.url = WEB_SERVICE_URL;

                var loader:URLLoader = new URLLoader();

                loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
                loader.addEventListener(IOErrorEvent.IO_ERROR, onError);

                loader.addEventListener(Event.COMPLETE, onFormSubmitSuccess)
                try
                {
                    loader.load(request);
                }
                catch (error:Error)
                {
                    trace("Unable to dispatch load request: " + error);
                }
            }

            /**
             * Event handler called when server returns with a valid response.
             */
            private function onFormSubmitSuccess(event:Event):void
            {
                var loader:URLLoader = URLLoader(event.target);
                Alert.show("Form was submitted successfully\n\n" + loader.data);
                resetFields();
            }

            /**
             * Event handler to deal with any issues during file load/upload.
             */
            private function onError(event:Event):void
            {
                Alert.show(event.toString());
                submitBtn.enabled = true;
            }

            /**
             * Reset all fields to their defaults.
             */
            private function resetFields():void
            {
                fileLoadCount = 0;
                variables = new URLVariables();
                filesGrid.dataProvider = null;
                submitBtn.enabled = true;
            }
        



    //    supporting function definitions for properties, events, styles, effects
/**
 * @private
 **/
public function ___BasicExample_Application1_applicationComplete(event:mx.events.FlexEvent):void
{
    setup()
}

private function _BasicExample_DataGridColumn1_c() : mx.controls.dataGridClasses.DataGridColumn
{
    var temp : mx.controls.dataGridClasses.DataGridColumn = new mx.controls.dataGridClasses.DataGridColumn();
    temp.headerText = "Name";
    temp.dataField = "name";
    return temp;
}

private function _BasicExample_DataGridColumn2_c() : mx.controls.dataGridClasses.DataGridColumn
{
    var temp : mx.controls.dataGridClasses.DataGridColumn = new mx.controls.dataGridClasses.DataGridColumn();
    temp.headerText = "Size";
    temp.dataField = "size";
    return temp;
}

/**
 * @private
 **/
public function ___BasicExample_Button1_click(event:flash.events.MouseEvent):void
{
    filePicker.browse();
}

/**
 * @private
 **/
public function __submitBtn_click(event:flash.events.MouseEvent):void
{
    submit()
}




    mx_internal static var _BasicExample_StylesInit_done:Boolean = false;

    mx_internal function _BasicExample_StylesInit():void
    {
        //    only add our style defs to the StyleManager once
        if (mx_internal::_BasicExample_StylesInit_done)
            return;
        else
            mx_internal::_BasicExample_StylesInit_done = true;

        var style:CSSStyleDeclaration;
        var effects:Array;


        StyleManager.mx_internal::initProtoChainRoots();
    }




}

}