///////////////////////////////////////////////////////////////////////////////
//                                                                           //
//                       Copyright (c) 2003-2006                             //
//                            Marc Peterson                                  //
//                     marc.s.peterson at gmail.com                          //
//                                                                           //
///////////////////////////////////////////////////////////////////////////////

// Last updated:
//   2006-08-22 - all form access now used getElementById() so XHTML forms don't need name attribute
//   2005-05-11 - added "form_id" variable to orderBy()

// Shows the results of previous/next page
function movePage(dir)
{
	var oForm = document.getElementById("main_search");
	oForm.curPage.selectedIndex = oForm.curPage.selectedIndex + dir;
	oForm.submit();
}

// Clicking 'Go' should cause result to start on first page, otherwise the new search may have fewer
// pages than the current page and no results will show.
function resetCurPage(oForm)
{
	if ( document.getElementById("main_search") )
	{
		if ( document.getElementById("main_search").curPage ) document.getElementById("main_search").curPage.selectedIndex = 0;
	}

	if (oForm)
	{
		if ( oForm.curPage )
		{
			oForm.curPage.selectedIndex = 0;
		}
	}
}

// Reloads the page with the desired sorting applied to a table.
// VARIABLES
//   field_id	- a list of table column names to sort on ie) "first_name,last_name"
//	 pref_dir	- the preferred direction of sorting "ASC" or "DESC".
//	 [form_id]	- id of the listing form.  if not passed, assume form "info"
// The currently sorted column and direction must be in the hidden field "cur_order".
// ie) "first_name ASC"
function orderBy(field_id, pref_dir, form_id)
{
	if ( form_id !== undefined ) oForm = document.getElementById(form_id);
	else oForm = document.getElementById("info");

	var cur_order		= oForm.q_orderBy.value;
	var temp			= cur_order.split(' ');
	var cur_order_cols	= temp[0].split(',');		// The columns currently being sorted
	var cur_dir			= temp[1];					// The direction currently being sorted
	var new_order_cols	= field_id.split(',');		// The new columns to sort
	var new_dir			= pref_dir;					// The new direction to sort

	// See if column clicked on is already being sorted
	var sort_same_col = false;
	search_for_col:
		for (var i=0; i<cur_order_cols.length; i++)
		{
			for (var j=0; j<new_order_cols.length; j++)
			{
				if ( cur_order_cols[i] == new_order_cols[j] )
				{
					sort_same_col = true;
					break search_for_col;
				}	
			}
		}

	// If resorting on same column, flip ASC/DESC
	if ( sort_same_col )
	{
		if ( cur_dir == "ASC" ) new_dir = "DESC";			// If current sort is ASC, sort by DESC
		else if ( cur_dir == "DESC") new_dir = "ASC";		// If current sort is DESC, sort by ASC
	}

	oForm.q_orderBy.value = field_id+" "+new_dir;	// Save the new sort order
	oForm.submit();									// Submit the form
}
