function getCookie(name) {
        var prefix = name + "="
        var cookieStartIndex = document.cookie.indexOf(prefix)
        if (cookieStartIndex == -1)
                return ""
        var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length)
        if (cookieEndIndex == -1)
                cookieEndIndex = document.cookie.length
        return (unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex)))
}
function setCookie(name, value, expires, path, domain, secure) {
        var curCookie = name + "=" + escape(value) +
                ((expires) ? "; expires=" + expires.toGMTString() : "") +
                ((path) ? "; path=" + path : "") +
                ((domain) ? "; domain=" + domain : "") +
                ((secure) ? "; secure" : "")
                document.cookie = curCookie
}


function handle(delta) {
myspeed=-delta
setTimeout("myspeed=0",20);
}

/** Event handler for mouse wheel event.
 */
function wheel(event){
        var delta = 0;
        if (!event) /* For IE. */
                event = window.event;
        if (event.wheelDelta) { /* IE/Opera. */
                delta = event.wheelDelta;
                /** In Opera 9, delta differs in sign as compared to IE.
                 */
                if (window.opera)
                        delta = delta*5;
        } else if (event.detail) { /** Mozilla case FF. */
                /** In Mozilla, sign of delta is different than in IE.
                 * Also, delta is multiple of 3.
                 */
                delta = -event.detail*50;
        }
        /** If delta is nonzero, handle it.
         * Basically, delta is now positive if wheel was scrolled up,
         * and negative, if wheel was scrolled down.
         */
        if (delta)
                handle(delta);
        /** Prevent default actions caused by mouse wheel.
         * That might be ugly, but we handle scrolls somehow
         * anyway, so don't bother here..
         */
        if (event.preventDefault)
                event.preventDefault();
	event.returnValue = false;
}

/** Initialization code. 
 * If you use your own event management code, change it as required.
 */
if (window.addEventListener)
        /** DOMMouseScroll is for mozilla. */
        window.addEventListener('DOMMouseScroll', wheel, false);
/** IE/Opera. */
window.onmousewheel = document.onmousewheel = wheel;

//function clickIE() {if (document.all) {return false;}}
//function clickNS(e) {if (document.layers||(document.getElementById&&!document.all)) {if (e.which==2||e.which==3) {return false;}}}
//if (document.layers) 
//	{document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
//else {document.onmouseup=clickNS;document.oncontextmenu=clickIE;}
//document.oncontextmenu=new Function("return false")

/*
 * 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, highlightStartTag, highlightEndTag) 
{
  // the highlightStartTag and highlightEndTag parameters are optional
  if ((!highlightStartTag) || (!highlightEndTag)) {
    highlightStartTag = "<b>";
    highlightEndTag = "</b>";
  }
  
  // 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)
  if (searchTerm.substr(searchTerm.length-1,1) == '*') {
	  searchTerm = searchTerm.substr(0,searchTerm.length-1);
  }
  var re = /[a-zа-я]/;
  var newText = "";
  var i = -1;
  var lcSearchTerm = searchTerm.toLowerCase();
  var lcBodyText = bodyText.toLowerCase();
    
  if (lcSearchTerm.substr(0,1) == '!') {
	  lcSearchTerm = lcSearchTerm.substr(1);
	  comm = 1;
  } else {comm = 0;}
  
  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)) {
			if (!re.test(lcBodyText.substr(i-1,1))) {
				newText += bodyText.substring(0, i);
				is = i; i +=lcSearchTerm.length;
					
				if (comm == 1) {
					if (re.test(lcBodyText.substr(i,1))) {
						newText += bodyText.substring(is,i);
					} else {
						newText += highlightStartTag + bodyText.substring(is,i) + highlightEndTag;
					}
				} else {
				  while (re.test(lcBodyText.substr(i++,1))) {} i-- ;
				  	if ((i - is -lcSearchTerm.length) < 10) {
						newText += highlightStartTag + bodyText.substring(is,i) + highlightEndTag;
					} else {newText += bodyText.substring(is,i);}
				}
				bodyText = bodyText.substr(i);
				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, 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 (searchText == '' || searchText == '!') {return;}
  if (searchText.indexOf('"') < 0) {
    searchArray = searchText.split(" ");
  } 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++) {
	  highlightStartTag='<span id="term'+i+'" name="term'+i+'">'
	  highlightEndTag='</span>'
  	if (searchArray[i].length > 2) bodyText = doHighlight(bodyText, searchArray[i], highlightStartTag, highlightEndTag);
  }
  i = bodyText.indexOf('<span id="term')+10;
  if (document.div_name) {document.div_name[1]=bodyText.substr(i,5);}
  
  document.body.innerHTML = bodyText;
  return true;
}
