var FormGuard = Class.create();

FormGuard.prototype = {
  
  initialize : function(form, options) {
    this.submitted = false;
    this.form = $(form);
    this.origcontents = $(form).serialize(false);
    this.form.onsubmit = function(){ this.submitted = true; }.bind(this);
    window.onbeforeunload = this.confirmExit.bind(this);
  },
  confirmExit : function(ev) {
    this.newcontents = this.form.serialize(false);
    // save the notes slot if available
    if ($('content_notes_form')){
      new Ajax.Request($('content_notes_form').action, {asynchronous:true, evalScripts:false, parameters:'entity_module=' + $('notes_entity_module').value + '&entity_id=' + $('notes_entity_id').value + '&content=' + $('notes_content').value });
    }
    if (!skipOnBeforeUnloadCheck && !(this.submitted)) {
      return "Any changes you have made will be lost.";
    }
  }
}

window.onload = function() {
  if ($('afta_generic_form')){
    new FormGuard('afta_generic_form');    
    Event.observe($('afta_generic_form'), 'keypress', function(event) {
      var elem = Event.element(event);
      if (event.keyCode == Event.KEY_RETURN && elem.tagName != 'TEXTAREA'){
        Event.stop(event);
      }
    });
  }
  if ($('content_ident')){
    document.cookie = 'unlock_request=' + $('content_ident').value + '; path=/';
  }
}

