
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;
    }
}

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


