// Sovrascrittura del metodo serialize() per l'encoding di dati inviati da un form. 
// Pare che utilizzando il metodo su pagine aventi il charset "latin" le risposte ajax producano effetti indesiderati con i caratteri accentati. 
// Tale soluzione risolve il problema!
// FONTE: forum ufficiale di jquery "http://forum.jquery.com/topic/serialize-problem-with-latin-1-iso-8859-1-and-solution"
jQuery.fn.extend({
    param: function( a ) {
        var s = [];
 
        // If an array was passed in, assume that it is an array
        // of form elements
        if ( a.constructor == Array || a.jquery ){
            // Serialize the form elements
            jQuery.each( a, function(){
                s.push(unescape(encodeURIComponent(escape(this.name))) + "=" + unescape(encodeURIComponent(escape(this.value))));
            });
        }
        // Otherwise, assume that it's an object of key/value pairs
        else{
            // Serialize the key/values
            for ( var j in a )
                // If the value is an array then the key names need to be repeated
                if ( a[j] && a[j].constructor == Array )
                    jQuery.each( a[j], function(){
                        s.push(unescape(encodeURIComponent(escape(j)) + "=" + encodeURIComponent(escape(this))));
                    });
                else
                    s.push(unescape(encodeURIComponent(escape(j)) + "=" + encodeURIComponent(escape(a[j]))));
        }
        // Return the resulting serialization
        return s.join("&").replace(/ /g, "+");
    },

    serialize: function() {
        return this.param(this.serializeArray());
    }
});
