//Adds sprintf style formatter to strings
String.prototype.format = function() {
    var pattern = /\{\d+\}/g;
    var args = arguments;
    return this.replace(pattern, function(capture) { return args[capture.match(/\d+/)]; });
}

//jQuery addons
;(function($) {

    //Submit a form asynchronously with jQuery post
    //Updated for jQuery 1.3.2 (attr selectors and disabled filter)
    jQuery.fn.submitAjax = function(url, responseHandler) {
        var params = {};

        $(this).find("input[checked], input[type='text'], input[type='hidden'], input[type='password'], input[type='submit'], option[selected], textarea")
        .not(':disabled')
        .each(function() { params[this.name || this.id || this.parentNode.name || this.parentNode.id] = this.value; });

        $.post(url, params, responseHandler);

        return this;
    }

    //Clear all inputs to a form
    jQuery.fn.clearForm = function() {
        $(this).
        find("input[type='text'], input[type='password'], textarea").attr('value', '').end().
        find("input[type='checkbox'], input[type='radio']").attr('checked', false).end().
        find('select').attr('selectedIndex', -1);
    }

    jQuery.log = function(message) {
        if (window.console) {
            console.log(message);
        } else {
            alert(message);
        }
    }

    //Adds a new check box to the parent of a group of check boxes
    //that will check/uncheck all of them when clicked
    //
    //@param this A jQuery object of type input:checkbox, ie $('#formSection input:checkbox')
    //@param labelHtmlStr A string of html to be parsed and added after the new checkbox
    jQuery.fn.addSelectAllCheckbox = function(labelHtmlStr) {

        //Must run on collection of input:checkbox
        var checkBoxes = this;
        var allCheck = $('<input type="checkbox" />');
        var label = $(labelHtmlStr);
        
        checkBoxes.parent().prepend(label).prepend(allCheck);

        //Check to see if all are selected already
        var allAreSelected = (function() {
            //Go through each item (need to convert jQuery obj to array using get())
            //Note: $.each won't work here, don't try :)
            for (i in checkBoxes.get()) {
                if (!checkBoxes[i].checked) { return false; }
            }
            return true;
        })();

        //Init the All check box
        if (allAreSelected) { allCheck.click(); }

        allCheck.click(function() {

            //If user is checking it, check all boxes
            if (this.checked) {
                checkBoxes.filter(':checkbox:not(:checked)').click();
                allAreSelected = true;
            }

            //If user unchecking, uncheck all boxes
            else {
                checkBoxes.filter(':checkbox:checked').click();
                allAreSelected = false;
            }
        });

        //Uncheck "Select All" when any box is un-selected
        checkBoxes.click(function() {
            if (allAreSelected) {
                allCheck.attr('checked', false);
                allAreSelected = false;
            }
        });

        return this;
    }

})(jQuery);