var FormProtector = Class.create({

    form    : null,     // the form being protected
    alert   : false,    // whether or not to show the confirm box

    message : 'Please remember to submit your form',
  
    initialize : function(form, only_focus_first_field)
    {
        this.form = $(form);

        if ( typeof(only_focus_first_field) == 'undefined' ) {
            only_focus_first_field = false;
        }

        if (this.form.getInputs('text').size() > 0) {
            this.form.getInputs('text').first().focus();
        } else {
            this.form.focusFirstElement();
        }

        if (only_focus_first_field) { 
            return;
        }
        
        this.form.observe('submit', this._onFormSubmit.bindAsEventListener(this));

        this.form.getElements().each(function(elt) {
            elt.observe('change', function() {
                this.alert = true;
            }.bindAsEventListener(this));
        }.bind(this));

        Event.observe(window, 'beforeunload', this._onBeforeUnload.bindAsEventListener(this));
    },

    setMessage : function(str)
    {
        this.message = str;
    },

    _onFormSubmit : function(e)
    {
        this.alert = false;
    },

    _onBeforeUnload : function(e)
    {
        if (this.alert)
            e.returnValue = this.message;
    }
});