// print page
function printpage() {
window.print();
}

// quicklink
function _goTopic(url)
 {
String(url).match(/^#/) ? null : self.location = url;
 }

function OpenNewWin(NewURL) {
window.open(NewURL,'WebProPractice','width=760,height=600,scrollbars=yes,menubar=yes');
}

// Show all hidden content when searching,
function ShowAll(){
var elements;
elements = document.getElementsByTagName('div');
for(var i = 0; i < elements.length; i++){
	var node = elements.item(i);
	for(var j = 0; j < node.attributes.length; j++) {
		if(node.attributes.item(j).nodeName == 'class') {
			if(node.attributes.item(j).nodeValue == 'hidden') {
					eval('node.style.display = '+ "'block'");
								}
			}
		}
	}				
}

// Show search,
function ShowSearch(){			
var HMenuItems=document.getElementById('Searchhide').style;
var SearchBox=document.getElementById('SearchBox').style;
HMenuItems.display="none";
SearchBox.display="block";
}

// view hidden content,
function ViewCont(ItemID){
var elements;
elements = document.getElementsByTagName('div');
for(var i = 0; i < elements.length; i++){
	var node = elements.item(i);
	for(var j = 0; j < node.attributes.length; j++) {
		if(node.attributes.item(j).nodeName == 'class') {
			if(node.attributes.item(j).nodeValue == 'hidden') {
					eval('node.style.display = '+ "'none'");
								}
			}
		}
	}				
var listStyle=document.getElementById(ItemID).style;
var IntroStyle=document.getElementById('Intro').style;
listStyle.display="block";
IntroStyle.display="none";
}

// Expand a specific menu,
function ExpList(list){			
var listStyle=document.getElementById(list).style;
listStyle.display="list-item";
}

function delconfirm(DelID,ItemName) {
	if(location.href.indexOf("?") < 1) 
   {
   var attr = "?"
   }
   else {
   var attr = "&"
	}
	var answer = confirm('Delete ' + ItemName + '?')
	if (answer){
		window.location = location.href + attr +'del=' + DelID;
	}
	else{
		window.location = location.href;
	}
}

function GotoURL(URL,Attr) {
	window.location = URL + Attr;
}


// Check the search box before submitting
function CheckForm () {

	//Check that box is not blank
	if (document.WordSearch.search.value==""){
		alert("Please enter a keyword to search");
		document.WordSearch.search.focus();
		return false;
	}
	
	return true
}

function winBRopen(theURL, Name, popW, popH, scroll, resize) {
	var winleft = (screen.width - popW) / 2;
	var winUp = (screen.height - popH) / 2;
	winProp = 'width='+popW+',height='+popH+',left='+winleft+',top='+winUp+',scrollbars='+scroll+',resizable='+resize+'';
	Win = window.open(theURL, Name, winProp);
	Win.window.focus();
}


//Text highlights
function doHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag) 
{
  // the highlightStartTag and highlightEndTag parameters are optional
  if ((!highlightStartTag) || (!highlightEndTag)) {
    highlightStartTag = "<font style='color:blue; background-color:yellow;'>";
    highlightEndTag = "</font>";
  }
  
  // find all occurences of the search term in the given text,
  var newText = "";
  var i = -1;
  var lcSearchTerm = searchTerm.toLowerCase();
  var lcBodyText = bodyText.toLowerCase();
    
  while (bodyText.length > 0) {
    i = lcBodyText.indexOf(lcSearchTerm, i+1);
    if (i < 0) {
      newText += bodyText;
      bodyText = "";
    } else {
      // skip anything inside an HTML tag
      if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
        // skip anything inside a <script> block
        if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
          newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
          bodyText = bodyText.substr(i + searchTerm.length);
          lcBodyText = bodyText.toLowerCase();
          i = -1;
        }
      }
    }
  }
  
  return newText;
}


// Find Text

function highlightSearchTerms(searchText, treatAsPhrase, warnOnFailure, highlightStartTag, highlightEndTag)
{
  // if the treatAsPhrase parameter is true, then we should search for 
  // the entire phrase that was entered; otherwise, we will split the
  // search string so that each word is searched for and highlighted
  // individually
  if (treatAsPhrase) {
    searchArray = [searchText];
  } else {
    searchArray = searchText.split(" ");
  }
  
  if (!document.body || typeof(document.body.innerHTML) == "undefined") {
    if (warnOnFailure) {
      alert("Sorry, for some reason the text of this page is unavailable. Searching will not work.");
    }
    return false;
  }
  
  var bodyText = document.body.innerHTML;
  for (var i = 0; i < searchArray.length; i++) {
    bodyText = doHighlight(bodyText, searchArray[i], highlightStartTag, highlightEndTag);
  }
  
  document.body.innerHTML = bodyText;
  return true;
}

// Parse search term

function highlightPageSearchTerms(referrer)
{
  var queryPrefix = "search=";
  var startPos = referrer.toLowerCase().indexOf(queryPrefix);
  if ((startPos < 0) || (startPos + queryPrefix.length == referrer.length)) {
    return false;
  }
  
  var endPos = referrer.indexOf("&", startPos);
  if (endPos < 0) {
    endPos = referrer.length;
  }
  
  var queryString = referrer.substring(startPos + queryPrefix.length, endPos);
  // fix the space characters
  queryString = queryString.replace(/%20/gi, " ");
  queryString = queryString.replace(/\+/gi, " ");
  // remove the quotes (if you're really creative, you could search for the
  // terms within the quotes as phrases, and everything else as single terms)
  queryString = queryString.replace(/%22/gi, "");
  queryString = queryString.replace(/\"/gi, "");
  
  return highlightSearchTerms(queryString, false);
}