var MyFormHandler = Class.create({
initialize: function(form) {
this.form = $(form);
this.index = 0;
// attach handlers
this.elements = {};
this.elements.day = this.form.select('.day')[0];
this.elements.day.observe('change', this.onDayChanged.bindAsEventListener(this));
},
onDayChanged: function(event) {
// check date value and see if we should focus to the next element
if (this.elements.day.value.match(/\d+/) && 0 < this.elements.day.value && ... ) {
this.focusNext();
}
},
focusNext: function() {
this.index++;
this.form.getElements()[this.index].focus();
}
});
// crate a new object for each form (after they exist in the DOM of course)
new MyFormHandler('formId');