    /*  ==============================================
            Helper functions
            ==============================================  */
            function replace_many(s, c, q)
            {
                newS = '';
                for (i = 0; i < s.length; ++i)
                {
                    if (s.charAt(i) == c)
                        newS += q;
                    else
                        newS += s.charAt(i);
                }
                return newS;
            }
            
            function deGUID(GUID)
            {
                GUID    = replace_many(GUID,'-','_');
                GUID    = GUID.replace('{', 'X')
                GUID    = GUID.replace('}', 'Z')
                
                return GUID;
            }
            
            function undeGUID(GUID)
            {
                GUID    = replace_many(GUID,'_','-');
                GUID    = GUID.replace('X', '{')
                GUID    = GUID.replace('Z', '}')
                
                return GUID;
            }
            
            function plusMinusGUID(str)
            {
                GUID = String( str ).replace( 'plusMinus', '' )
                return GUID;
            }
            
            function spanGUID(str)
            {
                GUID = String( str ).replace( 'span', '' )
                return GUID;
            }
        
        /*  ==============================================
            Called externally, if there is a node representing
            the given key, it's childnodes will be
            reloaded.
            
            NOTE the node itself will NOT be reloaded, thus
            if it has received child documents these will
            not show.
            ==============================================  
            function reloadNode(key)
            {
                var childNodes = document.getElementById.item("childNodes"+deGUID(key));
                
                if(childNodes)
                {
                    var plusMinus = document.getElementById("plusMinus"+deGUID(key));
                    
                    getChildren(childNodes, key, plusMinus.indentions);
                }
               // else
                 //   alert('no node');
            }*/
        
        /*  ==============================================
            Called externally. Sets the provided key as
            focus. It doesnt get clicked, just focused.
            ==============================================  */
            function setFocusToNode(key, doExpand)
            {
                if(key)
                {
                    // check to see if the node is already in focus
                    // this happens when the user click the node in
                    // the tree and when the editor loads it will
                    // try to focus the already focused node
                    var csKey = deGUID(key);
                    var labelEl = document.getElementById("txt" + csKey);
                    if(!labelEl || (labelEl.className != 'selectedTreeNode'))
                    {
                        keyToFocusUpon = key;
                        if(doExpand)
                            getParentKeys(key);
                    }
                    else
                    {
                        keyToFocusUpon = null;
                    }
                }
            }
            
        /*  ==============================================
            Reloads the iframe, instructing it to retreive
            the childnodes of a given document key.
            ==============================================  */
            function getChildren(childNodes, key, indentions, loadAll)
            {
                selectedChildNodesSpan    = childNodes;
                
                var docLoadURL    = urlDocLoader;
                
                if( urlDocLoader.indexOf('?') == -1 )
                    docLoadURL        +='?actionType=expand&imagePath='+imagePath;
                else
                    docLoadURL        +='&actionType=expand&imagePath='+imagePath;
                    
                docLoadURL        += '&key='+key+'&loadAll='+loadAll+'&indentions='+indentions + settingsSuffix;
                docLoader.location.href = docLoadURL;
            }
            
        /*  ==============================================
            Called when a plus or minus sign is clicked.
            It will expand or collapse that branch. Also,
            if the branch has not yet been loaded, it will
            make the reload call.
            ==============================================  */
            function toggleNode(plusMinusEl,indentions, loadAll)
            {
                if(!plusMinusEl)
                {
                    // get the image element and the node element
                    var plusMinusEl = window.event.srcElement;
                }
                
                // get some data
                var csKey =plusMinusGUID(plusMinusEl.id);
                var key = undeGUID(String(csKey));
                var childNodesSpan = document.getElementById("childNodes"+csKey);
                
                if(childNodesSpan.style.display=='block')
                {
                    childNodesSpan.style.display='none'
                    if(plusMinusEl.last)
                        plusMinusEl.src = treePath+'/lastPlus.gif';
                    else
                        plusMinusEl.src = treePath+'/plus.gif';
                    
                    return true;
                }
                else
                {
                    // get the nodes from the server
                    getChildren(childNodesSpan, key, indentions, loadAll);
                    
                    childNodesSpan.style.display='block'
                    if(plusMinusEl.last)
                        plusMinusEl.src = treePath+'/lastMinus.gif';
                    else
                        plusMinusEl.src = treePath+'/minus.gif';
                    
                    return false;
                }
                
                if(dragAndDrop)
                    dragedEl=null;
            }
            
        /*  ==============================================
            Called whenever a label or icon is clicked. The
            label will turn dark blue and the icon may change.
            ==============================================  */
            var selectedOriginalClassName;
            function standardClickActions(csKey, isFolder)
            {
                if(!csKey)
                    alert('Error key = ' + undeGUID(csKey))
            
                // locate some elements
                labelEl     = document.getElementById("txt"+csKey);
                iconEl      = document.getElementById("icon"+csKey);
                
                
                if(!labelEl)
                    alert('no labelEl, key was ' +  undeGUID(csKey))
                
                name        = labelEl.innerText;
                
                // restore the previously focused node
                if(selectedLbl)
                {
                    // check if the last element still exists
                    var spanEl = document.getElementById("span" + selectedKey);
                    if(spanEl)
                    {
                        selectedLbl.className = selectedOriginalClassName;
                        
                        // check if the previous node was a folder
                        if(selectedIsFolder == 1)
                            selectedIcon.src = folderIconOff;
                        else
                            selectedIcon.src = nodeIconOff;
                    }
                }
                
                selectedLbl = labelEl;
                selectedKey = csKey;
                selectedIcon = iconEl;
                selectedOriginalClassName = labelEl.className;
                selectedIsFolder = isFolder;
                
                labelEl.className = 'selectedTreeNode';
                if(isFolder == 1)
                    iconEl.src = folderIconOn;
                else
                    iconEl.src = nodeIconOn;
                
                //  Scoll the node into view... an incredibly annoying feature....!!!
                //iconEl.scrollIntoView();
                
                if(dragAndDrop)
                    dragedEl=null;
             }
        
        
        /*  ==============================================
            Function changing the background of the node.
            ==============================================  */
            function changeNodeStyle(csKey, isFolder)
            {
                var theTextEl = document.getElementById("txt"+csKey);
                
                if(oldSpanElId != theTextEl)
                {
                    theTextEl.style.fontStyle        = "italic";
                    if( isFolder == 1 )
                        theTextEl.style.fontWeight        = "bold";
                    
                    if(oldSpanElId)
                    {
                         oldSpanElId.style.fontStyle        = "normal";
                    }
                    
                    oldSpanElId = theTextEl;
                }
            }   
        
            
        /*  ==============================================
            Functions handling clicking and doubleclicking
            on folders and nodes. All functions also call
            the standardClickActions() function.
            ==============================================  */
            //var isCatFolderKey = false;
            function nodeClick(spanEl, doubleClicked, isFolder, indentions )
            {
                // get the image element and the node element
                
                var csKey = spanGUID(spanEl.id);
                var key = undeGUID(String(csKey));
                plusMinusEl = document.getElementById("plusMinus"+csKey);
                
                /*if(spanEl.catFolderKey)
                    isCatFolderKey = true;
                else
                    isCatFolderKey = false;
                */
                
                changeNodeStyle(csKey, isFolder);
                
                standardClickActions(csKey, isFolder);
                
                if(isFolder == 1)
                {
                    if(doubleClicked)
                    {
                        eval(folderDblClickCode);
                        toggleNode(plusMinusEl,indentions, 0);
                    }
                    else
                    {
                        eval(folderClickCode);
                    }
                }
                else
                {
                    if(doubleClicked)
                    {
                        eval(nodeDblClickCode);
                    }
                    else
                    {
                        eval(nodeClickCode);
                    }
                }
                
                if(dragAndDrop)
                    dragedEl=null;
            }
            
        /*  ==============================================
            Catches data from the serverSide docLoader script
            ==============================================  */
            function catchNodeOpenResult(key, nodeCode, hasChildren, indentions)
            {
                try
                {
                    // make the key "serverside friendly"
                    var csKey = deGUID(key);
                
                    // Get the "plus" image
                    var plusMinusEl = document.getElementById("plusMinus"+ csKey );
                    
                    // Get the icon image
                    var iconEl = document.getElementById("icon"+ csKey );
                    
                    // get the node element (the span tag which holds the child nodes)
                    var childNodesEl = document.getElementById("childNodes" + csKey);
                    
                    // get the span element which spans the icon and the text label
                    var spanEl = document.getElementById("span" + csKey);
                    
                    if(hasChildren == 1)
                    {
                        // make sure there is a plus/minus-image
                        if(childNodesEl.style.display=='block')
                            plusMinus = 'minus';
                        else
                            plusMinus = 'plus';
                        
                        /*if(plusMinusEl.last==1)
                            plusMinusEl.src = treePath+'/last'+plusMinus+'.gif';
                        else
                            plusMinusEl.src = treePath+'/'+plusMinus+'.gif';
                        */
                        // make sure the icon is correct
                        iconEl.src = folderIconOff ;
                        
                        // add the events
                        //plusMinusEl.onclick = toggleNode;
                    }
                    else
                    {
                        // check if the child nodes are expanded
                        if(childNodesEl.style.display == 'block')
                        {
                            // close the expanded node by simulating a click
                            plusMinusEl.click();
                        }
                        
                        // Replace the "plus" image with a dots-only image
                        /*if(plusMinusEl.last==1)
                            plusMinusEl.src = treePath+'/lastdots.gif';
                        else
                            plusMinusEl.src = treePath+'/dots.gif';
                        */
                        iconEl.src = nodeIconOff;
                        
                        // remove the onClick events from the ex-plus image
                        plusMinusEl.onclick = '';
                    }
                    
                    selectedChildNodesSpan.innerHTML = nodeCode;
                }
                catch(e)
                {
                }
                
                expandTree();
                
                if(dragAndDrop)
                    dragedEl=null;
            }
            
        /*  ==============================================
            Will try to expand the tree to a given node.
            ==============================================  */
            var keysToExpand = new Array();
            
            function expandTree(keyIndex)
            {
                if(keysToExpand.length < 1)
                {
                    if(keyToFocusUpon)
                    {
                        getParentKeys(keyToFocusUpon);
                    }
                    return;
                }
                
                // keyIndex should default to the first element
                // which is assumed to be root
                if(!keyIndex)
                    keyIndex = 0;
                
                
                var key = keysToExpand[keyIndex];
                var finalKey = keysToExpand[keysToExpand.length-1];
                var tempKey = deGUID(key);
                var childrenLoaded = true;
                
                // try to find the child container in the tree
                var childNodesEl = document.getElementById("childNodes" + tempKey);
                
                
                if(childNodesEl)
                {
                    // if the final key has been reached, see
                    // if focus should be set
                    if(keyIndex == (keysToExpand.length-1))
                    {
                        // reset the list
                        keysToExpand = new Array();
                        
                        if(keyToFocusUpon)
                        {
                            // setting focus
                            standardClickActions(deGUID(keyToFocusUpon));
                            keyToFocusUpon = null;
                        }
                        else
                        {
                            keyToFocusUpon = null;
                        }
                        
                        return;
                    }
                    
                    // make sure the element is displayed
                    if(childNodesEl.style.display == 'none')
                    {
                        // find the plusMinus and toggle it
                        var plusMinusEl = document.getElementById("plusMinus" + tempKey);
                        childrenLoaded = toggleNode(plusMinusEl);
                    }
                    
                    // there are more keys to expand, but if the toggling
                    // made a server call, we must wait for it to complete.
            
                    if(true)//childrenLoaded)
                    {
                        expandTree(keyIndex + 1);
                    }
                }
            }
            
        /*  ==============================================
            Reloads the iframe to get a chain of parentkeys
            from a given document to the root
            ==============================================  */
            function getParentKeys(key)
            {
                // Not finding the parent, could mean that
                // we didnt have a complete parent list, so
                // lets retreive all parents up to the root
                if( urlDocLoader.indexOf('?') == -1 )
                    docLoader.location.href = urlDocLoader+'?actionType=getParents&startKey='+startKey+'&key=' + key;
                else
                    docLoader.location.href = urlDocLoader+'&actionType=getParents&startKey='+startKey+'&key=' + key;
            }
            
        /*  ==============================================
            Receives any amount of keys to expand in order
            of appearance (parent, grandparent, great grandparent, etc)
            ==============================================  */
            function catchKeysToExpand()
            {
                var argsLen = arguments.length;
                
                keysToExpand = new Array();
                
                for(var a=0; a < argsLen; ++a)
                {
                    keysToExpand[keysToExpand.length] = arguments[a];
                }
            }                                                                                                                                                                                                                                                                   
     