
// Called when user presses a key in a text field 
// with onkeypress="onEnter(myFunc,event)"
//
// If Enter is pressed, it will call the specified
// javascript function, e.g. myFunc()
//
function onEnter(func, e)
{
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;

	if (keycode == 13)
	{
   		func();
   		return false;
	}
	else
		return true;
}

// Called when user hits enter in the MainSearch field (in the header)
// or clicks the submit button next to it.
function search(query)
{		
	showFormTopTen(contextRoot + "/search.do?topTen=true&query=" + escape(query));
}

function closeTopTenPopup()
{
	winTopTen.hide();
}

// Used when user clicks Search (section) Tab or "All Results" TAB
// Or clicks a Section link in the TopTen results window.
function searchSection(sectionId)
{
	document.location = contextRoot + "/search.do?topTen=false&sectionId=" + sectionId + "&within=false&startIndex=0";
}

function searchArticleLibrary()
{
    var query = $F('articleSearch');
    document.location = contextRoot + "/search.do?topTen=false&sectionId=articles&query=" + escape(query);
}

// Search Again in searchHeader.jsp
function searchAgain()
{
	var query = $F('SearchAgain');
	var within = $RF('Within');
	_searchAgain(query, within);
}

// Search Again in searchNoResults.jsp
function searchAgainZeroResults()
{
	_searchAgain($F('SearchAgainZeroResults'), false);
}

// Search Again in searchTopTen.jsp
function searchTopTenAgain()
{
	var query = $F('SearchTopTenAgain');
	if (query == "" || query == null || query == undefined)
	{
		alert("Please enter a Search value"); //TODO: change to ErrorMessage popup
		return false;
	}
	
	winTopTen.setAjaxContent(contextRoot + "/search.do?query=" + escape(query), 
		{}, false, true); // centered=false, modal=true
}

function _searchAgain(query, within)
{
	if (query == "" || query == null || query == undefined)
	{
		alert("Please enter a Search value"); //TODO: change to ErrorMessage popup
		return false;
	}

	//alert('query=' + query + ' within=' + within);
	document.location = contextRoot + "/search.do?query=" + escape(query) + "&sectionId=all&within=" + within;
}

function searchTag(tag)
{
	_searchAgain(tag, false);
}



/**
 * Returns the value of the selected radio button in the radio group
 * 
 * @param {radio Object} or {radio id} el
 * OR
 * @param {form Object} or {form id} el
 * @param {radio group name} radioGroup
 */
// see http://xavisys.com/blog/2007/03/01/using-prototype-javascript-to-get-the-value-of-a-radio-group/
// (had to fix it first! - enno)
//
// Use like this:
//
// <form name="foo">
//  <input id="Within" type="radio" name="Within" value="true" ${SearchForm.within ? 'CHECKED' : ''}/> within these results
//  <input             type="radio" name="Within" value="false" ${SearchForm.within ? '' : 'CHECKED'}/> new search
// </form>
//
function $RF(el, radioGroup) {
	if($(el).type == 'radio') {
		var radioGroup = $(el).name;
		var el = $(el).form;
	} else if ($(el).tagName.toLowerCase() != 'form') {
		return false;
	}
	return $F($(el).getInputs('radio', radioGroup).find(
		function(re) {return re.checked;}
	));
}
