﻿////// Define the functions common to the entire site.



//// Master function that loads all other functions for a page.
function addToPage(newFunction)
    {
    // Check whether functions are already set to run when the page loads.
    if (typeof window.onload != 'function')
        {
        // No other functions are set to run.
        // Set this function to run when the page loads.
        window.onload = newFunction;
        }
    else
        {
        // Other functions are already set to run.
        // Store the other functions.
        var existingFunctions = window.onload;

        // Overwrite the existing page load.
        window.onload = function()
            {
            // When the page loads,
            // run the stored functions first,
            // then the new function.
            existingFunctions();
            newFunction();
            }
        }
    }



//// Given a URL, open a new browser window.
function externalWindow(theURL)
    {
    if (window.open)
        {
        var theWindow = window.open(theURL);
        if (window.focus)
            {
            // Bring the new window forward.
            theWindow.focus();
            }
        // Supress the browser's default behavior.
        return false;
        }
    }



//// Given a parent DIV element, expand or colapse a section of a page.
function changeSection(theParent)
    {
    if (theParent.getElementsByTagName)
        {

        // Change the section link.
        var theHeaders = theParent.getElementsByTagName('h3');
        for (var i = 0; i < theHeaders.length; i++)
            {
            var theLinks = theHeaders[i].getElementsByTagName('a');
            for (var j = 0; j < theLinks.length; j++)
                {
                if ((theLinks[j].getAttribute) && (theLinks[j].setAttribute))
                    {
                    var theClass = theLinks[j].getAttribute('class');

                    // Compensate for when 'className' is used instead of 'class'.
                    var theClassName = theLinks[j].getAttribute('className');
                    if (theClass == null) { theClass = theClassName; }

                    if (theClass == 'colapse')
                        {
                        // Display a link to expand a section.
                        theLinks[j].setAttribute('class', 'expand');
                        theLinks[j].setAttribute('className', 'expand');
                        theLinks[j].setAttribute('title', 'expand');
                        }
                    else if (theClass == 'expand')
                        {
                        // Display a link to colapse a section.
                        theLinks[j].setAttribute('class', 'colapse');
                        theLinks[j].setAttribute('className', 'colapse');
                        theLinks[j].setAttribute('title', 'colapse');
                        }
                    else
                        {
                        // Do nothing.
                        }
                    }
                }
            }

        // Change the section.
        var theDivs = theParent.getElementsByTagName('div');
        for (var k = 0; k < theDivs.length; k++)
            {
            if ((theDivs[k].getAttribute) && (theDivs[k].setAttribute))
                {
                var theClass = theDivs[k].getAttribute('class');

                // Compensate for when 'className' is used instead of 'class'.
                var theClassName = theDivs[k].getAttribute('className');
                if (theClass == null) { theClass = theClassName; }

                if (theClass == 'replace')
                    {
                    // Colapse an expanded section.
                    theDivs[k].setAttribute('class', 'hidden');
                    theDivs[k].setAttribute('className', 'hidden');
                    }
                else if (theClass == 'hidden')
                    {
                    // Expand a colapsed section.
                    theDivs[k].setAttribute('class', 'replace');
                    theDivs[k].setAttribute('className', 'replace');
                    }
                else
                    {
                    // Do nothing.
                    }
                }
            }

        }
    else
        {
        return true;
        }
    }



//// Modify a set of sections on a page to expand and colapse.
function setSections()
    {
    if ( (document.getElementsByTagName) && (document.createElement) && (document.createTextNode) )
        {

        // Create a link within each section header
        // to expand and colapse each section.
        var theHeaders = document.getElementsByTagName('h3');
        for (var i = 0; i < theHeaders.length; i++)
            {
            var theDivs = theHeaders[i].getElementsByTagName('span');
            for (var j = 0; j < theDivs.length; j++)
                {
                var newText = theDivs[j].firstChild.nodeValue;
                if (theDivs[j].getAttribute)
                    {
                    var theClass = theDivs[j].getAttribute('class');

                    // Compensate for when 'className' is used instead of 'class'.
                    var theClassName = theDivs[j].getAttribute('className');
                    if (theClass == null) { theClass = theClassName; }

                    if (theClass == 'replace_link')
                        {
                        var newLink = document.createElement('a');
                        if ( (newLink.setAttribute) && (newLink.appendChild) )
                            {
                            newLink.setAttribute('href', '#');
                            newLink.setAttribute('title', 'expand');
                            newLink.setAttribute('class', 'expand');
                            newLink.setAttribute('className', 'expand');
                            newLink.appendChild(document.createTextNode(newText));
                            if (theDivs[j].parentNode.replaceChild)
                                {
                                theDivs[j].parentNode.replaceChild(newLink, theDivs[j]);
                                }
                            }
                        }

                    }
                }
            }

        // Colapse each section.
        var theSections = document.getElementsByTagName('div');
        for (var k = 0; k < theSections.length; k++)
            {
            if (theSections[k].getAttribute)
                {
                var theClass = theSections[k].getAttribute('class');

                // Compensate for when 'className' is used instead of 'class'.
                var theClassName = theSections[k].getAttribute('className');
                if (theClass == null) { theClass = theClassName; }
                
                if ( (theClass == 'replace') && (theSections[k].setAttribute) )
                    {
                    theSections[k].setAttribute('class', 'hidden');
                    theSections[k].setAttribute('className', 'hidden');
                    }
                }
            }

        }
    else
        {
        return true;
        }
    }



//// Assign behaviors to certain types of links.
function setLinks()
    {
    if ((document.getElementsByTagName) && (document.getElementById))
        {

        var theLinks = document.getElementsByTagName('a');

        for (var i = 0; i < theLinks.length; i++)
            {
            if ((theLinks[i].getAttribute) && (theLinks[i].setAttribute))
                {
                // Modify links for external sites and arbitrarily defined new windows.
                var theHREF  = theLinks[i].getAttribute('href');
                var theClass = theLinks[i].getAttribute('class');

                // Compensate for when 'className' is used instead of 'class'.
                var theClassName = theLinks[i].getAttribute('className');
                if (theClass == null) { theClass = theClassName; }
                
                if (theClass == 'external')
                    {
                    // Open the link in a new window.
                    theLinks[i].onclick = function() { externalWindow(this.href); return false; }

                    var theTitle = theLinks[i].getAttribute('title');
                    var newTitle = theTitle + ' (opens a new window)';
                    theLinks[i].setAttribute('title', newTitle);
                    }
                }
            }

        var theHeaders = document.getElementsByTagName('h3');

        for (var j = 0; j < theHeaders.length; j++)
            {
            var theHeaderLinks = document.getElementsByTagName('a');

            for (var k = 0; k < theHeaderLinks.length; k++)
                {
                if (theHeaderLinks[k].getAttribute)
                    {
                    // Modify links to expand and colapse a section of the page.
                    var theHeaderLinkClass = theHeaderLinks[k].getAttribute('class');

                    // Compensate for when 'className' is used instead of 'class'.
                    var theHeaderLinkClassName = theHeaderLinks[k].getAttribute('className');
                    if (theHeaderLinkClass == null) { theHeaderLinkClass = theHeaderLinkClassName; }
                
                    // Expand and colapse a section of the page.
                    if ( (theHeaderLinkClass == 'expand') || (theHeaderLinkClass == 'colapse') )
                        {
                        theHeaderLinks[k].onclick = function() { changeSection(this.parentNode.parentNode); return false; }
                        }
                    }
                }
            }

        var theBannerSearchField = document.getElementById('q');
        // Clears a search field.
        theBannerSearchField.onclick = function() { theBannerSearchField.value=''; return false; }

        }
    else
        {
        return true;
        }
    }



////// Call all the functions common to every page.
addToPage(setSections);
addToPage(setLinks);