/**
 * 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@inventurismedia.com>
    - completely 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
 */

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

    for (i = 0; i < df.length; i++)
    {
        elem = df.elements[i];
        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)
                    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)
                        {
                            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('&');
}