/**
 *   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
 * */
 
package com.scripts
{
    import mx.validators.DateValidator;
    import mx.validators.ValidationResult;

    public class StartEndDateValidator extends DateValidator
    {
        
        // Define Array for the return value of doValidation().
        private var results:Array;

        // Define compare String
        private var _matches : String = "";
        
        //Define dateformat
        private var _dFormat : String = "DD/MM/YYYY";

        // Define mismatch error messsage
        private var _mismatchError : String = "End date smaller than start date? Do you own a DeLorean with Flux Capacitor?";        
        
        // Constructor.
        public function StartEndDateValidator()
        {
            super();
        }

        // let's get the start date 
        public function set matches (s : String ) : void {
            _matches = s;
        }

        // let's get the correct date format
        public function set dFormat (s : String ) : void {
            _dFormat = s;
        }
        
        // create the mismatcherror
        public function set mismatchError (s : String) : void {
                _mismatchError = s;
        }        
        
        // Define the doValidation() method.
        override protected function doValidation(value:Object):Array 
        {

            var sDate: String = _matches; //start date
            var eDate: String = source.text; //end date
            var dFormat: String= _dFormat; //date format

            results = [];
            results = super.doValidation(value);

            if(sDate == "" || eDate == ""){
                return results;
            }

            var Yindex:Number = dFormat.indexOf("YYYY"); //let's get the correct year from to place
            var Dindex:Number = dFormat.indexOf("DD"); //let's get the correct day from to place
            var Mindex:Number = dFormat.indexOf("MM"); ////let's get the correct month from to place

            var StartDate:Date = new Date(sDate.substr(Yindex, 4), sDate.substr(Mindex, 2), sDate.substr(Dindex, 2)); //create a real date from it
            var StartEnd:Date  = new Date(eDate.substr(Yindex, 4), eDate.substr(Mindex, 2), eDate.substr(Dindex, 2)); //create a real date from it

            var StartTimestamp : Number = StartDate.getTime (); // create milisec vrom start date
            var EndTimestamp : Number = StartEnd.getTime ();    // create milisec vrom end date        
                       

    
            // any results yet ?
            if (results.length > 0)
            {
                return results;
            }
                
            if (StartTimestamp == EndTimestamp){// start date same as end date = OKAY
                 return results;
            } else if (StartTimestamp < EndTimestamp){ //end date bigger then start date = OKAY
                 return results;
            } else { // error error, get the delorean ready to fix this time disortion.
                results.push(new ValidationResult(true, null, "Date Error", _mismatchError));
                return results;
            }

        }

    }
}