function fixNav()
{
	var linkCol = document.getElementById("topNavTopItems").getElementsByTagName("a");

	for (var i=0; i < linkCol.length; i++ )
	{
		linkCol[i].innerHTML = linkCol[i].innerHTML.replace(/ /gi,"&amp;nbsp;");
	}

	linkCol = document.getElementById("topNavBottomArea").getElementsByTagName("a");

	for (var i=0; i < linkCol.length; i++ )
	{
		linkCol[i].innerHTML = linkCol[i].innerHTML.replace(/ /gi,"&amp;nbsp;");
	}
}

function disableEnterKey(e,searchType)
{
     var key;     
     if(window.event)
          key = window.event.keyCode; //IE
     else
          key = e.which; //firefox
		  
	if (key == 13)
	searchRefinement(searchType);

     return (key != 13);
}

function disableEnterKey_Funding(e,searchType)
{
	 var charCode = (e.which) ? e.which : e.keyCode;
	 if(charCode == 13){
		searchRefinement(searchType);
		return false;
	 }
	 if ((charCode > 47 && charCode < 58) || charCode == 8 || charCode == 127)
		return true;

	 //If invalid char show digits only hint
	 document.getElementById('fundingHint').style.visibility='visible';
	 return false;
}

function clearForm(){
	document.getElementById("advancedSearchForm").reset();
}

var advancedSearchCrit = "";
var advancedSearchToggle = "";
var notFirstHit = "false";
var noParams = true;

function enableSearchSubmit () {
	if (document.getElementById('hideAdvancedSearch') != null)
		document.getElementById('hideAdvancedSearch').style.visibility='visible';
	if (document.getElementById('hideAdvancedSearch1') != null)
		document.getElementById('hideAdvancedSearch1').style.visibility='visible';
}

function searchRefinement(advanced){
	//Load form element
	var searchCrit = document.getElementById("fakeSearchCrit").value;

	noParams = true;

	var emptyCrit = false;

	var searchSetting = "default";

	if (advanced == true)
	{

		//Find the selected Search mode from radio buttons
		for (var i=0; i < document.advancedSearch.searchSetting.length; i++)
		{
		   if ( document.advancedSearch.searchSetting[i].checked)
		   {
			  searchSetting =  document.advancedSearch.searchSetting[i].value;
		   }
		}
	}
	
	


	//Trim start and end of string
	searchCrit = searchCrit.replace(/^\s\s*/, '').replace(/\s\s*$/, '');


	//Do run these checks for un-assisted search
	//Check for empty search (Empty searches allowed on Grant Ready -- Search All)
	if (searchCrit.length == 0)
	{
		emptyCrit = true;
	}
	
	//Check for invalid Wildcard *
	if ((searchCrit.charAt(0) == "*") || searchCrit.indexOf(" *") != -1)
	{
		alert("No word in your search can start with an asterisk '*' (Wildcard). Please include at least one character first.");
		return false;
	}

	//Check for invalid Wildcard ?
	if ((searchCrit.charAt(0) == "?") || searchCrit.indexOf(" ?") != -1)
	{
		alert("No word in your search can start with an question mark '?' (Wildcard). Please include at least one character first.");
		return false;
	}

	//Check for invalid Fuzzy
	if ((searchCrit.charAt(0) == "~"))
	{
		alert("Your search cannot begin with the Fuzzy Search operator '~', please move this after a character.");
		return false;
	}

	//Check for ranges [ ]
	if ((searchCrit.indexOf("[") != -1) || (searchCrit.indexOf("]") != -1))
	{
		alert("Live Site search does not support date ranges, please remove all square brackets '[ ]' from your search.");
		return false;
	}

	//Check for ranges { }
	if ((searchCrit.indexOf("{") != -1) || (searchCrit.indexOf("}") != -1))
	{
		alert("Live Site search does not support word ranges, please remove all parentheses '{ }' from your search.");
		return false;
	}

	

	//Check for search boosting
	if (searchCrit.indexOf("^") != -1)
	{
		alert("Live Site search does not support search boosting, please remove all carets '^' from your search.");
		return false;
	}

	//Check for search required terms
	if (searchCrit.indexOf("+") != -1)
	{
		alert("Live Site search does not support required terms, please remove all plus symbols '+' from your search.");
		return false;
	}

	

	//Check for double ampersand
	if (searchCrit.indexOf("&&") != -1)
	{
		alert("Live Site search does not double ampersands '&&', please remove all double ampersands from your search.");
		return false;
	}

	//Check for double pipe
	if (searchCrit.indexOf("||") != -1)
	{
		alert("Live Site search does not double pipes '||', please remove all double pipes from your search.");
		return false;
	}

	//Check for exclamation mark
	if (searchCrit.indexOf("!") != -1)
	{
		alert("Live Site search does not support exclamation marks '!', please remove all exclamation marks from your search.");
		return false;
	}

	

	//Grant Ready searches with an empty criteria.
	if (emptyCrit == true){
		//Pass through 'All Items' as searched text
		document.getElementById("searchedText").value = "All Items";
	}else{
		//Pass through the original search (For 'Your search for...')
		document.getElementById("searchedText").value = searchCrit;
		noParams = false;
	}

	
	//Escape Colon (These are done after searched text to not pollute it
	if (searchCrit.indexOf(":") != -1)
	{
		searchCrit = searchCrit.replace(/:/g,"\\:");		
	}

	//Remove Dash
	if (searchCrit.indexOf("-") != -1)
	{
		searchCrit = searchCrit.replace(/-/g,"");	
	}
	
	//convert double spaces to single
	searchCrit = searchCrit.replace(/ {2,}/g,' ');
	
	//Grant Ready searches with an empty criteria.
	if (emptyCrit == true)
		//searchCrit = "str_dateTimeSortable:2*";
		


	var searchString = "";

	//input Exact, All and Any search types and attach the advanced search filters if the advanced function parameter is set to true.
	if (advanced == true)
	{

		//Empty crit only works if search mode is set to any (no special search)
		if (emptyCrit == true)
			searchSetting = "Any";

		//All of the words
		if (searchSetting == "All")
		{
			searchString = searchCrit.replace(/\s/g,' AND ');
		}
		
		//Exact Phrase
		if (searchSetting == "Exact")
		{
			searchString = "\"" + searchCrit + "\"";
		}

		//Any of the words or Expert
		if (searchSetting == "Any")
		{
			searchString = searchCrit;
		}

		//Any of the words or Expert
		if (searchSetting == "Expert")
		{
			searchString = searchCrit;
		}

		if (advancedSearchBuild() == false){
			document.getElementById("searchCriteria").value = "";
			return false;
		}
		searchString = searchCrit + advancedSearchCrit;
	}else{
		//Non advanced search just uses the 'Any' search method.
		searchString = searchCrit;
	}

	//Update the query
document.getElementById("searchQueryFilters").value = advancedSearchCrit;
	document.getElementById("searchCriteria").value = searchString;


	


	

	if (noParams == false){
		//Submit the search form
		document.getElementById("advancedSearchSubmit").submit();
	}else{
		alert("Please enter at least one search parameter before proceeding");
		return false;
	}
}

function advancedSearchBuild() {
	
	//Search type is found out before processing the others so the javascript knows how to manage additional critieria in relation to the pre-existing criteria.

	getSearchType();	

	//Adds in each of the selected multisection fields to the criteria. If there is an error detected the process aborts returning false (Handled in the parent function)

	if (addMultiple(document.getElementById("Sector"),"cs_Sector") == false)
		return false;
	if (addMultiple(document.getElementById("Location"),"cs_Location") == false)
		return false;
	if (addMultiple(document.getElementById("Status"),"cs_Status") == false)
		return false;
	if (addMultiple(document.getElementById("Community_Gender"),"cs_Community_Gender") == false)
		return false;
	if (addMultiple(document.getElementById("Community_Age"),"cs_Community_Age") == false)
		return false;
	if (addMultiple(document.getElementById("Community_Region"),"cs_Community_Region") == false)
		return false;
	if (addMultiple(document.getElementById("Administrator_Government_Tier"),"cs_Administrator_Government_Tier") == false)
		return false;		

	//Manages the min and max funding additions.
	calculateMinMaxFunds();

	//Add start and end of crit.
	if (advancedSearchCrit != ""){
		advancedSearchCrit = "(" + advancedSearchCrit + ")";
	}

	return true;

}

function getSearchType()
{
	//Finds the type of search the user requires.
	for (var i=0; i < document.advancedSearch.advancedSearchType.length; i++)
	{
		if (document.advancedSearch.advancedSearchType[i].checked)
		{
			advancedSearchToggle = document.advancedSearch.advancedSearchType[i].value;
		}
	}
}

function addMultiple(formElement,fieldName)
{
	//Finds all selected values within a single form element drop list with multiple selections. Adds appropriately to search critiera using users selected search type.
	var SelectedElements = new Array();
	for (var i = 0; i < formElement.options.length; i++){
		if (formElement.options[i].selected){
			SelectedElements.push(formElement.options[ i ].value);
		}
	}
	
	//If at least one element found and seperator operation and start bracket
	if ( SelectedElements.length > 0){

		
		//Add in seperation operations
		if (notFirstHit == "true")
		{
			//Exact and One have ANDs inbetween
			if (advancedSearchToggle == "Exact" || advancedSearchToggle == "One"){
				advancedSearchCrit = advancedSearchCrit + " AND ";
			}

			//Any has ORs
			if (advancedSearchToggle == "Any"){
				advancedSearchCrit = advancedSearchCrit + " OR ";
			}
		}

		//Set to true after first item has tested
		notFirstHit = "true";
		
		advancedSearchCrit = advancedSearchCrit + "(";

	}

	//Loop through all found elements
	for (var j = 0; j < SelectedElements.length; j++){
		noParams = false;
		//ERROR ERROR -- Ie allows the clicking of invalid left sectors. This causes a render issue.
		if (SelectedElements[j] == "")
		{
			alert("Please select only the indented subsectors from the left filter column");
			return false;
		}

		//Add inner operations but not to first loop
		if (j > 0){		
			//Exact has ANDs
			if (advancedSearchToggle == "Exact")
				advancedSearchCrit = advancedSearchCrit + " AND ";

			//One and Any have ORs
			if (advancedSearchToggle == "One" || advancedSearchToggle == "Any")
				advancedSearchCrit = advancedSearchCrit + " OR ";		
		}
		
		advancedSearchCrit = advancedSearchCrit + "(" + fieldName + ":\"" + SelectedElements[j] + "\")";
	}

	//If at least one element found and start bracket
	if ( SelectedElements.length > 0)
		advancedSearchCrit = advancedSearchCrit + ")";
	
	return true;
}


function calculateMinMaxFunds ()
{
	var formMin = document.getElementById("Funding_Min").value;
	var formMax = document.getElementById("Funding_Max").value;

	var minMaxSection = "";

	
	
	
	if (advancedSearchCrit != ""){
		//Exact and One have ANDs inbetween
		if (advancedSearchToggle == "Exact" || advancedSearchToggle == "One"){
			minMaxSection = minMaxSection + " AND ";
		}

		//Any has ORs
		if (advancedSearchToggle == "Any"){
			minMaxSection = minMaxSection + " OR ";
		}
	}

	

	//Check for any values
	if (formMin != "" || formMax != "")
	{
		noParams = false;
		//Min only
		if (formMin != "" && formMax == ""){
			minMaxSection = minMaxSection + "((cs_Funding_Min:[" + formMin + " TO 2000000000]) OR (cs_Funding_Min:\"-1\" AND cs_Funding_Max:[" + formMin + " TO 2000000000]))";
		}

		//Max only
		if (formMin == "" && formMax != ""){
			minMaxSection = minMaxSection + "((cs_Funding_Max:[0 TO " + formMax + "]) OR (cs_Funding_Max:\"-1\" AND cs_Funding_Min[0 TO " + formMax + "]))";
		}

		//Min and Max
		if (formMin != "" && formMax != ""){
			minMaxSection = minMaxSection + "(((cs_Funding_Min:[" + formMin + " TO " + formMax + "]) OR (cs_Funding_Min:\"-1\")) AND ((cs_Funding_Max:[" + formMin + " TO " + formMax + "]) OR (cs_Funding_Max: \"-1\")))";
		}

		advancedSearchCrit = advancedSearchCrit + minMaxSection;
	}
	
}
