﻿////////////////////////////////////////////////
///  Declarations
////////////////////////////////////////////////
var OT_CABINET = 1;
var OT_DRAWER = 2;
var OT_FOLDER = 3;
var OT_DOCUMENT = 4;
	
var m_selectedIDs = [];
var _sortColumn = CT_NAME;
var _sortDirection = SD_ASCENDING;
var _columns = "";
var _evenRowColor = "#addab1";
var _defaultNative = "";
var _scaleToGray = "true";
var _maximizeViewer = "";
var _expandedSearchResults = "false";

////////////////////////////////////////////////
///  selectRange(RowID)
////////////////////////////////////////////////
function selectRange(trEnd) {
	var trStart = document.getElementById(m_selectedIDs[m_selectedIDs.length -1]);
	if(!trStart) return;
			
	var nStart = 0;
	var nEnd = 0;
		
	if(trStart.rowIndex < trEnd.rowIndex) {
		nStart = trStart.rowIndex;
		nEnd = trEnd.rowIndex;
	}
	else {
		nStart = trEnd.rowIndex ;
		nEnd = trStart.rowIndex;
	}
		
	for(var i = nStart; i <= nEnd; i++) {
		var tr = omsTree.rows[i];
		selectRow(tr, false);
	}
}

////////////////////////////////////////////////
///  selectRow(RowID, Clear)
////////////////////////////////////////////////
function selectRow(tr, Clear) {		
	var sNodeClass = "node_text_cell_selected";
	var sListClass = "list_text_cell_selected";
		
	if(Clear)
		clearSelections();
			
	setRowStyleSheets(tr, "node_text_cell_selected", "list_text_cell_selected");		
		
	// Add Selection
	m_selectedIDs[m_selectedIDs.length] = tr.id;
}

////////////////////////////////////////////////
///  ensureVisible(tr)
////////////////////////////////////////////////
function ensureVisible(tr) {
    // Ensure this is in scroll range
	window.scrollTo(tr.offsetLeft, tr.offsetTop);
}

////////////////////////////////////////////////
///  rowClick(tr) 
////////////////////////////////////////////////
function rowClick(tr) {
	if(!tr) return;
		
	// Check for MultiMode
	var bMultiSelect = window.event.ctrlKey;
		
	if(window.event.shiftKey)
		selectRange(tr);
	else {
		if(isSelected(tr) && bMultiSelect) 
			deselectRow(tr);	    
		else  			
			selectRow(tr, !bMultiSelect);
	}    	
			
    //displayAttributes(tr);
    
    //Load any related profile			
    if(window.parent != null) {
        if(parent.ProfileBar != null) {	
            var id = getObjectID(tr);
            var sType = tr.id.substr(0,1);
            
            parent.ProfileBar.navigate("asp/ProfileBar.asp?ID=" + id.toString() + "&TP=" + sType);		
        }
    }    
}
	
////////////////////////////////////////////////
///  clearSelections()
////////////////////////////////////////////////
function clearSelections() {	
    try {
	    for(var i = 0; i < m_selectedIDs.length; i++) {	
	        var tr = document.getElementById(m_selectedIDs[i]);
		    deselectRow(tr);
	    }
	}
	catch(e) {
	    //
	}
	    	
	// Clear the array
	m_selectedIDs.length = 0;
}

////////////////////////////////////////////////
///  deselectRow(RowID)
////////////////////////////////////////////////	
function deselectRow(tr) {		
    if(!tr) return;    
    setRowStyleSheets(tr, "node_text_cell", "list_text_cell");
}

////////////////////////////////////////////////
///  setRowStyleSheets(tr, nodeClass, listClass)
////////////////////////////////////////////////	
function setRowStyleSheets(tr, nodeClass, listClass) {
	if(!tr) return;

	for(i = 0; i < tr.cells.length; i++) {
		var td = tr.cells[i];
			
		if(td.className.indexOf("node_text") > -1)
			td.className = nodeClass;
				
		else if(td.className.indexOf("list_text") > -1) 
			td.className = listClass;
	}
}

////////////////////////////////////////////////
///  isSelected(RowID)
////////////////////////////////////////////////	
function isSelected(tr) {
	for(var i = 0; i < m_selectedIDs.length; i++) {
		if(m_selectedIDs[i] == tr.id)
			return true;			
	}
		
	return false;
}
	
////////////////////////////////////////////////
///  isEven(num)
////////////////////////////////////////////////
function isEven(num) {
	return !(num % 2);
}	

////////////////////////////////////////////////
///  tr getNextRow(RowID)
////////////////////////////////////////////////
function getNextRow(tr) {
    var idx = tr.rowIndex + 1;
    
    if(idx > omsTree.rows.length -1) {
        // we've reached the end of the table
        return null;
    }
    			
	// return next row
	return omsTree.rows[idx];
}
	
////////////////////////////////////////////////
///  tr getPrevRow(RowID)
////////////////////////////////////////////////
function getPrevRow(tr) {
	var idx = tr.rowIndex - 1;
    
    if(idx <= 0) {
        // we've reached the beginning of the table
        return null;
    }
    			
	// return prev row
	return omsTree.rows[idx];
}

////////////////////////////////////////////////
///  getFirstChild(tr)
////////////////////////////////////////////////
function getFirstChild(tr) {
    if(!tr.getAttribute("isExpanded").toString() == "true") return null;   
	
	// return sequential row
	return getNextRow(tr);
}
	
////////////////////////////////////////////////
///  getLastChild(tr)
////////////////////////////////////////////////
function getLastChild(tr) {
    if(!tr.getAttribute("isExpanded").toString() == "true") return null;   
		
	var sChildID = tr.getAttribute("lastChildID");	
	return document.getElementById(sChildID);
}	

////////////////////////////////////////////////
///  getParent(tr)
////////////////////////////////////////////////
function getParent(tr) {
    if(getRowType(tr) == OT_CABINET)
        return omsTree;
        
    if(!tr.getAttribute("parentID")) return null;
        
    var sParentID = tr.getAttribute("parentID");
    return document.getElementById(sParentID);
}

////////////////////////////////////////////////
///  getNextSibling(tr)
////////////////////////////////////////////////
function getNextSibling(tr) {
    if(tr.getAttribute("isLast").toString() == "true") return null;
    
    var id = tr.getAttribute("nextSiblingID");
    if(!id) return null
        
    return document.getElementById(id);
}
	
////////////////////////////////////////////////
///  getPrevSibling(tr)
////////////////////////////////////////////////
function getPrevSibling(tr) {
    if(isFirstChild(tr)) return null;
        
    var id = tr.getAttribute("prevSiblingID");
    if(!id) return null
        
    return document.getElementById(id);
}

////////////////////////////////////////////////
///  getChildRows(tr) 
////////////////////////////////////////////////
function getChildRows(tr) {
    var lst = [0];
	var ctr = -1;
	var nStart;
	var nEnd;	
    
    if(!tr.getAttribute("isExpanded").toString() == "true") return null;
	
	var trChild = getFirstChild(tr);
	if(trChild) nStart = trChild.rowIndex; else return;
	
	trChild = getLastChild(tr);
	if(trChild) nEnd = trChild.rowIndex; else return;
		
    for(var i = nStart; i <= nEnd; i++) {
	    ctr++;        
        lst[ctr] = omsTree.rows[i];
    }   
         
    return lst;	
}
	
////////////////////////////////////////////////
///  removeChildren(ParentRowID)
////////////////////////////////////////////////
function removeChildren(trParent, deleteNodes) {
	var lst = getChildRows(trParent);		
				
	for(var i = lst.length - 1; i > -1; i--) {			
		var tr = lst[i];		
		if(tr) {		    
		    if(deleteNodes)
		        tr.removeNode(true);
		    else
		        tr.style.display = "none";
		}
	}	
}
	
////////////////////////////////////////////////
///  getRowType(RowID)
////////////////////////////////////////////////
function getRowType(tr) {
	var nVal = tr.getAttribute("omsType");		
	return parseInt(nVal, 0);
}

////////////////////////////////////////////////
///  getNodeValue(xDoc, XPath, Default)
////////////////////////////////////////////////
function getNodeValue(xDoc, XPath, Default) {
    var e = xDoc.selectSingleNode(XPath);
    if(!e) return Default;
    
    return e.text;
}

////////////////////////////////////////////////
///  setWaitCursor()
////////////////////////////////////////////////
function setWaitCursor() {
    showProgress();
    document.body.style.cursor = "wait";
}

////////////////////////////////////////////////
///  clearWaitCursor()
////////////////////////////////////////////////
function clearWaitCursor() {
    hideProgress();
    document.body.style.cursor = "";
}

////////////////////////////////////////////////
///  getAncestor(tr)
////////////////////////////////////////////////	
function getAncestor(tr, ancestorType) {    
    var nType = getRowType(tr);
    var nLevels = nType - ancestorType;
    var trTmp = tr;
    
    for(i = 0; i < nLevels; i++) {
        var trTmp = getParent(trTmp);    
    }
    
    return trTmp;
}

////////////////////////////////////////////////
/////  getChildType(objectType)
////////////////////////////////////////////////
function getChildType(objectType) {
    if(objectType == OT_DOCUMENT)
        return 0;
    else
        return objectType + 1;       
}

////////////////////////////////////////////////
/////  displayContextMenu(tr)
////////////////////////////////////////////////
function displayContextMenu(tr) {
    // determine proper context menu    
    if(m_selectedIDs.length > 1) {
        // Show Multi-Selection Context
        displayMultiContext(tr);       
    }
    else {
        // Determine object type
        var nType = getRowType(tr);        
        
        switch(nType) {
            case OT_CABINET: case OT_DRAWER: case OT_FOLDER: 
                // Storage Context Menu               
                
                displayStorageContext(tr);                
                break;
                
            case OT_DOCUMENT:
                // Document Context Menu
                displayDocumentContext(tr);
                break;
                
            default:
                // Unknown
                return;
         }
    }
    
    // Prevent the system context menu from appearing
    return false;    
}

////////////////////////////////////////////////
/////  displayMultiContext(tr)
////////////////////////////////////////////////
function displayMultiContext(tr) {
   if(userIsReadOnly()) {
        // no available context actions        
        return;  
   }
   
   var div = document.getElementById("mnuMultiSelectContext");
   if(!div) return;
   
   div.style.posLeft = window.event.clientX ;
   div.style.posTop = window.event.clientY + document.body.scrollTop;
   div.style.display = "";
}

////////////////////////////////////////////////
/////  displayStorageContext(tr)
////////////////////////////////////////////////
function displayStorageContext(tr) {
    return false;
    
    var nType = getRowType(tr);
    
    // only show context menu for non-fixed items
    if(tr.getAttribute("isFixed") == "true") return;
        
    if(nType == OT_FOLDER) {
        // We don't use the "New" menu item for folders... we use the "import" instead
        document.getElementById("mnuStgNew").style.display = "none";
        setItemByPermission(tr, "mnuStgImport", PERM_CREATE);
        
        // Save for Scan & Import
        document.getElementById("mnuStgScan").style.display = "none";
        setItemByPermission(tr, "mnuStgScan", PERM_CREATE);        
    }
    else {
        document.getElementById("mnuStgImport").style.display = "none";
        document.getElementById("mnuStgScan").style.display = "none";
        
        if(objectHasPermission(tr, PERM_CREATE)) {
            document.getElementById("mnuStgNewLabel").innerHTML = "&nbsp;New " + getTypeDesc(getChildType(nType));
            document.getElementById("mnuStgNew").style.display = "";
        }    
        else
            document.getElementById("mnuStgNew").style.display = "none";        
    }
    
    setItemByPermission(tr, "mnuStgDelete", PERM_DELETE);
    setItemByPermission(tr, "mnuStgShred", PERM_DELETE);
        
    showMenu("mnuStorageContext");
}

////////////////////////////////////////////////
/////  displayDocumentContext(tr)
////////////////////////////////////////////////
function displayDocumentContext(tr) {    
    showMenu("mnuDocumentContext");
    return false;
}

////////////////////////////////////////////////
/////  setItemByPermission(tr, menuItemName, permissionFlag)
////////////////////////////////////////////////
function setItemByPermission(tr, menuItemName, permissionFlag) {
    document.getElementById(menuItemName).style.display = ((objectHasPermission(tr, permissionFlag)) ? "" : "none"); 
}

////////////////////////////////////////////////
/////  showMenu(menuName)
////////////////////////////////////////////////
function showMenu(menuName) {
    var div = document.getElementById(menuName);
    if(!div) return;
   
    var scrollXY = getScrollXY();
   
    div.style.posLeft = window.event.clientX + scrollXY[0];
    div.style.posTop = window.event.clientY + scrollXY[1];
        
    div.style.display = "";
}

////////////////////////////////////////////////
/////  hideMultiContext()
////////////////////////////////////////////////
function hideMenu(menuName) {
    var div = document.getElementById(menuName);
    if(!div) return;
    
    div.style.display = "none";
}

////////////////////////////////////////////////
/////  getIndentClass(tr, ObjectType)
////////////////////////////////////////////////
function getIndentClass(tr, ObjectType) {
    var trAncestor = getAncestor(tr, ObjectType);
    if(!trAncestor) return "";    
    
    if(trAncestor.getAttribute("isLast").toString() == "true")     
        return "indent_cell_no_bkg";
    else
        return "indent_cell";
}

////////////////////////////////////////////////
/////  getStateClass(childCount, isLast)
////////////////////////////////////////////////
function getStateClass(childCount, isLast, isExpanded) {
    var sPrefix = "";
    
    if(childCount == 0)
        sPrefix = "childless";
    else    
        sPrefix = (isExpanded ? "open" : "closed");
    
    var sSuffix = (isLast ? "last" : "norm");
        
    return "state_" + sPrefix + "_" + sSuffix;
} 

////////////////////////////////////////////////
/////  getRowState(tr)
////////////////////////////////////////////////
function getRowState(tr) {
    // states: open, closed, childless
    if(tr.getAttribute("childCount") = 0)
        return "childless";
    else
        return (tr.getAttribute("isExpanded").toString() == "true" ? "open" : "closed");
}

////////////////////////////////////////////////
/////  refreshStateClass(tr)
////////////////////////////////////////////////
function refreshStateClass(tr) {    
    var childCount = tr.getAttribute("childCount");
    var isLast = (tr.getAttribute("isLast").toString() == "true");
    var isExpanded = tr.getAttribute("isExpanded").toString() == "true";

    var td = getStateCell(tr);
    if(td) td.className = getStateClass(childCount, isLast, isExpanded);
}

////////////////////////////////////////////////
/////  CheckBit
////////////////////////////////////////////////
function checkBit(bitFlags, bit) {
    return ((bitFlags & bit) == bit);        
}

////////////////////////////////////////////////
///  insertCabinet(eCabinet, isLast, tblParent)
////////////////////////////////////////////////
function insertCabinet(eCabinet, isLast, trPrevSibling) {  
    var cab = null;
    
    if(!eCabinet.ID) {
        // this is a tr        
        cab = new Cabinet(eCabinet);
    }
    else {        
        // this is a cabinet object
        cab = eCabinet;
    }
    
    // trPrevSibling's next sibling is me
    if(trPrevSibling) trPrevSibling.setAttribute("nextSiblingID", cab.ID);
    
    // Cabinet Row
	var tr = omsTree.insertRow();
	tr.id = cab.ID;		
	
	tr.style.height = "16px";	
	tr.style.padding="0";
	tr.style.margin="0";
	
	tr.setAttribute("omsType", OT_CABINET);
	tr.setAttribute("rights", cab.Rights);
	tr.setAttribute("isExpanded", "false");
	tr.setAttribute("isLast", isLast.toString());
	tr.setAttribute("childCount", cab.ChildCount);
	
	// Set my previous sibling's id to supplied trPrevSibling
	if(trPrevSibling) tr.setAttribute("prevSiblingID", trPrevSibling.id);	
	
	tr.onclick = new Function("evt", "rowClick(this);"); 
	tr.ondblclick = new Function("evt", "toggleExpansion(this);");
	tr.onselectstart = new Function("evt", "return true");
	tr.oncontextmenu = new Function("evt", "return displayContextMenu(this);");
		
	// State Cell 
	var td = tr.insertCell();
	td.id = createCellID(tr.id, "state");
	td.className = getStateClass(cab.ChildCount, isLast.toString(), false );
	td.onclick = new Function("evt", "toggleExpansion(this.parentNode);");
	td.innerText = " ";
			
	// Cabinet Icon Cell 
	td = tr.insertCell();	
	td.className = "cabinet_icon";
	td.innerText = " ";
	
	// Name Cell
	td = tr.insertCell();
	td.id = createCellID(tr.id, "name");
	td.className = "node_text_cell";
	td.colSpan = 4	
	td.innerText= cab.Name;		
	
	appendStorageListCells(tr, cab);
	
	return tr;
}

////////////////////////////////////////////////
///  insertDrawer(CabID, eDrawer, isLast)
////////////////////////////////////////////////
function insertDrawer(eDrawer, insertAfter, isLast, trPrevSibling) {
    var drw = null;
    
    if(!eDrawer.ID) {
        // this is a tr        
        drw = new Drawer(eDrawer);
    }
    else {        
        // this is a drawer object
        drw = eDrawer;
    }
    
    // trPrevSibling's next sibling is me
    if(trPrevSibling) trPrevSibling.setAttribute("nextSiblingID", drw.ID);
    			
	var tr = omsTree.insertRow(insertAfter);
	tr.id = drw.ID;
	tr.height = "16px";	
	
	tr.setAttribute("omsType", OT_DRAWER);
	tr.setAttribute("rights", drw.Rights);
	tr.setAttribute("parentID", "C" + drw.ParentID.toString());	
	tr.setAttribute("isExpanded", "false");
	tr.setAttribute("isLast", isLast.toString());
	tr.setAttribute("childCount", drw.ChildCount);
	
	// Set my previous sibling's id to supplied trPrevSibling
	if(trPrevSibling) tr.setAttribute("prevSiblingID", trPrevSibling.id);	
	
	tr.onclick = new Function("evt", "rowClick(this);");
	tr.ondblclick = new Function("evt", "toggleExpansion(this);"); 
	tr.onselectstart = new Function("evt", "return true");
	tr.oncontextmenu = new Function("evt", "return displayContextMenu(this);");
		
	// Cabinet Indent Cell 
	var td = tr.insertCell();			
	td.id = createCellID(tr.id, "cabinet_indent");
	td.className = getIndentClass(tr, OT_CABINET);
	
	// State Cell 
	var td = tr.insertCell();	
	td.id = createCellID(tr.id, "state");
	td.className = getStateClass(drw.ChildCount, isLast.toString(), false);	 
	td.onclick = new Function("evt", "toggleExpansion(this.parentNode);");
	
	// Drawer Icon Cell 
	td = tr.insertCell();		
	td.className = "drawer_icon";
	
	// Text Cell
	td = tr.insertCell();
	td.id = createCellID(tr.id, "name");
	td.className = "node_text_cell";
	td.colSpan = 3
	td.innerText= drw.Name;
	
	appendStorageListCells(tr, drw);
	
	return tr;
}

////////////////////////////////////////////////
///  insertFolder(DrwID, eFolder, isLast)
////////////////////////////////////////////////
function insertFolder(eFolder, insertAfter, isLast, trPrevSibling) {
    var fld = null;
    
    if(!eFolder.ID) {
        // this is a tr        
        fld = new Folder(eFolder);
    }
    else {        
        // this is a folder object
        fld = eFolder;
    }
	
	// trPrevSibling's next sibling is me
    if(trPrevSibling) trPrevSibling.setAttribute("nextSiblingID", fld.ID);
	
	var tr = omsTree.insertRow(insertAfter);
	tr.id = fld.ID;
	tr.height = "16px";
	tr.setAttribute("omsType", OT_FOLDER);
	tr.setAttribute("rights", fld.Rights);
	tr.setAttribute("parentID", "D" + fld.ParentID.toString());	
	tr.setAttribute("isExpanded", "false");
	tr.setAttribute("isLast", isLast.toString());
	tr.setAttribute("childCount", fld.ChildCount);
	
	// Set my previous sibling's id to supplied trPrevSibling
	if(trPrevSibling) tr.setAttribute("prevSiblingID", trPrevSibling.id);	
	
	tr.onclick = new Function("evt", "rowClick(this);"); 
	tr.ondblclick = new Function("evt", "toggleExpansion(this);"); 
	tr.onselectstart = new Function("evt", "return true");
	tr.oncontextmenu = new Function("evt", "return displayContextMenu(this);");
		
	// Cabinet Indent Cell 
	var td = tr.insertCell();	
	td.id = createCellID(tr.id, "cabinet_indent");	
	td.className = "indent_cell";
	td.className = getIndentClass(tr, OT_CABINET);
	
	// Drawer Indent Cell 
	var td = tr.insertCell();
	td.id = createCellID(tr.id, "drawer_indent");	
	td.className = getIndentClass(tr, OT_DRAWER);
	
	// State Cell 
	var td = tr.insertCell();	
	td.id = createCellID(tr.id, "state");
	td.className = getStateClass(fld.ChildCount, isLast.toString() == "true", false);
	td.onclick = new Function("evt", "toggleExpansion(this.parentNode);");
	
	// Folder Icon Cell 
	td = tr.insertCell();		
	td.className = "folder_icon";
	
	// Text Cell
	td = tr.insertCell();
	td.id = createCellID(tr.id, "name");
	td.className = "node_text_cell";
	td.colSpan = 2
	td.innerText= fld.Name;
	
	appendStorageListCells(tr, fld);
	
	return tr;
}

////////////////////////////////////////////////
///  insertDocument(FldID, eDoc, isLast)
////////////////////////////////////////////////
function insertDocument(eDoc, insertAfter, isLast, trPrevSibling) {
    var doc = null;
    
    if(!eDoc.ID) {
        // this is a tr        
        doc = new Document(eDoc);
    }
    else {        
        // this is a Document object
        doc = eDoc;
    }
    
    // trPrevSibling's next sibling is me
    if(trPrevSibling) trPrevSibling.setAttribute("nextSiblingID", doc.ID);
	
	var tr = omsTree.insertRow(insertAfter);
	tr.id = doc.ID;		
	tr.height = "16px";
	tr.setAttribute("omsType", OT_DOCUMENT);
	tr.setAttribute("rights", doc.Rights);
	tr.setAttribute("status", doc.Status);
	tr.setAttribute("parentID", "F" + doc.ParentID.toString());
	tr.setAttribute("docType", doc.DocType);
	tr.setAttribute("isLast", isLast.toString());
	tr.setAttribute("isExpanded", "false");
	tr.setAttribute("childCount", 0);
	
	// Set my previous sibling's id to supplied trPrevSibling
	if(trPrevSibling) tr.setAttribute("prevSiblingID", trPrevSibling.id);	
	
	tr.onclick = new Function("evt", "rowClick(this);"); 
	tr.ondblclick = new Function("evt", "openDocument(" + eDoc.ID + ");"); 
	tr.onselectstart = new Function("evt", "return true");
	tr.oncontextmenu = new Function("evt", "return displayDocumentContext(this);");
	
	// Cabinet Indent Cell 
	var td = tr.insertCell();
	td.id = createCellID(tr.id, "cabinet_indent");
	td.className = getIndentClass(tr, OT_CABINET);
	
	// Drawer Indent Cell 
	var td = tr.insertCell();
	td.id = createCellID(tr.id, "drawer_indent");
	td.className = getIndentClass(tr, OT_DRAWER);
	
	// Folder Indent Cell 
	var td = tr.insertCell();
	td.id = createCellID(tr.id, "folder_indent");
	td.className = getIndentClass(tr, OT_FOLDER);
	
	// State Cell 
	var td = tr.insertCell();
	td.id = createCellID(tr.id, "state");
	
	if(isLast)
	    td.className = "state_childless_last";	
	else
	    td.className = "state_childless_norm";	    
	
	// Document Icon Cell 
	td = tr.insertCell();	
	td.className = "icon_cell";
	td.background = doc.DocIcon;
	
	// Text Cell
	td = tr.insertCell();
	td.id = createCellID(tr.id, "name");
	td.className = "node_text_cell";			
	td.innerText= doc.Name;
	
	appendDocumentListCells(tr, doc);
	
	return tr;	
}

////////////////////////////////////////////////
/// updateStorageRow(stg)
////////////////////////////////////////////////
function updateStorageRow(stg) {
    // Get Storage Object Row
    var tr = document.getElementById(stg.ID);
    if(!tr) return;
    
    updateCell(tr, "name", stg.Name);
    updateCell(tr, "description", stg.Description);
    updateCell(tr, "profileField1", stg.ProfileField1);
    updateCell(tr, "profileField2", stg.ProfileField2);
    updateCell(tr, "profileField3", stg.ProfileField3);
    updateCell(tr, "profileField4", stg.ProfileField4);
    updateCell(tr, "profileField13", stg.ProfileField13);
    updateCell(tr, "profileField15", stg.ProfileField15);
    updateCell(tr, "createdBy", stg.CreatedBy);
    updateCell(tr, "createdDate", stg.CreatedDate);
    updateCell(tr, "modifiedBy", stg.ModifiedBy);
    updateCell(tr, "modifiedDate", stg.ModifiedDate);
}

////////////////////////////////////////////////
/// updateDocument(eDoc)  
////////////////////////////////////////////////
function updateDocumentRow(eDoc) {
    var doc = null;
    
    if(!eDoc.ID) {
        // this is a tr        
        doc = new Document(eDoc);
    }
    else {        
        // this is a Document object
        doc = eDoc;
    }
    
    // Get Cabinet Row
    var trDoc = document.getElementById(doc.ID);
    if(!trDoc) return;
    
    updateCell(trDoc, "name", doc.Name);
    updateCell(trDoc, "description", doc.Description);
    updateCell(trDoc, "profileField1", doc.ProfileField1);
    updateCell(trDoc, "profileField2", doc.ProfileField2);
    updateCell(trDoc, "profileField3", doc.ProfileField3);
    updateCell(trDoc, "profileField4", doc.ProfileField4);    
    updateCell(trDoc, "profileField5", doc.ProfileField5);
    updateCell(trDoc, "profileField6", doc.ProfileField6);
    updateCell(trDoc, "profileField7", doc.ProfileField7);
    updateCell(trDoc, "profileField8", doc.ProfileField8);    
    updateCell(trDoc, "profileField9", doc.ProfileField9);
    updateCell(trDoc, "profileField10", doc.ProfileField10);
    updateCell(trDoc, "profileField11", doc.ProfileField11);
    updateCell(trDoc, "profileField12", doc.ProfileField12);    
    updateCell(trDoc, "profileField13", doc.ProfileField13);
    updateCell(trDoc, "profileField14", doc.ProfileField14);
    updateCell(trDoc, "profileField15", doc.ProfileField15);
    updateCell(trDoc, "profileField16", doc.ProfileField16);    
    updateCell(trDoc, "createdBy", doc.CreatedBy);
    updateCell(trDoc, "createdDate", doc.CreatedDate);
    updateCell(trDoc, "modifiedBy", doc.ModifiedBy);
    updateCell(trDoc, "modifiedDate", doc.ModifiedDate);
}

////////////////////////////////////////////////
///  updateCell(tr, cellName, value)
////////////////////////////////////////////////
function updateCell(tr, cellName, value) {
    var id = createCellID(tr.id, cellName);
    var td = document.getElementById(id);    
    if(!td) return;
    
    td.innerText = value;
}

////////////////////////////////////////////////
///  toggleExpansion(obj) 
////////////////////////////////////////////////
function toggleExpansion(tr) {         
    if(tr.getAttribute("childCount") == 0) return;
    if(tr.getAttribute("isFixed") == "true") return;
    
    if(tr.getAttribute("isExpanded").toString() == "true")
        collapseNode(tr);
    else
        expandNode(tr);
}

////////////////////////////////////////////////
///  expandNode(tr)
////////////////////////////////////////////////
function expandNode(tr) {    
    // fetch all child items
    getChildItems("[" + tr.id + "]");    
}

function restoreChildItems(tr) {
    var trNext = getNextSibling(tr);
    
    for(var i = 0; i < _cache.length; i++) {
        var trInsert = _cache[i];
        
        if(trNext)
            tr.insertBefore(trInsert);
        else   
            omsTree.appendChild(trInsert);
    }
}

////////////////////////////////////////////////
///  collapseNode(tr)
////////////////////////////////////////////////
function collapseNode(tr) {
    var nType = getRowType(tr);
    
    switch(nType) {
        case OT_CABINET: 
            removeCabinet(tr, true, true);         
            break;
            
        case OT_DRAWER:            
            removeDrawer(tr, true, true); 
            break;
            
        case OT_FOLDER:
            removeFolder(tr, true, true); 
            break;
    }
    
    // Mark node as collapsed
    setRowExpansion(tr, false);    
}

function setCache(tr) {
    var nStart = tr.rowIndex + 1;
    var nEnd = omsTree.rows.length;
    var trNext = getNextSibling(tr);
    
    if(trNext) nEnd = trNext.rowIndex - 1;
    
    for(var i = nStart; i < nEnd; i++) {
        _cache[i - nStart] = omsTree.rows[i];        
    }
}

////////////////////////////////////////////////
///  setRowExpansion(tr, value) 
////////////////////////////////////////////////
function setRowExpansion(tr, value) {
    tr.setAttribute("isExpanded", value);
    
    var td  = getStateCell(tr);
    if(!td) return;
    
    td.className = getStateClass(tr.getAttribute("childCount"), tr.getAttribute("isLast").toString() == "true", value);
}


////////////////////////////////////////////////
///  setNodeLast(tr, value)
////////////////////////////////////////////////
function setNodeLast(tr, value) {
    tr.setAttribute("isLast", value);
    
    var td = getStateCell(tr);
    if(!td) return;
    
    td.className = getStateClass(tr.getAttribute("childCount"), value, tr.getAttribute("isExpanded").toString() == "true");
}

////////////////////////////////////////////////
///  displayAttributes(tr)
////////////////////////////////////////////////
function displayAttributes(tr) {
    var sInfo = "id=" + tr.id;
 
    sInfo += "; parentID=" + getAttrInfo(tr, "parentID"); 
    sInfo += "; childCount=" + getAttrInfo(tr, "childCount");    
    sInfo += "; isLast=" + getAttrInfo(tr, "isLast");
    sInfo += "; isExpanded=" + getAttrInfo(tr, "isExpanded");    
    sInfo += "; NS=" + getAttrInfo(tr, "nextSiblingID");
    sInfo += "; PS=" + getAttrInfo(tr, "prevSiblingID");    
    sInfo += "; FC=" + getAttrInfo(tr, "firstChildID");
    sInfo += "; LC=" + getAttrInfo(tr, "lastChildID");
    
    window.status = sInfo;
}

////////////////////////////////////////////////
///  getAttrInfo(tr, attrName)
////////////////////////////////////////////////
function getAttrInfo(tr, attrName) {
    try {
        var s = tr.getAttribute(attrName).toString();
     
        if(s)
            return s;
        else
            return "null";
    }
    catch(e) {
        return "null";
    }
}   
    
////////////////////////////////////////////////
///  appendStorageListCells(tr, stg)
////////////////////////////////////////////////
function appendStorageListCells(tr, stg) {	    
    if(columnExists("tdDescCol")) addListCell(tr, stg.Description, "description");	
	if(columnExists("tdProfile1Col")) addListCell(tr, stg.ProfileField1, "profileField1");
	if(columnExists("tdProfile2Col")) addListCell(tr, stg.ProfileField2, "profileField2");
	if(columnExists("tdProfile3Col")) addListCell(tr, stg.ProfileField3, "profileField3");
	if(columnExists("tdProfile4Col")) addListCell(tr, stg.ProfileField4, "profileField4");	
	if(columnExists("tdProfile5Col")) addListCell(tr, "", "profileField5");
	if(columnExists("tdProfile6Col")) addListCell(tr, "", "profileField6");
	if(columnExists("tdProfile7Col")) addListCell(tr, "", "profileField7");
	if(columnExists("tdProfile8Col")) addListCell(tr, "", "profileField8");
	if(columnExists("tdProfile9Col")) addListCell(tr, "", "profileField9");
	if(columnExists("tdProfile10Col")) addListCell(tr, "", "profileField10");
	if(columnExists("tdProfile11Col")) addListCell(tr, "", "profileField11");
	if(columnExists("tdProfile12Col")) addListCell(tr, "", "profileField12");
	if(columnExists("tdProfile13Col")) addListCell(tr, stg.ProfileField13, "profileField13");
	if(columnExists("tdProfile14Col")) addListCell(tr, "", "profileField14");
	if(columnExists("tdProfile15Col")) addListCell(tr, stg.ProfileField15, "profileField15");
	if(columnExists("tdProfile16Col")) addListCell(tr, "", "profileField16");
	if(columnExists("tdCreatedByCol")) addListCell(tr, stg.CreatedBy, "createdBy");
	if(columnExists("tdCreatedDateCol")) addListCell(tr, stg.CreatedDate, "createdDate");
	if(columnExists("tdModifiedByCol")) addListCell(tr, stg.ModifiedBy, "modifiedBy");
	if(columnExists("tdModifiedDateCol")) addListCell(tr, stg.ModifiedDate, "modifiedDate");	
}

////////////////////////////////////////////////
///  columnExists(colName)
////////////////////////////////////////////////
function columnExists(colName) {
    var col = document.getElementById(colName);
    if(col) 
        return true;
    else   
        return false;
}

////////////////////////////////////////////////
///  addListCell(tr, value)
////////////////////////////////////////////////
function addListCell(tr, value, cellName) {
    td = tr.insertCell();
    td.id = createCellID(tr.id, cellName);
	td.className = "list_text_cell";
	td.innerText = value;	
	
	return td;
}

////////////////////////////////////////////////
///  appendDocumentListCells(tr, doc)
////////////////////////////////////////////////
function appendDocumentListCells(tr, doc) {	
	if(columnExists("tdDescCol")) addListCell(tr, doc.Description, "description");	
	if(columnExists("tdProfile1Col")) addListCell(tr, doc.ProfileField1, "profileField1");
	if(columnExists("tdProfile2Col")) addListCell(tr, doc.ProfileField2, "profileField2");
	if(columnExists("tdProfile3Col")) addListCell(tr, doc.ProfileField3, "profileField3");
	if(columnExists("tdProfile4Col")) addListCell(tr, doc.ProfileField4, "profileField4");	
	if(columnExists("tdProfile5Col")) addListCell(tr, doc.ProfileField5, "profileField5");
	if(columnExists("tdProfile6Col")) addListCell(tr, doc.ProfileField6, "profileField6");
	if(columnExists("tdProfile7Col")) addListCell(tr, doc.ProfileField7, "profileField7");
	if(columnExists("tdProfile8Col")) addListCell(tr, doc.ProfileField8, "profileField8");
	if(columnExists("tdProfile9Col")) addListCell(tr, doc.ProfileField9, "profileField9");
	if(columnExists("tdProfile10Col")) addListCell(tr, doc.ProfileField10, "profileField10");
	if(columnExists("tdProfile11Col")) addListCell(tr, doc.ProfileField11, "profileField11");
	if(columnExists("tdProfile12Col")) addListCell(tr, doc.ProfileField12, "profileField12");
	if(columnExists("tdProfile13Col")) addListCell(tr, doc.ProfileField13, "profileField13");
	if(columnExists("tdProfile14Col")) addListCell(tr, doc.ProfileField14, "profileField14");
	if(columnExists("tdProfile15Col")) addListCell(tr, doc.ProfileField15, "profileField15");
	if(columnExists("tdProfile16Col")) addListCell(tr, doc.ProfileField16, "profileField16");
	if(columnExists("tdCreatedByCol")) addListCell(tr, doc.CreatedBy, "createdBy");
	if(columnExists("tdCreatedDateCol")) addListCell(tr, doc.CreatedDate, "createdDate");
	if(columnExists("tdModifiedByCol")) addListCell(tr, doc.ModifiedBy, "modifiedBy");
	if(columnExists("tdModifiedDateCol")) addListCell(tr, doc.ModifiedDate, "modifiedDate");	
}

////////////////////////////////////////////////
///  removeCabinet(CabID)
////////////////////////////////////////////////
function removeCabinet(trCab, childrenOnly, deleteNodes) {
    if(trCab.getAttribute("isExpanded").toString() == "true") {
	    // Delete Child Drawers			
	    var lstDrws = getChildRows(trCab);
    	if(lstDrws) {						
	        for(iDrw = lstDrws.length - 1; iDrw >= 0; iDrw--) {			
		        var trDrw = lstDrws[iDrw];
        		
		        if(trDrw.getAttribute("isExpanded").toString() == "true") {			
			        // Delete Child Folders
			        var lstFlds = getChildRows(trDrw);
			        if(lstFlds) {
			            for(iFld = lstFlds.length - 1; iFld >= 0; iFld--) {								
				            removeFolder(lstFlds[iFld], false, deleteNodes);
			            }
			        }
		        }
		        
		        // delete drawer				
		        if(deleteNodes)
	                trDrw.removeNode(true);				        
	            else {
	                // hide it
	                trDrw.style.display = "none";	                
	            }
		    }
		}
    }

    if(!childrenOnly) {
        if(deleteNodes) 
	        // Remove the Cabinet
	        trCab.removeNode(true);
	    else  { 
	        // hide it
	        trCab.style.display = "none";	        
	    }
	}
}

////////////////////////////////////////////////
///  removeDrawer(DrwID)
////////////////////////////////////////////////
function removeDrawer(trDrw, childrenOnly, deleteNodes) {
    if(trDrw.getAttribute("isExpanded").toString() == "true") {		
	    // Delete child folders
	    var lstFlds = getChildRows(trDrw);
	    if(lstFlds) {
	        for(iFld = lstFlds.length - 1; iFld >= 0; iFld--) {			
		        removeFolder(lstFlds[iFld], false, deleteNodes); 
	        }	
	    }
	}
	
	if(!childrenOnly) {
	    if(deleteNodes)
	        // remove drawer
	        trDrw.removeNode(true);			
	    else   
	        // hide it
	        trDrw.style.display = "none";
	}
}

////////////////////////////////////////////////
///  removeFolder(FldID)
////////////////////////////////////////////////
function removeFolder(trFld, childrenOnly, deleteNodes) {				
    if(trFld.getAttribute("isExpanded").toString() == "true") {
        var lst = getChildRows(trFld);
        
        if(lst) {		
            for(i = lst.length - 1 ; i >= 0; i--) {			
	            var trDoc = lst[i];
	            if(deleteNodes)							
		            if(trDoc) trDoc.removeNode(true);					    				
		        else   
		            if(trDoc) trDoc.style.display = "none";
            }
        }
    }
		
	if(!childrenOnly) {
	    if(deleteNodes)
	        trFld.removeNode(true);
	    else    
	        trFld.style.display = "none";
    }
}

////////////////////////////////////////////////
///  removeDocument(DocID)
////////////////////////////////////////////////	
function removeDocument(trDoc, deleteNodes) {
    if(deleteNodes)
	    trDoc.removeNode(true);		
	else   
	    trDoc.style.display = "none";
}

////////////////////////////////////////////////
///  checkError(xDoc)
////////////////////////////////////////////////
function checkError(xDoc) {
    var sType = xDoc.childNodes[0].nodeName;
    
    if(sType.toLowerCase() == "error") {        
        var sErr = xDoc.childNodes[0].text;
        
        if(sErr.indexOf("license") > -1) {
            // license error... show retry message
            showRetry();
        }
        else {
            // just display message
            alert("Error:  " + sErr);
        }
        
        return true;
    }
    
    return false;
}

////////////////////////////////////////////////
///  hideAllMenus()
////////////////////////////////////////////////
function hideAllMenus() {
    hideMenu("mnuStorageContext");
    hideMenu("mnuDocumentContext");
    hideMenu("mnuMultiSelectContext");
}

////////////////////////////////////////////////
///  getSelectedItems()
////////////////////////////////////////////////
function getSelectedItems() {
    // returns all selected tr's
    var lst = [];
    var ctr = -1;
    
    for(var i = 0; i < m_selectedIDs.length; i++) {
        var tr = document.getElementById(m_selectedIDs[i]);        
        ctr ++;
        lst[ctr] = tr;    
    }
    
    return lst;
}

////////////////////////////////////////////////
///  getSelectedItems()
////////////////////////////////////////////////
function getSelectedIDs() {
    var sRet = "";
    
    for(var i = 0; i < m_selectedIDs.length; i++) {
        if(sRet.length > 0) sRet += "|";
        sRet += m_selectedIDs[i];
    }
    
    return sRet;
}

////////////////////////////////////////////////
///  getSelectedItem()
////////////////////////////////////////////////
function getSelectedItem() {
    // returns first selected tr
    return document.getElementById(m_selectedIDs[0]);    
}

////////////////////////////////////////////////
///  getObjectID(tr)
////////////////////////////////////////////////
function getObjectID(tr) {
    var s = tr.id.substr(1, tr.id.length - 1);    
    return parseInt(s, 0);
}

////////////////////////////////////////////////
///  createCellID(trID, cellName)
////////////////////////////////////////////////
function createCellID(trID, cellName) {
    return trID + "$" + cellName;
}

////////////////////////////////////////////////
///  getStateCell(tr)
////////////////////////////////////////////////
function getStateCell(tr) {
    var id = createCellID(tr.id, "state");
    return document.getElementById(id);
}

////////////////////////////////////////////////
///  isFirstChild(tr)
////////////////////////////////////////////////
function isFirstChild(tr) {
    // I am the first child if my parent's first child attribute points to me
    var trParent = getParent(tr);
    if(!trParent) return false;  // cabinet
    
    return (trParent.getAttribute("firstChildID") == tr.id);        
}

////////////////////////////////////////////////
///  incrementChildCount(tr)
////////////////////////////////////////////////
function incrementChildCount(tr) {
    // Increment childCount
    var nCount = tr.getAttribute("childCount");    
    tr.setAttribute("childCount", parseInt(nCount, 0) + 1);
    
    return nCount;
}

////////////////////////////////////////////////
///  decrementChildCount(tr)
////////////////////////////////////////////////
function decrementChildCount(tr) {
    // Increment childCount
    var nCount = tr.getAttribute("childCount");
    
    nCount --;    
    if(nCount < 0) nCount = 0;
        
    tr.setAttribute("childCount", nCount);
        
    return nCount;
}

////////////////////////////////////////////////
///  paintSiblingLines(trStart, trEnd)
////////////////////////////////////////////////
function paintSiblingLines(trStart, trEnd, paint) {
    // determine sibling type
    var nType = getRowType(trStart);
    var sCellSuffix = "";
    var sClass = (paint ? "indent_cell" : "indent_cell_no_bkg" );
    
    // deterine indent column to reference 
    switch(nType) {            
        case OT_CABINET:
            sCellSuffix = "cabinet_indent";
            break;
            
        case OT_DRAWER:
            sCellSuffix = "drawer_indent";
            break;
            
        case OT_FOLDER:
            sCellSuffix = "folder_indent";
            break; 
    }
    
    for(var i = trStart.rowIndex + 1; i < trEnd.rowIndex; i++) {
        var tr = omsTree.rows[i];
        var td = document.getElementById(createCellID(tr.id, sCellSuffix));
        
        td.className = sClass;    
    }
}

////////////////////////////////////////////////
///  paintSiblingLines(trStart, trEnd)
////////////////////////////////////////////////
function getScrollXY() {
    var scrOfX = 0, scrOfY = 0;

    if( typeof( window.pageYOffset ) == 'number' ) {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;

    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;

    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }

    return [ scrOfX, scrOfY ];
}

////////////////////////////////////////////////
/////  getStateInfo(tr)
////////////////////////////////////////////////
function getFetchParams(trParent) {
    var sRet = "";
    
    if(trParent) {
        var nType = getRowType(trParent);
        var nChildType = getChildType(nType);
           
        // Ancestor Indents
        if(nChildType >= OT_DRAWER) {         
            // Cabinet 
            var trCab = getAncestor(trParent, OT_CABINET);        
            if(trCab.getAttribute("isLast").toString() == "true") sRet = "&CabIndent=0"; else sRet += "&CabIndent=1";
        }
        
        if(nChildType >= OT_FOLDER) {
            // Drawer
            var trDrw = getAncestor(trParent, OT_DRAWER);
            if(trDrw.getAttribute("isLast").toString() == "true") sRet += "&DrwIndent=0"; else sRet += "&DrwIndent=1";        
        }
        
        if(nChildType >= OT_DOCUMENT) {
            // Folder
            var trFld = getAncestor(trParent, OT_FOLDER);        
            if(trFld.getAttribute("isLast").toString() == "true") sRet += "&FldIndent=0"; else sRet += "&FldIndent=1";        
        }
    }
    
    // Even Row Color
    if(_evenRowColor.length > 0) {
        // Even Row Color
        sRet += "&EvenColor=" + _evenRowColor;
        
        // Start Even
        if(trParent)
            sRet += "&StartEven=" + (!isEven(trParent.rowIndex)).toString();
        else   
            sRet += "&StartEven=false";
    }
    
    // Sort Column
    sRet += "&Sort=" + _sortColumn.toString();
    
    // Sort Direction
    sRet += "&SortDirection=" + _sortDirection.toString();
    
    // Column Info
    sRet += "&Columns=" + _columns;
    
    return sRet;
}

////////////////////////////////////////
///    getExpansionString()
////////////////////////////////////////
function getExpansionString() {
    var dStart = new Date();
    
    var tbl = document.getElementById("omsTree");
    var sRet = "";
    
    for(var i = 1; i < tbl.rows.length; i++) {
        if(tbl.rows[i].getAttribute("isExpanded") == "true") {
            if(i > 1) sRet += "|";
            sRet += tbl.rows[i].id;
        }
    }
    
    var dEnd = new Date();
    
    //alert((dEnd.getTime() - dStart.getTime())/ 1000);
    
    return sRet;
}

////////////////////////////////////////
///  setSortColumn(mnuItem, direction)
////////////////////////////////////////
function setSortColumn(menuItem, direction) {
    // Get Parent Menu
    var mnu = document.getElementById("mnuColumnContext");
    if(!mnu) return;
    
    // Reference Selected Column
    var id = mnu.getAttribute("columnID");
    var col = document.getElementById(id);    
    if(!col) return;
    
    // Clear selected column
    mnu.setAttribute("columnID", "");
    
    // Hide the menu
    hideMenu("mnuColumnContext");
    
    // Get Column Type from Column
    var colType = col.getAttribute("columnType");
    
    // Has the sort Column Changed?
    var bRefresh = (_sortColumn != colType);
    
    if(!bRefresh) {
        // No... sort Column has not changed. What about the direction?
        bRefresh = (_sortDirection != direction);
    }
    
    // Set the new params
    _sortColumn = colType;
    _sortDirection = direction;
    
    // Set Cookies
    setCookie("SortColumn", _sortColumn);
    setCookie("SortDirection", _sortDirection);
    
    if(bRefresh) { 
        // Refresh the Tree
        refreshTree();        
    }
}

function testCode() {
    var tbl = document.getElementById("omsTree");
    var s = "";
    
    for(var i = 0; i < tbl.rows.length; i++) {
        if(tbl.rows[i].innerHTML.indexOf("node_text_cell_selected") > -1)
            s += "|" + tbl.rows[i].id;
    }    
}

///////////////////////////////////////////////////////////////////////////////////
/////  refreshSortGlyph()
///////////////////////////////////////////////////////////////////////////////////
function refreshSortGlyph() {
    var trHeader = document.getElementById("trHeaderRow");
    if(!trHeader) return;
    
    for(var i = 5; i < trHeader.cells.length; i++) {
        var colType = trHeader.cells[i].getAttribute("columnType");
        if(colType == _sortColumn)
            displayGlyph(trHeader.cells[i], _sortDirection);
        else    
            hideGlyph(trHeader.cells[i]);
    }
}

function displayGlyph(td, direction) {
    var div = null;
    
    if(td.id == "omsTreeHeader$tdNameCol") {
        // reference text div
        div = document.getElementById("divNameColText");
    }
    else {
        div = td;
    }

    // make it bold
    div.style.fontWeight = "bold";
    
    // show glyph
    div.firstChild.style.display = "";
    div.firstChild.innerText = (direction == SD_DESCENDING ? "6 " : "5 ");    
}

function hideGlyph(td) {
    var div = null;
    
    if(td.id == "omsTreeHeader$tdNameCol") {
        // reference text div
        div = document.getElementById("divNameColText");
    }
    else {
        div = td;
    }

    // remove bold
    div.style.fontWeight = "";
    
    // hide glyph
    div.firstChild.style.display = "none";    
}

function hideRetry() {
    var cmd = document.getElementById("lblRetry");
    if(cmd) cmd.style.display = "none";
}

function showRetry() {
    var cmd = document.getElementById("lblRetry");
    if(cmd) cmd.style.display = "";
}
