<?xml version="1.0" encoding="utf-8"?>
<!--
 *   NL4B / NL for Business
 * 
 *   Copyrights 2008 NL for Business / Theo van der Sluijs
 *
 *     Author : Theo van der Sluijs / theo.van.der.sluijs@nl4b.com
 *
 *   All Rights Reserved.
 *
 *   Creative Commons Attribution-Noncommercial 3.0
 *   
 *   You are free:
 *     to Share — to copy, distribute and transmit the work
 *     to Remix — to adapt the work
 *   
 *   Under the following conditions:
 *        Attribution — You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work).
 *       Noncommercial — You may not use this work for commercial purposes
 * 
 *   With the understanding that:
 *     Waiver — Any of the above conditions can be waived if you get permission from the copyright holder.
 * 
 *     Other Rights —     In no way are any of the following rights affected by the license:
 *             Your fair dealing or fair use rights;
 *            The author's moral rights;
 *            Rights other persons may have either in the work itself or in how the work is used, such as publicity or privacy rights.
 *
 *     Notice — For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to this web page. 
 * 
 *   http://creativecommons.org/licenses/by-nc/3.0/
 *   
 *   www.nl4b.com              info@nl4b.com
 *   www.iamboredsoiblog.eu    info@iamboredsoiblog.eu
-->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" 
    xmlns:vl1="com.scripts.*" viewSourceURL="srcview/index.html">
<mx:Script>
    <![CDATA[
        import mx.events.ValidationResultEvent;
        import mx.validators.Validator;
        import com.scripts.StartEndDateValidator;
        
        // Holds a reference to the currently focussed 
        // control on the form.
        private var focussedFormControl:DisplayObject;
        
        [Bindable]
        public var formIsValid:Boolean=false;
        
        [Bindable]
        public var formIsEmpty:Boolean=true;
                
                // Validate the form
        private function validateForm(event:Event):void
        {
            // Save a reference to the currently focussed form control
            // so that the isValid() helper method can notify only
            // the currently focussed form control and not affect
            // any of the other form controls.
            focussedFormControl=event.target as DisplayObject;
        
            // Mark the form as valid to start with                
            formIsValid=true;
        
            // Check if form is empty
            formIsEmpty=(tfProjEnd.text == "");
        
            // Run each validator in turn, using the isValid() 
            // helper method and update the value of formIsValid
            // accordingly.
        
            validate(ValStartEndDate);
        }
        
        // Helper method. Performs validation on a passed Validator instance.
        // Validator is the base class of all Flex validation classes so 
        // you can pass any validation class to this method.  
        private function validate(validator:Validator):Boolean
        {
            // Get a reference to the component that is the
            // source of the validator.
            var validatorSource:DisplayObject=validator.source as DisplayObject;
        
            // Suppress events if the current control being validated is not
            // the currently focussed control on the form. This stops the user
            // from receiving visual validation cues on other form controls.
            var suppressEvents:Boolean=(validatorSource != focussedFormControl);
        
            // Carry out validation. Returns a ValidationResultEvent.
            // Passing null for the first parameter makes the validator 
            // use the property defined in the property tag of the
            // <mx:Validator> tag.
            var event:ValidationResultEvent=validator.validate(null, suppressEvents);
        
            // Check if validation passed and return a boolean value accordingly.
            var currentControlIsValid:Boolean=(event.type == ValidationResultEvent.VALID);
        
            // Update the formIsValid flag
            formIsValid=formIsValid && currentControlIsValid;
        
            return currentControlIsValid;
        }
                
        /**
         * a nice function to open a url
         * */
        public function getURL(url:String, window:String):void {
            var request:URLRequest;
            request = new URLRequest(url);
            navigateToURL(request, window);
        }            
        
    ]]>
</mx:Script>
<vl1:StartEndDateValidator id="ValStartEndDate"
        source="{tfProjEnd}"
        property="text"
        inputFormat="DD.MM.YYYY"
        matches="{tfProjStart.text}" />

    <mx:Panel width="270" height="250" layout="absolute" horizontalCenter="0" verticalCenter="0" title="Start &amp; End Date Validator">
        <mx:Label x="41" y="23" text="Start"/>
        <mx:Label x="47" y="69" text="End"/>
        <mx:DateField x="80" y="21" id="tfProjStart" showToday="true" yearNavigationEnabled="true" formatString="DD.MM.YYYY" change="validateForm(event);" />
        <mx:DateField x="80" y="67" id="tfProjEnd" showToday="true" yearNavigationEnabled="true" formatString="DD.MM.YYYY" change="validateForm(event);"/>
        <mx:Button x="80" y="97" label="Check" click="validateForm(event);"/>
        <mx:ControlBar>
            <mx:Label text="www.nl4b.com" textDecoration="underline" color="#FFFFFF" fontWeight="bold" useHandCursor="true" buttonMode="true" mouseChildren="false" click="getURL('http://www.nl4b.com', '_blank');"/>
            <mx:Label text="www.iamboredsoiblog.eu" textDecoration="underline" color="#FFFFFF" fontWeight="bold" useHandCursor="true" buttonMode="true" mouseChildren="false" click="getURL('http://www.iamboredsoiblog.eu', '_blank');"/>
        </mx:ControlBar>
    </mx:Panel>
    
</mx:Application>