// ========================================================================
// Display toggling
// From Danny Goodman's JavaScript & DHTML Cookbook
//   section 13.3 p. 371
//     author    : Danny Goodman
//     title     : JavaScript & DHTML Cookbook
//     publisher : O'Reilly
//     ISBN      : 0-596-00467-2
// ========================================================================
// $Id: toggleDisplay.js 223 2005-01-31 09:08:51Z tprice $
// ========================================================================

// Use this pair of functions to retrieve a reference to an element in NN 4
// and IE 4 or later (i.e. across browsers and generations).

// Note that this code is self-initialising IF no other onload event
// handlers are defined for the page. If you specify any onload event
// handlers in your page, be sure to include a call to the library's
// initialisation routine.

// Global variables
var isCss, isW3c, isIe4, isNn4, isIe6Css;

// Initialize upon load to let all browsers establish content objects
function initDhtmlApi() {
    if (document.images) {
        isCss = (document.body && document.body.style) ? true : false;
        isW3c = (isCss && document.getElementById) ? true : false;
        isIe4 = (isCss && document.all) ? true : false;
        isIe6Css = (document.compatMode && document.compatMode.indexOf("CSS1") >= 0) ? true : false;
    }
}

// Set event handler to initialize API
window.onload = initDhtmlApi;

// Seek nested NN4 layer from string anme
function seekLayer(doc, name) {
    var theObj;
    for (var i = 0; i < doc.layers.length; i++) {
        if (doc.layers[i].name == name) {
            theObj = doc.layers[i];
            break;
        }
        // dive into nested layers if necessary
        if (doc.layers[i].document.layers.length > 0) {
            theObj = seekLayer(document.layers[i].document, name);
        }
    }
    return theObj;
}

// Convert object name string or object reference
// into a valid element object reference
function getRawObject(obj) {
    var theObj;
    //alert("getting element object reference for '" + obj + "'");
    if (typeof obj == "string") {
        if (isW3c) {
            theObj = document.getElementById(obj);
        } else if (isIe4) {
            theObj = document.all(obj);
        } else if (isNn4) {
            theObj = seekLayer(document, obj);
        }
    } else {
        // pass through object reference
        theObj = obj;
    }
    return theObj;
}

// Toggle display of an element on and off
function toggleContentDisplay(elemId) {

    var elem = getRawObject(elemId);
    if (elem != null) {
        var displayProp;
        if (visible[elemId]) {
            displayProp = "none";
        } else {
            displayProp = "block";
        }
        elem.style.display = displayProp;
        visible[elemId] = !visible[elemId];
    }
}
