(function($){
  // Apply this to form elements
  $.fn.unsavedWarning = function(options) {
    function setConfirmUnload(on) {
      //alert('changed');
      window.onbeforeunload = on ? function(){
        return "You have made changes on this page. If you navigate away, your changes will be lost. Do you want to continue anyway?";
      } : null;
    }
    // Watch all inputs
    $(":input", this.get(0)).bind("change", function() { setConfirmUnload(true); });
    // Don't confirm when submitting
    $(this.get(0)).submit(function () {setConfirmUnload(false)});
  };
  
})(jQuery);
