if (!DGN) var DGN = { };
DGN.DateInput = Class.create({
    initialize: function(inputname) {
        this.input = $(inputname);
        this.input.maxLength = 10;

        this.input.observe('keydown', this.onKeypress.bindAsEventListener(this));
        this.input.observe('change',  this.onChange.bindAsEventListener(this));

        // Onchange alternative for IE. Onchange doesn't fire in IE when the value is set, for some reason.
        this.startValue = this.input.getValue();
    },
    onChange: function(e) {
        rawvalue   = this.input.getValue();
        cleanvalue = rawvalue.replace(/[^0-9]/g,'');
        if(cleanvalue.length > 8) {
            cleanvalue = cleanvalue.substring(0,8);
        }
        splitvalue = rawvalue.match(/[\/-]/)
                         ? rawvalue.replace(/\//g, '-')
                                   .replace(/[^0-9\/-]/g, '')
                                   .split('-')
                                   .filter(function(e){ return !!e })
                         : false;

        if(cleanvalue.length == 8 || splitvalue !== false) {
            day   = splitvalue ? splitvalue[0] : cleanvalue.substring(0,2);
            month = splitvalue ? splitvalue[1] : cleanvalue.substring(2,4);
            year  = splitvalue ? splitvalue[2] : cleanvalue.substring(4,8);
        }
        else {
            now = new Date();

            if (cleanvalue.substr(cleanvalue.length-4) > 1990 && cleanvalue.substr(cleanvalue.length-4) <= parseInt(now.getFullYear().toString())) {
                if (cleanvalue.substring(1,cleanvalue.length-4) <= 12 || cleanvalue.length == 6) {
                    day   = cleanvalue.substring(0,1);
                    month = cleanvalue.substring(1,cleanvalue.length-4);
                }
                else {
                    day   = cleanvalue.substring(0,2);
                    month = cleanvalue.substring(2,cleanvalue.length-4);
                }
                year  = cleanvalue.substr(cleanvalue.length-4);
            }
            else {
                day   = cleanvalue.substring(0,2);
                month = cleanvalue.substring(2,4);
                year  = cleanvalue.substr(4);
            }
        }

        if (day.length == 1)   day   = '0' + day;
        if (month.length == 1) month = '0' + month;
        if (year.length == 2) {
            year = year < now.getFullYear().toString().substr(2,2) ? '20' + year : '19' + year;
        }

        value = day + '-' + month + '-' + year;
        this.input.setValue(value);
    },
    onKeypress: function(e) {
        if(e && e.keyCode) {
            switch(e.keyCode) { // allow moving through field
                case Event.KEY_LEFT:
                case Event.KEY_UP:
                case Event.KEY_RIGHT:
                case Event.KEY_DOWN:
                case Event.KEY_DELETE:
                case Event.KEY_HOME:
                case Event.KEY_END:
                case Event.KEY_PAGEUP:
                case Event.KEY_PAGEDOWN:
                case 16: //SHIFT
                case 17: //CONTROL
                case 18: //ALT
                case 8:  //BACKSPACE
                case 9:  //TAB
                case 13: //ENTER
                    return;
            }
        }
        if(e.ctrlKey === true) return;
        
        if (((e.keyCode < 48 || e.keyCode > 57)  // 0-9 
            && (e.keyCode < 96 || e.keyCode > 105) // 0-9 numpad
            && e.keyCode  != 109 // dash
            && e.keyCode  != 189 // dash
            && e.keyCode  != 111 // slash
            && e.keyCode  != 191) // slash
            ) {
            e.stop();
        }
    }
});
