var minSWlen=3;
var linkstyle="cursor:pointer;color:#cc0000;padding:3px;text-decoration:underline";
//var searchWordsArea=document.getElementById('searchArea');
//var searchWordsArea=document.body;
var foundWords=0;
var from=false
var nofound='';
var relwords;
function setGuiBoxStatus(txt){
	gbs=document.getElementById('guiBoxStatus');
	if(gbs){
		gbs.value = txt?txt:'';
		gbs.style.display = txt?'block':'none';
	}
}

function trim(inputString){
   if(typeof inputString != "string"){return inputString;}
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while(ch == " "){
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while(ch == " "){
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while(retValue.indexOf("  ") != -1){ 
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length);
   }
   return retValue;
}

function setAction(w,u){
	
	if(u=='' && !from){
		url="document.location.href='"+siteurl+"/"+lang+"search?st=any&lang="+clang+"&swd="+encodeURIComponent(w)+"'";
	}else if(u=='' && from){
		url="document.location.href='"+searchurl+encodeURIComponent(w)+"'";
	}else{
		url="window.open('"+u+"')";//encodeURIComponent(w)+"')";
	}
	return url;
}

function doHighlight(bodyText, sterm, init){
//action=(!init || relwords)?'onclick="'+setAction(searchTerm)+'" style="'+linkstyle+'"':'';
//hlclass=(!init || relwords)?((relwords && !from)?'rellinkHL':'wordHL'):((relwords)?'rellinkHL':((!from)?'linkHL':'wordHL'));
relwords=(relwords!=null && relwords!='')?relwords:'';
if(relwords){
	sterm_arr=sterm.split(',');
	searchTerm=sterm_arr[0];
	slink=(sterm_arr[1] ?sterm_arr[1] :'');
	//alert(searchTerm)

}else{
	searchTerm=sterm;
	slink='';
}

if(!init || relwords){
	action='onclick="'+setAction(searchTerm,slink)+'" style="'+linkstyle+'"';
	hlclass=(relwords && !from)?'rellinkHL':'wordHL';
}else{
	action='';
	hlclass=((relwords && !from)?'rellinkHL':((from)?'linkHL':'wordHL'))
}

var hlStart='<span '+((relwords && !from)?'rsword':'isword')+'="true" class="'+hlclass+'" '+action+'>';
var hlEnd="</span>";
var newText = "";
var i = -1;
var lcSearchTerm = searchTerm.toLowerCase();
var lcBodyText = bodyText.toLowerCase();
f=0;
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)){
          newText += bodyText.substring(0, i) + hlStart + bodyText.substr(i, searchTerm.length) + hlEnd;
          bodyText = bodyText.substr(i + searchTerm.length);
          lcBodyText = bodyText.toLowerCase();
          i = -1;
		  f++;
        }
      }
    }
  }  
if(!init && !f){
	nofound += " - "+searchTerm+"\n";
}
foundWords=foundWords+f
	return newText;
}

function setHighlight(mode){
hlclass=mode?'wordHL':'linkHL';
allspans = searchWordsArea.getElementsByTagName('SPAN');
num=allspans.length;
var found=Array;
x=0;
	for(i=0;i<num;i++){
		s=allspans[i].getAttribute('isword');
		if(s!=null && s=='true'){
			found[x]=allspans[i];
			x++;
		}
	}		
	for(i=0;i<x;i++){
		if(mode){
			found[i].setAttribute('className','linkHL');
			found[i].setAttribute('onClick',setAction(found[i].innerText));
		}else{
			found[i].clearAttributes();  // doesn't work in firefox
			found[i].setAttribute('isword','true');
		}
	}
}

function unsetHighlight(){
allspans = searchWordsArea.getElementsByTagName('SPAN');
num=allspans.length;
var found=Array;
x=0;
	for(i=0;i<num;i++){
		s=allspans[i].getAttribute('isword');
		if(s!=null && s=='true'){
			found[x]=allspans[i];
			x++;
		}
	}		
	for(i=0;i<x;i++){
		found[i].clearAttributes();  // doesn't work in firefox
	}
}

function hlSwords(sxt,init,sphrase,opt){
sbtn=document.getElementById('searchHLbtn');
unsbtn=document.getElementById('unsetHLbtn');
if(opt){
	if(sbtn)sbtn.style.visibility='visible';
	if(unsbtn)unsbtn.style.visibility='visible';
	from=true;
}else{
	if(sbtn)sbtn.style.visibility='hidden';
	from=false;
}

if(sxt!=''){
	unsetHighlight();
	searchText=trim(sxt);
}else{
	if((swords=='' && relwords=='') || (swords==null && relwords==null)){
		if(!init){alert('Please, enter term!');}
		return;
	}else{
		searchText=(swords)?swords:relwords;
	}	
}
if(searchText.length<minSWlen){
	alert('Search term should be at least '+minSWlen+' letters!');
	return;
}
if(sphrase){
	searchArray = [searchText];
}else{
	if(relwords){
		searchArray = searchText.split(";");
	}else{
		searchArray = searchText.split(" ");
	}
}  
if(!searchWordsArea || typeof(searchWordsArea.innerHTML) == "undefined") {
	alert("Sorry, for some reason the text of this page is unavailable. Hightlight Search Terms will not work.");
return false;
}  
var bodyText = searchWordsArea.innerHTML;
for(var i = 0; i < searchArray.length; i++) {
    bodyText = doHighlight(bodyText,searchArray[i],init);
} 

	if(bodyText==''){
		return false;
	}else{
		searchWordsArea.innerHTML = bodyText;
		if(!init){
			if(foundWords==0){
				document.getElementById('searchHLbtn').style.visibility='hidden';
				document.getElementById('unsetHLbtn').style.visibility='hidden';				
			}
			if(nofound!=''){alert('Your terms\n'+nofound+' No found in this document!!!');}
			setGuiBoxStatus('Words Found: '+foundWords);
		}
		nofound='';
		foundWords=0;
		return true;
	}
}