var InstructElement = Class.create();

InstructElement.prototype = {

    control : null,

    initialize: function( controlId, options ) {

        this.options = Object.extend( {
            defaultText : "Enter plate or VIN",
            defaultClass : "default",
            trigger : "keypress",
            focus: false,
            uppercase: true
        }, options || {} );

        this.control = $( controlId );
        this.control.observe( this.options.trigger, this.onKeyPress( this ) );
        if ( this.options.uppercase ) {
            this.control.observe( 'keyup', this.onKeyUp( this ) );
        }
        this.setupDefault();
        if ( this.options.focus ) {
            this.control.focus();
        }
    },

    setupDefault: function() {
        if ( this.control.value == '' || this.control.value == this.options.defaultText ) {
            this.control.setValue( this.options.defaultText );
            this.control.addClassName( this.options.defaultClass );
        }
    },

    onKeyPress: function( parent ) {
        return function( event ) {
            var element = event.element();

            var char = String.fromCharCode( event.which || event.keyCode );
            if ( parent.options.trigger != "keypress" || /[a-z,A-Z,0-9]/.test( char ) ) {
                if ( element.value == parent.options.defaultText ) {
                    element.setValue( "" );
                    element.removeClassName( parent.options.defaultClass );
                }
            }
        }
    },

    onKeyUp: function( parent ) {
        return function( event ) {
            var char = String.fromCharCode( event.which || event.keyCode );
            if ( /[A-Z]/.test( char ) ) {
                var element = event.element();
                element.value = element.value.toUpperCase();
            }
        }
    },

    onSubmit: function() {
        if ( this.control.value == this.options.defaultText ) {
            this.control.value = '';
        }
    }
};
