Convert = function()
{
}

Convert.prototype.toElement = function(node)
{
	if (node == null)
	{
		return null;
	}
	
	var element = null;
	// element
    if(node.nodeType == 1)
    {
        element = document.createElement(node.tagName);
        var attribute;
        for (var i = 0; i < node.attributes.length; i++)
        {
            attribute = node.attributes[i];
            if (attribute.nodeName == "class")
            {
                element.className = attribute.nodeValue;
                continue;
            }
            if (attribute.nodeName == "onclick")
            {
                element.onclick = new Function ("evt", attribute.nodeValue);
                continue;
            }
            element.setAttribute(attribute.nodeName, attribute.nodeValue);
        }

        var childNode;
        for (var i = 0; i < node.childNodes.length; i++)
        {
            childNode = node.childNodes[i];

            // element
            if (childNode.nodeType == 1)
            {
                element.appendChild(this.toElement(childNode));
            }
            // textnode
            if (childNode.nodeType == 3)
            {
				// todo: check for &#160;
                element.appendChild(document.createTextNode(childNode.nodeValue));
            }
            // cdata node
            if (childNode.nodeType == 4)
            {
                // TODO: &nbsp; disapears!
                //element.appendChild(document.createTextNode(childNode.nodeValue));
                element.innerHTML = childNode.nodeValue;
            }
        }
        return element
    }
    // textnode
    if (node.nodeType == 3)
    {
		element = document.createTextNode(node.nodeValue);
		return element;
    }
    
    return element;
}

var convert = new Convert();