/**
 * setViews
 * Sets the state of the tabs and tag cloud
 * interfaces.
 **/
function setViews(viewId, tagCloudId) {
	toggleView(viewId);
	toggleTagCloud(tagCloudId);
}

/**
 * toggleView
 * Sets the view to a specific tab.  This function will:
 * 		-- Show the appropriate view, and hide all others
 *		-- Show the appropriate tab, and hide all others
 *		-- Show the appropriate meta information about
 *		   the view (shown in the same row as the tabs; 
 *		   includes pagination in the results view and
 *		   # of records in the tags view)
 *		-- Hides the sorting and results per page controls
 *		   when not in the results view.
 **/
function toggleView(viewId)
{
	// Show the appropriate view
	var viewContainer = document.getElementById("ResultsView");
	var views = viewContainer.childNodes;
	
	for(i=0; i<views.length; i++)
	{
		if(views[i].id == viewId)
			views[i].className = "activesection";
		else
			views[i].className = "hiddensection";
	}

	// Show the appropriate tab
	var tabList = document.getElementById("TabList");
	var tabs = tabList.getElementsByTagName("LI");	 /* being explicit here b/c mozilla 
														treats breaks or whitespace as nodes
														when using childNodes.  Above, doesn't matter */
	for(i=0; i<tabs.length; i++)
	{
		if(tabs[i].id.substring(4) == viewId.substring(2))	//substring after tabs_  == substring after v_
			tabs[i].className = "selectedTab";
		else
			tabs[i].className = "unselectedTab";
	}
	
	// Show the appropriate meta information
	var metaContainer = document.getElementById("Tabs");
	var tabmeta_objects = metaContainer.getElementsByTagName("DIV");

	for (i=0; i<tabmeta_objects.length; i++) {
		if(tabmeta_objects[i].id.substring(8) == viewId.substring(2))	//substring after tabmeta_  == substring after v_
			tabmeta_objects[i].className = "activesection";
		
		else
			tabmeta_objects[i].className = "hiddensection";
	}
	
	// Hide the sorting & rpp controls when not in the results view
	var sortContainer = document.getElementById("SortControls");
	if(viewId != "v_results")
		sortContainer.className = "hiddensection";
	else
		sortContainer.className = "activesection";
}

/**
 * toggleTagCloud
 * Sets the view to a specific tag cloud (e.g. frequency or
 * relevancy).  This function will:
 * 		-- Show the appropriate tag cloud
 *		-- Change the style of the links to represent
 *		   the context of the view
 *
 **/
function toggleTagCloud(tagCloudId)
{
	var viewContainer = document.getElementById("v_tags");
	var views = viewContainer.childNodes;
	
	for(i=0; i<views.length; i++)
	{
		// Show the correct tag cloud
		if(views[i].nodeName == "DIV")
		{
			if(views[i].id == tagCloudId)
				views[i].className = "activesection";
			else
				views[i].className = "hiddensection";
		}
		// Change the style of the links
		else if(views[i].nodeName == "A")// anchors
		{
			if(views[i].id.substring(2) == tagCloudId)
				views[i].className = "activelink";
			else
				views[i].className = "inactivelink";
		}
	}
}

/**
 * switchView
 * This function is used to switch the main view
 * or the tag cloud.  It takes two arguments, the
 * first is the name of the parameter to pass in 
 * the query (such as "view" or "tcview"), and the
 * second is the value of this parameter, specifying
 * which view to switch to (such as "v_results" or
 * "v_tags").
 *
 **/
function switchView(paramName, viewId) {
	
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	var newVars = "";

	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] != paramName) {
			// this second conditional is to make sure that the tags view resets
			// to the frequency tag cloud when a user selects the results view.
			if(paramName != "view" || (paramName == "view" && pair[0] != 'tcview'))
				newVars = newVars + pair[0] + "=" + pair[1] + "&";
		}
	}

	window.location = window.location.pathname + "?" + newVars + paramName + "=" + viewId;
}

/**
 * submitSearch
 * Performs validation on a search term (requires a value),
 * and submits the search query.
 **/
function submitSearch() {
	
	if(document.searchForm.newNtt.value == "*"){
		document.searchForm.newNtt.value = "";
	}	
	var local_ntt= "";
	var mode = "mode+matchallpartial";
	
	if (document.searchForm.searchWithin != null && document.searchForm.searchWithin.value == 'true') {
		if(document.searchForm.newNtt.value == ""){
			alert("Please enter a search term");
			return;
		}		
			document.hiddenSearchForm.N.value = document.searchForm.N.value;
			
			if (document.searchForm.Ntk.value && document.searchForm.Ntk.value.length > 0) {
				document.hiddenSearchForm.Ntk.value = document.searchForm.Ntk.value + "|" + document.searchForm.newNtk.value;
			} else {			
				document.hiddenSearchForm.Ntk.value = document.searchForm.newNtk.value;
			}
			if (document.searchForm.Ntt.value && document.searchForm.Ntt.value.length > 0) {
			   local_ntt = document.searchForm.newNtt.value.toLowerCase().split(" ");			    
			   for (i=0; i<local_ntt.length; i++){
			       if (local_ntt[i] == "or" || local_ntt[i] == "and" || local_ntt[i] == "not"){
			          mode = "mode+matchboolean";
			       }
			   }
			   document.hiddenSearchForm.Ntt.value = document.searchForm.Ntt.value.replace(/\'/g,"\\'") + "searchwithin" + document.searchForm.newNtt.value.replace(/\'/g,"\\'");
			   //document.hiddenSearchForm.Ntt.value = document.searchForm.Ntt.value + "searchwithin" + document.searchForm.newNtt.value;
			} else {
			    local_ntt = document.searchForm.newNtt.value.toLowerCase().split(" ");
			    for (i=0; i<local_ntt.length; i++){
			       if (local_ntt[i] == "or" || local_ntt[i] == "and" || local_ntt[i] == "not"){
			          mode = "mode+matchboolean";
			       }
			   }
			   document.hiddenSearchForm.Ntt.value = document.searchForm.newNtt.value.replace(/\'/g,"\\'");			   
			   //document.hiddenSearchForm.Ntt.value = document.searchForm.newNtt.value;			   
			}
			if (document.searchForm.Ntx.value && document.searchForm.Ntx.value.length > 0) {
				//document.hiddenSearchForm.Ntx.value = document.searchForm.Ntx.value + "|" + document.searchForm.newNtx.value;
				document.hiddenSearchForm.Ntx.value =  mode;
			} else {
				//document.hiddenSearchForm.Ntx.value = document.searchForm.newNtx.value;
				document.hiddenSearchForm.Ntx.value = mode;
			}
		}
		
	else {
		    local_ntt = document.searchForm.newNtt.value.toLowerCase().split(" ");
			for (i=0; i<local_ntt.length; i++){
			    if (local_ntt[i] == "or" || local_ntt[i] == "and" || local_ntt[i] == "not"){
			          if (mode != "mode+matchboolean"){
			                mode = "mode+matchboolean";
			          }
			    }
			}
			document.hiddenSearchForm.Ntt.value = document.searchForm.newNtt.value.replace(/\'/g,"\\'");   
			//document.hiddenSearchForm.Ntt.value = document.searchForm.newNtt.value;
			document.hiddenSearchForm.Ntx.value = mode;			   
			document.hiddenSearchForm.Ntk.value = document.searchForm.newNtk.value;
			//document.hiddenSearchForm.Ntx.value = document.searchForm.newNtx.value;			
			//document.hiddenSearchForm.Ntt.value = document.searchForm.newNtt.value;
	}
	document.hiddenSearchForm.submit();
}

/**
 * submitQuickSearch
 * Performs processsing on a search term (doesn't require a value), and submits the search query.
 **/
function submitQuickSearch() {	
	if(document.quickSearchForm.newNtt2.value == "*"){
		document.quickSearchForm.newNtt2.value = "";
	}	
	var local_ntt = document.quickSearchForm.newNtt2.value.toLowerCase().split(" ");	   
	var mode = "mode+matchallpartial";
	for (i=0; i<local_ntt.length; i++){
		if (local_ntt[i] == "or" || local_ntt[i] == "and" || local_ntt[i] == "not"){
			mode = "mode+matchboolean";
		}
	}			   
	document.hiddenQuickSearchForm.Ntk.value = document.quickSearchForm.newNtk.value;
	//document.hiddenQuickSearchForm.Ntx.value = document.quickSearchForm.newNtx.value;	
	document.hiddenQuickSearchForm.Ntx.value = mode;	
	document.hiddenQuickSearchForm.Ntt.value = document.quickSearchForm.newNtt2.value.replace(/\'/g,"\\'");		
	//document.hiddenQuickSearchForm.Ntt.value = document.quickSearchForm.newNtt2.value;		
	document.hiddenQuickSearchForm.submit();
}

/** 
 * submitOnEnter
 * This function ensures that if someone hits the Enter 
 * key in the search text box it will default to calling 
 * the function above.
 **/
function submitQuickSearchOnEnter(e) {
	if(e.keyCode == 13){
		submitQuickSearch();
		return false;
	}
	else{
		return true;
	}
}

/** 
 * submitOnEnter
 * This function ensures that if someone hits the Enter 
 * key in the search text box it will default to calling 
 * the function above.
 **/
function submitOnEnter(e) {
	if(e.keyCode == 13){
		submitSearch();
		return false;
	}
	else{
		return true;
	}
}

/**
 * showNumRecords
 * Used by the drop-down that allows a user to specify N number of results per page.
 **/
function showNumRecords(index)
{
	location = document.recsPerPageForm.recsPerPage.options[index].value;
}