var $formHasBeenEdited = 0;
var $formIsBeingSubmitted = false;

$(document).ready(function() {
  var m = "form[method='post'] input, form[method='post'] select, form[method='post'] textarea";
  $(m).change(function() {
    $formHasBeenEdited++;
  });

  $("form[method='post']").submit(function() {
    $formIsBeingSubmitted = true;
    return true;
  });
});

$(window).bind('beforeunload', function() {
  if($formIsBeingSubmitted) {
    $formIsBeingSubmitted = false;
    return null;
  }

  if($formHasBeenEdited > 0)
    return 'Are you sure you want to leave?';
  else
    return null;
});

@@ -424,32 +424,117 @@
 ubmitted) %7B%0A    
+// timeout required because beforeunload may be fired multiple times%0A    setTimeout(%22
 $formIsBeingSubm
@@ -546,16 +546,23 @@
  = false
+%22, 500)
 ;%0A    re

handle multiple beforeunload events