// For cursor setter control
/*
function mfHasChildData(node, bAddIfNotIn) {
    var i, len=this.m_qChildLoaded.length;
    for (i=0; i<len; i++) {
        if (this.m_qChildLoaded[i] == node) return true;
    }

    if (bAddIfNotIn == true) this.m_qChildLoaded.push(node);
    return false;
}*/

function CursorSetter() {
    this.m_nIdShown = null;
    this.m_sCurrentForm = null;
    this.m_sCurrentPathId = null;

//    this.m_qChildLoaded = new Array();
//    this.hasChildData = mfHasChildData;
}

// Global variables
var gCursorSetter = new CursorSetter();

// Following constants need to be insyc with WTreeAction.java
var ACTION_NONE           = "0";
var ACTION_EXPAND_NODE    = "1";
var ACTION_COLLAPSE_NODE  = "2";
var ACTION_PAGING         = "3";
var ACTION_SEPARATOR      = "|";

// Constants for browser type
var BROWSER_IE            = 1;
var BROWSER_NC4           = 4;
var BROWSER_NC5PLUS       = 5;
var gBrowserType = null;

// Get browser type
function getBrowserType() {
    if (gBrowserType != null) return gBrowserType;

    var app = navigator.appName;
    if (app.indexOf("Netscape") == 0) {
        var ver = navigator.appVersion;
        gBrowserType = (ver.indexOf("4") == 0 ? BROWSER_NC4 : BROWSER_NC5PLUS);
    }
    else {
        gBrowserType = BROWSER_IE;
    }

    return gBrowserType;
}

// Debug
function alertObject(o) {
    var c = 0;
    var os = "Object " + o + " has following attribute:\n";
    for (i in o) {
        if ((c%4) != 0) os += "\t";
        c++;
        os += i + " = " + o[i];
        if ((c%4) == 0) os += "\n";
    }
    alert(os);
}

// Event handler for WTree node expansion
function _wtreeHE(sFormName, bExpand, sNodePath, sRealHandler) {
    /*
    if (getBrowserType() == BROWSER_IE) {
        // Some performance boost
        if (bExpand) {
            if (gCursorSetter.hasChildData(sNodePath)) {
                // Just need to re-display those nodes
                _displayChildNodes(sFormName, sNodePath, true);
                return;
            }
        }
        else {
            gCursorSetter.hasChildData(sNodePath, true);
            _displayChildNodes(sFormName, sNodePath, false);
            return;
        }
    }*/

    _appendDisplayState(sFormName, sNodePath + "," + (bExpand ? "1" : "0") + "|");

      // Action code is defined in WTreeAction
    _setAction(sFormName,
               (bExpand ? ACTION_EXPAND_NODE : ACTION_COLLAPSE_NODE) +
               ACTION_SEPARATOR + sNodePath);

    var action = document.forms[sFormName].action;
    action = action + "#" + sNodePath;
    document.forms[sFormName].action = action;
	document.forms[sFormName].submit();
}

// Event handler for paging operations
function _wtreePg(sFormName, nPgPos, nPgSize, sParentNodePath, sRealHandler) {
    // Action code is defined in WTreeAction
    _setAction(sFormName, ACTION_PAGING + ACTION_SEPARATOR +
               sParentNodePath + ACTION_SEPARATOR +
               nPgPos + ACTION_SEPARATOR + nPgSize);
    document.forms[sFormName].submit();
}

// Set display state
function _appendDisplayState(sFormName, sState) {
    document.forms[sFormName].elements["_displayState"].value += sState;
}

// Set event
function _setAction(sFormName, sAction) {
    document.forms[sFormName].elements["_action"].value  = sAction;
}

// Originally from prescontent/operations/includes/shared/javascript/WFRHierarchy.js
function _wtreePgCfg(sFormName, sParentNodePath, nInternalId) {
    gCursorSetter.m_sCurrentForm = sFormName;
    gCursorSetter.m_sCurrentPathId = sParentNodePath;

    var sElem = "cursorSetter";
    var browserType = getBrowserType();

    if (gCursorSetter.m_nIdShown == null || gCursorSetter.m_nIdShown != nInternalId) {
        if (browserType == BROWSER_IE) {
            var es = document.getElementById(sElem).style;
            var cursorMarker = document.getElementById("cur" + nInternalId);
            var aTop = cursorMarker.offsetTop;
            var aLeft = cursorMarker.offsetLeft;

            es.position = "absolute";
            es.posTop   = aTop-2;
            es.posLeft  = aLeft + 15;
            es.posWidth = 300;
            es.display  = "";
        }
        else {
            var markerLayer = document.layers["cur"+nInternalId];
            var cursorLayer = document.layers[sElem];
            cursorLayer.x = markerLayer.pageX + 14;
            cursorLayer.y = markerLayer.pageY - 6;
            cursorLayer.visibility = "show";
        }

        gCursorSetter.m_nIdShown = nInternalId;
    }
    else {
        if (browserType == BROWSER_IE) {
            var es = document.getElementById(sElem).style;
            es.display = "none";
        }
        else {
            document.layers[sElem].visibility = "hide";
        }

        gCursorSetter.m_nIdShown = null;
    }
}

// Submit page change
function _wtreePgConfigSubmit() {
    // Set the action
    var fems;
    var browserType = getBrowserType();
    if (browserType == BROWSER_IE) {
        fems = document.forms["cursorSetterForm"].elements;
    }
    else {
        // Weird
        fems = document.layers["cursorSetter"].document.forms[0].elements;
    }

    var pgPos = fems["pgPos"].value;
    var pgSize;
    if (browserType == BROWSER_IE) {
        pgSize = fems["pgSize"].value;
    }
    else {
        var qValues = new Array(10, 25, 50, 100, 150);
        pgSize = qValues[fems["pgSize"].selectedIndex];
    }
    //set default of pgPos if user did not enter anything.
    if (pgPos == "") pgPos="1";
    var nPagePos  = parseInt(pgPos);
    var nPageSize = parseInt(pgSize);
    if (isNaN(nPagePos) || isNaN(nPageSize) || nPagePos <= 0 || nPageSize <= 0) {
        alert("Invalid page setting.");
    }
    else {
        nPagePos -= 1;
        var sAction = ACTION_PAGING +
                      ACTION_SEPARATOR + gCursorSetter.m_sCurrentPathId +
                      ACTION_SEPARATOR + nPagePos +
                      ACTION_SEPARATOR + nPageSize;
        _setAction(gCursorSetter.m_sCurrentForm, sAction);
//        alert("Current form is " + gCursorSetter.m_sCurrentForm);
        document.forms[gCursorSetter.m_sCurrentForm].submit();
    }
}

// Display/hide child nodes
function _displayChildNodes(sFormName, sNodePath, bShow) {
    alert((bShow ? "Show " : "Hide ") + sNodePath);
    _appendDisplayState(sFormName, sNodePath + "," + (bShow ? "1" : "0") + "|");

    var str = "";
    var sd = (bShow ? "" : "none");
    var row, oldLen = (sNodePath==null ? 0 : sNodePath.length);
    var tbl = document.getElementById(sFormName + "Tbl");
    for (i=0; i<1000; i++) {
        row = tbl.rows(i);
        if (row == null) break;
        if (row.id.length != oldLen && row.id.indexOf(sNodePath)==0) {
            row.style.display = sd;
        }

    }

    tbl.refresh();
}
