﻿// JScript File

/**
*	// Serialize an array of form elements or a set of
*	// key/values into a query string
*	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( encodeURIComponent(this.name) + "=" + encodeURIComponent( 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( encodeURIComponent(j) + "=" + encodeURIComponent( this ) );
*					});
*				else
*					s.push( encodeURIComponent(j) + "=" + encodeURIComponent( a[j] ) );
*
*		// Return the resulting serialization
*		return s.join("&");
*	}
*	
*/
$.fn.paramForm = 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( $.fn.encodeValue(this.name) + "=" + $.fn.encodeValue( 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( $.fn.encodeValue(j) + "=" + $.fn.encodeValue( this ) );
				});
			else
				s.push( $.fn.encodeValue(j) + "=" + $.fn.encodeValue( a[j] ) );

	// Return the resulting serialization
	return s.join("&");
}

$.fn.encodeValue = function(ps){

	var s = new String(ps);
	return escape(s);
/*	
	s = s.replace(/\%/,"%25");
	s = s.replace(/ /,"%20");
	s = s.replace(/\?/,"%3F");
	s = s.replace(/&/,"%26");
	s = s.replace(/=/,"%3D");
	return s;
*/
}


/**
 * Serializes form data into a 'submittable' string. This method will return a string
 * in the format: name1=value1&amp;name2=value2
 *
 * The semantic argument can be used to force form serialization in semantic order.
 * If your form must be submitted with name/value pairs in semantic order then pass
 * true for this arg, otherwise pass false (or nothing) to avoid the overhead for
 * this logic (which can be significant for very large forms).
 *
 * @example var data = $("#myForm").formSerialize();
 * $.ajax('POST', "myscript.cgi", data);
 * @desc Collect all the data from a form into a single string
 *
 * @name formSerialize
 * @param semantic true if serialization must maintain strict semantic ordering of elements (slower)
 * @type String
 * @cat Plugins/Form
 */
$.fn.formSerialize = function(semantic) {
    //hand off to jQuery.param for proper encoding
    return $.fn.paramForm(this.formToArray(semantic));
};


/**
 * Serializes all field elements in the jQuery object into a query string.
 * This method will return a string in the format: name1=value1&amp;name2=value2
 *
 * The successful argument controls whether or not serialization is limited to
 * 'successful' controls (per http://www.w3.org/TR/html4/interact/forms.html#successful-controls).
 * The default value of the successful argument is true.
 *
 * @example var data = $("input").formSerialize();
 * @desc Collect the data from all successful input elements into a query string
 *
 * @example var data = $(":radio").formSerialize();
 * @desc Collect the data from all successful radio input elements into a query string
 *
 * @example var data = $("#myForm :checkbox").formSerialize();
 * @desc Collect the data from all successful checkbox input elements in myForm into a query string
 *
 * @example var data = $("#myForm :checkbox").formSerialize(false);
 * @desc Collect the data from all checkbox elements in myForm (even the unchecked ones) into a query string
 *
 * @example var data = $(":input").formSerialize();
 * @desc Collect the data from all successful input, select, textarea and button elements into a query string
 *
 * @name fieldSerialize
 * @param successful true if only successful controls should be serialized (default is true)
 * @type String
 * @cat Plugins/Form
 */
$.fn.fieldSerialize = function(successful) {
    var a = [];
    this.each(function() {
        var n = this.name;
        if (!n) return;
        var v = $.fieldValue(this, successful);
        if (v && v.constructor == Array) {
            for (var i=0,max=v.length; i < max; i++)
                a.push({name: n, value: v[i]});
        }
        else if (v !== null && typeof v != 'undefined')
            a.push({name: this.name, value: v});
    });
    //hand off to jQuery.param for proper encoding
    return $.fn.paramForm(a);
};
