var xmlHttpS;
var myResultS;
var myPartTextS;

function searchText(keyword, resultdiv, parttext) {

    if (keyword == '') {
	document.getElementById (resultdiv).style.display = "none";
	return;
    }
    myPartTextS = parttext;
    
    xmlHttpS = GetxmlHttpSObject ()
    if (xmlHttpS == null) {
	alert ("Browser does not support HTTP Request");
	return;
    }
    myResultS = resultdiv;
    var url = "/ape/contents/search.php";
    url = url + "?keyword=" + keyword;
    url = url + "&sid=" + Math.random ();
    xmlHttpS.onreadystatechange = stateChangedS;
    xmlHttpS.open ("GET", url, true);
    xmlHttpS.send (null)
}

function stateChangedS (){
    if (xmlHttpS.readyState == 4 || xmlHttpS.readyState == "complete") {
	document.getElementById (myResultS).innerHTML = xmlHttpS.responseText;
	document.getElementById (myResultS).style.display = "block";
    }
}

function setKeyword(keyword) {
    document.getElementById("searchText").value = keyword;
    document.getElementById("searchResult").style.display = "none";
    if (keyword != '') {
	location.href='/'+myPartTextS+'/kereses/?kereses='+keyword;
    }
}

function GetxmlHttpSObject (){
    var xmlHttp = null;
    
    try { xmlHttp = new XMLHttpRequest (); }
    catch (e) {
	try {
	    xmlHttp = new ActiveXObject ("Msxml2.xmlHttp");
	}
	catch (e) {
	    xmlHttp = new ActiveXObject ("Microsoft.xmlHttp");
	}
    }

    return xmlHttp;
}

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,
  // 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) {
    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;
}


/*
 * 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(searchText, treatAsPhrase, highlightStartTag, highlightEndTag)
{
  if (treatAsPhrase) {
    searchArray = [searchText];
  } else {
    searchArray = searchText.split(" ");
  }
  
  if (!document.body || typeof(document.body.innerHTML) == "undefined") {
    return false;
  }
  
  var bodyText = document.getElementById("content-text").innerHTML;
  for (var i = 0; i < searchArray.length; i++) {
    bodyText = doHighlight(bodyText, searchArray[i], highlightStartTag, highlightEndTag);
  }
  
  document.getElementById('content-text').innerHTML = bodyText;
  return true;
}

function is_child_of(parent, child) {
    if( child != null ) {			
	while( child.parentNode ) {
	    if( (child = child.parentNode) == parent ) {
		return true;
	    }
	}
    }
    
    return false;

}

function fixOnMouseOut(element, event, JavaScript_code) {
    var current_mouse_target = null;
	if( event.toElement ) {				
	    current_mouse_target = event.toElement;
	} else if( event.relatedTarget ) {				
	    current_mouse_target = event.relatedTarget;
	}
	
	if( !is_child_of(element, current_mouse_target) && element != current_mouse_target ) {
	    eval(JavaScript_code);
	}
}

