
var isIE = navigator.userAgent.indexOf("MSIE") != -1;
var globalEvent ;
var globalTarget ;
function getTarget (e) { 
    if (!e) e = window.event ; globalEvent = e ; if (e.target) {
        globalTarget = e.target ;
    } else {
        globalTarget = e.srcElement
    }
    targetPath(globalTarget, 0) ;
}

function targetPath(el, level) {
    if (el == null)
        return ;
    // alert("target[" + level + "] " + el.nodeType + " " + el.name + " " + el.id) ;
    targetPath(el.parentNode, level+1) ;
}
document.onclick = getTarget ;

var popRef, popGroup, popWhat, popTimer ;
    
function show_div(what, ref) {
    if (document.getElementById) {
        var elem = document.getElementById(what) ;
        if (elem != null) {
            var css = elem.style;
            if (ref != null) {
                var offsetLeft = 0 ;
                var offsetTop = 0 ;
                while (ref) {
                    offsetLeft += ref.offsetLeft ;
                    offsetTop += ref.offsetTop ;
                    ref = ref.offsetParent ;
                }
                css.position = "absolute" ;
                css.top = (offsetTop + 2)+ 'px' ;
                css.left = (offsetLeft + 130) + 'px';
            }
            css.display = "block";
        }
    }
}

function hide_div(what) {
    if (document.getElementById) {
        var css = document.getElementById(what).style;
        css.display = "none";
    }
}

function toggle_div(what) {
    if (document.getElementById) {
        var css = document.getElementById(what).style;
        if (css.display != "block")
            css.display = "block";
        else
            css.display = "none";
    }
}

function selectFromGroup(id, name, type, color) {
    var css ;
    var fields = getObjsByName(name, type) ;
    for (var i = 0; i < fields.length; i++) {
        css = fields[i].style ;
        css.border = null ;
    }
    css = document.getElementById(id).style ;
    css.border = "1px inset " + (color == null ? "#aaaaaa" : color) ;
}

function showFrame(what, where, src) {
    if (document.getElementById) {
        var css = document.getElementById(what).style;
        css.display = "none";
        var myFrame = document.getElementById(where) ;
        myFrame.src = src ;
        css.display = "block";
    }
}

var fieldName  = new Array() ;
var fieldValue = new Array() ;
var aidx = 0 ;

function queueField(name, content) {
    fieldName[aidx]  = name ;
    fieldValue[aidx] = content ;
    aidx++ ;
}

function setField(name, content) {
    var fields = getObjsByName(name, 'input') ;
    for (var i = 0; i < fields.length; i++) {
        // alert('setField ' + name + ' ' + i + ' ' + content) ;
        fields[i].value = content ;
    }
}

function setFormField(fName, name, content) {
    var i ;
    var forms = getObjsByName(fName, 'form') ;
    if (forms == null || forms[0] == null)
        return ;
    
    var els = forms[0].elements ;
    if (els == null)
        return ;

    for (i = 0 ; i < els.length; i++) {

        if (els[i].name == name) {
            els[i].value = content ;
            return ;
        }
    }
}

function setQFields() {
    for (var i = 0; i < aidx; i++) {
        var fields = getObjsByName(fieldName[i], 'input') ;
        for (var j = 0; j < fields.length; j++)
            fields[j].value = fieldValue[i] ;
        fields = getObjsByName(fieldName[i], 'textarea') ;
        for (j = 0; j < fields.length; j++)
            fields[j].value = fieldValue[i] ;
    }
    aidx = 0 ;
}

function pop_fixed(ref, group, what) {
    popRef   = ref ;
    popGroup = group ;
    popWhat  = what ;
    // popTimer = setTimeout('pop_div_delay(true)', 110);
    pop_div_delay(true) ;
}

function pop_div(ref, group, what) {
    popRef   = ref ;
    popGroup = group ;
    popWhat  = what ;
    // popTimer = setTimeout('pop_div_delay(false)', 110);
    pop_div_delay(false) ;
}
 
function pop_abort() {
    aidx = 0 ;
    clearTimeout(popTimer) ;
}

function getDivsByName(name) {
    return getObjsByName(name, 'div') ;
}

function getObjsByName(name, type) {
    return getObjsByName2(document, name, type) ;
}

function getObjsByName2(parent, name, type) {
    var arr = new Array();
    if (isIE) {
        if (parent.getElementsByTagName) {
            var elem = parent.getElementsByTagName(type);
            for(var i = 0,iarr = 0; i < elem.length; i++)
                if (name == elem[i].getAttribute("name"))
                    arr[iarr++] = elem[i];
        }
    }
    else {
        if (parent.getElementsByName) {
            arr = parent.getElementsByName(name) ;
        }
    }
    return arr;
}

function getObjsByPrefixId(name, prefix, type) {
    var elem = getObjsByName(name, type) ;
    var arr = new Array(), count = 0 ;
    for(var i = 0; i < elem.length; i++) {
        var elemId = elem[i].getAttribute("id") ;
        if (elemId != null && elemId.match("^" + prefix))
            arr[count++] = elem[i];
    }
    return arr ;
}

function getObjsById(id, type) {
    var arr = new Array();
    if (isIE) {
        if (document.getElementsByTagName) {
            var elem = document.getElementsByTagName(type);
            for(var i = 0,iarr = 0; i < elem.length; i++)
                if (id == elem[i].getAttribute("id"))
                    arr[iarr++] = elem[i];
        }
    }
    else {
        if (document.getElementById)
            arr[0] = document.getElementById(id) ;
    }
    return arr;
}

function getObjById(id) {
    var arr = getObjsById(id, '*') ;
    return arr[0];
}

function mapElementsById(Id) {
    var arr = new Array();
    if (isIE) {
        if (document.getElementsByTagName) {
            var elem = document.getElementsByTagName('*');
            for(var i = 0,iarr = 0; i < elem.length; i++) {
                att = elem[i].getAttribute("id");
                if(att == Id) {
                    arr[iarr] = elem[i];
                    iarr++;
                }
            }
        }
    }
    else {
        if (document.getElementById)
            arr[0] = document.getElementById(id) ;
    }
    return arr;
}

function pop_section(group, section) {
    var i, all = getObjsByName(group, 'div') ;
    for (i = 0; i < all.length; i++)
        all[i].style.display = "none" ;

    all = getObjsById(section, 'div') ;
    for (i = 0; i < all.length; i++)
        all[i].style.display = "block" ;
}

var popScreen  = null ;
var popSection = null ;

function plop_div(plopWhat) {
    if (plopWhat == popScreen) {
        popScreen = null ;
        setField('popScreen', '') ;
    }
    hide_div(plopWhat) ;
}

function pop_div_delay(fixed) {

    var i, all = getDivsByName('popEdit') ;
    for (i = 0; i < all.length; i++)
        all[i].style.display = "none" ;

    all = getDivsByName(popGroup) ;
    for (i = 0; i < all.length; i++)
        all[i].style.display = "none" ;

    setQFields() ;

    if (fixed) {
        popScreen = popWhat ;
        setField('popScreen', popWhat) ;
    }

    if (document.getElementById) {
        var elem = document.getElementById(popWhat);
        if (elem == null) return ;

        var css  = elem.style;
        var offsetTop = 0 ;
        var lref = popRef;
        while (lref) {
            if (lref.clientTop)
                offsetTop += lref.offsetTop + lref.clientTop ;
            else
                offsetTop += lref.offsetTop ;
            lref = lref.offsetParent ;
        }

        var offsetScroll = 0 ;
        lref = popRef ;
        while (lref && lref.nodeName != 'BODY') {
            offsetScroll += lref.scrollTop ;
            lref = lref.parentNode ;
        }
        offsetTop -= offsetScroll ;

        var offsetLeft = 0 ;
        var frame = document.getElementById('imageHeader') ;
        while (frame) {
            offsetLeft += frame.offsetLeft ;
            frame = frame.offsetParent ;
        }

        css.left = (offsetLeft + 70) + 'px'
        css.top = -1000 + 'px' ;
        css.display = "block";
        var topPos = offsetTop - elem.offsetHeight - 15 ;
        if (topPos < 15) {
            if (fixed == true)
                topPos = 15 ;
            else
                topPos = offsetTop + popRef.offsetHeight + 20 ;
        }
        css.top = topPos + 'px' ;
    }
}

var tabGroups  = new Array() ;
var groupTab   = new Array() ;

function getTabIndex(tabGroup) {
    var i ;
    for (i = 0; i < tabGroups.length; i++)
        if (tabGroups[i] == tabGroup)
            return i ;
    tabGroups[i] = tabGroup ;
    return i ;
}

function actualColor(elem) {
    if (elem.currentStyle)
        return elem.currentStyle.color ;
    else if (window.getComputedStyle)
        return getComputedStyle(elem,null).getPropertyValue("color") ;
    return null ;
}

function actualBgColor(elem) {
    if (elem.currentStyle)
        return elem.currentStyle.backgroundColor ;
    else if (window.getComputedStyle)
        return getComputedStyle(elem,null).getPropertyValue("background-color") ;
    return null ;
}

function setElemColor(elem, color) {
    elem.style.color = color ;
}

function setElemBgColor(elem, color) {
    elem.style.backgroundColor = color ;
}

function selectTab(tabGroup, tabId, panelGroup, panel) {
    var tabIndex = getTabIndex(tabGroup) ;
    var color, bgcolor, i ;
    if (groupTab[tabIndex] == tabId) return ;

    var tabElem = getObjsById(tabId, 'div') ;
    if (tabElem == null || tabElem.length == 0 || tabElem[0] == null) return ;
    var tabRef = tabElem[0] ;

    groupTab[tabIndex] = tabId ;

    color   = actualColor(tabRef) ;
    bgcolor = actualBgColor(tabRef) ;

    if (color == null || bgcolor == null) {
        color   = 'darkgrey' ;
        bgcolor = 'white' ;
    }

    var allTabs = getDivsByName(tabGroup) ;
    for (i = 0 ; i < allTabs.length ; i++) {
        setElemColor  (allTabs[i], color) ;
        setElemBgColor(allTabs[i], bgcolor) ;
    }

    for (i = 0 ; i < tabElem.length ; i++) {
        setElemColor  (tabElem[i], bgcolor) ;
        setElemBgColor(tabElem[i], color) ;
    }
	
    var panels = getDivsByName(panelGroup) ;
    for (i = 0 ; i < panels.length ; i++)
        panels[i].style.display = "none" ;

    panels = getObjsByPrefixId(panelGroup, panel, 'div') ;
    for (i = 0 ; i < panels.length ; i++)
        panels[i].style.display = "block" ;

    setField("showTab", tabId) ; 		// should become a multi-level field
}

function clickTab(tabId) {
    var i ;
    if (tabId == null || tabId == "") return ;

    var tabElem = getObjsById(tabId, 'div') ;
    if (tabElem == null || tabElem.length == 0 || tabElem[0] == null) return ;

    if (document.createEvent) {
        var clickevent = document.createEvent("MouseEvents")
        clickevent.initEvent("click", true, true)
        for (i = 0 ; i < tabElem.length ; i++)
            tabElem[i].dispatchEvent(clickevent) ;
    }
    else {
        if (isIE) {
            for (i = 0 ; i < tabElem.length ; i++) {
                tabElem[i].click() ;
            }
        }
    }
}

function submitById(formId) {
    if (formId == null || formId == "") return ;

    var formElem = getObjsById(formId, 'form') ;
    if (formElem == null || formElem.length == 0 || formElem[0] == null) return ;
    /* setField('sourcePage', location.href) ;	*/
    formElem[0].submit() ;
}

function clickInput(name) {
    if (name == null || name == "") return ;

    var submitElem = getObjsById(name, 'input') ;
    if (submitElem == null || submitElem.length == 0 || submitElem[0] == null) return ;
    // alert('clickInput ' + name) ;
    submitElem[0].click() ;
}

function adjustScroll(name) {
    if (isIE){
        var all = getObjsByName(name, '*') ;
        if (all == null || all.length == 0) return ;

        for (var i = 0; i < all.length; i++) {
            var elem = all[i] ;
            var parent = elem.parentNode ;
            //  alert('scroll ' + elem.offsetHeight + ' ' + parent.offsetHeight) ;

            if (elem.offsetHeight > parent.offsetHeight) {
                var newWidth = elem.offsetWidth - 20 ;
                // parent.style.width = (parent.offsetWidth - 2) + 'px' ;
                elem.style.width = newWidth + 'px' ;
            }
            if (elem.offsetWidth > parent.offsetWidth) {
                var newHeight = elem.offsetHeight + 18 ;
                parent.style.height = newHeight + 'px' ;
            }
        }
    }
}

// Calendar Stuff ...
function setupCal(iName) {
    Zapatec.Calendar.setup({
        firstDay          : 1,
        electric          : false,
        inputField        : iName,
        button            : 'B.' + iName,
        ifFormat          : '%Y-%m-%d',
        daFormat          : '%Y-%m-%d'
    });
}

// walk the document and setup any calendar links ...
function restoreCal() {
    var buttons = document.getElementsByName("calButton") ;
    for (i = 0 ; i < buttons.length; i++) {
        setupCal(buttons[i].id.substr(2)) ;
    }
}

var popupActive = false ;
// Code to setup special viewing arrangements for popup-edit en document viewing ...
function popFG() {
    grayOut(true, {
        'zindex':'50',
        'bgcolor':'#444444',
        'opacity':'70'
    });
    popupActive = true ;
    var fg = document.getElementById('sitePopup');
    // alert('popFG: ' + fg) ;
    fg.style.display = 'block' ;
    fg.style.zIndex = '200' ;
// alert('pop-ready') ;
}

function plopFG() {
    var fg = document.getElementById('sitePopup');
    fg.innerHTML = '...' ;
    fg.style.display='none' ;
    popupActive = false ;
    grayOut(false) ;
}

function popDocView(action, param) {
    var wSize = new getBrowserWindowSize() ;

    grayOut(true, {
        'zindex':'50',
        'bgcolor':'#045FB4',
        'opacity':'70'
    });
    var fg = document.getElementById('siteDocView');
    var aa = getHttpBase() + action + '?' + param;
    var fWidth = (wSize.width - 20) + 'px' ;
    var fHeight = (wSize.height - 20) + 'px';
    var iWidth = (wSize.width - 40) + 'px' ;
    var iHeight = (wSize.height - 40) + 'px';
    // alert('setup popDocView ' + aa) ;
    fg.innerHTML='<div class="hCenter"><iFrame style="border: none ; text-align: center; width:' + iWidth + '; height:' + iHeight + ';" src="' + aa + '">....</iFrame><br><span class="invButton hCenter" onclick="plopDocView();">Close</span></div>' ;

    // alert('popFG: ' + fg.innerHTML) ;
    fg.style.display = 'block' ;
    fg.style.zIndex = '200' ;
    fg.style.position = 'absolute' ;
    fg.style.border = "2px inset #444444" ;
    fg.style.top = '10px' ;
    fg.style.left = '10px' ;
    fg.style.width = fWidth ;
    fg.style.height = fHeight ;
    fg.style.overflow = 'auto' ;
    fg.style.backgroundColor='white' ;
//  alert('popDoc-ready ' + fg.style.width + ' ' + fg.style.height + ' ' +  fg.style.zIndex) ;
}

function plopDocView() {
    // alert('plopDocView') ;
    var fg = document.getElementById('siteDocView');
    fg.innerHTML = '...' ;
    fg.style.display='none' ;
    grayOut(false) ;
}

function placePopEdit() {
    var pop = document.getElementById('sitePopup');
    var popEdit = document.getElementById('popupEdit');
    if (popEdit != null) {
        popEdit.style.position = 'absolute' ;
        popEdit.style.left = pop.clientLeft + pop.clientWidth + 'px' ;
        popEdit.style.top = '0px' ;
    }
}

// Darken Screen ....
function grayOut(vis, options) {
    if (popupActive) {
        return ;
    }
    // Pass true to gray out screen, false to ungray
    // options are optional.  This is a JSON object with the following (optional) properties
    // opacity:0-100         // Lower number = less grayout higher = more of a blackout
    // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
    // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
    // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
    // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
    // in any order.  Pass only the properties you need to set.
    options = options || {};
    var zindex = options.zindex || 50;
    var opacity = options.opacity || 70;
    var opaque = (opacity / 100);
    var bgcolor = options.bgcolor || '#000000';
    var dark=document.getElementById('darkenScreenObject');
    if (!dark) {
        // The dark layer doesn't exist, it's never been created.  So we'll
        // create it here and apply some basic styles.
        // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
        var tbody = document.getElementsByTagName("body")[0];
        var tnode = document.createElement('div');           // Create the layer.
        tnode.style.position='absolute';                 // Position absolutely
        tnode.style.top='0px';                           // In the top
        tnode.style.left='0px';                          // Left corner of the page
        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars
        tnode.style.display='none';                      // Start out Hidden
        tnode.id='darkenScreenObject';                   // Name it so we can find it later
        tbody.appendChild(tnode);                            // Add it to the web page
        dark=document.getElementById('darkenScreenObject');  // Get the object.
    }
    if (vis) {
        var ww = new getBrowserWindowSize() ;
        pageWidth='100%';
        pageHeight='100%';
        //set the shader to cover the entire page and make it visible.
        dark.style.opacity=opaque;
        dark.style.MozOpacity=opaque;
        dark.style.filter='alpha(opacity='+opacity+')';
        dark.style.zIndex=zindex;
        dark.style.backgroundColor=bgcolor;
        dark.style.width= pageWidth;
        dark.style.height= pageHeight;
        dark.style.display='block';
    } else {
        dark.style.display='none';
    }
}

function getBrowserWindowSize() {
    this.width = 0, this.height = 0;
    this.fWidth = 0 , this.fHeight = 0 ;
    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        this.width = window.innerWidth;
        this.height = window.innerHeight;
        this.fWidth = window.outerWidth;
        this.fHeight = window.outerHeight;
    }
    else if( document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        this.width = document.documentElement.clientWidth;
        this.height = document.documentElement.clientHeight;
    }
    else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        this.width = document.body.clientWidth;
        this.height = document.body.clientHeight;
    }
}

function inverseTrack(location) {
    try {
        var pageTracker = _gat._getTracker("UA-10572705-1");
        if (location == null)
            location = 'Unknown' ;
        pageTracker._trackPageview(location);
    } catch(err) {}
}

var  rapKeyRef ;
var rapKeyRef2 ;

//  -- get  keypress
function initKeyCheck() {
    document.onkeydown = saveKey  ;
    document.onkeyup = clearKey ;
}

function saveKey(e) {
    if (e) {
        if (rapKeyRef != 'clear'){
            rapKeyRef2 = rapKeyRef ;
        }
        rapKeyRef = e.keyCode ;
        if (rapKeyRef2 == 17 && rapKeyRef == 13) {
            var el = e.target ;
            while (el != null && (!el.getAttribute || el.getAttribute('Name') != 'saveSector')) {
                el = el.parentNode ;
            }
            if (el != null && el.onclick != null) {
                el.onclick() ;
            }
            rapKeyRef2 = rapKeyRef = 'clear' ;
        }
    }
}

function blockEnter() {
   return rapKeyRef != 13;
}

function clearKey(e) {
    rapKeyRef2 = rapKeyRef ;
    rapKeyRef = 'clear' ;
}

function keyControl() {
    if (rapKeyRef == 17) return true ;
    return false ;
}

function keyEnter() {
    if (rapKeyRef == 13) return true ;
    return false ;
}

function keySelect() {      // 's' ...
    if (rapKeyRef == 83) return true ;
    return false ;
}

function keyExpand() {      // 'x' ...
    if (rapKeyRef == 88) return true ;
    return false ;
}
function keyGroup() {      // 'g' ...
    if (rapKeyRef == 71) return true ;
    return false ;
}

function keyFull() {      // 'a' ...
    if (rapKeyRef == 65) return true ;
    return false ;
}

function keyConnect() {      // 'c' ...
    if (rapKeyRef == 67) return true ;
    return false ;
}
function keyZoom() {      // 'z' ...
    if (rapKeyRef == 90) return true ;
    return false ;
}

// cntrl enter is save
function keySave() {
    if (rapKeyRef2 == 17 && rapKeyRef == 13){
        return true ;
    }
    return false ;
}

function keyClick() {
    if (keyControl()) {
        return 'Edit' ;
    }
    if (keySelect()) {
        return 'List' ;
    }
    if (keyExpand()) {
        return 'Expand' ;
    }
    if (keyGroup()) {
        return 'Group' ;
    }
    if (keyFull()) {
        return 'Full' ;
    }
    if (keyConnect()) {
        return 'Connect' ;
    }
    if (keyZoom()) {
        return 'Zoom' ;
    }
    return 'Select' ;
}

// Functions to control the movement of three related areas as if they
// are 'one'

function slideState() {
    this.wUp = null ;
    this.wMove = null;
    this.wOut = null;
    this.wClick = null;
    this.prefix = null ;
    this.areaLeft = null;
    this.areaHead = null ;
    this.areaBody = null;
    this.startX = null ;
    this.startY = null ;
    this.xPix = null ;
    this.yPix = null ;
    this.stopTimer = true ;
    this.stopSelect = false ;
    this.docMouseDown = null ;
    this.docClick = null ;
}
var ssc = new slideState() ;
var gridLeft = 'gridLeft' ;
var gridHead = 'gridHead' ;
var gridBody = 'gridBody' ;

function canvasState() {
    this.elemId = null ;
    this.elem = null;
    this.prefix = null ;
    this.areaLeft = null;
    this.areaHead = null ;
    this.areaBody = null;
    this.top = null ;
    this.left = null ;
    this.width = null ;
    this.height = null ;
    this.border = null ;
    this.position = null;
    this.gWidth = null ;
    this.gHeight = null ;
}
var canvas = new canvasState() ;

function saveCanvas(cv, elem, id) {
    cv.elemId = id ;
    cv.elem = elem;
    cv.top = elem.style.top ;
    cv.left = elem.style.left ;
    cv.width = elem.style.width ;
    cv.height = elem.style.height ;
    cv.border = elem.style.border ;
    cv.position = elem.style.position;
    if (cv.areaBody != null) {
        cv.gWidth = cv.areaBody.style.width ;
        cv.gHeight = cv.areaBody.style.height ;
    }
}

function restoreCanvas(cv) {
    var elem = cv.elem ;
    elem.style.top = cv.top ;
    elem.style.left = cv.left;
    elem.style.width = cv.width;
    elem.style.height = cv.height;
    elem.style.border = cv.border ;
    elem.style.position = cv.position ;

    if (cv.areaBody != null) {
        cv.areaBody.style.width = cv.gWidth ;
        cv.areaBody.style.height = cv.gHeight ;
    }
    if (cv.areaHead != null) {
        cv.areaHead.style.width = cv.gWidth ;
    }
    if (cv.areaLeft != null) {
        cv.areaLeft.style.height = cv.gHeight ;
    }
    cv.elemId = null ;
}

function zoomElem(cv) {
    grayOut(true, {
        'zindex':'50',
        'bgcolor':'#444444',
        'opacity':'70'
    });
    var wSize = new getBrowserWindowSize() ;
    cv.elem.style.zIndex = '200' ;
    cv.elem.style.position = 'fixed' ;
    cv.elem.style.top = '20px' ;
    cv.elem.style.left = '20px' ;
    var elWidth = cv.elem.offsetWidth ;
    var popWidth = wSize.width - 50 ;
    var addHeight = wSize.height - 50 - cv.elem.offsetHeight ;
    cv.elem.style.width = popWidth + 'px' ;
    cv.elem.style.height = wSize.height - 50 + 'px' ;
    cv.elem.style.border = '2px inset #333333' ;

    // var nHeight = wSize.height - 150 - ofsHeight + 'px' ;
    var nHeight = cv.areaBody.offsetHeight - 5 + addHeight + 'px' ;
    var nWidth = wSize.width - 50 - elWidth ;
    if (cv.areaBody != null) {
        cv.areaBody.style.height = nHeight ;
        // alert(' w n el ' + cv.areaBody.offsetWidth + ' ' + nWidth + ' ' + elWidth + ' ' + popWidth) ;
        if (cv.areaBody.offsetWidth + nWidth < popWidth) {
            cv.areaBody.style.width = cv.areaBody.offsetWidth + nWidth + 'px' ;
        }
    }
    if (cv.areaLeft != null) {
        cv.areaLeft.style.height = nHeight ;
    }
    if (cv.areaHead != null) {
        if (cv.areaHead.offsetWidth + nWidth < popWidth) {
            cv.areaHead.style.width = cv.areaHead.offsetWidth + nWidth + 'px';
        }
    }
}

// Code to setup special viewing arrangements for popup-edit en document viewing ...
function slideZoom(id, prefix) {
    if (canvas.elemId != null && id != null) {
        grayOut(false) ;
        restoreCanvas(canvas) ;
        return ;
    }
    if (id == null) {
        id = canvas.elemId ;
        prefix = canvas.prefix ;
    }
    if (id == null)
        return ;
    var elem = document.getElementById(id) ;
    
    if (prefix == null)
        prefix = '' ;
    canvas.prefix = prefix ;
    canvas.areaLeft = document.getElementById(prefix + gridLeft) ;
    canvas.areaHead = document.getElementById(prefix + gridHead) ;
    canvas.areaBody = document.getElementById(prefix + gridBody) ;

    // alert("slideZoom height: " + elem.offsetHeight + ' prefix ' + prefix) ;

    saveCanvas(canvas, elem, id) ;
    //    grayOut(true, {
    //        'zindex':'50',
    //        'bgcolor':'#444444',
    //        'opacity':'70'
    //    });
    zoomElem(canvas) ;
}

// we use gridLeft, gridHead and gridBody; supporting one slideGrid per page
// This is not enough, we need to ID the grids for proper restore ...
function slideSave(prefix) {
    var gridTarget = prefix + gridBody ;
    var b = document.getElementById(gridTarget) ;
    if (b == null) {
        return ;
    }

    if (prefix == null || prefix == '') {
        prefix = 'noPref' ;
    }
    // Need to push this data to the Server ...
    makeRequest('RapView.action', 'SaveSlideGrid=SaveSlideGrid&slideId=' + prefix + '&top=' + b.scrollTop + '&left=' + b.scrollLeft, 'evaluateNow');
}

function sizeSave(ssc) {
    var b = ssc.areaBody ;
    if (b == null) {
        return ;
    }

    // alert('sizeSave: ' + b.offsetWidth) ;
    // Need to push this data to the Server ...
    makeRequest('RapView.action', 'SaveDetailSize=SaveDetailSize&detailSize=' + b.offsetWidth + '&detailId=' + b.id, 'evaluateNow');
}

function slideReset(prefix) {
    if(ssc.wUp != null)
        return ;
    ssc.areaLeft = document.getElementById(prefix + gridLeft) ;
    ssc.areaHead = document.getElementById(prefix + gridHead) ;
    ssc.areaBody = document.getElementById(prefix + gridBody) ;

    if (ssc.areaLeft != null) {
        ssc.areaLeft.scrollTop = 0 ;
    }
    if (ssc.areaHead != null) {
        ssc.areaHead.scrollLeft = 0 ;
    }
    if (ssc.areaBody != null) {
        ssc.areaBody.scrollTop = 0 ;
        ssc.areaBody.scrollLeft = 0 ;
    }
    slideSave(prefix) ;
}

function slideRestore(top, left, prefix) {
    if (prefix == 'noPref')
        prefix = '' ;
    var b = document.getElementById(prefix + gridBody) ;
    var l = document.getElementById(prefix + gridLeft) ;
    var h = document.getElementById(prefix + gridHead) ;
    if (b == null)
        return ;

    if (b != null) {
        b.scrollTop = top ;
        b.scrollLeft = left ;
    }
    if (l != null) {
        l.scrollTop = top ;
    }
    if (h != null) {
        h.scrollLeft = left ;
    }
}

function mouseRestore(ssc, token) {
    var bb = isIE ? document : window ;
    
    if (ssc.timer != null) {
        clearInterval(ssc.timer) ;
        ssc.timer = null ;
    }
    ssc.stopTimer = true ;
    bb.onmousemove = ssc.wMove ;
    bb.onmouseup = ssc.wUp ;
    // bb.onmouseout = ssc.wOut ;
    bb.onclick = ssc.wClick ;
    if (ssc.stopSelect == true) {
        document.onmousedown = ssc.docMouseDown ;
        document.onclick = ssc.docClick ;
        ssc.stopSelect = false ;
    }

    ssc.wUp = null ;
    ssc.wMove = null ;
    if (token == 1) {
        sizeSave(ssc) ;
    } else {
        slideSave(ssc.prefix) ;
    }
}


// make sure mousemovement during private scroll does not
// select any object on screen. This makes the screen much more
// 'relaxed' to look at ....
function disableText(e) {
    return false ;
}
function reEnable(e) {
    return true ;
}
function mouseSave(ssc, stopSelect) {
    var bb = isIE ? document : window ;
    if (ssc.timer != null) {
        clearInterval(ssc.timer) ;
        ssc.timer = null ;
    }
    ssc.wMove = bb.onmousemove ;
    ssc.wUp = bb.onmouseup ;
    // ssc.wOut = bb.onmouseout ;
    ssc.wClick = bb.onclick ;
    if (stopSelect == true) {
        ssc.docMouseDown = document.onmousedown ;
        ssc.docClick = document.onclick ;
        document.onmousedown = disableText ;
        document.onclick = reEnable ;
        ssc.stopSelect = true ;
    } else {
        ssc.stopSelect = false ;
    }
}

function adjustOffset(ofs, cur) {
    if (ofs > 0)
        cur += ofs >= 7 ? 2 : ofs >= 1 ? 1 : 0;
    else
        cur += ofs <= -7 ? -2 : ofs <= -1 ? -1 : 0 ;
    return cur > 7 ? 7 : cur < -7 ? -7 : cur ;
}

function slideMove(ssc) {
    var offsX = ssc.startX - event.screenX ;
    var offsY = ssc.startY - event.screenY ;
    if (offsX == 0 && offsY == 0)
        return ;
    slideAction(offsX, offsY) ;
    ssc.startX  = event.screenX ;
    ssc.startY = event.screenY ;

    // ssc.xPix = adjustOffset(offsX, ssc.xPix) ;
    // ssc.yPix = adjustOffset(offsY, ssc.yPix) ;
    ssc.xPix = offsX  ;
    ssc.yPix = offsY  ;
// alert("start timer") ;
}

function slideTimer() {
    slideAction(ssc.xPix, ssc.yPix) ;
    if (ssc.stopTimer == true && ssc.timer != null) {
        clearInterval(ssc.timer) ;
        ssc.timer = null ;
        return ;
    }
// setTimeout("slideTimer()",20)
}

function slideAction(x, y) {
    if (ssc.areaLeft != null) {
        ssc.areaLeft.scrollTop += y ;
    }
    if (ssc.areaHead != null) {
        ssc.areaHead.scrollLeft += x ;
    }
    ssc.areaBody.scrollTop += y ;
    ssc.areaBody.scrollLeft += x ;
}

function slideId(prefix, name) {
    return prefix == null ? name : prefix + name ;
}

function slideStart(prefix) {
    if(ssc.wUp != null) {
        return ;
    }
    ssc.areaLeft = document.getElementById(slideId(prefix, gridLeft)) ;
    ssc.areaHead = document.getElementById(slideId(prefix, gridHead)) ;
    ssc.areaBody = document.getElementById(slideId(prefix, gridBody)) ;
    if (ssc.areaBody == null) {
        alert('Cannot Find areas: ' + prefix + gridBody) ;
        return ;
    }
    ssc.prefix = prefix == null ? '' : prefix;

    ssc.startX  = event.screenX ;
    ssc.startY = event.screenY ;

    mouseSave(ssc, true) ;

    var bb = isIE ? document : window ;
    ssc.stopTimer = false ;

    ssc.xPix = ssc.yPix = 0 ;
    // Forget auto-motion stuff for now ... movement will be more predictable
    // ssc.timer = setInterval("slideTimer()", 20) ;

    bb.onmouseup = function() {
        mouseRestore(ssc) ;
    } ;
    // bb.onmouseout = mouseRestore ;
    bb.onclick = function() {
        mouseRestore(ssc) ;
    } ;
    bb.onmousemove = function() {
        slideMove(ssc) ;
    } ;

// alert('complete setup') ;
}

ssc2 = new slideState() ;
function sizeDetail(el, dir) {
    if (el == null) {
        return ;
    }
    if(ssc2.wUp != null)
        return ;

    ssc2.areaBody = el ;
    ssc2.startX  = event.screenX ;
    ssc2.startY = dir ;

    mouseSave(ssc2) ;

    var bb = isIE ? document : window ;
    ssc2.timer = null ;

    bb.onmouseup = function() {
        mouseRestore(ssc2,1);
    } ;
    // bb.onmouseout = mouseRestore ;
    bb.onclick = function() {
        mouseRestore(ssc2,1);
    } ;
    bb.onmousemove = function() {
        sizeDo(ssc2,1) ;
    } ;

// alert('complete setup') ;
}

function detailRestore(id, width) {
    if (id == '' || width == '')
        return ;
    var el = document.getElementById(id) ;
    if (el == null) {
        return;
    }
    el.style.width = width + 'px' ;
}

function sizeDo(ssc) {
    var offsX = ssc.startX - event.screenX ;
    var width = ssc.areaBody.offsetWidth ;

    ssc.areaBody.style.width = width + (offsX * ssc.startY) + 'px';
    ssc.startX  = event.screenX ;
}

// print stack trace ...
function printStackTrace() {
    var callstack = [];
    var isCallstackPopulated = false;

    if (!isCallstackPopulated || true) { //IE and Safari
        var currentFunction = arguments.callee.caller;
        while (currentFunction) {
            var fn = currentFunction.toString();
            var fname = fn.substring(fn.indexOf('function') + 8, fn.indexOf(')')+1) || 'anonymous';
            callstack.push(fname);
            currentFunction = currentFunction.caller;
        }
    }
    alert(callstack.join('\n\n'));
}

function selectMenu(id) {
    makeRequest('SiteMenu.action', 'menuId=' + id, 'evaluateNow') ;
}

