﻿var Search = {
	searchMsgTimeout: null,
	
	Search: function() {
		var searchLoc = FindControl("searchLoc","select");
		var loc = searchLoc.options[searchLoc.selectedIndex].value;
		var ln = Get("searchLN").value;
		var fn = Get("searchFN").value;
		var pos = Get("searchPos").value;
		
		var params = "";
		if (loc != "" && loc != "-1") {
			params += "&loc=" + escape(loc);
		}
		
		if (ln != "") {
			params += "&ln=" + escape(ln);
		}
		
		if (fn != "") {
			params += "&fn=" + escape(fn);
		}
		
		if (pos != "") {
			params += "&pos=" + escape(pos);
		}
		
		if (params == "") {
			alert("You must enter something to search by.");
			return;
		}
		
		this.ClearResults();
		this.Query("/StaffDirectory/View.aspx?m=list&v=render&qs=central" + params);
	},
	
	Browse: function(loc) {
		this.ClearResults();
		this.Query("/StaffDirectory/View.aspx?m=list&v=render&qs=central&loc=" + loc);
	},
	
	Query: function(url) {
	    $("browseSection").style.display = 'none'; //Hide the Browse by Location table
		
		//Hide the entire location editor section.
		this.ShowLocationLink(false);
		
		//Close the Staff Location editor if it's open.
		this.ShowLocationFrame(false);
		this.Message(true);
		
		View.Search(url);
	},
	
	Keyup: function(ev) {
		var eo = (window.event ? window.event : ev);
		
		if (eo.keyCode == 13) {
			Search.Search();
			return false;
		}
		
		return true;
	},
	
	Clear: function() {
		this.ClearResults();		
		this.Message(false);

		$("browseSection").style.display = ''; //Show the Browse by Location table
		
		//Show/hide admin functions.
		this.OnSecurityCheck();
		
		//Clear out of the search parameters.
		FindControl("searchLoc","select").selectedIndex = 0;
		Get("searchLN").value = "";
		Get("searchFN").value = "";
		Get("searchPos").value = "";
		
		View.Clear();
	},
	
	ClearResults: function() {
		//Hide the previous search results and any search message.
		var searchResults = Get("searchResults");
		searchResults.style.display = 'none';
		searchResults.innerHTML = '';
		
		//Close a map if it's open.
		MapClose();
	},
	
	Message: function(show) {
		if (this.searchMsgTimeout != null) {
			clearTimeout(this.searchMsgTimeout);
		}
		
		if (show) {
			//Display a "searching" message after a short time.
			this.searchMsgTimeout = setTimeout("Get('searchMsg').style.display = 'block'", 200);
		}
		else {
			Get('searchMsg').style.display = 'none';
		}
	},
	
	//Called after View.Search() is run in View.js.
	OnSearchDone: function() {
		this.Message(false);
	},
	
	//Called after Security.Check() is run in Security.js.
	OnSecurityCheck: function() {
		//Show/hide the Location editor
		this.ShowLocationLink(null);
	},
	
	//Show or hide the Location editor link.
	//Parameters:
	//	showLink - true, false, or null.  If null, shows or hides depending on user security.
	ShowLocationLink: function(showLink) {
		var locEditDiv = FindControl("locEditDiv","div");
				
		if (showLink == null)
			showLink = Security.IsAdmin;
		
		locEditDiv.style.display = (showLink ? '' : 'none');
		
		var locationFrame = Get("locationFrame");
		locationFrame.src = "/StaffDirectory/" + (showLink ? "EditLocation.aspx" : "Loading.htm");
	},
	
	//Show or hide the Location editor iframe.
	//Parameters:
	//	showFrame - true, false, or null.  If null, toggles visibility.
	ShowLocationFrame: function(showFrame) {
		var locationFrame = Get("locationFrame");
		
		if (showFrame != null) {
			locationFrame.style.display = (showFrame ? "" : "none");
		}
		else {
			locationFrame.style.display = (locationFrame.style.display == "" ? "none" : "");
		}
	}

}	//END SEARCH
