if (!window.Baobaz)
    var Baobaz = new Object();
    
    
Baobaz.DOB = Class.create();

Baobaz.DOB.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 input')[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.year.setAttribute('autocomplete','off');
    
        this.advice.hide();
    },
    
    validate: function() {
        var error = false;
        this.clearErrors();     
        
        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=='') {
           
            if(this.day.value=='')
                this.day.addClassName('validation-dob-failed');
            if(this.month.value=='')
                this.month.addClassName('validation-dob-failed');
            if(this.year.value=='')
                this.year.addClassName('validation-dob-failed');
                
            error = 'Please enter a valid full date.';
        } else {
            var date = new Date();
            if (this.day.value<1 || this.day.value>31) {
                this.day.addClassName('validation-dob-failed');
                error = 'Please enter a valid day (1-31).';
            } else if (this.month.value<1 || this.month.value>12) {
                this.month.addClassName('validation-dob-failed');
                error = 'Please enter a valid month (1-12).';
            } else if (this.year.value<1900 || this.year.value>date.getFullYear()) {
                this.year.addClassName('validation-dob-failed');
                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)) {
                    this.day.addClassName('validation-dob-failed');
                    this.month.addClassName('validation-dob-failed');
                    this.year.addClassName('validation-dob-failed');
                    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();
            return false;
        }

        if (this.day.value=='' && this.month.value=='' && this.year.value=='')
            this.year.up('.customer-dob').removeClassName('validation-error');
        else        
            this.year.up('.customer-dob').removeClassName('validation-error').addClassName('validation-passed');
    
        this.advice.hide();
        return true;
    },

    clearErrors: function() {
        this.day.removeClassName('validation-dob-failed');
        this.month.removeClassName('validation-dob-failed');
        this.year.removeClassName('validation-dob-failed');
    }

}

