/// Method to toggle a node in the tree by displaying or hiding relevant elements.
function BackData(item)
{
	var div = document.getElementById("D" + item);
	var visible =(div.style.display!="none");
	var key = document.getElementById("P" + item);
	//var closed = document.getElementById("C" + item);
	//var open = document.getElementById("O" + item);
	
	// Check if the item clicked has any children. If it does not then remove the plus/minus icon
	// and replace it with a transaparent gif.
	var removeIcon = div.hasChildNodes() == false;

	if( key != null )
	{
		if( !removeIcon )
		{
			if (visible)
			{
		 		// Hide open icon and the node contents
		 		div.style.display="none";
		 		//open.style.display="none";
		 		// Show the closed icon
		 		//closed.style.display="block";
		 		//key.innerHTML="<img src='img/plus.gif' width='16' height='16' hspace='0' vspace='0' border='0'>";
		 		key.innerHTML="<img src='img/Cerrado.gif' width='9' height='9' hspace='0' vspace='0' border='0'>";
			}
			else
			{
			    // Show node contents and the icon
		  		div.style.display="block";
		 		//open.style.display="block";
		 		// Hide the closed icon
		 		//closed.style.display="none";
				//key.innerHTML="<img src='img/minus.gif' width='16' height='16' hspace='0' vspace='0' border='0'>";
				key.innerHTML="<img src='img/Abierto.gif' width='9' height='9' hspace='0' vspace='0' border='0'>";
			}
		}
		else
			key.innerHTML="<img src='img/transparent.gif' width='9' height='9' hspace='0' vspace='0' border='0'>";
	}else
	{
	    alert("Ultimo Nivel - sin hijos")
	}
}

///
/// Return a XMLHTTPRequest in a browser independent fashion.
///
function GetXMLHttp()
{
    var xmlhttp=false;
    
    try 
    {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } 
    catch (e) 
    {
        try 
        {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (E)
        {
            xmlhttp = false;
        }
    }

    // Mozilla then?
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
       xmlhttp = new XMLHttpRequest();
    }
    
    return xmlhttp;
}

///
/// Perform an AJAX style request for the contents of a node and put the contents
/// into the empty div.
///
function DemandLoadNode(treeid, item, idtopic)
{
    //si no hay subtemas cambiar la D (DIV) por uno de ultimo nivel
    var div = document.getElementById("D" + item);
    var xmlhttp = GetXMLHttp();
    var textHttp = "";
    // Make sure the node is empty really, and if so fill it
    if (div.innerHTML == "") 
    {
        xmlhttp.open("GET", "TreeFill.aspx?tree="+treeid+"&id="+item+"&Topic="+idtopic, true);
        xmlhttp.onreadystatechange = function()
        {
            if (xmlhttp.readyState == 4) 
            {
                if (xmlhttp.status == 200) 
                {
                    textHttp = xmlhttp.responseText;
                    if (textHttp.indexOf("Error") < 0)
                    {
                        div.innerHTML = textHttp;
                        BackData(item);
                    }
                    else
                    {
                        window.location = "/Default.aspx";
                    }
                }
            }
        }
        xmlhttp.send(null)
    }
    else // The node is already populated
    {
        BackData(item);
    }
}
