/*

ShowHideMenu 1.0
----------------
      
Attributi obbligatori:
  id = E' necessario che l'elemento abbia un ID
        
Attributi opzionali:
  class, classo, classc = Gli stili impostati a seconda dello stato
  grp = Indica il gruppo di appartenenza 
  grpchl = Indica il gruppo del livello inferiore suoi figli 
  radiosel = Se presente gli elementi si compotano come radio 
  switchsel
  A) In un elemento singolo permette al "bottone" di rimanere cliccato
  B) In un elemento "radio" permette la deselezione del elemento 
          
Metodi "pubblici":
  explodelement(id_el) = apre un livello del ramo del elemento indicato
  collapseall(id_el) = chiude tutti i rami dei livelli inferiori del elemento indicato
      
ToDo:
  ...

Done:
  Pandemia: Permettere l'utilizzo fra diversi tag come div, span

Testato:
  Con elemento div su IE e Firefox  
			
*/
      
lasteldown = "";
      
function initel(el)
{
  if(el.getAttribute('checked') == null) 
  {
	  el.setAttribute('checked', 'false');
		if(el.getAttribute('classn') == null) el.setAttribute('classn', el.className);
	  if(el.getAttribute('classo') == null) el.setAttribute('classo', el.className);
		if(el.getAttribute('classc') == null) el.setAttribute('classc', el.getAttribute('classo'));
	}
}
			
function mergearray(arr1, arr2)
{
  all_array = new Array;
  var val;
	for(var m=0;m<arr1.length;m++) 
	{ 
	  if(typeof(arr1[m]) != "object") val = arr1[m];
    else val = arr1[m].id;
    if(val != "") all_array.push(val);
 	}
	for(var m=0;m<arr2.length;m++) 
	{
	  if(typeof(arr2[m]) != "object") val = arr2[m];
    else val = arr2[m].id;
    if(val != "") all_array.push(val);
	}
  return all_array;
}
			
function getarrayelements(tag)
{ 
  all_array = new Array;
  div_array = document.getElementsByTagName("DIV");
  span_array = document.getElementsByTagName("SPAN");
  td_array = document.getElementsByTagName("TD");
  table_array = document.getElementsByTagName("TABLE");
	all_array = mergearray(all_array, div_array);
	all_array = mergearray(all_array, span_array)
	all_array = mergearray(all_array, td_array);
	all_array = mergearray(all_array, table_array);
			
	tag = tag.toUpperCase();
  if(tag != undefined)
	{
		if( (tag != "TD") && (tag != "TABLE") && (tag != "DIV") && (tag != "SPAN") ) 
		{
		  marray = document.getElementsByTagName(tag);
			all_array = mergearray(all_array, marray);
		}
	}
	return all_array;
}
      
function do_highlight(el) 
{ 
  initel(el);
  el.downst = false;        
  if( typeof(lasteldown.id) != "undefined" ) if(el.id == lasteldown.id) 
  {
    el.downst = true;
  }
  if(el.c_lock == "true") 
  {
    //alert("evento over inutile e dannoso bloccato");
    return;
  } 
  el.c_lock = "true";
	if(el.getAttribute('checked') == "false") 
  {
    //if(!el.downst) 
    {
      el.className = el.getAttribute('classo');
    }
  }
  el.downst = false;
  el.c_lock = "false";
}
			 
			function do_selection(el) 
			{
        if(typeof(el) == "string") el = document.getElementById(el);
        initel(el);
        if(el.c_lock == "true") 
        {
          //alert("evento click inutile e dannoso bloccato");
          return;
        } 
        el.c_lock = "true"; 
        //alert("el.id="+el.id +"\nel.grp tipo=" + typeof(el.getAttribute('grp')) + " value="+ el.getAttribute('grp')+"\nel.switchsel tipo=" + typeof(el.getAttribute('switchsel')) + " value="+ el.getAttribute('switchsel'));
  	    if(el.getAttribute('checked') == "true") 
				{  
          //radioel = (el.getAttribute('radiosel') != null);
          radioel = (el.getAttribute('grp') != null) && (el.getAttribute('radiosel') != null);
          switchel = (el.getAttribute('switchsel') != null);
          //Se non fa parte di un gruppo  o  se ne fa parte ma è interruttore
          if(!radioel || (radioel && switchel))  
          {
				    el.setAttribute('checked', 'false');
					  el.className = el.getAttribute('classo');
					  if(el.getAttribute('grpchl') != null)
						{
						  collapseallgrpchl(el.tagName, el.getAttribute('grpchl'));
						}
          }
        }
			  else if(el.getAttribute('checked') == "false") //if(el.checked == "false")
				{
					//Cerca e deseleziona tutti gli elementi del gruppo
          elems = getarrayelements(el.tagName);
          for (var i=0;i<elems.length;i++)
          {            
					  foritem = document.getElementById(elems[i]);
            if((el.getAttribute('grp') != null) && (el.getAttribute('radiosel') != null)) //Se fa parte di un gruppo
            {
              if(foritem.getAttribute('grp') == el.getAttribute('grp')) 
              {
                if(foritem.id == el.id)
                {
                  //Seleziona el definito
                  foritem.setAttribute('checked', 'true');
				  	      foritem.className = foritem.getAttribute('classc');
									if(foritem.getAttribute('grpchl') != null)
									{
									  explodefirstlevchl(el.tagName, foritem.getAttribute('grpchl'));
									}									
                }
                else
                {
								  initel(foritem);
                  foritem.setAttribute('checked', 'false');
                  foritem.className = foritem.getAttribute('classn');
									if(foritem.getAttribute('grpchl') != null)
									{
									  collapseallgrpchl(el.tagName, foritem.getAttribute('grpchl'));
									}
                }
              }
            }
            else //Se non fa parte di un gruppo
            {
              if(el.getAttribute('switchsel') == null)
              {
                el.setAttribute('checked', 'false');//[Chiude singolo elemento]
				  	    el.className = el.getAttribute('classo');
				    	  if(el.getAttribute('grpchl') != null)
			    			{
				    		  collapseallgrpchl(el.tagName, el.getAttribute('grpchl'));
			    			}
              }
              else
              {
                el.setAttribute('checked', 'true');//[Apre singolo elemento]
				  	    el.className = el.getAttribute('classc');
							  if(el.getAttribute('grpchl') != null)
								{
									explodefirstlevchl(el.tagName, el.getAttribute('grpchl'));
								}									
              }
            }            
          }//end for				  
				}
        el.c_lock = "false";
			}
			 
			function do_restore(el) 
			{   
        initel(el);
        if(el.c_lock == "true") 
        {
          //alert("evento out inutile e dannoso bloccato");
          return;
        } 
        el.c_lock = "true";
			  if(el.getAttribute('checked') == "false") 
        {
          el.className = el.getAttribute('classn');
        }
			  else if(el.getAttribute('checked') == "true") 
        {
          el.className = el.getAttribute('classc');
        }        
        el.c_lock = "false";
			}
      
      function do_down(el)
      {
        initel(el);
        if(el.c_lock == "true") 
        {
          //alert("evento down inutile e dannoso bloccato");
          return;
        } 
        el.c_lock = "true";
        if(el.getAttribute('checked') == "false") 
        {
          el.className = el.getAttribute('classc');
          lasteldown = el;
        }
        el.c_lock = "false";
      }
			
			function explodefirstlevchl(tagel, grp)
			{
				var elems2 = getarrayelements(tagel);
        for (var j=0;j<elems2.length;j++)
				{
				  foritem = document.getElementById(elems2[j]);
          if(foritem.getAttribute('grp') != null) //Se fa parte di un gruppo
          {
            if(foritem.getAttribute('grp') == grp) 
					  { 
						  initel(foritem);
              foritem.setAttribute('checked', 'false');
              foritem.className = foritem.getAttribute('classn');
              foritem.style.position = "relative";
              foritem.style.top = 0;
              foritem.style.left = 0;
					  }
					}	
				}//end for
			}
			
			//Collassa i figli
			function collapseallgrpchl(tagel, grp)
			{
        var elems2 = getarrayelements(tagel);
        for (var j=0;j<elems2.length;j++)
				{
					foritem = document.getElementById(elems2[j]);
					
          if(foritem.getAttribute('grp') != null) //Se fa parte di un gruppo
          {
            if(foritem.getAttribute('grp') == grp) 
					  { 
              foritem.style.position = "absolute";
              foritem.style.top = -1000;
              foritem.style.left = -1000;
							if(foritem.getAttribute('grpchl') != null)
							{
							  collapseallgrpchl(tagel, foritem.getAttribute('grpchl'));
							}							
					  }
					}	
				}//end for
			}			
			
			//Uccidi tutti gli elementi figli e figli dei fratelli
			function collapseall(firstel)
			{
        if(typeof(firstel) == "string") firstel = document.getElementById(firstel);
        initel(firstel);
        
        if(firstel.getAttribute('grp') != null) //Se fa parte di un gruppo
        {
          var elems2 = getarrayelements(firstel.tagName);
          for (var j=0;j<elems2.length;j++)
				  {
						foritem = document.getElementById(elems2[j]);
 
            if(foritem.getAttribute('grp') == firstel.getAttribute('grp')) 
					  { 
							if(foritem.getAttribute('grpchl') != null)
							{
							  collapseallgrpchl(firstel.tagName, foritem.getAttribute('grpchl'));
							}							
					  }
					}//end for	
				}
        else
        {
					if(firstel.getAttribute('grpchl') != null)
					{
						collapseallgrpchl(firstel.tagName, firstel.getAttribute('grpchl'));
					}
        }
			}
      
function explodelement(el)
{
  collapseall(el);
  do_selection(el);
}
