function Tree(id)
{
this.prepare(id);
}
Tree.prototype.prepare = function(id)
{
var container = document.getElementById(id);
var initLength = container.getElementsByTagName("li").length;
for (var i = 0; i < initLength; i++)
document.getElementById(id).getElementsByTagName("li")[i].addEventListener("click", this.clickHandler, false);
}
Tree.prototype.clickHandler = function()
{
if (this.getElementsByTagName("ul").length)
{
this.removeChild(this.getElementsByTagName("ul")[0]);
}
else
{
var clickedId = this.getAttribute("id");
var xhr = new XMLHttpRequest();
var url = "/base/loadTree?id="+clickedId;
xhr.onreadystatechange = obj.processXML;
xhr.open("get", url, true);
xhr.send(null);
}
}
Tree.prototype.processXML = function()
{
if (this.readyState != 4 || this.status != 200) return;
var xml = this.responseXML;
var length = xml.getElementsByTagName("item").length;
var ul = document.createElement("ul");
for (var i = 0; i < length; i++)
{
var li = document.createElement("li");
li.setAttribute("id", xml.getElementsByTagName("id")[i].firstChild.nodeValue);
li.innerHTML = xml.getElementsByTagName('name')[i].firstChild.data;
ul.appendChild(li);
}
document.getElementById(clickedId).appendChild(ul);
}