//
// array to contain the tab ids. Needed to know the
// tabs to be reset as well as the tab to be set to
// selected.
//
var tabIds = new Array();

//
// the selected and unselected class names. These can
// be different for different pages. For example, the
// top main tabs have different tab names from those
// found in the left subnav pages.
//
var selectedClassname = "";
var unselectedClassname = "";

function setSubNavTab(){
    var id = window.parent.subNavTabId;

    if ( id ) { selectTab(id); }
}

function setNavTab(){
    var id = window.parent.navTabId;

    if ( id ) { selectTab(id); }
}

//
// select a tab, deselecting all others in the array.
//
function selectTab(id){
    //
    // number of tabs in the array.
    //
    var len = tabIds.length;

    //
    // spin through the array, setting each tab found,
    // using the id passed in to determine if the
    // tab in question is the selected one.
    //
    for( var i = 0; i < len; i++) {
        var name = tabIds[i];
        setTab(name,  (name == id));
    }
    
}

//
// set a specific tab to be either selected or unselected.
//
function setTab(id, selected){
    //
    // set the class name this tab should be set to, depending
    // on the selected flag.
    //
    var cName = selected?selectedClassname:unselectedClassname;

    //
    // get the element identified by the id.
    //
    var el = document.getElementById(id);

    //
    // if an element is found, set it's classname.
    //
    if ( el ) { el.className = cName; }
}

//
// Initialize the tabIds array with the ids associated with the tabs.
// Also set the selected and unselected class names.
//
function tabselInit(scn, ucn){
    selectedClassname = scn;
    unselectedClassname = ucn;


    // get all TD elements on this page. They are the ones to be altered.
    var tds = document.getElementsByTagName("td");

    // save the number of elements - this will speed up the for-loop.
    var len = tds.length;

    // set the regular expression to be used to find the TDs containing a tab.
    var re = /.*Tab$/;

    // loop through each element found.
    for(var i = 0; i < len; i++) {

        // get the element from the list.
        var el = tds.item(i);

        // if the element is accessible...
        if ( el ) {

            // get the id of the element.
            var elId = el.id;

            // if the id could be found...
            if ( elId ) {

                // if the id ends in Tab...
                if ( elId.search(re) != -1 ) {

                    // add the id to the array of tab ids.
                    tabIds.push(elId);
                }
            }
        }
    }
}

//
// function called by all pages to set the left subnav page and the 
// tab to be set on the top nav and left subnav pages.
//
function subnavLoadHandler(subnav, navId, subNavId) {
    //
    // set the subnav page.
    //
    window.parent.subNavFrame.document.location = subnav;

    //
    // set the selected subnav tab.
    //
    //setTimeout("window.parent.subNavFrame.selectTab('" + subNavId + "')", 200); 
    window.parent.subNavTabId = subNavId;

    //
    // set the selected top nav tab
    //
    //setTimeout("window.parent.navFrame.selectTab('" +    navId +    "')", 200); 
    window.parent.navTabId = navId;
    //
    // refresh the nav page, just to be safe.
    //
    window.parent.navFrame.location.reload();
}
