function exptreeInit(theId, samtliga, expandIfOnlyOne) {
  exptreeInitExpand(theId, samtliga, expandIfOnlyOne, true);
}

function exptreeInitExpand(theId, samtliga, expandIfOnlyOne, expandIfSelected) {
  var x, y, i, j, boolExpanded, boolIsTopNode, topNodeChildrenCount;
  x = getObject(theId).getElementsByTagName('div');
  if (expandIfOnlyOne!=false)
  	expandIfOnlyOne = true;
  // Skapa räknare för toppnoder
  var topNodeChildrenCount = new Object();
  for (i=0;i<x.length;i++) 
  {
    boolExpanded=false;
    x[i].id.indexOf('_content')<0 ? boolIsTopNode=true : boolIsTopNode=false
    if (boolIsTopNode==true) 
    {
    	// Hämta toppnod
    	var nodPrefix = x[i].id.replace(/\d+\D*/g,"")
    	// Ignorera toppnod "utomlands"
    	if (nodPrefix!='nodTrue') 
    	{
    		// Starta toppnodens barnräknare
    		if (!topNodeChildrenCount[nodPrefix]) 
    		{
    			topNodeChildrenCount[nodPrefix] = new Object();
    			topNodeChildrenCount[nodPrefix]._count = 1;
    		} 
    		// Öka toppnodens barnräknare med 1
    		else 
    		{
    			topNodeChildrenCount[nodPrefix]._count++;
    		}
    		// Lagra nodens id
   			topNodeChildrenCount[nodPrefix]._name = x[i].id;
	    }
      x[i].onclick = function() { exptreeToggle(this.id); }
      x[i].onmouseover = function() { exptreeOver(this.id); }
      x[i].onmouseout = function() { exptreeOut(this.id); }
 		} 
 		else if (boolExpanded == false)
 		{
      y = x[i].getElementsByTagName('input');
      // Loopa igenom checkboxar
      for (j=0;j<y.length;j++) 
      {
      	if(y[j].id.indexOf('_samtliga')>0) 
      	{
		      y[j].onclick = function() { exptreeSamtliga(this.id); }
      	}
        if(y[j].checked==true) 
        {
          boolExpanded=true;
					if (expandIfSelected == true)
          	exptreeExpand(getObject(x[i].id.substring(0,x[i].id.lastIndexOf('_content'))).id);
          break;
        }
      }
    }
  }
  
  if (expandIfOnlyOne == true)
  {
		for (var i in topNodeChildrenCount) 
		{
			if (topNodeChildrenCount[i]._count == 1) 
			{
		    exptreeExpand(topNodeChildrenCount[i]._name);
		  }
		}
	}
//	if (topNodeChildrenCount == 1) { 
//	  x = getObject(theId).getElementsByTagName('div');
//    exptreeExpand(x[0].id);
//  }
}

function exptreeToggle(theId) 
{
	var theContent, theDiv;
	theDiv = getObject(theId);
	theContent = getObject(theId+'_content');
	if (theContent.style.display == 'block') 
	{
    theDiv.className = 'nod collapsed';
		theContent.style.display = 'none';
	} 
	else 
	{
    theDiv.className = 'nod expanded';
		theContent.style.display = 'block';
	}
}

function exptreeExpand(theId) 
{
	var theContent, theDiv;
	theDiv = getObject(theId);
	theContent = getObject(theId+'_content');
	if (theDiv)
	  theDiv.className = 'nod expanded';
	if (theContent)
		theContent.style.display = 'block';
}

function exptreeOver(theId) 
{
	var theDiv, theClass;
	theDiv = getObject(theId);
  theClass = theDiv.className;
  if (theClass == 'nod collapsed') 
  {
    theDiv.className = 'nod collapsed hover';
  } 
  else if (theClass == 'nod expanded') 
  {
    theDiv.className = 'nod expanded hover';
  }
}

function exptreeOut(theId) 
{
	var theDiv, theClass;
	theDiv = getObject(theId);
  theClass = theDiv.className;
  if (theClass == 'nod collapsed hover') 
  {
    theDiv.className = 'nod collapsed';
  } 
  else if (theClass == 'nod expanded hover') 
  {
    theDiv.className = 'nod expanded';
  }
}

function exptreeSamtliga(theId) 
{
  var x, i, isChecked;
  isChecked = getObject(theId).checked;
  x = getObject(theId.substring(0,theId.lastIndexOf('_samtliga'))+'_content').getElementsByTagName('input');
  for (i=0;i<x.length;i++) 
  {
  	if(x[i].id!=theId) 
  	{
      x[i].checked = isChecked;
    }
  }
}

function getObject(theId) 
{
	if (document.getElementById)
	{
		return document.getElementById(theId);
	}
	else if (document.all)
	{
    return document.all[theId];
  }
}
