var modern = document.getElementById || document.all;

function getEl(name,win) {
	var doc;
	win = win || window;
	doc = win.document || win.contentDocument;
	if (doc.all) {
		return doc.all(name);
	} else if (doc.getElementById) {
		return doc.getElementById(name);
	} else {
		return null;
	}
}

if (window.parent.frames && window.parent.frames.sideFrame && window.parent.frames.sideFrame.hilite) {
	window.parent.frames.sideFrame.hilite(pageDir,pageName);
}

function setSide(side) {
	var page = "../index.html?side="+pageDir+"/"+side+"&main="+String(window.location);
	window.top.location = page;
	return false;
}


function loader() {
  	window.top.document.title = window.document.title;

 	if (!window.top.fromTOC && typeof(window.top.fromTOC) != "undefined" && window.top.lastLink && window.parent.frames.sideFrame.removeTOCHighLight) {
		window.parent.frames.sideFrame.removeTOCHighLight();
 	}
	else if (!window.top.fromSearch && typeof(window.top.fromSearch) != "undefined" && window.top.lastSearch && window.top.lastSearchWindow) {

		var win;
		if (window.top.lastSearchWindow == "topSearch") {
			win = window.parent.frames.sideFrame.frames.topSearch;
		}
		else if (window.top.lastSearchWindow == "bottomSearch") {
			win = window.parent.frames.sideFrame.frames.bottomSearch;
		}
		else if (window.top.lastSearchWindow == "tabResults") {
			win = window.parent.frames.sideFrame.frames.tabResults;
		}
	
		if (win && win.removeSearchHighLight) {
			win.removeSearchHighLight();
		}
 	}

	window.top.fromTOC = false;
	window.top.fromSearch = false;
}

function highlight(bodyText, searchTerm) 
{
  highlightStartTag = "<font style='color:navy; background-color:#FFFF99;'>";
  highlightEndTag = "</font>";

  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 {
      if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
        if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i) && lcBodyText.lastIndexOf("/p>", i) >= lcBodyText.lastIndexOf("<p id=chapter", i) && lcBodyText.lastIndexOf("/table>", i) >= lcBodyText.lastIndexOf("<table class=head", 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;
}

function highLightSearch()
{
  loader();

  if (!document.body || typeof(document.body.innerHTML) == "undefined") {
    return false;
  }

  var queryPrefix = "hl=";
  var hl = String(window.location.search).substr(1);
  var startPos = hl.toLowerCase().indexOf(queryPrefix);
  if ((startPos < 0) || (startPos + queryPrefix.length == hl.length)) {
    return false;
  }
  var endPos = hl.indexOf("&", startPos);
  if (endPos < 0) {
    endPos = hl.length;
  }
  
  var queryString = hl.substring(startPos + queryPrefix.length, endPos);
  queryString = queryString.replace(/%20/gi, " ");
  queryString = queryString.replace(/\+/gi, " ");
  queryString = queryString.replace(/\"/gi, "");
  queryString = queryString.replace(/%22/gi, "\"");

  var quotes = new Array;
  var last = -1;
  
  for (var i = 0; i < queryString.length; i++) {   
    pos = queryString.indexOf("\"", i);

    if (pos < 0) {
      i = queryString.length;
    } 
    else {
      i = pos;

      if (last == -1) {
        last = pos;
      } 
      else { 
	quotes.push(queryString.substring(last+1, pos));
	last = -1;
      }
    }  
  }

  for (var i = 0; i < quotes.length; i++) {  
    re = new RegExp ("\"" + quotes[i] + "\"", 'gi') ;
    queryString = queryString.replace(re, "");
  } 
  
  searchArray = queryString.split(" ");
  for (var i = 0; i < quotes.length; i++) {
    searchArray.push(quotes[i]);
  } 

  var bodyText = document.body.innerHTML;
  for (var i = 0; i < searchArray.length; i++) { 
    if (searchArray[i] != "" && searchArray[i] != "AND" && searchArray[i] != "OR") {
      if (searchArray[i].length < 3) {
        searchArray[i] = " " + searchArray[i] + " ";
      }
      bodyText = highlight(bodyText, searchArray[i]);
    }
  }
  
  document.body.innerHTML = bodyText;
  return true;
}

function searchFor(str) {

	 var hpp = "";
	 var loadFrame = "../searchFrame.html?";
	 var cookieName = "mayaPrefs";

	 if (document.cookie.length > 0) { 
	    begin = document.cookie.indexOf(cookieName+"=");
	   
	    if (begin != -1) { 
	       begin += cookieName.length+1; 
	       end = document.cookie.indexOf(";", begin);
	       if (end == -1) { end = document.cookie.length; }

	       settings = unescape(document.cookie.substring(begin, end));
	       prefs = settings.split(",");
	       hpp = "&hpp=" + prefs[1];
	       
	       if (prefs[0] == "false") {
		  loadFrame = "../Search.html?loadFrame=tabs&text=";
	       } 
	    } 
	 }
	  
	 var txt = str;
	 txt = txt.replace(/\+/g,"%2B");
	 if (txt != "") {
	    top.setSide(loadFrame+txt+hpp);
	 }
				
	 return false;
}

window.top.contentLocation = String(window.location);


