/*
 *
 * gcGo - change the browser location to the path of the selected element
 *
 */
function gcGo( ui )
{
    selected_value = ui.options[ui.selectedIndex].value;
    if ( selected_value ) {
        /*
         * clean up the url - seee clean_gs_params() function for notes
         */
        selected_value = clean_gs_params ( selected_value );
        document.location = selected_value;
    }
    else {
        return false;
    }
}
/*
 *
 * contact_form - opens up "Call Me" form   
 *
 */
function contact_form (solution_title) {
        /*
         *
         * note: the incoming solution_title is already encoded
         * 
         */
	screen_width = window.screen.width;
	screen_height = window.screen.height;
	contact_win_width = 413;
	contact_win_height = 533;
	x_start = (screen_width - contact_win_width) / 2;
	y_start = (screen_height - contact_win_height) / 2;
	dest_url = '/forms/contactus_finder.html?contactbutton=right&solution='+ solution_title;
	params = 'height=' + contact_win_height +
			 ', width=' + contact_win_width +
			 ', left=' + x_start +
			 ', top=' + y_start +
			 ', toolbar=no, location=no, menubar=no, scrollbars=no, status=no, resizable=yes';
	contact_win = window.open( dest_url
							  ,'contact_win'
							  ,params
	);
	contact_win.focus();
}
/*
 *
 * clean_gs_params - clean up the input url to:
 *
 * 1. remove any keyword related params (ie, u1=q and q=foo). This is to
 *    account for the fact that client does NOT want facet search to depend
 *    on keyword
 *
 * 2. reorder the user params so that these params start at 1 and
 *    and go in sequence (eg, abc.com?u2=foo&u4=fee becomes abc.com?u1=foo&u2=fee).
 *    This is to account for the fact that we remove u1=q
 *
 * These are necessary because:
 * A. WebTrends depends on url so they must be accurate
 * B. -reset- functionality depends on the correct user param sequence
 *
 */
function clean_gs_params ( url ) {
	ret_url = url; // the modified url to return
	names_user = new Array(); // array of original user parameter names
	values_user = new Array(); // array of original user parameter values
	order = new Array(); // array of original user parameter order (ie, strip out the 'u' and just keep the number)
	order_sorted = new Array(); // array of order that has been sorted
	order_new = new Array(); // array of re-numbered order (ie: 7,2,3 becomes 3,1,2)
	names_user_new = new Array(); // array of re-named user names (ie: u7,u2,u3 becomes u3,u1,u2);
	names_other = new Array(); // array of all other url param names (ie, non-user params)
	values_other = new Array(); // array of all other url param values (ie, non-user params)
	names_all = new Array(); // array of all parameter names
	names_all_sorted = new Array(); // array of all parameter names that's been sorted
	values_all = new Array(); // array of all parameter values
	values_all_new = new Array(); // array of all paramter values that correspond with names_all_sorted
	if ( url.match(/\?/) )
	{
	   tmp_arr = url.split( /\?/i ); // extract the script name and query string
	   script_name = tmp_arr[0];
	   if ( tmp_arr.length == 2 ) // continue only if there is a query string
	   {
			query_string = tmp_arr[1];
			params = query_string.split( /&/i );
			for ( i in params )
			{
				if ( params[i] && params[i].match( /=/i ) ) // continue only if the parameter is not null (to catch cases like a=1&&b=2&c3)
				{
					tmp_arr_1 = params[i].split( /=/i );
					name = tmp_arr_1[0];
					if ( tmp_arr_1.length == 2 ) // continue only if there is a value
					{
						value = tmp_arr_1[1];
						if ( value )
						{
							if ( ( name == 'u1' && value == 'q' ) || ( name == 'q' ) || ( name == 'cs' ) ) // ignore any keyword related params; also ignore the clear selection (cs) param once the "Clear All" button has been clicked
							{
								continue;
							}
							if ( name.match( /^u\d+/ ) ) // store the user paramters (eg, u1, u2, u3, ...)
							{
								names_user.push( name );
								values_user.push( value );
								order.push ( name.match( /\d+/) );
								order_sorted.push ( name.match( /\d+/) ); // can't set order_sorted = order cuz of pass by reference and not value
							}
							else // store all other parameters
							{
								names_other.push( name );
								values_other.push( value );
							}
						}
					}
				}
			}

			if ( order_sorted.length > 0 )  // process the user params, if any
			{
				order_sorted.sort(); // in place sort so no need for assignment
				$counter = 0;
				for ( j in order )
				{
					for ( k in order_sorted )
					{
						if ( order[j].toString() == order_sorted[k].toString() )
						{
							order_new[j] = parseInt(k) + 1; // set the new order for each user param; k is zero-based
							names_user_new[j] = 'u' + order_new[j].toString(); // set the new name for each user param
							break;
						}
					}
				}
				//document.write ( '<br />user order:<br />original [' + order + ']<br />sorted   [' + order_sorted + ']<br />new name [' + names_user_new + ']<br />' );
			}
			names_all = names_other.concat( names_user_new );
			values_all = values_other.concat( values_user );
			names_all_sorted = names_other.concat( names_user_new ).sort();
			//document.write ( '<br />names all:<br />original [' + names_all + ']<br />sorted [' + names_all_sorted + ']<br />' );
			for ( l in names_all_sorted )
			{
				for ( m in names_all )
				{
					if ( names_all_sorted[l].toString() == names_all[m].toString() )
					{
						values_all_new[l] = values_all[m];
						break;
					}
				}
			}
			ret_url = script_name + '?';
			length_names_all_sorted = names_all_sorted.length;
			for ( n in names_all_sorted )
			{
				ret_url += names_all_sorted[n] + '=' + values_all_new[n];
				ret_url += parseInt(n) < (length_names_all_sorted - 1) ? '&' : '';
			}
			//document.write('<br />old url:<br />' + url + '<br />');
			//document.write('<br />new url:<br />' + ret_url + '<br />');
	   }
	}
	return ret_url;
}

function finder_help() {
	screen_width = window.screen.width;
	screen_height = window.screen.height;
	contact_win_width = 413;
	contact_win_height = 533;
	x_start = (screen_width - contact_win_width) / 2;
	y_start = (screen_height - contact_win_height) / 2;
	dest_url = '/finder/finder_help.html';
	params = 'height=' + contact_win_height +
			 ', width=' + contact_win_width +
			 ', left=' + x_start +
			 ', top=' + y_start +
			 ', toolbar=no, location=no, menubar=no, scrollbars=no, status=no, resizable=yes';
	finder_help_win = window.open( dest_url
							  ,'finder_help_win'
							  ,params
	);
	finder_help_win.focus();
}
/* unit test */
//clean_gs_params('http://www.alltelsolutions.com/finder.html?finder-company-size=1+employee&finder-industry=Agiculture&finder-solution-type=Business+Intelligence&i=1&&u7=finder-industry&u2=finder-solution-type&u3=finder-company-size&cs=1');
//clean_gs_params('www.alltelsolutions.com/finder.html?finder-company-size=1');
