(function($) {

    $.fn.serialize = function(options) {
        return $.param(this.serializeArray(options));
    };

    $.fn.serializeArray = function(options) {
        var o = $.extend({
            checkboxesAsBools: false
        }, options || {});

        var rselectTextarea = /select|textarea/i;
        var rinput = /text|hidden|password|date|search/i;

        return this.map(function() {
                return this.elements ? $.makeArray(this.elements) : this;
            })
            .filter(function() {
                return this.name && !this.disabled &&
                    (this.checked || (o.checkboxesAsBools && this.type === 'checkbox') || rselectTextarea.test(this.nodeName) || rinput.test(this.type));
            })
            .map(function(i, elem) {

                var val = $(this).val();

                //this block changed here by me to break out the overly tight but obscure code

                if (val == null) return null;

                if ($.isArray(val)) {
                    //Array return
                    return $.map(val, function(val, i) {
                        return {
                            name: elem.name,
                            value: val
                        };
                    })
                } else {

                    if (o.checkboxesAsBools && this.type === 'checkbox') {
                        return {
                            name: elem.name,
                            value: (this.checked ? 'true' : 'false')
                        }
                    }

                    //for now dates are handled at the backend
                    //by convention of field name ending in "Date" (sentDate, readDate etc)
                    // if (this.type === 'date') {
                    //     return {
                    //         name: elem.name,
                    //         value: (val === '' ? '' : val) //empty dates sb null for mongo db backend
                    //     }
                    // }


                    //default all other types
                    return {
                        name: elem.name,
                        value: val
                    }
                }


                // /changed by me


                //ORIGINAL BLOCK:
                //  return val == null ?
                //  null :
                //  $.isArray(val) ?
                //  $.map(val, function (val, i) {
                //      return { name: elem.name, value: val };
                //  }) :
                //  {

                //      name: elem.name,
                //      value: (o.checkboxesAsBools && this.type === 'checkbox') ? //moar ternaries!
                //             (this.checked ? 'true' : 'false') :
                //             val
                //  };


            }).get();
    };

})(jQuery);