/* Mechanism to prevent double clicking on forms and links. Should be loaded on all pages */
/* Problem - only works for submits - my ajax buttons do not work correctly */

/* For use in ajax forms  - I would prefer something generic */
function checkSingleClick() {
    if (this.beenSubmitted){
      var now = new Date();
      if ((now - this.submitTime) < 3000) {
        window.status = 'double-click ignored';
        return false;
      }
    }
    this.beenSubmitted = true;
    this.submitTime = new Date();
    window.status = 'submitted';
    return true;
}

$(document).ready(function() {
  $('form').submit(checkSingleClick);

  var clickFunction = function() {
        if (this.beenSubmitted){
          var now = new Date();
          if ((now - this.submitTime) < 3000) {
            window.status = 'double-click ignored';
            return false;
          }
        }
        this.beenSubmitted = true;
        this.submitTime = new Date();
        window.status = 'submitted';
        return true;
    };

    var preventLinkDoubleClick = function() {
      if (this.onclick === undefined) {
          var events = jQuery.data(this, "events");
          if (events && events.click){return;}
          $(this).click(clickFunction);
      }
    };
    $('a').not('.no-dblclick-prevention').each(preventLinkDoubleClick);
});

