/**
 * Gets the list of selected options in combo
 *
 * @param   object  the form to check
 *
 * @return  string  the list of selected options
 *
 * Taken from phpMyAdmin 3.2.3, but this one gets the field value
 * instead
 */
function getSelectedValue(the_field) {
    var the_list = '';
    var opts     = the_field.options;
    var opts_cnt = opts.length;

    for (var i = 0; i < opts_cnt; i++) {
        if (opts[i].selected) {
            the_list += opts[i].value + ', ';
        }
    } // end for

    return the_list.substring(0, the_list.length - 2);
} // end of the 'getSelectedValue()' function

/**
 * Reloads the page to get tables names in a database or fields names in a
 * table
 *
 * @param  object  the input text box to build the query from
 * 
 * expanded and heavily modified from phpMyAdmin 3.2.3
 */
function change(the_field) {
    var l        = location.href;
    var box_name = the_field.name;
    var lpos     = l.indexOf('?' + box_name);
    var question_found = false;

    /* if we didn't find the element at beginning, try anywhere */
    if (lpos <= 0) {
      lpos = l.indexOf('&' + box_name);
      if (lpos > 0) {
	question_found = true;
      }
    } // end if

    /* find end of parameter */
    endpos = l.indexOf('&', lpos + 1);
    
    if (lpos > 0) {
      l = l.substring(0, lpos);
    }

    question_found = (l.indexOf('?') >= 0);

    /* there are other parameters after the one we want to modify */
    if (endpos > 0) {
      if (!question_found) {
	l += '?';
	question_found = true;
      } else {
	l += '&';
      }
      /* append the rest of the url */
      l += location.href.slice(endpos+1, location.href.length);
    }

    if (!question_found) {
      l += '?';
    } else {
      l += '&';
    }

    /* add our changed parameter at the end */
    location.href = l + box_name + '=' + escape(getSelectedValue(the_field));
    
} // end of the 'change()' function
