// Reference: http://salesforce.stackexchange.com/questions/7729/disable-commandbutton-after-first-click-to-prevent-double-submission
function enableButtons(enabled) {
    // retrieve all of the buttons or links on the page
    // with the css class of btn
    var buttons = jQuery('.btn');
    if (enabled === false) {
        // add the btnDisabled class to give it the look of being disabled
        // add the disabled attribute to actually disable interactability
        buttons.toggleClass('btnDisabled', true).attr('disabled', 'disabled');
    } else {
        // remove the css class and the disabled attribute
        buttons.toggleClass('btnDisabled', false).attr('disabled', null);
    }
}

// Reference: http://salesforce.stackexchange.com/questions/44368/scroll-down-automatically-after-clicking-on-commandbutton-or-we-can-say-pagebloc
function scrollTo(id) {
    document.querySelector(id).scrollIntoView();
}

function submitButton(pageAction) {
    // first, call the action function to post the form
    pageAction();

    // second, disable the buttons
    enableButtons(false);

    // third, return false to prevent the click from
    // posting the form a second time
    return false;
}

/**
  * @description Converts Salesforce's rendered HTML Ids to ones jQuery can use.
  *              Reference: http://th3silverlining.com/2010/02/17/visualforce-ids-in-jquery-selectors/
  * @param oldId The Id to convert.
  */
function sfIdTojQueryFriendlyId(oldId) {
    return oldId.replace( /(:|\.|\[|\]|,)/g, "\\$1" );
}

/**
  * @description Hides the specified element.
  * @param elementId The Id of the element to hide.
  */
function disablePage(elementId) {
    $('#' + elementId).hide();
}

/**
  * @description Shows the specified element.
  * @param elementId The Id of the element to show.
  */
function enablePage(elementId) {
    $('#' + elementId).show();
}

/**
 * @description Hides the specified Salesforce Component by its Id.
 * @param elementId The Id of the Salesforce Component to hide.
 */
function hideSalesforceComponent(elementId) {
    var cleanId = sfIdTojQueryFriendlyId(elementId);
    disablePage(cleanId);
}