//************** JQuery extension **************//

(function ($) {
    var jqAppend = $.fn.append;
    $.fn.append = function () {
        // Make a list of arguments that are jQuery objects
        var appendages = $.makeArray(arguments).filter(function (arg) {
            return arg instanceof $;
        });

        // Call the actual function
        var returnValue = jqAppend.apply(this, arguments);

        // Trigger "append" event on all jQuery objects that were appended
        for (var i = 0; i < appendages.length; ++i) {
            appendages[i].trigger('append');
        }

        return returnValue;
    };
})(jQuery)

jQuery.fn.restrictInput = function () {
    return this.each(function () {
        var input = $(this);
        var restriction = input.attr("data-restriction");

        switch (restriction) {
            case "numberOnly":
                input.keydown(function (event) {
                    var keyCode = ('which' in event) ? event.which : event.keyCode;

                    isNumeric = (keyCode >= 48 /* KeyboardEvent.DOM_VK_0 */ && keyCode <= 57 /* KeyboardEvent.DOM_VK_9 */) ||
                        (keyCode >= 96 /* KeyboardEvent.DOM_VK_NUMPAD0 */ && keyCode <= 105 /* KeyboardEvent.DOM_VK_NUMPAD9 */);

                    isControlChar = keyCode === 8;
                    isDecimalChar = keyCode === 188;

                    modifiers = (event.altKey || event.ctrlKey || event.shiftKey);

                    if ((!isNumeric && !isControlChar && !isDecimalChar) || modifiers) {
                        event.preventDefault();
                    }
                });
                break;
        }
    });
};

//************** Array extension **************//

Array.prototype.sum = function () {
    var r = 0;
    for (var i = 0; i < this.length; i++) {
        r += this[i];
    }
    return r;
};

Array.prototype.avg = function () {
    var r = 0;
    for (var i = 0; i < this.length; i++) {
        r += this[i];
    }
    return r / this.length;
};

//************** Number extension *************//

Number.prototype.toUntrailingFixed = function (decimalPlaces) {
    return (this).toFixed(decimalPlaces).replace(/([0-9]+(\.[0-9]+[1-9])?)(\.?0+$)/, '$1');
}