//JavaScript Container


function enableSearchSubmit () {
	document.getElementById('searchSubmit').style.visibility='visible';

}

/* Search Submission */
function searchRefinement (){
	
	//Load form element
	var searchCrit = document.getElementById("searchCriteria").value;

	//Remove quotes (Previous searches)
	searchCrit = searchCrit.replace(/"/g," ");

	//Replace Search AND / OR / NOT (Previous Searches)
	searchCrit = searchCrit.replace(/\bAND\b/g,"");
	searchCrit = searchCrit.replace(/\bOR\b/g,"");	
	
	//convert double spaces to single
	searchCrit = searchCrit.replace(/ {2,}/g,' ');

	//Trim start and end of string
	searchCrit = searchCrit.replace(/^\s\s*/, '').replace(/\s\s*$/, '');	

	//Check for empty search
	if (searchCrit.length == 0)
	{
		alert("Please enter some characters into your search");
		return false;
	}

	//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 specific field
	if (searchCrit.indexOf(":") != -1)
	{
		alert("Live Site search does not support direct field searches, please remove all colons ':' 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 search not terms (Minus)
	if (searchCrit.indexOf("-") != -1)
	{
		alert("Live Site search does not support minus terms, please remove all minus symbols '-' from your search.");
		return false;
	}

	//Check for search not terms (NOT)
	if (searchCrit.indexOf("NOT") != -1)
	{
		alert("Live Site search does not support capital 'NOT' terms, please remove all capital 'NOT' words from your search.");
		return false;
	}

	//Check for grouping (Brackets)
	if ((searchCrit.indexOf("(") != -1) || (searchCrit.indexOf(")") != -1))
	{
		alert("Live Site search does not support brackets '( )', please remove all brackets 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;
	}
	
	//Split the search string up
	var searchCritArray = searchCrit.split(" ");

	var searchString = "";

	var searchMode = "default";

	//Find the selected Search mode from radio buttons
	for (var i=0; i < document.advancedSearch.searchMode.length; i++)
	{
	   if ( document.advancedSearch.searchMode[i].checked)
	   {
		  searchMode =  document.advancedSearch.searchMode[i].value;
	   }
	}

	//All of the words
	if (searchMode == "All")
	{
		searchString = searchCrit.replace(/\s/g,' AND ');
	}
	
	//Exact Phrase
	if (searchMode == "Exact")
	{
		searchString = "\"" + searchCrit + "\"";
	}

	//Any of the words
	if (searchMode == "Any")
	{
		searchString = searchCrit;
	}

	//Update the query
	document.getElementById("searchCriteria").value = searchString;

	//Pass through the original search (For 'Your search for...')
	document.getElementById("searchedText").value = searchCrit;

	//Submit the search form
	document.advancedSearch.submit();
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

function createString(tempString,oper)
{
	if(oper == " AND ")
	{
		tempString = tempString.replace(/ /g,oper);
	}
	if(oper == "")
	{
		tempString = "\"" + tempString + "\"";
	}
	if(oper == " OR ")
	{
		tempString = "(" + tempString + ")";
	}
	return tempString;
}

function cycleAdvanced(obj)
{
	if(obj.innerHTML == "Show Advanced Options")
	{
		document.getElementById("advancedSearchOptions").style.display = "block";
		document.getElementById("advancedSearchOptions").style.position = "static";
		obj.innerHTML = "Advanced Options";
		obj.title = "Click to cancel advanced search options";
	}
	else
	{
		document.getElementById("advancedSearchOptions").style.display = "none";
		document.getElementById("advancedSearchOptions").style.position = "absolute";
		obj.innerHTML = "Show Advanced Options";
		obj.title = "Click to display advanced search options";
		document.getElementById("allWords").value = "";
		document.getElementById("anyWords").value = "";
		document.getElementById("exactWords").value = "";
		document.getElementById("notWords").value = "";
	}
}


function eventFilter()
{
	var x = document.getElementById("eventFilterSelect");

	if(x.selectedIndex > 0)
	{
		var y = x.selectedIndex;
		var url = window.location.toString();
		url.match(/\?(.+)$/);
		var params = RegExp.$1;
		var params = params.split("&");
		var queryStringNames = {};
		var queryStringList = {};
		var returnedQuery = "";
		 
		if(document.URL.indexOf('?') != -1)
		{
			for(var i=0; i<params.length; i++)
			{
				var tmp = params[i].split("=");
				queryStringNames[tmp[0]] = unescape(tmp[0]);
				queryStringList[tmp[0]] = unescape(tmp[1]);
			}
			 
			// print all querystring in key value pairs
			for(var i in queryStringList)
			{
				if(queryStringNames[i].toLowerCase() != "searchcriteria" && queryStringNames[i].toLowerCase() != "category" && queryStringNames[i].toLowerCase() != "cleared")
				{
					returnedQuery += "&" + queryStringNames[i] + "=" + queryStringList[i];
				}
			}
		}

		if(x.options[y].value != currentOption)
		{
			if(x.options[y].value != "all")
			{
				window.parent.location.href = "../Events/index.aspx?searchCriteria=cs_Catagory:(\"" + x.options[y].value + "\") AND (cs_Start_Date:[" + isoDate + " TO 9999-99-99])" + returnedQuery + "&category=" + x.options[y].value;
			}
			else
			{
				window.parent.location.href = "../Events/index.aspx?searchCriteria=cs_Start_Date:[" + isoDate + " TO 9999-99-99]" + returnedQuery + "&cleared=true";
			}
		}
	}
}

function favoriteSite()
{
	var x = document.getElementById("siteDropDown");

	if(x.selectedIndex > 0)
	{
		var y = x.selectedIndex;
		 
		window.open(x.options[y].value);
	}
}


function changeTextSize(obj)
{
	if(obj.innerHTML == "A-")
	{
		obj.innerHTML = "A+";
		document.body.style.fontSize = "0.75em";
		createCookie("fontSize","0.75em",30);
	}
	else
	{
		obj.innerHTML = "A-";
		document.body.style.fontSize = "0.85em";
		createCookie("fontSize","0.85em",30);
	}
}

function createCookie(name,value,days) {
	if (days) {
				var date = new Date();
				date.setTime(date.getTime()+(days*24*60*60*1000));
				var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
				var c = ca[i];
				while (c.charAt(0)==' ') c = c.substring(1,c.length);
				if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function setTextSize()
{
	var tempString = readCookie("fontSize");

	if(tempString != null)
	{
		document.body.style.fontSize = tempString;
		if(tempString == "0.85em")
		{
			document.getElementById("textSize").innerHTML = "A-";
		}
	}
	else
	{
		document.body.style.fontSize = "0.75em";
	}
}
