// Neal Landry
// 18-5-2005

var g_isIE = (window.ActiveXObject != null);
// Last high-level struct visited (i.e. 'sch.6')
var g_tocArea = "";
// Last url path name of struct visited
var g_tocPath = "";
// Used to restore the original links if they are modified.
var g_lastLink;
var g_lastHref;

function toggleGroup(id)
{
    if (document.getElementById("div_" + id).style.display == 'block')
    {
        document.getElementById("div_" + id).style.display = 'none';
        document.getElementById("button_" + id).src = "/trianglePlus.gif";
    }
    else
    {
        document.getElementById("div_" + id).style.display = 'block';
        document.getElementById("button_" + id).src = "/triangleMinus.gif";
    }
}    

//
// This function changes the TOC links to internal document anchors if a
// high-level structure (i.e. chapter, part, div, etc) is being viewed.  This
// occurs until the user navigates to a different high-level structure.
//
function checkTocLink(link)
{
    var splitpath = link.pathname.split("/");
    var id = g_isIE ? splitpath[2] : splitpath[3];
    var pathstr = id.split("+")[3];
    var istop = (pathstr.search(/TOP$/) >= 0);
    var path = pathstr.match(/^[a-z\d\.\-]+/)[0];
    var prefix = "";

    // Don't do anything if user clicked on the fragment already loaded.
    if (istop && link.hash != "")
    {
        return;
    }

//    alert("CURRENT: " + link.href);

    // Reset any links that we're changed in a previous call to their original
    // values.
    if (g_lastLink != null)
    {
        g_lastLink.href = g_lastHref;
    }
    g_lastLink = link;
    g_lastHref = link.href;

    // Get the path without the sec. or clause at the end.
    if (path != null)
    {   
        var pathsplit = path.split("-");
        prefix = pathsplit[0];
        for (var i = 1; i < pathsplit.length - 1; i++)
        {
            prefix += "-" + pathsplit[i];
        }
    }

    // User wants to link to an underlying fragment such as a clause.
    if (!istop)
    {
        // See if this clause is a sub-path of the current high-level struct (if
        // any) we're tracking. If so change link to an internal document link.
        if (g_tocArea && prefix.indexOf(g_tocArea) != -1)
        {
//            alert("Linking internally");
            // JS seems to like to decode URL's as soon as a pathname or hash
            // property is modified so we have to re-encode the bloody thing
            // after every property that is modified.
            link.pathname = g_tocPath;
            link.href = encodeURI(link.href);
            link.hash = pathstr;
            link.href = encodeURI(link.href);
        }
        // Otherwise reset global vars and link to the individual fragment.
        else
        {
            g_tocArea = "";
            g_tocPath = "";
        }
    }
    // TOP level click, so remember what high-level struct we're in.
    else
    {
//        alert("TOP click");
        g_tocArea = prefix;
        g_tocPath = link.pathname;
    }
}


//
// Function for tickbox magic in Search As Made when user limits search with a
// year.  This function unticks "Current Year" and ticks "All" for Sessional
// Acts when the user enters a limit year.
//
function updateAsMadeYear(ele)
{
    var acts_am = document.getElementById("acts_am_id");
    var acts_am_old = document.getElementById("acts_am_old_id");
    var regs_am = document.getElementById("regs_am_id");
    var regs_am_old = document.getElementById("regs_am_old_id");
    var epis_am = document.getElementById("epis_am_id");
    var epis_am_old = document.getElementById("epis_am_old_id");
    var xn = document.getElementById("xn_id");
    var xn_old = document.getElementById("xn_old_id");

    if (acts_am != null && acts_am_old != null)
    {
        if (acts_am.checked && ele.value != "")
        {
            acts_am.checked = false;
            acts_am_old.checked = true;
        }            
        else if (acts_am_old.checked && ele.value == "")
        {
            acts_am.checked = true;
            acts_am_old.checked = false;
        }
    }
    if (regs_am != null && regs_am_old != null)
    {
        if (regs_am.checked && ele.value != "")
        {
            regs_am.checked = false;
            regs_am_old.checked = true;
        }            
        else if (regs_am_old.checked && ele.value == "")
        {
            regs_am.checked = true;
            regs_am_old.checked = false;
        }
    }
    if (epis_am != null && epis_am_old != null)
    {
        if (epis_am.checked && ele.value != "")
        {
            epis_am.checked = false;
            epis_am_old.checked = true;
        }            
        else if (epis_am_old.checked && ele.value == "")
        {
            epis_am.checked = true;
            epis_am_old.checked = false;
        }
    }
    if (xn != null && xn_old != null)
    {
        if (xn.checked && ele.value != "")
        {
            xn.checked = false;
            xn_old.checked = true;
        }            
        else if (xn_old.checked && ele.value == "")
        {
            xn.checked = true;
            xn_old.checked = false;
        }
    }
}
