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



//// 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);
                    }
                }
            }
        }
    else
        {
        return true;
        }
    }



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