
function dynLoad() {
    this.requestList = new Array() ;
    this.httpBase = null ;
    this.targetId = 0 ;
    this.winHead = null ;
    this.respHead = null;
    this.responder = null ;
}

var dlRoot ;

// set the Root Context to enable global access ...
// $$$ we need to fix the undocking code, since the 'opener'
// member is not safe when site is started in a new window ...
function setWinRoot() {
    if (window.opener == null || true) {
        dlRoot = new dynLoad() ;
        window.winName = 'Root' ;
        window.parentName = null ;
        // alert('Root add Window') ;
        addWindow (null, 'Root', window) ;
    } else {
        dlRoot = wRoot().dlRoot ;
    }
}

// get the global root window context
function wRoot() {
    if (window.rootWindow != null) {
        return window.rootWindow ;
    }
    return window ;
}

function setHttpBase(base) {
    dlRoot.httpBase = base ;
}

function getHttpBase() {
    return dlRoot.httpBase ;
}

function requestState() {
    this.request = null ;
    this.target  = null ;
    this.parent  = null ;
}

var currentTick = 0 ;
function forceRefresh() {
    return currentTick++ ;
}

function makeRequest(url, parameters, target, ext) {
    // alert('wRoot: ' + wRoot().winName) ;
    wRoot()._makeRequest(url, parameters, target, window.winName, ext) ;
}
var headerInfo = [] ;
function headerData(name, value) {
    this.name = name ;
    this.value = value ;
}
function clearHeaderInfo() {
    headerInfo = [] ;
}
function addHeaderInfo(name, value) {
    headerInfo.push(new headerData(name, value));
}
function setHeaderInfo(req) {
    alert("add authorization header") ;
    for (var i = 0 ; i < headerInfo.length; i++) {
        var hi = headerInfo[i] ;
        req.setRequestHeader(hi.name, hi.value) ;
    }
}

var loadingDock ;
var loadTried = 0 ;
var loadCount = 0 ;
function _makeRequest(url, parameters, target, parent,ext) {
    if (dlRoot.requestList[target] != null) {
        // alert('nested request for: ' + target) ;
        return ;
    }

    // need a global search for 'target' if target is a popup window we will create it ...
    var fState = doGlobal('document.getElementById', true, target);
    if (fState.element == null && target.search('.PopUp') == -1) {
        // alert('useless request for ' + target) ;
        return ;
    }
    if (loadingDock == null && loadTried == 0) {
        loadingDock = document.getElementById('siteLoading') ;
        loadTried = 1 ;
    }
    // create Unique Id for Target ...
    //    target = target + '_' + dlRoot.targetId++ ;
    //    tgt.id = target ;
    if (target != 'evaluateNow') {
        parameters += '&sourceTarget=' + target ;
    } else {
        target = target + '_' + dlRoot.targetId++ ;
    }
    if (loadingDock != null) {
        loadingDock.innerHTML = '<img src="/Inverse/Images/ajax-loader.gif"/>' ;
    }
    loadCount++ ;
    var http_request = false ;
    url = dlRoot.httpBase + '/' + url ;
    // alert("request: " + url + ' ' + target + ' ' + parameters) ;
    if (wRoot().XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType)
            http_request.overrideMimeType("text/html");
        if (ext) {
            setHeaderInfo(http_request) ;
        }
    } else if (wRoot().ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }
    if (!http_request) {
        loadCount--;
        alert('Cannot create XMLHTTP instance');
        return;
    }
    //    http_request.cookie = document.cookie ;
    //    alert("Cookie: " + http_request.cookie) ;
    var rqState = new requestState() ;
    rqState.request = http_request ;
    rqState.target = target ;
    rqState.parent = parent ;
    dlRoot.requestList[target] = rqState ;
    //    var handler = new Function("http_request.onreadystatechange = function() { placeResult('" + target + "') ;}") ;
    //    handler() ;
    eval("http_request.onreadystatechange = function() { placeResult('" + target + "') ;}") ;
    http_request.open('POST', url, true);

    http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http_request.setRequestHeader("Content-length", parameters.length);
    http_request.setRequestHeader("Connection", "close");
    http_request.send(parameters);
}

function placeResult(target) {
    var result = null ;
    var htmlText = null ;
    var evalText = null ;
    try {
        var fState ;
        var rqState = dlRoot.requestList[target] ;
        var http_request = rqState.request ;
        if (http_request == null) {
            // alert('Stray response: ' + target) ;
            return ;
        }
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                // Capture and strip the evaluateNow container
                var fullText = http_request.responseText ;
                var evalIndex = fullText.indexOf('<evaluateNow>') ;
                if (evalIndex != -1) {
                    htmlText = fullText.substr(0,evalIndex) ;
                    evalText = fullText.substr(evalIndex) ;
                    evalText = evalText.replace(/<evaluateNow>/, "").replace(/<\/evaluateNow>/, "") ;
                } else
                    htmlText = fullText ;
                if (target.search('.PopUp') != -1) {
                    // alert('popup') ;
                    // need a nested window for chained cleanup ...
                    openWindow(target, rqState.parent, htmlText, evalText) ;
                } else {
                    if (target.search('evaluateNow') != -1)
                        fState = doGlobal('document.getElementById', true, 'evaluateNow') ;
                    else {
                        fState = doGlobal('document.getElementById', true, target) ;
                    }
                    if (fState.element == null) {
                        // use the alert for debugging, this can happen when a request is launched and
                        // the page-structure has changed before the response is placed. Runtime this
                        // is not an issue to report; during development it may be useful to be aware
                        // of it.
                        // alert('Cannot Place innerHTML @ ' + target) ;
                    } else {
                        // alert('HTML-result: ' + target + ' ' + fState.element + '\n' + htmlText) ;
                        // var start = htmlText.length > 200 ? htmlText.length - 200 : 0 ;
                        // alert('Html-Tail: ' + target + '\n' + htmlText.substr(start, htmlText.length)) ;
                        // if targetWindow is not equal to the RootWindow, we need
                        // to resize to fit the 'body' ...
                        var win = null;
                        if (fState.window.winName != 'Root')
                            win = fState.window ;
                        // alert('2: ' + target + " " + fState.element + " " + htmlText.substr(htmlText.length > 200 ? htmlText.length - 200 : 0)) ;
                        placeResponse(win, fState.element, target, htmlText, evalText) ;
                    }
                }
                result = 'InVerse-IT OK' ;
            } else {
                dlRoot.requestList[target] = null ;
                result = 'Server error: ' + http_request.status ;
            // var de = document.getElementById('dumpError');
            // de.innerHTML = http_request.responseText ;
            }
            loadCount-- ;
            if (loadCount <= 0) {
                loadCount = 0 ;
                if (loadingDock != null) {
                    loadingDock.innerHTML = '<span style="font-size: 8px;">' + result + '</span>' ;
                }
            }
        }
    } catch (err) {
        dlRoot.requestList[target] = null ;
        if (loadingDock != null) {
            alert("Internal error: " + evalText) ;
            loadingDock.innerHTML = 'unexpected result ' + err ;
        }
    }
}

// Code to deal with cookies
function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function clearTarget(target) {
    wRoot()._clearTarget(target) ;
}

function _clearTarget(target) {
    // alert('clearTarget ' + target) ;
    var fState = doGlobal('document.getElementById', true, target) ;
    if (fState.element != null) {
        if (target.search('.PopUp') != -1 && false) {
            closeWindow(target) ;
        } else {
            fState.element.innerHTML = null ;
        }
    }
}

function setTarget(target, value) {
    wRoot()._setTarget(target,value) ;
}

function _setTarget(target, value) {
    var fState = doGlobal('document.getElementById', true, target) ;
    if (fState.element != null)
        fState.element.innerHTML = value ;
}

// we need to setup a memory structure of all our windows
// to make sure we maintain a transparent map of all the
// objectIds in a multi-window context. We want the root
// to be able to find all the objects (as targets) to place
// our data no matter in what window the object is placed.
function winArea(parent, name, win) {
    this.parent = parent ;
    this.name   = name ;
    this.window = win ;
    this.rTop   = 0 ;
    this.rLeft  = 0 ;
    this.width  = 0 ;
    this.height = 0 ;
    this.stepX = 0 ;
    this.stepY = 0 ;
}

function dLink(obj, prv, nxt) {
    this.object = obj ;
    this.prv = prv ;
    this.nxt = nxt ;
}

// add a window to the linked window list
// this is to be done at the master-window context only ...
function addWindow(parent, name, win) {
    // alert('addWindow: ' + name + ' from: ' + parent) ;
    var w = new winArea(parent, name, win) ;
    var prv = null, nxt = dlRoot.winHead ;
    var nl = new dLink(w, prv, nxt) ;
    if (dlRoot.winHead != null)
        dlRoot.winHead.prv = nl ;
    dlRoot.winHead = nl ;
}

function delWindow(name, children) {
    // alert('delWindow ' + name) ;
    var wLink = dlRoot.winHead ;
    while (wLink) {
        //        if (children) {
        //            alert('Nested request: ' + name + ' ' + wLink.object.parent) ;
        //        }
        if ((children && wLink.object.parent == name) || (!children && wLink.object.name == name)) {
            wLink.nxt.prv = wLink.prv ;
            if (wLink.prv != null) {
                wLink.prv.nxt = wLink.nxt ;
            } else {
                dlRoot.winHead = wLink.nxt ;
            }
            if (!wLink.object.window.closed) {
                wLink.object.window.close() ;
            }
            // alert('Del Children for: ' + wLink.object.name) ;
            delWindow(wLink.object.name, true) ;
            wLink = dlRoot.winHead ;
            continue ;
        }
        wLink = wLink.nxt ;
    }
}

// Called on page refresh, or exit : body unload=" " ...
function unloadWindows() {
    if (dlRoot != null) {
        var wLink = dlRoot.winHead ;
        while (wLink) {
            // alert('findWindow: ' + wLink.object.name) ;
            if (wLink.object.name != 'Root')
                wLink.object.window.close() ;
            wLink = wLink.nxt ;
        }
    }
}

function findWinArea(name) {
    var wLink = dlRoot.winHead ;
    while (wLink) {
        // alert('findWindow: ' + wLink.object.name + ' ' + name) ;
        if (wLink.object.name == name)
            return wLink.object ;
        wLink = wLink.nxt ;
    }
    return null ;
}

function displayState() {
    this.width = 0 ;
    this.height = 0 ;
}
function rewind(head) {
    while (head) {
        if (head.nxt == null)
            return head ;
        head = head.nxt ;
    }
    return null ;
}
// we need to walk the sibblings in reverse, since we grow the
// linked list at the head, to provide a natural cascade view ...
function positionWindows(parent, state) {
    var rArea = null ;
    var pName = parent.name ;
    var wLink = rewind(dlRoot.winHead) ;
    while (wLink) {
        if (wLink.object.parent == pName) {
            var wArea = wLink.object ;
            // alert('positionWindow: ' + wArea.name) ;
            if (rArea == null) {
                if (pName == 'Root') {
                    wArea.rTop = 0 ;
                    wArea.rLeft = 0 ;
                } else {
                    wArea.rTop = parent.rTop + parent.height ;
                    wArea.rLeft = parent.rLeft + 40 ;
                }
            } else {
                wArea.rTop = rArea.rTop + 40 ;
                wArea.rLeft = rArea.rLeft + 40 ;
            }
            var width  = wArea.rLeft + wArea.width ;
            var height = wArea.rTop  + wArea.height ;
            if (width > state.width)
                state.width = width ;
            if (height > state.height)
                state.height = height ;
            rArea = wArea ;
            positionWindows(wArea, state) ;
        }
        wLink = wLink.prv ;
    }
}

function placeWindows(bX, bY) {
    var wLink = rewind(dlRoot.winHead) ;
    while (wLink) {
        var w = wLink.object.window ;
        var a = wLink.object ;
        if (a.name != 'Root') 
            w.moveTo(bX + a.rLeft, bY + a.rTop) ;
        // alert('placeWindow: ' + a.name + ' ' + a.rLeft + ' ' + a.rTop) ;
        wLink = wLink.prv ;
    }
    return null ;
}

function dumpWindow() {
    var wLink = dlRoot.winHead ;
    var all = 'Wins: ' ;
    while (wLink) {
        all += '[' + wLink.object.name + '] ';
        var win = wLink.object.window ;
        if (win != null && win.document != null) {
            all += '{' + win.document + ':' ;
            var els = win.document.getElementsByTagName('div') ;
            for (var i = 0 ; i < els.length; i++) {
                all += ' (' + els[i].id + ') ' ;
            }
            all += '}' ;
        }
        wLink = wLink.nxt ;
    }
    alert(all) ;
}

function responseState(win, target, content, exec) {
    this.window  = win ;
    this.target  = target ;
    this.content = content ;
    this.exec    = exec ;
}

function queueResponse(win, target, content, exec) {
    var div = canRespond(win, target) ;
    if (div != null) {
        placeResponse(win, div, target, content, exec) ;
        return ;
    }
    var l = new responseState(win, target, content, exec) ;
    var lh, prv = null, nxt = dlRoot.respHead ;
    var nl = new dLink(l, prv, nxt) ;
    lh = dlRoot.respHead ;
    if (lh != null)
        lh.prv = nl ;
    dlRoot.respHead = nl ;
    if (dlRoot.responder == null) {
        // alert('queue timer ' + nl + ' ' + win.winName) ;
        dlRoot.responder = setTimeout('deferResponder()', 100) ;
    }
}

function dequeueResponse(state) {
    // alert('delWindow ' + name) ;
    var ll = dlRoot.respHead ;
    while (ll) {
        if (ll.object == state) {
            if (ll.nxt != null)
                ll.nxt.prv = ll.prv ;
            if (ll.prv != null) {
                ll.prv.nxt = ll.nxt ;
            } else {
                dlRoot.respHead = ll.nxt ;
            }
            return ;
        }
        ll = ll.nxt ;
    }
}

function canRespond(win, target) {
    if (win.document == null || win.document.getElementById == null)
        return null ;
    // alert('canRespond: ' + win.winName) ;
    return win.document.getElementById(target) ;
}

function farRight(element, right) {
    // alert('Walk Right: ' + element.id + ' ' + right) ;
    var nodes = element.childNodes ;
    for (var i = 0 ; i < nodes.length; i++) {
        var child = nodes[i] ;
        var newRight = child.offsetLeft + child.offsetWidth ;
        if (newRight > right)
            right = newRight ;
        right = farRight(child, right) ;
    }
    return right ;
}

function fadeState() {
    this.content = null ;
    this.exec = null ;
    this.valueStep = 1 ;
    this.timeStep = 1 ;
    this.opacHigh = 100 ;
    this.opacLow = 0 ;
    this.opacNow = 100 ;
    this.element = null ;
}

function opacTimer(fState) {
    changeOpac(fState.opacNow, fState.element) ;
    if (fState.opacNow >= 100) {
        return ;
    }
    fState.opacNow += fState.valueStep ;
    var timer = function() { 
        opacTimer(fState) ;
    } ;
    setTimeout(timer, fState.timeStep);
}

// assume proper eye-appreciation at 25 frames per second (40ms/frame)
function fadeIn(element, millis) {
    var fState = new fadeState() ;
    // alert('Fade: ') ;
    if (millis < 40) {
        fState.timeStep = 1 ;
        fState.valueStep = 100 ;
    } else {
        fState.timeStep = 20 ;
        fState.valueStep = 100 / (millis/ 20) ;
    }
    fState.element = element ;
    fState.opacNow = fState.valueStep ;
    fState.opacHigh = 100 ;
    // alert('loop ' + fState.valueStep + ' ' + fState.timeStep) ;
    var timer = function() { 
        opacTimer(fState) ;
    } ;
    setTimeout(timer, fState.timeStep) ;
}

// fade the change of window content ...
//change the opacity for different browsers
function changeOpac(opacity, element) {
    // alert('ChangeOpac: ' + opacity + ' ' + element)
    var css = element.style;
    css.opacity = (opacity / 100);
    css.MozOpacity = (opacity / 100);
    css.KhtmlOpacity = (opacity / 100);
    css.filter = "alpha(opacity=" + opacity + ")";
}

function reSize(wArea) {
    if (Math.abs(wArea.height - wArea.window.innerHeight) < Math.abs(wArea.stepY) || wArea.stepY == 0) {
        wArea.window.innerHeight = wArea.height ;
    } else {
        wArea.window.innerHeight += wArea.stepY ;
    }
    if (Math.abs(wArea.width - wArea.window.innerWidth) < Math.abs(wArea.stepX) || wArea.stepX == 0) {
        wArea.window.innerWidth = wArea.width ;
    } else {
        wArea.window.innerWidth += wArea.stepX ;
    }

    if (wArea.height != wArea.window.innerHeight || wArea.width != wArea.window.innerWidth) {
        var timer = function() { 
            reSize(wArea);
        } ;
        wArea.timer = setTimeout(timer, 4) ;
    } else {
        wArea.timer = null ;
    }
}

function placeResponse(win, div, target, content, exec) {
    // alert('Place Response .. ' + win + ' ' + content + '\n' + exec ) ;
    // changeOpac(0, div) ;
    div.innerHTML = content ;
    // fadeIn(div, 350) ;
    if (win != null)
        win.eval(exec) ;
    else {
        if (exec != null) {
            // alert('EvalNow: ' + exec) ;
            eval(exec) ;
        }
    }

    dlRoot.requestList[target] = null ;

    // resize if sub-window ...
    if (win != null) {
        // alert('Adjust size: ' + win.winName) ;
        // alert('Check ' + win.document.documentElement.scrollWidth) ;
        var outer = win.document.getElementById(win.winName) ;
        if (outer != null) {
            var wArea = findWinArea(win.winName) ;
            if (wArea != null) {
                var right = farRight(outer, 0) ;
                // alert('resize? ' + win.innerWidth + ' ' + right) ;
                if (win.innerWidth >= right) {
                    wArea.width = win.innerWidth ;
                } else {
                    if (wArea.width < (right + 10))
                        wArea.width = right + 10 ;
                }
                wArea.height = outer.scrollHeight + 10 ;
                var body = win.document.getElementById('scrollBody') ;
                if (wArea.height > (screen.height - 300)) {
                    wArea.height = (screen.height - 300) ;
                    body.style.overflow = 'auto' ;
                } else {
                    body.style.overflow = 'hidden' ;
                }
                wArea.stepX = (wArea.width  - wArea.window.innerWidth) / 15 ;
                wArea.stepY = (wArea.height - wArea.window.innerHeight) / 15 ;
                if (wArea.stepX > -1 && wArea.stepX < 1)
                    wArea.stepX = wArea.stepX > 0 ? 1 : -1 ;
                if (wArea.stepY > -1 && wArea.stepY < 1)
                    wArea.stepY = wArea.stepY > 0 ? 1 : -1 ;
                if (wArea.timer == null) {
                    var timer = function() {
                        reSize(wArea) ;
                    } ;
                    wArea.timer = setTimeout(timer, 150) ;
                }
            }
        }
        
        if (outer != null && false) {
            // sibblings are positioned cascade from top/left
            // children are positioned cascade from bottom/left
            // which means we need to walk the tree from 'Root' and
            // 'position' the windows relatively. We use two passes:
            // 1) calculate the span of the result, and the 'optimal' top/left
            // 2) place the windows relative to this position
            var dState = new displayState() ;
            positionWindows(findWinArea('Root'), dState) ;

            // position at central position ..
            // alert('MaxSize: ' + dState.width + ' ' + dState.height) ;
            var oY = (screen.height - dState.height) / 4 ;
            var oX = (screen.width - dState.width) / 2 ;
            placeWindows(oX, oY) ;
        }
    // win.alert('Focus: ' + win.winName) ;
    // win.focus() ;
    }
}

function deferResponder() {
    // alert('defer responder ' + dlRoot.respHead + ' ' + wRoot().winName) ;
    var ll = dlRoot.respHead ;
    while (ll != null) {
        var state = ll.object ;
        var div = canRespond(state.window, state.target) ;
        ll = ll.nxt ;
        // alert('CanRespond: ' + div + ' ' + state.target) ;
        if (div != null) {
            // alert('dequeueResponse') ;
            dequeueResponse(state) ;
            placeResponse(state.window, div, state.target, state.content, state.exec) ;
        }
    }
    if (dlRoot.respHead != null) {
        setTimeout('deferResponder()', 100) ;
    } else {
        clearTimeout(dlRoot.responder) ;
        dlRoot.responder = null ;
        if (dlRoot.respHead != null) {
            dlRoot.responder = setTimeout('deferResponder()', 100) ;
        }
    }
}

// encapsulation to make window structure transparent
function openWindow(target, parent, content, exec, width, height) {
    return wRoot()._openWindow(target, parent, content, exec, width, height) ;
}

// may need to pass position information ....
function _openWindow(target, parent, content, exec, width, height) {
    // alert('openWindow ' + name + ' parent: ' + parent + ' target: '+ target + '\n' + content) ;
    // dumpWindow() ;
    var wArea = findWinArea(target) ;
    tw = (wArea == null ? null : wArea.window) ;
    //    if (tw != null) {
    //        delWindow(target, false) ;
    //        tw = null ;
    //    }
    // dumpWindow() ;
    // check for intermediate cleanup requirements ...
    if (tw != null && (tw.closed || tw.document == null)) {
        // alert('open-intermediate cleanup') ;
        delWindow(target, false) ;
        tw = null ;
    } else {
        delWindow(target, true) ;
    }
    if (tw == null) {
        if (width == null)
            width = 800 ;
        if (height == null)
            height = 400 ;
        var head = document.getElementsByTagName("head")[0];
        var layout = '<html><head>' + head.innerHTML.replace(/Inverse/, target) + '</head><body id="scrollBody" class="popWindow"><div class="maxWidth" id="' + target + '">' +
        '<table width="100%" height="100%"><tr><td align="center" valign="middle"><img id="loading" width="60" height="60" src="/Inverse/Images/ProCo.jpg"/></td></tr></table>' +
        '</div>\n<script type="text/javascript">setWinRoot() ; </script>\n</body></html>' ;
        var top = (screen.height - 400 ) / 4 ;
        var left = (screen.width - 1000) / 2 ;
        tw = window.open('', target,"height=" + height + ",width=" + width + ",top=" + top + ",left=" + left + ",resizable,scrollbars=1") ;
        tw.winName = target ;
        tw.parentName = parent ;
        // alert('RootWindow: ' + wRoot().winName + ' ' + window.winName) ;

        addWindow(parent, target, tw) ;

        tw.document.write(layout) ;
        tw.document.close() ;
    // tw.focus() ;
    }
    tw.winName = target ;
    tw.rootWindow = wRoot() ;
    // alert('Focus ' + target) ;
    // tw.focus() ;

    queueResponse(tw, target, content, exec)

    return tw ;
}

function closeWindow(name) {
    wRoot().delWindow(name, false) ;
}

// check if the current 'state' of a target does match
// with the desired state. When not, synch ...
// we cover the special 'overlay' case. If we do have a 'docked'
// overlay, the placement of our data is secundary.
// when we dock, we get the data from target to overlay
//   we only move our data to overlay, if overlay is empty
// when we undock, we get the data from overlay to target
function syncDock(target, docked, overlay) {
    // alert('syncDock ...') ;
    var divSource, divTarget ;
    var fState, winTarget = target + '.PopUp' ;
    var wArea = findWinArea(winTarget) ;
    
    if (docked == true) {
        if (wArea == null)
            return ;
        divSource = wArea.window.document.getElementById(winTarget) ;
        fState = doGlobal('document.getElementById', true, overlay == null ? target : overlay);
        divTarget = fState.element ;
        if (overlay == null || divTarget.innerHTML == null || divTarget.innerHTML == '') {
            changeOpac(0, divTarget) ;
            divTarget.innerHTML = divSource.innerHTML ;
            fadeIn(divTarget, 350) ;
        }
        closeWindow(winTarget) ;
        return ;
    } else {
        if (wArea != null)
            return ;
        fState = doGlobal('document.getElementById', true, overlay == null ? target : overlay);
        divSource = fState.element ;
        // alert('Source: ' + divSource + ' ' + divSource.innerHTML) ;
        openWindow(winTarget, window.winName, divSource.innerHTML, '', divSource.scrollWidth, divSource.scrollHeight) ;
        divSource.innerHTML = null ;
    }
}


function findState() {
    this.window = null ;
    this.element = null ;
}

// Generic call to loop over existing windows to
// execute local calls as if they are global
function doGlobal(fStr, first, a1, a2, a3, a4, a5) {
    var state = new findState() ;
    // if (a2 != null) alert('doGlobal ' + fStr + ' ' + a1 + ' ' + dlRoot.winHead) ;
    var wLink = dlRoot.winHead ;
    while (wLink) {
        var r, w = wLink.object.window ;
        state.window = w ;
        // if (a2 != null) alert('scan windowList ' + wLink.object.name + ' ' + w) ;
        if (w == null || w.closed || w.document == null) {
            // alert('intermediate cleanup') ;
            var name = wLink.object.name ;
            wLink = wLink.nxt ;
            closeWindow(name) ;
            continue ;
        }
        var lf ;
        eval('lf = w.' + fStr + ';') ;
        w.document.callNow = lf ;
        // if (a2 != null) alert('setup callback ' + lf) ;
        // if (a2 != null) alert('dynamic callback: ' + w.document.callNow) ;
        r = w.document.callNow(a1, a2, a3, a4, a5) ;
        state.element = r ;
        // if (a2 != null) alert('called in context: ' + r) ;
        if (first && r != null) {
            return state ;
        }
        // if (a2 != null) alert('Next') ;

        wLink = wLink.nxt ;
    }
    // if (a2 != null) alert('doGlobal: not found') ;
    return state ;
}
//----------------------------------------------------------------------
//
//
// collect the form data and submit with action and execution Bean
// allow for additional data to be passed outside of a form ...

function collectFormArgs(form, args) {
    for (var i = 0 ; i < form.elements.length; i++) {
        var input = form.elements[i] ;
        // skip the file input element
        if (input.disabled) {
            // alert("disabled input") ;
            continue ;
        }
        if (input.type == 'file' || input.type == 'submit') {
            // alert('skip file upload') ;
            continue ;
        }
        if (input.type == 'radio' && !input.checked) {
            continue ;
        }
        if (input.type == 'checkbox' && !input.checked) {
            args += '&' + input.name + '=false' ;
            continue ;
        }
        args += '&' + input.name + '=' + encodeURI(input.value) ;
    }
    return args ;
}

function submitForm(url, action, name, query) {
    var els = getObjsByName(name,'form') ;
    // alert('FORM: ' + name + ' ' + els + ' - ' + query) ;
    if (els.length == 0) {
        alert('Collection is null ' + name) ;
        return ;
    }
    var form = els[0] ;
    var args = action + '=' + action ;
    for (var i = 0 ; i < form.elements.length; i++) {
        var input = form.elements[i] ;
        // skip the file input element
        if (input.disabled) {
            // alert("disabled input") ;
            continue ;
        }
        if (input.type == 'file' || input.type == 'submit') {
            // alert('skip file upload') ;
            continue ;
        }
        if (input.type == 'radio' && !input.checked) {
            continue ;
        }
        if (input.type == 'checkbox' && !input.checked) {
            args += '&' + input.name + '=false' ;
            continue ;
        }
        args += '&' + input.name + '=' + encodeURI(input.value) ;
    }
    if (query != null && query != '') {
        args += '&' + query ;
    }

    // alert('Form: Make Request ' + args) ;
    makeRequest(url, args, 'evaluateNow') ;
}

function submitMark(cb, label,id) {
    var params = 'Mark=Mark&sourceLabel=' + label + '&sourceId=' + id + '&checked=' + cb.checked ;
    makeRequest('Browse.action', params, 'evaluateNow') ;
    var parent = cb.parentNode ;
    if (cb.checked) {
        parent.style.backgroundColor = '#ff0000' ;
    }
    else
        parent.style.backgroundColor = 'transparent' ;
}

function setMark(cb, action, source, rowId, prefix) {
    if (prefix == null)
        prefix = '' ;
    var params = prefix + 'Mark=Mark&source=' + source + '&rowId=' + rowId + '&checked=' + cb.checked ;
    makeRequest(action, params, 'evaluateNow') ;
    var parent = cb.parentNode ;
    if (cb.checked) {
        parent.style.backgroundColor = '#ff0000' ;
    }
    else
        parent.style.backgroundColor = 'transparent' ;
}

// fields are separated by :
function submitRequest(url, action, fields) {
    // alert('submitRequest') ;
    if (action == null)
        return ;
    var args = action + '=' + action ;
    if (fields != null) {
        var ff = fields.split(':') ;
        for (var i = 0 ; i < ff.length; i++) {
            var field = ff[i] ;
            // alert('field: ' + field) ;
            var els = getObjsByName(ff[i],'input') ;
            if (els.length > 0)
                args += '&'+ field + '=' + els[0].value ;
        }
    }
    makeRequest(url, args, 'evaluateNow') ;
}

