<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
    layout="vertical"
    creationComplete="init()" 
    xmlns:local="*" viewSourceURL="srcview/index.html">
    <mx:Script>
        <![CDATA[
            import forms.VOFormHelper;
            import forms.FormHelpEnabled;
            import dom.Address;
            import dom.User;
            import forms.FormEvent;
            import mx.controls.Alert;
            import forms.FormHelper;
            import forms.FormHelperImpl;
            import mx.containers.Form;
            import flash.utils.describeType;
            import mx.utils.ObjectUtil;

            [Bindable]
            public var labelContent:Object;
            
            private var formhelper:FormHelper;
            private var formhelper2:FormHelper;
            
            public function init():void {
                // configure a value object form helper
                formhelper = new VOFormHelper(FormHelpEnabled(regForm), User);
                VOFormHelper(formhelper).validatorFields = setupValidation();
                formhelper.addEventListener(FormEvent.SEND, function(fe:FormEvent):void {
                    if (fe.resultObject is User) {
                        var u:User = User(fe.resultObject);
                        Alert.show("Form processed and returning a User object:\n" + ObjectUtil.toString(u));
                    }
                });
                regForm.formHelper = formhelper;
            }
            private function initAddress():void {
                formhelper2 = new FormHelperImpl(FormHelpEnabled(regForm2), Address);
                formhelper2.addEventListener(FormEvent.SEND, function(fe:FormEvent):void {
                    if (fe.resultObject is Address) {
                        var p:Address = Address(fe.resultObject);
                        Alert.show("Form processed and returning an Address object:\n" + ObjectUtil.toString(p));
                    }
                });
                regForm2.formHelper = formhelper2;
            }
            /**
             * This method puts together an array where the key is the
             * field name of the target Class and the value being the
             * chosen validator.
             **/
            public function setupValidation():Array {
                var vals:Array = new Array();
                vals["username"] = nameValidator;
                vals["email"] = emailValidator;
                vals["recieveEmail"] = recieveEmailValidator;
                vals["gender"] = genderInputValidator;
                return vals;
            }
        ]]>
    </mx:Script>
    <!-- 
        Common set of Validators which are easily reused in different forms.
        Removes the tight coupling of the validators and the form.   
    -->
    <mx:StringValidator 
        id="nameValidator"
        property="text"
        minLength="2"/>
    <mx:StringValidator 
        id="recieveEmailValidator"
        property="selected"
        required="true"
        minLength="4"/>
    <mx:NumberValidator 
        id="genderInputValidator"
        property="selectedIndex"
        required="true"
        minValue="1"/>
    <mx:EmailValidator
        id="emailValidator"
        property="text"/>
    
    <mx:TabNavigator height="350" width="400">
        <local:RegistrationForm2 id="regForm"
            label="User information"/>
        <local:RegistrationForm id="regForm2"
            label="Address information"
            creationComplete="initAddress()">
        </local:RegistrationForm>
    </mx:TabNavigator>
</mx:Application>