function DoSearch_top() 
{
	var url = window.location.href.substring( 0, window.location.href.indexOf("?") );
	var params = 'module=search&action=ajaxsearch&searchTerm=' + $('searchTerm_top').value;

	if ($('searchTerm_top').value.length > 2) {
		new Ajax.Updater(
			'searchResults_top', 
			url,
			{
				method: 'get',
				parameters: params,
				asynchronous: true,
				onComplete: showResults
			}
		);
	} else {
		Element.hide('searchResults_top');
		showSelects();
	}
	
	$('searchTerm_top').focus();
}

function showResults(response)
{
	// In Safari 2, the onComplete event handler is not called if the AJAX call returns an empty string.
	// The workaround is to always return a space after any search results, and trim the string of HTML
	// returned from the call before using it.
	var resp = response.responseText.strip();
	if (resp.length > 0) 
	{ 
		var el = $('searchWrap_top');
		var sr = $('searchResults_top');
		sr.style.display='block';
		hideSelectsInArea( el.offsetLeft, el.offsetTop, sr.offsetWidth, sr.offsetHeight );
	}
	else
	{
		Element.hide('searchResults_top');
		showSelects();
	}
}

// Wrapper function for hideNow() to call it after some delay.
function hideLater(delay)
{
	if ( !delay )
		delay = 3000;

	setTimeout("hideNow()", delay);
}

// Hide the search term input and search results display DIV.
function hideNow()
{
	$('moduleTooltip').innerHTML = '&nbsp;';
	var sr = $('searchResults_top');
	sr.hide();
	Element.hide('searchTerm_top');
	showSelects();
}

// Show all SELECTs on the page. Since IE6 does not layer SELECTs, 
// they must be hidden on IE6 if they are under the search results DIV, 
// otherwise they will show through it.
function showSelects()
{
	var sel=document.getElementsByTagName("SELECT");
	for ( i=0; i<sel.length; i++)
		sel[i].style.visibility = 'visible';
}

// Show or hide search term input box. Called in onclick of Search icon.
function toggleSearchInput()
{
	var el = $('searchTerm_top');
	el.value = '';
	el.style.display = ( el.style.display == 'inline' ) ? 'none' : 'inline';
	el.focus();
}

// Hide SELECTs on the page if they are inside a certain area in IE6. 
// Since IE6 does not layer SELECTs, they must be hidden if they are 
// under the search results DIV, otherwise they will show through it.
// Code pinched from the web.
function hideSelectsInArea( x, y, w, h ) 
{
	var appVer = navigator.appVersion.toLowerCase();
	var iePos = appVer.indexOf('msie');
	if ( iePos !=-1 ) 
	{
		var is_minor = parseFloat(appVer.substring(iePos+5,appVer.indexOf(';',iePos)));
		var is_major = parseInt(is_minor);
	}
	if (navigator.appName.substring(0,9) == "Microsoft")
	{ // Check if IE version is 6 or older
		if (is_major <= 6) 
		{
			var selx,sely,selw,selh,i;
			var sel=document.getElementsByTagName("SELECT");
			for ( i=0; i<sel.length; i++)
			{
				selx=0; sely=0; var selp;
				if ( sel[i].offsetParent )
				{
					selp=sel[i];
					while( selp.offsetParent )
					{
						selp=selp.offsetParent;
						selx+=selp.offsetLeft;
						sely+=selp.offsetTop;
					}
				}
				selx+=sel[i].offsetLeft;
				sely+=sel[i].offsetTop;
				selw=sel[i].offsetWidth;
				selh=sel[i].offsetHeight;

				if (selx+selw>x && selx<x+w && sely+selh>y && sely<y+h)
					sel[i].style.visibility = 'hidden';
				else 
					sel[i].style.visibility = 'visible';
			}
		}
	}
} 
