
var iconMap = new Array();
var iconList = new Array( iconMap );

function Toggle(item)
{
	var idx = -1;
	for( i = 0; i < iconList.length; i++ )
	{
		if( iconList[i][0] == item )
		{
			idx = i;
			break;
		}
	}
	
	if( idx < 0 )
		alert( "Could not find key in Icon List." );
		   
	var div=document.getElementById("D"+item);
	var visible=(div.style.display!="none");
	var key=document.getElementById("P"+item);
	
	
	var removeIcon = div.hasChildNodes() == false;
	
	if( key != null )
	{
		if( !removeIcon )
		{
			if (visible)
			{
		 		div.style.display="none";
		 		key.innerHTML="<img src='images/plus.gif' width='16' height='16' hspace='0' vspace='0' border='0'>";
			}
			else
			{
		  		div.style.display="block";
				key.innerHTML="<img src='images/minus.gif' width='16' height='16' hspace='0' vspace='0' border='0'>";
			}
		}
		else
			key.innerHTML="<img src='images/transparent.gif' width='16' height='16' hspace='0' vspace='0' border='0'>";
	}

	key=document.getElementById("I"+item);
	if( key != null )
	{
		if (visible)
		{
	 		div.style.display="none";
	 		key.innerHTML="<img src='"+iconList[idx][1]+"' width='16' height='16' hspace='0' vspace='0' border='0'>";
		}
		else
		{
	  		div.style.display="block";
			key.innerHTML="<img src='"+iconList[idx][2]+"' width='16' height='16' hspace='0' vspace='0' border='0'>";
		}
	}	
}

function AddImage( parent, imgFileName )
{
	img=document.createElement("IMG");
	img.setAttribute( "src", imgFileName );
	img.setAttribute( "width", 16 );
	img.setAttribute( "height", 16 );
	img.setAttribute( "hspace", 0 );
	img.setAttribute( "vspace", 0 );
	img.setAttribute( "border", 0 );
	parent.appendChild(img);
}

function CreateUniqueTagName( seed )
{
	var tagName = seed;
	var attempt = 0;
	
	if( tagName == "" || tagName == null )
		tagName = "x";

	while( document.getElementById(tagName) != null )
	{
		tagName = "x" + tagName;
		if( attempt++ > 50 )
		{
			alert( "Cannot create unique tag name. Giving up. \nTag = " + tagName );
			break;
		}
	}
	
	return tagName;
}

function CreateTreeItem( parent, img1FileName, img2FileName, nodeName, url, target, haschildren, hasuploadicon, objectid)
{
	var uniqueId = CreateUniqueTagName( nodeName );
	for( i=0; i < iconList.length; i++ )
		if( iconList[i][0] == uniqueId )
		{
			alert( "Non unique ID in Element Map. '" + uniqueId + "'" );
			// return;
		}
	iconList[iconList.length] = new Array( uniqueId, img1FileName, img2FileName );

	table = document.createElement("TABLE");
	if( parent != null )
		parent.appendChild( table );

	table.setAttribute( "border", 0 );
	table.setAttribute( "cellpadding", 1 );
	table.setAttribute( "cellspacing", 1 );
		
	tablebody = document.createElement("TBODY");
	table.appendChild(tablebody);
		
   	row=document.createElement("TR");
	tablebody.appendChild( row );

  if(haschildren)
  {	
  	cell=document.createElement("TD");
  	cell.setAttribute( "width", 16 );
  	row.appendChild(cell);
  	
  	a=document.createElement("A");
  	cell.appendChild( a );
  	a.setAttribute( "id", "P"+uniqueId );
  	a.setAttribute( "href", "javascript:Toggle(\""+uniqueId+"\");" );
  	AddImage( a, "images/plus.gif" );
	}
	
	
	cell=document.createElement("TD");
	cell.setAttribute( "width", 16 );
	row.appendChild(cell);
		
	a=document.createElement("A");
	a.setAttribute( "id", "I"+uniqueId );
	if(haschildren)
    a.setAttribute( "href", "javascript:Toggle(\""+uniqueId+"\");" );
  else
    a.setAttribute( "href", url );
	cell.appendChild(a);	

	AddImage( a, img1FileName );

	cell=document.createElement("TD");
	cell.noWrap = true;
	a=document.createElement("A");
	a.setAttribute( "id", uniqueId );
	cell.appendChild( a );
	if( url != null )
	{
    if(haschildren)
      a.setAttribute( "href", "javascript:Toggle(\""+uniqueId+"\");" );
    else
		  a.setAttribute( "href", url );
		
		text=document.createTextNode( nodeName );
		a.appendChild(text);
	}
	else
	{
		text=document.createTextNode( nodeName );
		cell.appendChild(text);
	}
	
	if(hasuploadicon) {
		refAdd = document.createElement("a");
		refAdd.setAttribute("href", "javascript:xmlhttpPostFile(" + objectid + ");");
		img=document.createElement("img");
		img.setAttribute( "src", "images/add.png" );
		img.setAttribute( "border", "0" );
		refAdd.appendChild(img);
		cell.appendChild(refAdd);
	}
	row.appendChild(cell);

	return CreateDiv( parent, uniqueId );;
}

function CreateDiv( parent, id )
{
	div=document.createElement("DIV");
	if( parent != null )
		parent.appendChild( div );
		
	div.setAttribute( "id", "D"+id );
	div.style.display  = "none";
 	div.style.marginLeft = "2em";
	
	return div;
}

var rootCell = null;

function InitialiseTree()
{
	
	body = document.getElementById("TreeTable");
	
	table = document.createElement("TABLE");
	body.appendChild( table );

	table.setAttribute( "border", 1 );
	table.setAttribute( "cellpadding", 1 );
	table.setAttribute( "cellspacing", 1 );
	table.setAttribute("style", "background-image: url(images/bg_content2.png); background-repeat: repeat-y;");
		
	tablebody = document.createElement("TBODY");
	table.appendChild(tablebody);
		
	row=document.createElement("TR");
	tablebody.appendChild(row);
		
	cell=document.createElement("TD");
	row.appendChild(cell); 	
	
	rootCell = cell;
}

function TestIcons() {
  var idx = -1;
  var item = false;
  for( i = 0; i < iconList.length; i++ )
  {
    if (iconList[i].length != 0) {
      item = new String(iconList[i][0]);
    }
    
    if (item) {
      idx = i;
    
      if( idx < 0 )
        alert( "Could not find key in Icon List." );
    
      var div=document.getElementById("D"+item);
      var key=document.getElementById("P"+item);
    
      var removeIcon = false;
      if (div.hasChildNodes() == null || div.hasChildNodes() == false) {
        var removeIcon = true;
      }
    
      if( key != null )
      {
        if( removeIcon )
        key.innerHTML="<img src='images/transparent.gif' width='16' height='16' hspace='0' vspace='0' border='0'>";
      }
    }
  }
}
