﻿// Global Table of Content div collection
var toc_div_ids = new Array
var i = 0
var toc_div_ids2 = new Array
var j = 0
var collapseAltText = 'Collapse';   // The Localized version of this variable are set by the Transform
var expandAltText = 'Expand';       // The Localized version of this variable are set by the Transform

img_expand = new Image();                       
img_expand.src = "images/nav_expand.gif";

img_collapse = new Image();                       
img_collapse.src = "images/nav_collapse.gif";

function ToggleDisplay(divId) 
{
    ToggleDisplay(divId, '');
}

function ToggleDisplay(divId, classToAppend) 
{
    var ctl = document.getElementById(divId)

    if (ctl.style.display == 'none') 
    {
        ExpandCollapse(divId, 'expand', classToAppend);
    } 
    else 
    {
        ExpandCollapse(divId, 'collapse', classToAppend);
    }
}
  
function ExpandCollapse(divId, newDisplay) 
{
    ExpandCollapse(divId, newDisplay, '');
}

function ExpandCollapse(divId, newDisplay, classToAppend) 
{
    var ctl = document.getElementById( divId )

    if (newDisplay == 'collapse') 
    {
        ctl.style.display = 'none';
        if (document.images) 
        {
            document.getElementById('ImgToc_' + divId).src = img_expand.src;
            document.getElementById('ImgToc_' + divId).setAttribute("alt", expandAltText);
        } 
        // If the class to be appended is not null or empty, then append it to the root expand/collapse node
        if(classToAppend != null && classToAppend != "")
        {
            var rootCtl = document.getElementById(divId + 'Root');
            rootCtl.className = AddClassName(rootCtl.className, classToAppend);
        }
    } 
    else 
    {
        ctl.style.display = 'block';
        if (document.images) 
        {
            document.getElementById('ImgToc_' + divId).src = img_collapse.src;
            document.getElementById('ImgToc_' + divId).setAttribute("alt", collapseAltText);
        }  
        // If the class to be appended is not null or empty, then remove it from the root expand/collapse node
        if(classToAppend != null && classToAppend != "")
        {
            var rootCtl = document.getElementById(divId + 'Root');
            rootCtl.className = RemoveClassName(rootCtl.className, classToAppend);
        }                    
    }
}

// collapse the Table of Content
function ExpandCollapseAll(newDisplay) 
{
    ExpandCollapseAll(newDisplay, '');
}

// collapse the Table of Content
function ExpandCollapseAll(newDisplay, classToAppend) 
{
    for (var ctr = 0; ctr < toc_div_ids.length; ctr++) 
    {
        var curAnchor = document.getElementById('a_' + toc_div_ids[ctr])
        if (curAnchor != 'undefined') 
        {
            ExpandCollapse(toc_div_ids[ctr], newDisplay, classToAppend)
        }
    }
}

function ExpandCollapseAll2(newDisplay) 
{
    for (var ctr = 0; ctr < toc_div_ids2.length; ctr++) 
    {
        var curAnchor = document.getElementById('a_' + toc_div_ids2[ctr])
        if (curAnchor != 'undefined') 
        {
            ExpandCollapse( toc_div_ids2[ctr], newDisplay )
        }
    }
}

///**********************************************************************************
/// <name>    HighlightMeetingCells </name>
/// <summary> This function highlights a meeting cell based on the specified date. 
///           Once all the cells are highlighted, the page anchors to the first
///           occurrence of the highlighted item.</summary>
/// <param name="dateToHighlight">Date based on which to highlight the specific
///           cell. The date must be in yyyy-mm-dd format.</param>
///**********************************************************************************
function HighlightMeetingCells(dateToHighlight)
{
    HighlightMatchingElement(dateToHighlight, "td", "DT", "MeetingTableCellHighlighted", true);
}

///**********************************************************************************
/// <name>    HighlightMatchingElement </name>
/// <summary> This function highlights a given element with a specified date 
///           prefix. This is used within the meeting table to highlight based on the
///           specified meeting date. Once the item is highlighted, there is an 
///           option to anchor to the first occurrence of the highlighting.</summary>
/// <param name="dateToHighlight">Date based on which to highlight the specific
///           element. The date must be in yyyy-mm-dd format.</param>
/// <param name="elementTag">The element tag where to search for the specific
///           id pattern (i.e. "td").</param>
/// <param name="elementIdPrefix">Element Id prefix before the appended date.
///           For example if the specified date is 2007-07-07 and the prefix is
///           DT then the search pattern will be DT20070707.</param>
/// <param name="highlightClassName">The class name which is to be used to highlight
///           the found element(s).</param>
/// <param name="anchorToFirstItem">Boolean flag that indicates whether to anchor to
///           the first occurrence of the highlighted element.</param>
///**********************************************************************************
function HighlightMatchingElement(dateToHighlight, elementTag, elementIdPrefix, highlightClassName, anchorToFirstItem)
{
    var splitDate = dateToHighlight.split("-");
    var tableCells = document.getElementsByTagName(elementTag);   // Get all the table cell elements
    var searchIDPrefix = elementIdPrefix + splitDate[0] + splitDate[1] + splitDate[2];   //i.e. DT20071214
    var firstIdFound = ""; 
    
    // Traverse through the table cell elements
    for(var i = 0; i < tableCells.length; i++)
    {
        // Get the Cell Id
        cellId = tableCells[i].getAttribute("id");
        
        // If the cell matches the search id prefix, then append the highlight class name
        if(cellId != null && cellId.search(searchIDPrefix) == 0)
        {   
            // Append the highlighting class name.
            currentClassName = tableCells[i].className;
            tableCells[i].className = AddClassName(currentClassName, highlightClassName);
             
            // Find the first id to anchor to
            if(firstIdFound == "")
                firstIdFound = cellId;
        }
        // If the class name is exactly the same as the replace class name, then clear it
        else if(tableCells[i].className == highlightClassName)
            tableCells[i].className = "";
            
        // Otherwise if the class name contains the replace style name, then remove it
        else if(tableCells[i].className != null && tableCells[i].className != "" && tableCells[i].className.search(highlightClassName) >= 0)
        {
            currentClassName = RemoveClassName(tableCells[i].className, highlightClassName);
            tableCells[i].className = currentClassName;
        }
    }
    
    // Anchor to the first highlighted element (if any)
    if(anchorToFirstItem && firstIdFound != null && firstIdFound != "")
        window.location.hash = firstIdFound;
}

function AppendStyleClassToElement(elementId, styleClassName)
{
    var element = document.getElementById(elementId);
    var currentClassName =  element.className;
    element.className = AddClassName(currentClassName, styleClassName);
}

///**********************************************************************************
/// <name>    AddClassName </name>
/// <summary> This function adds a specified class to the given class name.</summary>
/// <param name="currentClassName">Current Class Name to which the given
///           class is to be added.</param>
/// <param name="classNameToRemove">The class that is to be added.</param>
///**********************************************************************************
function AddClassName(currentClassName, classNameToAdd)
{        
    // Make sure the class name to add doesn't already exsist
    if(currentClassName != null && currentClassName != "" && currentClassName != classNameToAdd)
    {   
        // Make sure that the class name is not contained within the exsisting class name.
        if(currentClassName.search(classNameToAdd + ' ') < 0 && currentClassName.search(' ' + classNameToAdd) < 0)
            currentClassName = currentClassName + " " + classNameToAdd;    
    }
    else
        currentClassName = classNameToAdd; 
        
    return currentClassName;
}

///**********************************************************************************
/// <name>    RemoveClassName </name>
/// <summary> This function removes a specified class from the given class name.</summary>
/// <param name="currentClassName">Current Class Name from which the given
///           class is to be removed.</param>
/// <param name="classNameToRemove">The class that is to be removed.</param>
///**********************************************************************************
function RemoveClassName(currentClassName, classNameToRemove)
{   
    // If the class name is exactly the same as the replace class name, then clear it
    if(currentClassName == classNameToRemove)
        currentClassName = "";
            
    // Otherwise if the class name contains the replace style name, then remove it
    else if(currentClassName != null && currentClassName != "" && currentClassName.search(classNameToRemove) >= 0)
    {
        var classNamePosition = currentClassName.search(classNameToRemove);
        var lastReplaceClassCharPosition = classNamePosition + (classNameToRemove.length - 1);

        // If the last character of the replace class name is at the end of the string, then simply remove it. (also make sure that the preceeding character is a space)
        if(lastReplaceClassCharPosition == (currentClassName.length - 1) && classNamePosition > 0 && currentClassName.charAt(classNamePosition - 1) == " ")
        {
            currentClassName = currentClassName.substring(0, classNamePosition - 1);
        }
        // Otherwise if it's at the beginning and the following character is a space, 
        else if(classNamePosition == 0 && currentClassName.charAt(lastReplaceClassCharPosition + 1) == " ") 
        {
            // Make sure that there is another style preceeding the class name to be remove
            if((currentClassName.length - 1) > lastReplaceClassCharPosition)
                currentClassName = currentClassName.slice(lastReplaceClassCharPosition + 2);
            else
                currentClassName = "";
        }
        // If its in the middle, and the preceeding and following character is a space, then remove it and keep the others
        else if((classNamePosition > 0 && currentClassName.charAt(classNamePosition - 1) == " ") && currentClassName.charAt(lastReplaceClassCharPosition + 1) == " ")
        {
            currentClassNameP1 = currentClassName.substring(0, classNamePosition);
            
            // Make sure that there is another style preceeding the class name to be remove
            if((currentClassName.length - 1) > lastReplaceClassCharPosition)
                currentClassName = currentClassNameP1 + currentClassName.slice(lastReplaceClassCharPosition + 2);
            else
                currentClassName = currentClassNameP1;
        }
    }
    
    return currentClassName;
}

