﻿addLoadEvent(doVisibilityRequests);
addLoadEvent(stripeTables);

function addLoadEvent(newFunc) {
    var currentOnload = window.onload;
    if (typeof window.onload != "function")
        window.onload = newFunc;
    else 
        window.onload = function() {
            currentOnload();
            newFunc();
        }
}

function getNextElement(node) {
    var sibling = node.nextSibling;
    if (!sibling)
        return null;
    else if (sibling.nodeType == 1)
        return sibling;
    else
        return getNextElement(sibling);
}

function getElementsByClassName(node, classname) {
    if (node.getElementsByClassName) 
        return node.getElementsByClassName(classname);
    else {
        var results = new Array();
        var elems = node.getElementsByTagName("*");
        for (var i=0; i<elems.length; i++)
            if (elems[i].className.indexOf(classname) != -1)
                results[results.length] = elems[i];
        return results;
    }
}

function addClass(element, newClass) {
    if (element.nodeType != 1 || !newClass || typeof newClass != "string" || newClass == "") return false;
    var elementClass = element.className;
    element.className = (elementClass ? elementClass + " " : "") + newClass;
}

// Add an 'EvenRow' class to every other row in the body of each table in the page.
function stripeTables() {
    if (!document.getElementsByTagName) return false;
    var tables = document.getElementsByTagName("tbody");
    for (var i=0; i<tables.length; i++) {
        var rows = tables[i].getElementsByTagName("tr");
        for (var j=1; j<rows.length; j+=2)
            addClass(rows[j], "EvenRow");
    }
}

/* The purpose of this function is to show or hide various controls *if javascript is enabled*.
 * This enables us to have a set of default controls (with a class of 'jsMakeInvisible') that will
 * work without javascript but may not be pretty, and an alternative set of controls (with a class
 * of 'jsMakeVisible') that only work if javascript is enabled.*/
function doVisibilityRequests() {
    var makeInvisibles = getElementsByClassName(document, "jsMakeInvisible");
    for (var i=0; i<makeInvisibles.length; i++)
        makeInvisibles[i].style.display = "none";
    var makeVisibles = getElementsByClassName(document, "jsMakeVisible");
    for (var i=0; i<makeVisibles.length; i++)
        makeVisibles[i].style.display = "inline";    
}
