/*
 * HTML Sections by Sjoerd Talsma.
 *
 * Check out the latest version on http://sjoerd.talsma.org/
 *
 * Free for non-commercial use. Feel free to use these in your personal
 * website or non-profit project. If you use them, I would appreciate
 * credit where due, but I will not force you to put links on your site
 * or whatever. I leave that up to your own discretion.
 *
 * It is allowed to modify the scripts, but if you feel that you have
 * added something useful, you should send it to me so I can include it
 * for others to enjoy as well. That way everybody is better off.
 *
 * Sjoerd Talsma (sjoerd@talsma.org)
 * November 1, 2002
 */

// Configure the images:
image_expanded_src = "http://www.myfreebanner.com/images/twistie_expanded2.gif";
image_collapsed_src = "http://www.myfreebanner.com/images/twistie_collapsed2.gif";
image_expanded_width = 14;
image_expanded_height = 12;
image_collapsed_width = 14;
image_collapsed_height = 12;

//
// Nothing below this line needs to be changed!
//
var allSections = new Array();
var sections_toggle = false;

function getObj(name) {
    dom=document.getElementById?1:0;
    if (dom) {
        return document.getElementById(name);
    } else if (document.all) {
        return document.all[name];
    } else {
        return null;
    }
}

function getStyle(name) {
    obj = getObj(name);
    if (obj == null) return null;
    return obj.style;
}

function section_insertImage(expanded) {
    img_src = null; img_width = null; img_height = null;
    if (expanded) {
        img_src = image_expanded_src;
        img_width = image_expanded_width;
        img_height = image_expanded_height;
        alt_text = "-";
    } else {
        img_src = image_collapsed_src;
        img_width = image_collapsed_width;
        img_height = image_collapsed_height;
        alt_text = "+";
    }
    if (img_src && img_src != "") {
        document.write("<img src=\"" + img_src + "\" ");
        if (img_width) { document.write("width=\"" + img_width + "\" "); }
        if (img_height) { document.write("height=\"" + img_height + "\" "); }
        document.write("border=\"0\" alt=\"" + alt_text + "\">");
    }
}

function section_nameExists(name) {
    for (var i=0; i<allSections.length; i++) {
        if (allSections[i] == name) {
            return true;
        }
    }
    return false;
}

function section_addNewSection(name) {
    if (section_nameExists(name)) {
        name = name + allSections.length;
    }
    allSections[allSections.length] = name;
    return name;
}

function sectionsSupported() {
    // Disabled for Opera, Netscape, IE 4 & WebTV
    agent=navigator.userAgent;
    dom=document.getElementById?1:0;
    
    if (agent.indexOf("Opera") > -1) return false;
    if (agent.indexOf("MSIE 4") > -1) return false;
    if (document.all || dom) return true;
    return false;
}

function startSection(name, title, isStatic) {
    if (isStatic || !sectionsSupported()) {
        document.writeln("<div>");
        document.writeln("<fieldset>");
        document.writeln("<legend class=\"sectionHeader\"><b>");
        document.writeln(title + "</b></legend>");
        document.writeln("<div class=\"sectionContent\">");
        return;
    }
    title_noquot = removeQuotes(title);
    expanded = true; collapsed = false;
    name = section_addNewSection(name);    // prevent duplicate names.
    document.writeln("<div class=\"sectionHeader\" id=\"" + name + "_off\" style=\"display: none;\">");
    document.writeln("<a href=\"javascript:showSection('" + name + "');\" onmouseover=\"window.status='" + title_noquot + "'; return true;\" onmouseout=\"window.status=''; return true;\">");
    section_insertImage(collapsed);
    document.writeln(title + "</a>");
    document.writeln("</div>");
    document.writeln("<div id=\"" + name + "_on\">");
    document.writeln("<fieldset>");
    document.writeln("<legend class=\"sectionHeader\">");
    document.writeln("<a href=\"javascript:hideSection('" + name + "');\" onmouseover=\"window.status='" + title_noquot + "'; return true;\" onmouseout=\"window.status=''; return true;\">");
    section_insertImage(expanded);
    document.writeln(title + "</a></legend>");
    document.writeln("<div class=\"sectionContent\">");
}

function endSection() {
    document.writeln("</div></fieldset>");
    document.writeln("</div>");
}

function showSection(name) {
    if (sections_toggle == true) collapseAll();
    sectionCSS = getStyle(name + '_on');
    if (sectionCSS != null) sectionCSS.display = "block";
    sectionCSS = getStyle(name + '_off');
    if (sectionCSS != null) sectionCSS.display = "none";
}

function expandAll() {
    if (sections_toggle == true) return;
    for (var i = 0; i < allSections.length; i++) {
        showSection(allSections[i]);
    }
}

function hideSection(name) {
    if (sections_toggle == true) { collapseAll(); return; }
    sectionCSS = getStyle(name + '_on');
    if (sectionCSS != null) sectionCSS.display = "none";
    sectionCSS = getStyle(name + '_off');
    if (sectionCSS != null) sectionCSS.display = "block";
}

function collapseAll() {
    for (var i = 0; i < allSections.length; i++) {
        sectionCSS = getStyle(allSections[i] + '_on');
        if (sectionCSS != null) sectionCSS.display = "none";
        sectionCSS = getStyle(allSections[i] + '_off');
        if (sectionCSS != null) sectionCSS.display = "block";
    }
}

function setToggle(value) {
    if (value && value == true) {
        sections_toggle = true;
    } else {
        sections_toggle = false;
    }
}

//
// Utility function that escapes all single and double quotes with backslashes.
//
function removeQuotes(value) {
    value_noquot = new String(escape(value));
    value_noquot = value_noquot.replace(/%22/g, "%5C%22");   // remove double-quote (")
    value_noquot = value_noquot.replace(/%27/g, "%5C%27");   // remove single-quote (')
    return unescape(value_noquot);
}
