/*
 * menu.js
 *
 * Version 2.0
 *
 * Date 10/Mayo/2006
 *
 * Copyright INB
 *
 * Created by: MŠ del Mar Rojano.
 *
 * library of function to tree.
 *
 */
if (!document.getElementById)
    document.getElementById = function() { return null; }
/*
 *
 * initializeMenu.
 *
 *
 *
 */     
function initializeMenu(menuId, actuatorId) {
    var menu = document.getElementById(menuId);
    var actuator = document.getElementById(actuatorId);

    if (menu == null || actuator == null) return;

    actuator.parentNode.style.backgroundImage = "url(../images/plus.gif)";
    actuator.onclick = function() {
        var display = menu.style.display;
        this.parentNode.style.backgroundImage =
            (display == "block") ? "url(../images/plus.gif)" : "url(../images/minus.gif)";
        menu.style.display = (display == "block") ? "none" : "block";
        self.location.href='#'+actuator.name;
        return false;
    }
}
/*
 * actionNode.
 *
 *
 *
 */    
function actionNode(menuId, actuatorId,action)
{
    var menu = document.getElementById(menuId);
    var actuator = document.getElementById(actuatorId);

    if (menu == null || actuator == null) return;
    var display = menu.style.display;
    if (action==1)
    {
        actuator.parentNode.style.backgroundImage ="url(../images/minus.gif)";
         menu.style.display ="block";
    }
    else
    {
        actuator.parentNode.style.backgroundImage ="url(../images/plus.gif)";
         menu.style.display ="none";
    }

    return false;


}
/*
 * reload.
 *
 *
 *
 */    
function reload(pag)
{
   self.location.href=pag;

}
/*
 * storeCategories.
 *
 *
 *
 */    
function storeCategories(menuId, actuatorId, nameId, nameDiv,action) 
{
    var menu = document.getElementById(menuId);
    var actuator = document.getElementById(actuatorId);
    var element=document.getElementsByName(nameId);
    var elementDiv=document.getElementById(nameDiv);
    
    if (menu == null || actuator == null) 
    {  
        if (element!=null)
        {
            if (action==1)
            {
                for (var i = 0; i < element.length; i++) 
                {
                    if (element[i].name == nameId) 
                    {
                        element[i].parentNode.style.display ='none';
                        elementDiv.style.display ="none";
                    }
                }
            }
            else
            {
                for (var i = 0; i < element.length; i++) 
                {
                    if (element[i].name == nameId) 
                    {
                       element[i].parentNode.style.display ='block';
                       elementDiv.style.display ="block";
                    }
               }
           }     
        }
    }
    else
    {  
        if (action==1)
	      {
           actuator.parentNode.style.display = "none";
           elementDiv.style.display ="none";
	     }
	     else
	     { 
          actuator.parentNode.style.display = "block";
          elementDiv.style.display ="block";
		      if (actuator.parentNode.style.backgroundImage=='url(../images/minus.gif)')
		      {
              menu.style.display ="block";
          }
	     }
	 }
	 return false;        
}
/*
 * checkInputCategories.
 *
 * function that checks the checkbox of Show all categories and it executes a action. 
 * 
 *  
 */   		
function checkInputCategories()
{
    if (window.document.forms[0].showCategories.checked==false)
    {
        eval(window.document.forms[0].all.value);
    }
    else
    {
        eval(window.document.forms[0].notAll.value);
    }
}
/*
 * keyPushed.
 *
 */  
function keyPushed( e ) {
   
   var navegator;
   if ( window.event != null)				//IE4+
   {
      key = window.event.keyCode;
      navegator='IE';
   }   
   else if ( e != null ) 				//N4+ o W3C compatibles
   {
      key = e.which;
      navegator='Firefox';
   }   
   else
   {
      return;
	}
   if (key == 13) 
   { 					
       
        document.forms[0].go.onclick(); 
        return false; 
      }
   
   }
/*
 * inicializeSearch.
 *
 *
 */   
function inicializeSearch()
{
    window.document.forms[0].newSearch.value='yes';
    window.document.forms[0].search.value='';
    document.onkeydown = keyPushed;			        
    if (document.captureEvents)				            //netscape 
	  document.captureEvents(Event.KEYDOWN)
	

}
/*
    Written by Jonathan Snook, http://www.snook.ca/jonathan
    Add-ons by Robert Nyman, http://www.robertnyman.com
*/

function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}    
/*
 * inicializeElement.
 *
 *
 */   
function inicializeElement(elements,patron)
{
    var i;
    var j;   
    var reg;
    var name;
    var theTextNode;
    
    for (i = 0; i < elements.length; i++) 
    {
        reg = new RegExp(patron, "gi");
        j=elements[i].name.search(reg);                                      //search the regular expresion in the string.
        if (j>=0)                                                            //if j>=0, exists one position where the word begins.
        {
            name=elements[i].name;
            theTextNode = document.createTextNode(name);                     //it creates a new element of text.
            elements[i].replaceChild(theTextNode,elements[i].childNodes[0]); //it changes the node by other node.
        }                    
    }
}
/*
 * clearSearch.
 *
 *
 */   
function clearSearch()
{
    if (window.document.forms[0].newSearch.value!='yes')             //if there isnīt text marked, it is cleaned.
    {
        elements=getElementsByClassName(document, "*", "greyText");
        inicializeElement(elements,window.document.forms[0].newSearch.value);
        window.document.forms[0].newSearch.value='yes';
        window.document.getElementById('result').innerHTML=" ";
    }
}
/*
 * markElements.
 *
 *
 */   
function markElements(elements,patron,className)
{
    var i;
    var l;
    var k;
    var name;
    var existence;     //it informs if result of search is empty or not.
    var textlight;
    var numMarked;
    var substringPre;
    var substringSuc;
    var theText;
    var theTextNode1;
    var theBoldText;
    var theTextNode2;
    var theTextNode3;
    
    numMarked=0;
    for (i = 0; i < elements.length; i++) 
    {
        reg = new RegExp(patron, "gi");
        j=elements[i].name.search(reg);                                  //search the regular expresion in the string.
        if (j>=0)                                                       //if j>=0, exists one position where the word begins.
        {
            l=patron.length;                                             
            k=j+l;
            name=elements[i].name;
            textlight= name.slice (j,k);                                 //it obtains the text that will be marked.
            substringPre=name.slice(0,j);                                //it obtains the text in front of marked.  
            substringSuc=name.slice(k,elements[i].name.length);          //it obtains the text behind of marked.
            theText = document.createElement('SPAN');                    //it creates a new element of type SPAN.
            theTextNode1 = document.createTextNode(substringPre);        //it creates a new element of text. 
            theText.appendChild(theTextNode1);                           //it creates a new child with the text.
            theBoldText = document.createElement('FONT');                //it creates a new element of type FONT.
            theBoldText.className=className;                             //it assigns a class.
            theTextNode2 = document.createTextNode(textlight);           //it creates a new element of text.
            theBoldText.appendChild(theTextNode2);                       //it creates a new child with the text.
            theText.appendChild(theBoldText);                            //it creates a new child with the text.
            theTextNode3 = document.createTextNode(substringSuc);        //it creates a new element of text.
            theText.appendChild(theTextNode3);                           //it creates a new child with the text.
            elements[i].replaceChild(theText,elements[i].childNodes[0]); //it changes the node by other node.
            numMarked=numMarked +1;
        }   
   }        
   return numMarked;              
}
/*
 *searchInTree.
 *
 *
 *
 */    
function searchInTree(patron)
{
    var elements;
    var reg;
    var num;
    var j;
    
    elements=getElementsByClassName(document, "*", "greyText");        
    if (patron!='')
    {     
        if ((window.document.forms[0].newSearch.value!='yes') && (patron!=window.document.forms[0].newSearch.value)) 
        {
                inicializeElement(elements,window.document.forms[0].newSearch.value); //if the patron is different then the tree is inicialized.
        }
        window.document.forms[0].newSearch.value=patron;                              //it saves the patron in variable.
        num=markElements(elements,patron,"realceText");                               //function to mark the patrons.
        if (num==0)                                                                     
        {
           window.document.getElementById('result').innerHTML="    There arenīt elements with this patron."; 
        }
        else
        {
			if (num==1)
			{	
                window.document.getElementById('result').innerHTML="  "+num+" hit found.";
			}
			else
			{
				window.document.getElementById('result').innerHTML="  "+num+" hits found.";
			}	
        }
    }
    else if (window.document.forms[0].newSearch.value!='yes') //if the patron is empty and there isnīt old patron, the tree is inicialized. 
    {
       inicializeElement(elements,window.document.forms[0].newSearch.value);
       window.document.forms[0].newSearch.value='yes';
        window.document.getElementById('result').innerHTML=" ";
    }
    window.document.forms[0].search.select();
}	
