function collapse(element,use_id,type)
{
  if(!use_id || use_id=='')	id='';
  else			id=element.getAttribute('page_id');
  if(!type || type=='')	type='normal';

  if(!element || typeof(element.nodeType)=='undefined')	//we have an event being passed
  {
    e = element;				//get element from event (e)
    if (!e) var e = window.event;
    if (e.target) element = e.target;
    else if (e.srcElement) element = e.srcElement;
  }

  if(document.getElementById("tooltip").style.display=='block')
  {
    document.getElementById("tooltip").style.display='none';
  }

  var div = element.parentNode.parentNode;
  var classes = div.className.split(" ");
  var state = classes[0];

  if(typeof(classes[1])=='undefined')
  {
    loaded = false;
  }
  else
  {
    loaded = true;
  }

  var plus;
  var minus;

  if(type=='normal')
  {
    plus='./images/plus.png';
    minus='./images/minus.png';
  }
  else
  {
    plus='./images/triangle_right.png';
    minus='./images/triangle_down.png';
  }

  div_children = getRealChildren(div);
  title_div_children = getRealChildren(div_children[0]);
  if(state=='collapsible')
  {
    //div_children[1].innerHTML = "";

    div_children[1].style.display = 'none';
    title_div_children[0].setAttribute('src',plus);
    div.className = 'collapsed '+loaded;
  }
  else
  {
    anchor = title_div_children[1].getAttribute('id');
    div_children[1].style.display = 'block';
    body_children = getRealChildren(div_children[1]);
    if(!loaded)
    {
      loadXMLDoc('./utility/get_page_text.php?id='+id+'&anchor='+anchor,body_children[0],body_children[1],'POST');
    }
    title_div_children[0].setAttribute('src',minus);
    div.className = 'collapsible loaded';
  }
}

function getRealChildren(element)
{
  var children = new Array();
  var count = 0;

//commented section doesn't work in Safari
/*  for(var child in element.childNodes)
  {
    if(typeof(element.childNodes[child].tagName)!='undefined')
    {
      children[count] = element.childNodes[child];
      count++;
    }
  }*/

  var clength = element.childNodes.length;

  for(var i=0;i<clength;i++)
  {
    if(typeof(element.childNodes[i].tagName)!='undefined')
    {
      children[count] = element.childNodes[i];
      count++;
    }
  }      

  return children;
}
