/* Browser check */
var IS_DOM = (document.getElementById) ? true : false;
var IS_IE = (document.all) ? true : false;
var IS_IE50 = (navigator.userAgent.indexOf("IE 5.0") != -1);
var IS_Mac = (navigator.appVersion.indexOf("Mac") != -1);

// preload level 1 buttons
var btnLevel1 = new Array();
btnLevel1 = ["", "high-risk-areas", "patient-safety-strategies", "case-studies", "educationinterventions", "research-resources"];
for (var i = 0; i < btnLevel1.length; i++) { 
	eval("var menu" + i + "on = new Image();"); eval("var menu" + i + "off = new Image();");
	eval("menu" + i + "on.src = \"" + sFolderLevel + "img/navigation/l1-" + btnLevel1[i] + "-r.gif\";");
	eval("menu" + i + "off.src = \"" + sFolderLevel + "img/navigation/l1-" + btnLevel1[i] + "-0.gif\";");
	if (sSelectedButton == btnLevel1[i]) {
		eval("menu" + i + "off.src = \"" + sFolderLevel + "img/navigation/l1-" + btnLevel1[i] + "-r.gif\";");
	}
}

var tabs = new Array();
tabs = ["", "diagnosis", "surgery", "obstetrics", "medication"];
for (var i = 0; i < tabs.length; i++) { 
	eval("var tab" + i + "on = new Image();"); eval("var tab" + i + "off = new Image();");
	eval("tab" + i + "on.src = \"" + sFolderLevel + "img/homepage/tab-" + tabs[i] + "-r.gif\";");
	eval("tab" + i + "off.src = \"" + sFolderLevel + "img/homepage/tab-" + tabs[i] + "-0.gif\";");
}

var constants = new Array();
constants = ["", "company", "insurance", "events", "contacts", "search"];
for (var i = 0; i < constants.length; i++) { 
	eval("var constant" + i + "on = new Image();"); eval("var constant" + i + "off = new Image();");
	eval("constant" + i + "on.src = \"" + sFolderLevel + "img/navigation/cnst-" + constants[i] + "-r.gif\";");
	eval("constant" + i + "off.src = \"" + sFolderLevel + "img/navigation/cnst-" + constants[i] + "-0.gif\";");
	if (sSelectedButton == constants[i]) {
		eval("constant" + i + "off.src = \"" + sFolderLevel + "img/navigation/cnst-" + constants[i] + "-r.gif\";");
	}
}

// Image Swapping
function changeImages() 
{
  if (document.getElementById) {
    for (var i = 0; i < changeImages.arguments.length; i += 2) { 
  		document.getElementById(changeImages.arguments[i]).src = eval(changeImages.arguments[i + 1] + ".src"); 
	}
  }
}

// Get an ID 
function getThis(sId)
{
	var oObject;
	oObject = false;
	
	if (IS_DOM) {
		if (document.getElementById(sId)) {
			oObject = document.getElementById(sId);
		}
	}
	
	return oObject;
}

// Returns single element or multiple elements in an array
function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements[elements.length] = element;
	}
	return elements;
}
	
// accessible popup functions, this will work with or without javascript enabled
// reference like this "<a href="popup.htm" onclick="popUp(this.href,'console',400,200);return false;" target="newWin" title="This link will pop open a new window">This is my link</a>"
	function popUp(strURL,strType,strHeight,strWidth,strWindow)
	{
	  var strOptions="";
	  if (strType=="console") {
	    strOptions="resizable,height=" + strHeight + ",width=" + strWidth;
	  }
	  if (strType=="fixed") {
	    strOptions="status,height=" + strHeight + ",width=" + strWidth;
	  }
	  if (strType=="elastic") {
	    strOptions="toolbar,menubar,scrollbars,resizable,location,height=" + strHeight + ",width=" + strWidth;
	  }
	  window.open(strURL, strWindow, strOptions);
	}

function revealHide(sectionHeaderLink)
{
	var oContainer = sectionHeaderLink.parentNode.parentNode;
	theContentDiv = oContainer.getElementsByTagName("div").item(0);
	currentState = theContentDiv.style.display;
	var pattern = new RegExp("sectionClosed");
	if (currentState == "none" || pattern.test(oContainer.className)) {
		oContainer.className = "section";
		theContentDiv.style.display = "block";
		sectionHeaderLink.style.backgroundImage = "url(" + sFolderLevel + "img/content-design/bg-minus.gif)";
	} else {
		theContentDiv.style.display = "none";
		sectionHeaderLink.style.backgroundImage = "url(" + sFolderLevel + "img/content-design/bg-plus.gif)";
	}
}

function switchCase(oSelect) {
	var iIndex = oSelect.selectedIndex;
	if (iIndex != 0) {
		top.location.href = sFolderLevel + "case-studies/" + oSelect.options[iIndex].value;
	}
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function toggleAll(oLink) {
	var aSections = getElementsByClass("section", document, "div");
	var oLabel = oLink.firstChild;
	var sState = "block";
	var sGraphic = "minus";
	
	if (oLabel.nodeValue != "Expand All") {
		sState = "none";	
		sGraphic = "plus";	
	}
	for (var i = 0; i < aSections.length; i++) {
		aSections[i].id = "";
		aSections[i].getElementsByTagName("div")[0].style.display = sState;
		aSections[i].getElementsByTagName("a")[0].style.backgroundImage = "url(" + sFolderLevel + "img/content-design/bg-" + sGraphic + ".gif)";
	}
}

function handleEnter (oForm, event) {
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13) {
		runSearch(oForm);
		return true;
	} else {
		return false;
	}
}

function runSearch(oForm)
{
	if (oForm)
	{
		var sQueryTerms = oForm.elements["qt"].value;
		
		if (sQueryTerms != "")
		{
			window.location = "http://search.rmf.harvard.edu/rmf/query.html?col=rmf&qc=rmf&qt=" + URLEncode(sQueryTerms);
		}
	}
}

// ====================================================================
//       URLEncode and URLDecode functions
//
// Copyright Albion Research Ltd. 2002
// http://www.albionresearch.com/
//
// You may copy these functions providing that 
// (a) you leave this copyright notice intact, and 
// (b) if you use these functions on a publicly accessible
//     web site you include a credit somewhere on the web site 
//     with a link back to http://www.albionresarch.com/
//
// If you find or fix any bugs, please let us know at albionresearch.com
//
// SpecialThanks to Neelesh Thakur for being the first to
// report a bug in URLDecode() - now fixed 2003-02-19.
// ====================================================================
function URLEncode(sValue)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = sValue;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for
	return encoded;
};