function add_qs(elem,va)
{
	if(window.RegExp&&window.encodeURIComponent)
    {
        var ue = elem.href;
		
		if(document.cuba.q)
		{
			qe = encodeURIComponent(document.cuba.q.value);
		}	
		else
		{
			return 0;	
		}
		if(va == 'h') qe = document.cuba.h.value;
		
		if(ue.substr(0,4) != 'http')
		{
			return 0;
		}
		
        if(ue.indexOf(va+"=")!=-1)
        {
            elem.href=ue.replace(new RegExp(va+"=[^&$]*"),va+"="+qe);
        }
        else
        {
            elem.href=ue+"&"+va+"="+qe;
        }
		
    }
    return 1;
}

function refresh_qs(elem,va)
{
	for (i=0;i<document.links.length;i++)
	{
		if(document.links[i])
		{
			add_qs(document.links[i],va);
		}
	}
}

function morepop(divid,imgid,msgid)
{
	actualimg = document.getElementById(imgid);
	actualdiv = document.getElementById(divid);
	actualmsg = document.getElementById(msgid);
	/*
	h1 = document.getElementById('menucontainer').offsetHeight;
	document.getElementById('bodywraper').style.height = '400px'; 
	h = h1 + 400;
	*/
		
	if(actualdiv.style.display == "block" || !actualdiv.style.display)
	{ 
		actualimg.src="fileadmin/img/plus.gif"; 
		actualdiv.style.display = "none";
		actualmsg.innerHTML = "Show Menu";
		//document.getElementById('bodywraper').style.height = h + 'px';
		document.getElementById('is_hidden').value = 1;
		
	}
	else
	{
 		actualimg.src="fileadmin/img/minus.gif"; 
        actualdiv.style.display = "block";
		actualmsg.innerHTML = "Hide Menu";
 		document.getElementById('is_hidden').value = 0;
	}
	refresh_qs('rotativa','h');
}

function trimAll(sString) 
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function preparabusqueda(objeto,forma,lan)
{ 
	var key=objeto.value;
    key2=key.replace(/ /g," ")
 	stringarray = key2.split(" ");
	var w=0 ;
	for (var i=0; i<stringarray.length;i++) 
	{
		if(stringarray[i].length <=2){ key2 = key2.replace(stringarray[i],'');} else { w++; }
	}
	key2 = trimAll(key2);
	objeto.value = key2;
   
  	if(key2.length<=2)
	{ 
		return false; 
	}
 	else
	{
		aviso = Array("Por favor introduzca menos de 5 palabras clave para la b&uacute;squeda","Please introduce less than 5 keywords for the search"); 
		if(w>5)
		{ 
			alert(aviso[lan]); 
			return false;
		}
		else
		{
			forma.submit();
		}
	}
}

/*
 * 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 = "<font style='color:blue;'>";
    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, 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.");
	 	e = 1;
    }
    return false;
  }
  
  var bodyText = document.getElementById('results_content').innerHTML;
  for (var i = 0; i < searchArray.length; i++) {
    bodyText = doHighlight(bodyText, searchArray[i], highlightStartTag, highlightEndTag);
  }
  
  document.getElementById('results_content').innerHTML = bodyText;
  return true;
}
