
Varien.DOBDropDown = Class.create();
Varien.DOBDropDown.prototype = {
    initialize: function(selector, required, format) {
        var el        = $$(selector)[0];
        this.day      = Element.select($(el), '.dob-day select')[0];
        this.month    = Element.select($(el), '.dob-month select')[0];
        this.year     = Element.select($(el), '.dob-year select')[0];
        this.dob      = Element.select($(el), '.dob-full input')[0];
        this.advice   = Element.select($(el), '.validation-advice')[0];
        this.required = required;
        this.format   = format;

        this.day.validate = this.validate.bind(this);
        this.month.validate = this.validate.bind(this);
        this.year.validate = this.validate.bind(this);

        this.advice.hide();
    },

    validate: function(element) { /* ascendum change :- added the paramter element */
        var error = false;

        if (this.day.value=='' && this.month.value=='' && this.year.value=='') {
            if (this.required) {
                error = 'This date is a required value.';
            } else {
                this.dob.value = '';
            }
        } else if (this.day.value=='' || this.month.value=='' || this.year.value=='') {
            error = 'Please enter a valid full date.';
        } else {
            var date = new Date();
            if (this.day.value<1 || this.day.value>31) {
                error = 'Please enter a valid day (1-31).';
            } else if (this.month.value<1 || this.month.value>12) {
                error = 'Please enter a valid month (1-12).';
            } else if (this.year.value<1900 || this.year.value>date.getFullYear()) {
                error = 'Please enter a valid year (1900-'+date.getFullYear()+').';
            } else {
                this.dob.value = this.format.replace(/(%m|%b)/i, this.month.value).replace(/(%d|%e)/i, this.day.value).replace(/%y/i, this.year.value);
                var testDOB = this.month.value + '/' + this.day.value + '/'+ this.year.value;
                var test = new Date(testDOB);
                if (isNaN(test)) {
                    error = 'Please enter a valid date.';
                }
            }
        }

        if (error !== false) {
            try {
                this.advice.innerHTML = Translator.translate(error);
            }
            catch (e) {
                this.advice.innerHTML = error;
            }
            this.advice.show();

            // Ascendum change
            var invalidinput = false;
            
            switch(element.id)
            { 
                case 'month': 
                            
                        if (element.value<1 || element.value>12)
                        { 
                            invalidinput = true;
                        }
                        break;

                case 'day':
                        if (element.value<1 || element.value>31)
                        {  
                            invalidinput = true;
                        }
                        break;

                case 'year':
                        if (element.value<1900 || element.value>date.getFullYear())
                        {
                            invalidinput = true;
                        }
                        break;
            }
            
            if(invalidinput) return false;
            // End Ascendum change
        }

         if(!error)this.advice.hide();/* ascendum change :- added if condition in this line */
        return true;
    }
}

// Added by Ascendum #23
Validation.addAllThese([
    ['validate-date-mmddyyyy', 'Please use Valid Date with format:mm/dd/yyyy', function(v) {
                if(v=="00/00/0000") return true;
                if(Validation.get('IsEmpty').test(v)) return true;
                var regex = /^(\d{2})\/(\d{2})\/(\d{4})$/;
                if(!regex.test(v)) return false;
                var d = new Date(v.replace(regex, '$1/$2/$3'));
                return  (parseInt(RegExp.$1, 10) == (1+d.getMonth()) ) &&
                                (parseInt(RegExp.$2, 10) == d.getDate()) &&
                                (parseInt(RegExp.$3, 10) == d.getFullYear() );

      }]
]);

//Added by Deloitte - Validation for dob in account creation/edit page
Validation.addAllThese([
    ['validate-date-mmdd', 'Please use Valid Date with format:mm/dd', function(v) {
                if(v=="00/00/0000") return true;
                if(Validation.get('IsEmpty').test(v)) return true;
                var regex = /^(\d{2})\/(\d{2})\/(\d{4})$/;
                if(!regex.test(v)) return false;
                var d = new Date(v.replace(regex, '$1/$2/$3'));
                return  (parseInt(RegExp.$1, 10) == (1+d.getMonth()) ) &&
                                (parseInt(RegExp.$2, 10) == d.getDate()) &&
                                (parseInt(RegExp.$3, 10) == d.getFullYear() );

      }]
]);
Validation.addAllThese([
    ['validate-custom', ' ', function(v,elm) {
        return elm.validate(elm);/* ascendum change :- added the parameter elm */
    }]
]);



