function searchFocus(box){
	if(box.value=="search"){
		box.value="";
	} else {
		box.select();
	}
}

function searchBlur(box){
	if(box.value==""){
		box.value="search";
	}
}

var xmlhttp = null;
if (window.XMLHttpRequest) {
	xmlhttp=new XMLHttpRequest();
} else {
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

function queryAndReplace(query, replace_el_id) {
	document.body.style.cursor='wait';
	
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			document.getElementById(replace_el_id).innerHTML = xmlhttp.responseText;
			document.body.style.cursor='auto';
		}
	}

	xmlhttp.open("GET", query, true);
	xmlhttp.send(null);
}

/*
***************************************************************
                   GLOSSARY DEFINITIONS
***************************************************************
*/

//Get the glossary terms and pass them to highlightSearchTerms
var search_array = new Array();
function getTerms(){
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (err) {
			xmlhttp = null;
		}
	}
	if(!xmlhttp && typeof XMLHttpRequest != "undefined")
		xmlhttp = new XMLHttpRequest();
	if (!xmlhttp){
		//don't let them know nothing worked
	}

	xmlhttp.onreadystatechange = function() {
		switch (xmlhttp.readyState){
			case 1:
			case 2:
			case 3:
				break;
			case 4:
				response = xmlhttp.responseText;
				eval(response);
				/*search_array[4] = "Collaboration";
				search_array[32] = "CRI";
				search_array[56] = "Sustainability";
				search_array[65] = "West Michigan Strategic Alliance";
				search_array[70] = "WMSA";
				*/
				//alert(search_array[0]);
				highlightSearchTerms(search_array);
				break;
		}
	}

	xmlhttp.open('GET', 'get_glossary_terms.php', true);
	xmlhttp.send("");

}

/*
 * This is sort of a wrapper function to the doHighlight function.
 * It takes the searchText that you pass, optionally splits it into
 * separate words, and transforms the text on the current web page.
 * Only the "searchText" parameter is required; all other parameters
 * are optional and can be omitted.
 */

 function highlightSearchTerms(searchArray, treatAsPhrase, warnOnFailure){
	
	var content = document.getElementById('content');
	if (!content || typeof(content.innerHTML) == "undefined") {
		if (warnOnFailure) {
			//alert("Sorry, for some reason the text of this page is unavailable. Searching will not work.");
		}
		return false;
	}
	var bodyText = content.innerHTML;
	for (var i = 0; i < searchArray.length; i++) {
		if(searchArray[i]!=undefined){
			bodyText = doHighlight(bodyText, searchArray[i], i);
			content.innerHTML = bodyText;
		} else {
			//alert(i);
		}
	}
  
	
	return true;
}

/*
 * This is the function that actually highlights a text string by
 * adding HTML tags before and after all occurrences of the search
 * term. You can pass your own tags if you'd like, or if the
 * highlightStartTag or highlightEndTag parameters are omitted or
 * are empty strings then the default <font> tags will be used.
 */
 function doHighlight(bodyText, searchTerm, definition_id) {
 
  highlightStartTag = "<a class=\"definition\" href=\"glossary.php#"+definition_id+"\" onmouseover=\"ajax_showTooltip('get_definition.php?definition_id="+definition_id+"',this);return false\" onmouseout=\"ajax_hideTooltip()\">";
  highlightEndTag = "</a>";
  //<img src=\"layout_imgs/definition.png\" alt=\"define\" />
   
  // find all occurences of the search term in the given text,
  // and add some "highlight" tags to them (we're not using a
  // regular expression search, because we want to filter out
  // matches that occur within HTML tags and script blocks, so
  // we have to do a little extra validation)
  var newText = "";
  var i = -1;
  var lcSearchTerm = searchTerm.toLowerCase();
  var lcBodyText = bodyText.toLowerCase();
  
  //while (bodyText.length > 0) {
	if(lcBodyText.indexOf(" "+lcSearchTerm+" ") > 0){
		i = lcBodyText.indexOf(" "+lcSearchTerm+" ");

	} else if(lcBodyText.indexOf(" "+lcSearchTerm+".") > 0 ){
		i = lcBodyText.indexOf(" "+lcSearchTerm+".");

	} else if(lcBodyText.indexOf(" "+lcSearchTerm+",") > 0){
		i = lcBodyText.indexOf(" "+lcSearchTerm+",");

	} else if(lcBodyText.indexOf("("+lcSearchTerm+")") > 0){
		i = lcBodyText.indexOf("("+lcSearchTerm+")");

	}
	
	var found = false;
	if (i < 0) {
      newText = bodyText;
      //bodyText = "";
    } else {
      // skip anything inside an HTML tag
      if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
		found = false;
        // skip anything inside a <script> block
        if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
			found = false;
			//skip anything inside a <a> block
			if(lcBodyText.lastIndexOf("/a>", i) >= lcBodyText.lastIndexOf("<a", i)){
				found = false;
			//skip anything inside a heading block
			if(lcBodyText.lastIndexOf("/h", i) >= lcBodyText.lastIndexOf("<h", i)){
				found = true;
				newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length+1) + highlightEndTag + bodyText.substr(i + searchTerm.length+1);
				//bodyText = bodyText.substr(i + searchTerm.length+1);
				//lcBodyText = bodyText.toLowerCase();
				//i = -1;
			}
			}
        }
      }
    }
  //}
  if(!found){
	newText = bodyText;
  }
  return newText;
}