(function () {
    'use strict';

    document.querySelectorAll('[data-character-counter]').forEach(function (input) {
        var maximum = input.maxLength;
        if (maximum <= 0) {
            return;
        }

        var counter = document.createElement('small');
        counter.className = 'help-block text-right';
        counter.setAttribute('aria-live', 'polite');
        counter.setAttribute('data-character-counter-output', '');
        input.insertAdjacentElement('afterend', counter);

        function updateCounter() {
            counter.textContent = input.value.length + ' / ' + maximum;
        }

        input.addEventListener('input', updateCounter);
        updateCounter();
    });
})();
