/**
 * formData2QueryString
 * cycles thru elements of the supplied form object
 * and converts the submitted values to a URL query string
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @params  df   (object)       form object
 * @returns      (string)       formatted URL query string
 *
 * @author  Ignatius Teo        <ignatius@act28.com>
	- complete rewrite using arrays
	- added handling for select-multiple
	- rewrote checkbox handling
 * @author  Mark Pruett         <mark.pruett@comcast.net>
	- Additional bugfixes
 * @author  Matthew Eernisse    <mde@fleegix.org>
	- Original code
 * @copyright   Original code(C)2005
 * $Id: formdata2querystring.js,v 1.1 2007/05/12 06:47:02 Ignatius Teo Exp $
 */

function formData2QueryString(df)
{
	var qs = [];		// array of query parameters
	var elem;			// current form element
	var str = [];		// temp array for checkboxes & select-multiple
	var cb;				// temp form element for checkboxes
	var lastcb = [];	// array of checkboxes name already processed

	var l = df.length;	// form 
	for (i=0; i < l; i++) {
		elem = df.elements[i];
		//alert(elem.name + ' =' + elem.type);
		//console.log(elem.name + ', ' + elem.type + (elem.disabled ? ' DISABLED' : '') + (elem.readonly ? ' READONLY' : ''));
		if (elem == null) {
			continue;
		}
		
		// skip disabled/readonly fields
		if (elem.disabled || elem.readonly) {
			continue;
		}
		
		switch (elem.type)
		{
			// Text fields, hidden form elements
			case 'text':
			case 'hidden':
			case 'password':
			case 'textarea':
			case 'select-one':
				qs.push(elem.name + '=' + escape(elem.value));
				break;

			// multi-select
			case 'select-multiple':
				str = new Array;
				for (j = 0; j < elem.length; j++)
				{
					if (elem.options[j].selected) {
						str.push(elem.options[j].value);
					}
				}
				
				if (str.length > 0) {
					for (i in str)
						qs.push(elem.name + '=' + str[i]);
					//qs.push(elem.name + '=' + escape(str.join(',')));
				}
				break;

			// Radio buttons
			case 'radio':
				if (elem.checked)
				{
					qs.push(elem.name + '=' + escape(elem.value));
				}
				break;

			// Checkboxes
			case 'checkbox':
				if (elem.checked)
				{
					// check that we haven't already done checkboxes with this name
					var cbdone = false;
					for (x = 0; x < lastcb.length; x++)
					{
						if (lastcb[x] == elem.name)
						{
							cbdone = true;
							break;
						}
					}

					if (!cbdone)
					{
						// not done yet... loop thru all form elements again to get all checkboxes of the same name
						str = new Array;
						for (j = 0; j < df.length; j++)
						{
							cb = df.elements[j];
							if (cb.type == 'checkbox' && cb.name == elem.name && cb.checked)
							{
								str.push(cb.value);
							}
						}

						if (str.length > 0)
						{
							if (elem.name.match(/\[([^\]]*)\]$/)) {
								// multi-checkbox?
								for (i in str) {
									qs.push(elem.name + '=' + str[i]);
								}
							} else {
								// make it a comma separated list
								qs.push(elem.name + '=' + escape(str.join(',')));
							}
							lastcb.push(elem.name);     // set checkbox to done!
						}
					}
				}
				break;

			// ignore submit, button, reset, file
			case 'submit':
			case 'button':
			case 'reset':
			case 'file':
			default:
				break;
		}
	}
	return qs.join('&');
}

