////// Define all the functions used to enhance every page.



//// 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;
        }
    }



//// Check for and build links that require special behaviors.
function setLinks()
    {
    if (document.getElementsByTagName)
        {
        var theLinks = document.getElementsByTagName("a");

        for (var i = 0; i < theLinks.length; i++)
            {
            // Links to external sites.
            if (theLinks[i].getAttribute)
                {
                var theClass = theLinks[i].getAttribute('class');

                // Begin compensating for IE problems.
                var theClassName = theLinks[i].getAttribute("className");
                if (theClass == null) { theClass = theClassName; }
                // End compensating for IE problems.
                
                if (theClass == "external")
                    {
                    theLinks[i].onclick = function() { externalWindow(this.href); return false; }

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



////// Call all the functions used to enhance every page.
addToPage(setLinks);