// Remove nodes and any attached functions. Should suppress memory leaks.

// Foud this code at http://javascript.crockford.com/memory/leak.html
// It says that IE has bad memory leaks. This code is called before deleting any child nodes
function purge(d) {
    var a = d.attributes, i, l, n;
    if (a) {
        l = a.length;
        for (i = 0; i < l; i += 1) {
            n = a[i].name;
            if (typeof d[n] === 'function') {
                d[n] = null;
            }
        }
    }
    a = d.childNodes;
    if (a) {
        l = a.length;
        for (i = 0; i < l; i += 1) {
            purge(d.childNodes[i]);
        }
    }
}


// Recursively delete a node and its children
// Found this simple code at 
// http://www.mikechambers.com/blog/2006/01/24/removing-html-element-children-with-javascript
function removeChildrenRecursively(node) {
   if (!node) return;
   while (node.hasChildNodes()) {
	removeChildrenRecursively(node.firstChild);
        purge(node.firstChild);
   	node.removeChild(node.firstChild);
   }
}
