    /**
     * Copyright 2009 Michael Little, Christian Biggins
     *
     * This program is free software: you can redistribute it and/or modify
     * it under the terms of the GNU General Public License as published by
     * the Free Software Foundation, either version 3 of the License, or
     * (at your option) any later version.
     *
     * This program is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     * GNU General Public License for more details.
     *
     * You should have received a copy of the GNU General Public License
     * along with this program. If not, see <http://www.gnu.org/licenses/>.
     */

jQuery.formDiff = function () {
    var i,f,o,c
    if (this.elements) {
        for (i=0; this.elements[i]; i++) {
            f = this.elements[i]
            switch (f.type) {
                case 'file':
                case 'textarea':
                case 'text':
                    if (f.value == f.defaultValue) f.disabled = true
                    break

                case 'radio':
                case 'checkbox':
                    if (f.checked == f.defaultChecked) {
                        f.disabled = true
                    } else if (f.type == 'checkbox' && !f.checked) {
                        f.value = '0'
                        f.checked = true
                    }
                    break

                case 'select-one':
                case 'select-multiple':
                    c = false
                    for (o=0; f.options[o]; o++) {
                        if (f.options[o].selected != f.options[o].defaultSelected) {
                            c = true
                            break
                        }
                    }
                    if (!c) f.disabled = true;
                    break

            }
        }
    }
};

jQuery.fn.addFormDiff = function () {
    return this.bind('submit', jQuery.formDiff);
};
