if (!window.Collegien)
    var Collegien = new Object();
Collegien.DOB = Class.create();
Collegien.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 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() {
        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 date.';
        } else {
            var date = new Date();
            if(this.year.value%4==0 && this.year.value%100!=0 || this.year.value/400==0) {
        		// Année bissextile
        		
        		if(this.day.value>29 && this.month.value==02){
        			if (Translator) {
        				error=Translator.translate('Only 29 days in february in ')+this.year.value;
        			} else {
        				error='Only 29 days in february in '+this.year.value+'';
        			}
        		}
        		
        	} else {
        		// Année NON bissextile
        	
        		if(this.day.value>28 && this.month.value==02){
            		if (Translator) {
        				error=Translator.translate('Only 28 days in february in ')+this.year.value;
        			} else {
        				error='Only 28 days in february in '+this.year.value+'';
        			}
        		}
        		
        	}
            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 = Translator.error;
            }
            this.advice.show();
            return false;
        }

        this.advice.hide();
        return true;
    }
}

Validation.addAllThese([
    ['validate-custom', ' ', function(v,elm) {
        return elm.validate();
    }]
]);








/**
 *  Function to triger a blur from the form with a focus on the submit button
 */
function addBlurFormOnSubmitButton()
{
	jQuery(':submit').bind('mouseover',function(){this.focus();});
	jQuery('a[class*="black-button"]').bind('mouseover',function(){this.focus();});
}


jQuery(document).ready(function(){addBlurFormOnSubmitButton();});


/**
 * Function to show/hide checkout agreements
 */
function showAgreements(id)
{
    jQuery('#checkout-agreements-'+id).slideToggle();
}


/*
 * vars to avoid bug on IE
 */
var lastPrice = 0;
var quoteBaseGrandTotal = 0;

