var LOCKABLE_CLASS = '.s-lockable';
var CLICK_EVENT = 'click';
var ORIGINAL_ONCLICK_ATTR = 'data-original-onclick';
var ONCLICK_ATTR = 'onclick';

var lockedHandler = function(event) {
    event.preventDefault();
}

/**
  * @description Locks all page elements with a class of 's-lockable' while an AJAX request is still loading, preventing
  *              double submissions of the same content.
  */
function clickLockStart() {
    jQuery(LOCKABLE_CLASS).on(CLICK_EVENT, lockedHandler);

    var onClick = jQuery(LOCKABLE_CLASS).attr(ONCLICK_ATTR);

    jQuery(LOCKABLE_CLASS).attr(ORIGINAL_ONCLICK_ATTR, onClick);
    jQuery(LOCKABLE_CLASS).attr(ONCLICK_ATTR, null);
}

/**
  * @description Unlocks all page elements with a class of 's-lockable' allowing them to be clicked again.
  */
function clickLockStop() {
    jQuery(LOCKABLE_CLASS).off(CLICK_EVENT, lockedHandler);

    var originalOnclick = jQuery(LOCKABLE_CLASS).attr(ORIGINAL_ONCLICK_ATTR);

    jQuery(LOCKABLE_CLASS).attr(ORIGINAL_ONCLICK_ATTR, null);
    jQuery(LOCKABLE_CLASS).attr(ONCLICK_ATTR, originalOnclick);
}

/**
 * @description Determines if a given element's (usually a submit button) form will validate.
 */
function canLockForm(element) {
    return (element.form == null || element.form.checkValidity());
}