// graft() function
// Originally by Sean M. Burke from interglacial.com
// Closure support added by Maciek Adwent

function moveEntity(tgt_id, tgt_parent)
{
	var element   = document.getElementById(tgt_id);   
	var oldParent = element.parentNode;   
	var newParent = document.getElementById(tgt_parent);   
	
	// Move to new parent.   
	newParent.appendChild(element);    
}

// Remove an entity
function removeEntity(tgt_id)
{
	if	(document.getElementById(tgt_id))
	{
		// Locate entry row
		var entitytodelete = document.getElementById(tgt_id); 
		// End of the above
		
		// Delete entry row
		entitytodelete.parentNode.removeChild(entitytodelete); 
		// End of the above
	}
}
// End of the above

// Remove the CONTENTS (children) of a given entity
function removeContents(tgt_id)
{ 
	if	(document.getElementById(tgt_id))
	{
		var childrenInc = document.getElementById(tgt_id).childNodes.length;
		var ParentObj	= document.getElementById(tgt_id);
		
		for (i=0;i<=(childrenInc-1);i++)
		{ ParentObj.firstChild.parentNode.removeChild(ParentObj.firstChild); }
	}
}
// End of the above

function graft (parent, t, doc) {

    // Usage: graft( somenode, [ "I like ", ['em',
    //               { 'class':"stuff" },"stuff"], " oboy!"] )

    doc = (doc || parent.ownerDocument || document);
    var e;

    if		(t == undefined) 
	{ throw complaining( "Can't graft an undefined value"); } 
	else if	(t.constructor == String) 
	{ e = doc.createTextNode(t); } 
	else if	(t.length == 0) 
	{
        e = doc.createElement( "span" );
        e.setAttribute( "class", "fromEmptyLOL" );
    } 
	else 
	{
        for(var i = 0; i < t.length; i++) 
		{	
            if	( i == 0 && t[i].constructor == String ) 
			{
                var snared;
				
                snared = t[i].match(/^([a-z][a-z0-9]*)\.([^\s\.]+)$/i);
                
				if( snared ) 
				{ e = doc.createElement(snared[1]); e.setAttribute('class', snared[2]); continue; }
			   
			   	snared = t[i].match( /^([a-z][a-z0-9]*)$/i );
                
				if	(snared) 
				{ e = doc.createElement( snared[1] ); continue; }

                // Otherwise:
                e = doc.createElement("span");
                e.setAttribute("class", "namelessFromLOL");
            }

            if		(t[i] == undefined) 
			{ throw complaining("Can't graft an undefined value in a list!"); } 
			else if	(t[i].constructor == String || t[i].constructor == Array ) 
			{ graft(e, t[i], doc); } 
			else if	(t[i].constructor == Number) 
			{ graft(e, t[i].toString(), doc); } 
			else if	(t[i].constructor == Object) 
			{ 
                // hash's properties => element's attributes
                for	(var k in t[i]) 
				{

						// support for attaching closures to DOM objects
						if	(typeof(t[i][k]) == 'function')
						{ e[k] = t[i][k]; } 
						else 
						{ e.setAttribute( k, t[i][k] ); }
					
				}
            } 
			else 
			{ throw complaining("Object " + t[i] + " is inscrutable as an graft arglet."); }
        }
    }

    parent.appendChild( e );
    return e; // return the topmost created node
}

function complaining (s) { alert(s); return new Error(s); }
