function linkEmail(un) {
	var at = "@";
	var one = "mai";
	var two = "lto:";
	var mh = "oandp.com";     // specify default mailhost here
	var lbl = "";
	var qrystr = "";

	if (linkEmail.arguments.length >= 2) {
		mh = linkEmail.arguments[1];
	}
	if (linkEmail.arguments.length >= 3 && linkEmail.arguments[2] != "") {
		lbl = linkEmail.arguments[2];
	}
	else {
		lbl = un + at + mh;
	}
	if (linkEmail.arguments.length >= 4 && linkEmail.arguments[3] != "") {
		qrystr = "sub" + "ject=" + linkEmail.arguments[3];
	}
	if (linkEmail.arguments.length >= 5 && linkEmail.arguments[4] != "") {
		if (qrystr != "") { qrystr += "&"; }
		qrystr += "bo" + "dy=" + linkEmail.arguments[4];
	}
	if (qrystr != "") { qrystr = "?" + qrystr;}
	document.write("<a href=\"" + one + two + un + at + mh + qrystr + "\">" + lbl + "</a>");
}

function DisplaySection(strID, strElementType)
{
	try {
		   document.getElementById(strID).style.display = strElementType; //'table-row';
		}
		catch(exception) {
			document.getElementById(strID).style.display = 'block';
		}
}

function HideSection(strID)
{
	document.getElementById(strID).style.display = 'none';
}

function ToggleSectionVisibility(strID, strElementType)
{
	if (document.getElementById(strID).style.display == 'none' || document.getElementById(strID).style.display == '')
	{
		DisplaySection(strID, strElementType);
	}
	else
	{
		HideSection(strID);
	}
}

function SearchBox() {
	this.container = $("search");
	this.tabs = $("search-tabs");
	this.currentTab = $("search-all");
	this.defaultTabStyle = {fontWeight: "normal"};
	this.currentTabStyle = {fontWeight: "bold"};
	this.currentInputArea = $("search-input-default");
	this.searchButton = $("search-button");
	this.moreButton = $("search-more");
	this.moreTabs = $("search-tabs-more");
	this.moreDuration = 0.2;
	this.moreOffsetLeft = -6;
	this.inputHints = new Hash();
	this.inputHintColor = "#999";
	this.defaultInputColor = "#000";
	this.defaultFormAction = this.container.down("form").action;
	
	this.setTab = function(tab) {
		this.tabs.select("a").invoke("setStyle", this.defaultTabStyle)
		this.moreTabs.select("a").invoke("setStyle", this.defaultTabStyle)
		tab.setStyle(this.currentTabStyle);
		this.searchButton.update("Search " + tab.innerHTML);
		this.currentTab = tab;
		
		this.container.select("div.search-input").invoke("hide");
		this.currentInputArea = $("search-input-" + this.currentTab.id.replace("search-", "")) || $("search-input-default");
		this.container.down("form").action = this.currentInputArea.down("input[name='" + this.currentTab.id + "-action']") ? this.currentInputArea.down("input[name='" + this.currentTab.id + "-action']").value : this.defaultFormAction;
		this.currentInputArea.show();
		var currentInput = this.currentInputArea.down("input[type='text']");
		Event.stopObserving(currentInput);
		if (this.inputHints.get(currentInput.id)) {
			currentInput.value = this.inputHints.get(currentInput.id);
			currentInput.style.color = this.inputHintColor;
			Event.observe(currentInput, "focus", function(event) {
				if (Event.element(event).value == this.inputHints.get(Event.element(event).id)) {
					Event.element(event).value = "";
					Event.element(event).style.color = this.defaultInputColor;
				}
			}.bind(this));
			Event.observe(currentInput, "blur", function(event) {
				if (Event.element(event).value == "") {
					Event.element(event).value = this.inputHints.get(Event.element(event).id);
					Event.element(event).style.color = this.inputHintColor;
				}
			}.bind(this));
		} else {
			//currentInput.focus();
		}
		Event.observe(currentInput, "keydown", function(event) { if (event.keyCode == Event.KEY_RETURN) { this.doSearch(); Event.stop(event); } }.bind(this));
	}
	
	this.showMoreTabs = function() {
		this.moreTabs.absolutize();
		this.moreTabs.setStyle({position: "absolute", left: (this.moreButton.cumulativeOffset().left + this.moreOffsetLeft) + "px", top: (this.moreButton.cumulativeOffset().top + this.moreButton.getHeight()) + "px", width: "auto", height: "auto"});
		new Effect.Appear(this.moreTabs, {duration: this.moreDuration});
	}
	
	this.hideMoreTabs = function() {
		new Effect.Fade(this.moreTabs, {duration: this.moreDuration});
	}
	
	this.doSearch = function() {
		var form = this.container.down("form");
		var query = "";
		var currentInput = this.currentInputArea.down("input[type='text']");
		var baseHref = document.getElementsByTagName('base')[0].href;
		var scope = this.currentTab.id.replace("search-", "");
		
		if ($F(currentInput) && $F(currentInput) != this.inputHints.get(currentInput.id)) {
			// reformat parameters for specific search pages
			switch(form.action.replace(baseHref, "").toLowerCase()) {
				case "facilities/search.asp" : query = "qry=" + encodeURIComponent($F(currentInput)); break;
				default : query = "qu=" + encodeURIComponent($F(currentInput)) + "&scope=" + encodeURIComponent(scope); break;
			}
			window.location = baseHref + form.action.replace(baseHref, "") + "?" + query;
		} else {
			Effect.Shake(currentInput, {duration: 0.2, distance: 3});
			currentInput.focus();
		}
	}
	
	// initialize
	
	this.setTab(this.currentTab);
	//this.currentInputArea.down("input").focus();
	this.container.select("div.search-input input[type='text']").each(function(input) { if (input.getAttribute("title") != "") this.inputHints.set(input.id, input.getAttribute("title")); }.bind(this));
	$("container").insert(this.moreTabs.remove());
	
	// events
	Event.observe(this.tabs, "click", function(event) { if (Event.element(event).tagName == "A") this.setTab(Event.element(event)); }.bind(this));
	Event.observe(this.moreTabs, "click", function(event) { if (Event.element(event).tagName == "A") this.setTab(Event.element(event)); }.bind(this));
	Event.observe(this.moreButton, "click", function(event) { if (this.moreTabs.visible()) this.hideMoreTabs(); else this.showMoreTabs(); Event.stop(event); }.bind(this));
	Event.observe(this.searchButton, "click", function(event) { this.doSearch(); }.bind(this));
	Event.observe(document.body, "click", function(event) { this.hideMoreTabs(); }.bind(this));
}

Event.observe(document, "dom:loaded", function(event){
	if ($("search")) var newSearchBox = new SearchBox();
});