//        Tab Functions
//         To add tabs:
//        Copy this into a page using Master Pages:
//        <asp:Content ID="ContentScripts" ContentPlaceHolderID="Scripts" runat="server">
//            <script language="javascript" src="$[IncludeRoot]/tabFunctions.js"></script>
//            <script type="text/javascript">window.onload = function() {BuildTabs();}</script> 
//        </asp:Content>
//        or just add this to a page not using Master Pages:
//        <script language="javascript" src="$[IncludeRoot]/tabFunctions.js"></script>
//        <script type="text/javascript">window.onload = function() {BuildTabs();}</script> 

//        Usage:

//        Create a tab across the top using the following structure
//        and mark the one that is the default starting tab as class="active"

//        <div class="tabs" id="tabs">
//            <ul>
//                <li class="active"><a id="Group">By Group</a></li>
//                <li class="pipe">||</li>
//                <li><a id="Alpha">View Alphabetical Listing</a></li>
//            </ul>
//        </div>

//        Within the main content area of the page, surround the data for each of the tabs
//        with the following structure.

//        <div id="Group_Tab">
//        CONTENT HERE...
//        </div>
//        <div id="Alpha_Tab">
//        CONTENT HERE...
//        </div>

//        Created by Ben Pomeranz, 9/10/2008

function isActive(aTag)
{
    var parentNode = aTag.parentNode;
    if (parentNode.className == "active")
    {
        return true;
    } 
    return false;
}
function setActive(aTag)
{
    var parentNode = aTag.parentNode;
    parentNode.className = "active";
} 
function unSetActive(aTag)
{
    var liParent = aTag.parentNode;
    var ulParent = liParent.parentNode;
    var currentActiveTab = "";
    for(var i = 0; i < ulParent.childNodes.length; i++)
    {
        if (ulParent.childNodes[i].className != null && ulParent.childNodes[i].className == "active")
        {
            for(var a = 0; a < ulParent.childNodes[i].childNodes.length;a++)
            {
                if (ulParent.childNodes[i].childNodes[a].nodeName.toLowerCase() == "a")
                {
                    currentActiveTab =  ulParent.childNodes[i].childNodes[a].getAttribute("id");
                    break; 
                }  
            }
            ulParent.childNodes[i].className = "";
            break; 
        }
    }
    return currentActiveTab;
}
function tabRollover(aTag)
{
    if (!isActive(aTag))
    {
        var statusString = aTag.innerText;
        aTag.className += " rollover"; 
        window.status = statusString;
    }
    return false;
}
function resetStatus(aTag)
{
    if (aTag != null) { aTag.className = aTag.className.replace(" rollover", ""); }
    window.status = ""; 
    return false;
}
function openTab(aTag)
{
    if (!isActive(aTag))
    {
        var currentTab = "";
        currentTab = unSetActive(aTag);
        if (currentTab.length > 0) {
            currentTab += "_Tab";
            document.getElementById(currentTab).style.display = "none";
        }  
        var tabName = aTag.getAttribute("id") + "_Tab";
        document.getElementById(tabName).style.display = "block";
        setActive(aTag); 
        resetStatus();
    }
    return false;
}
function setPrintTabs()
{
    var tabHeaderDiv = document.getElementById("tabs");
    var ulParent;
    for(var c = 0; c < tabHeaderDiv.childNodes.length; c++)
    {
        if (tabHeaderDiv.childNodes[c].nodeName.toLowerCase() == "ul")
        {
            ulParent = tabHeaderDiv.childNodes[c];
            break;
        }  
    }
    if (ulParent != null) 
    {
        var liNodes = ulParent.childNodes;
        for(var i = 0; i < liNodes.length; i++)
        {
            if (liNodes[i].nodeName.toLowerCase() == "li") {
                if (liNodes[i].className != "pipe")
                {
                    var aTag = null; 
                    for(var a = 0; a < liNodes[i].childNodes.length;a++)
                    {
                        if (liNodes[i].childNodes[a].nodeName.toLowerCase() == "a")
                        {
                            aTag =  liNodes[i].childNodes[a];
                            break; 
                        }  
                    }
                    if (aTag != null)
                    {
                        var idValue = aTag.getAttribute("id");
                        var tabDiv = document.getElementById(idValue + "_Tab");
                        if (tabDiv.className != null && tabDiv.className.indexOf("noprint") < 0) { 
                            tabDiv.style.display = "block"; 
                        } 
                    } 
                }
            } 
        }
    } 
}
function BuildTabs()
{
    if ((document.location.search.indexOf("print=true") >= 0) || (document.location.search.indexOf("print=pdf") >= 0))
    {
        setPrintTabs(); 
        return true; 
    } 
    var tabHeaderDiv = document.getElementById("tabs");
    var ulParent;
    for(var c = 0; c < tabHeaderDiv.childNodes.length; c++)
    {
        if (tabHeaderDiv.childNodes[c].nodeName.toLowerCase() == "ul")
        {
            ulParent = tabHeaderDiv.childNodes[c];
            break;
        }  
    }
    if (ulParent != null) 
    {  
        var liNodes = ulParent.childNodes;
        for(var i = 0; i < liNodes.length; i++)
        {
            if (liNodes[i].nodeName.toLowerCase() == "li") {
                if (liNodes[i].className != "pipe")
                {
                    var aTag = null; 
                    for(var a = 0; a < liNodes[i].childNodes.length;a++)
                    {
                        if (liNodes[i].childNodes[a].nodeName.toLowerCase() == "a")
                        {
                            aTag =  liNodes[i].childNodes[a];
                            break; 
                        }  
                    }
                    if (aTag != null)
                    {
                        var idValue = aTag.getAttribute("id");
                        aTag.onmouseover = function() { return tabRollover(this);}
                        aTag.onmouseout = function() { return resetStatus(this);}
                        aTag.onclick = function() { return openTab(this);}
                        if (!isActive(aTag))
                        {
                            var tabDiv = document.getElementById(idValue + "_Tab");
                            tabDiv.style.display = "none"; 
                        }
                    } 
                }
            } 
        }
    } 
}
// This function is used to add a non tab link to use a tab function.  
// Send in the ID of the tab link that you want to open and it will handle it.
function nonTabOpen(tabLinkID)
{
    var tabLink = document.getElementById(tabLinkID);
    if (tabLink != null)
    {
         openTab(tabLink);
    }
    return false; 
}

