var selectedLetter;

function showHideAll(isShow)
{
    // pulls the array of ids for the elements on the page and loops through and shows or hides them then the show/hide text is toggled.
    var element;
    var showElement;
    var hideElement;
    
    for(var i=0; i<idArray.length; i++){
        element = document.getElementById(idArray[i]);
        if (element != null)
        {
            if (isShow) {
                element.style.display = '';
            } else {
                element.style.display = 'none'; 
            }
        }
    }
    showElement = document.getElementById('show');
    hideElement = document.getElementById('hide');
   
    if (hideElement != null)
    {
        if (isShow){
            hideElement.style.display = '';
        } else {
            hideElement.style.display = 'none';
        }
    }
    if (showElement != null)
    {
        if (isShow)
        {
            showElement.style.display = 'none';
        } else {
            showElement.style.display = '';
        }
    } 
}
function showAllContacts()
{
    if (selectedLetter != null){
        var selectedLetterElement = document.getElementById('sel_' + selectedLetter);
        if (selectedLetterElement != null) {
            selectedLetterElement.className = '';
        }
    }
    showHideAll(true);
    var viewAllElement = document.getElementById('viewAll');
    if (viewAllElement != null)
    {
    
        viewAllElement.className = 'ngoCategorySelected'; 
    }
}
function show(ID)
{
    // hides all existing items and then displays the specified item based on the ID
    showHideAll(false);
    var element = document.getElementById(ID);
  
    if (element == null) return;
    element.style.display = '';
}

function showFormatListItem(itemID)
{
    show(itemID);
    //handle special formatting here
}
function showFormatContact(letter)
{     
    // shows the contact for the specified letter and formats the selected letter appropriately. 
    show(letter.trim());
    var letterElement = document.getElementById('sel_' + letter);
    if (selectedLetter != null){
        var selectedLetterElement = document.getElementById('sel_' + selectedLetter);
        if (selectedLetterElement != null) {
            selectedLetterElement.className = '';
        }
    }
    if (letterElement != null) {
        letterElement.className = 'ngoCategorySelected';
        selectedLetter = letter;
    }
    var viewAllElement = document.getElementById('viewAll');
    if (viewAllElement != null)
    {
        viewAllElement.className = '';
    }
}
function trimString (str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

String.prototype.trim = trimString;

function toggleQuestion(id)
{
    // expands or collapses the question based on the state
    var ans = document.getElementById(id);
    if (ans == null) return;
   
    if (ans.style.display == '') ans.style.display = 'none';

    else ans.style.display = '';
}
function showOnLoad()
{
    var url = window.location.href;
    var pound = url.indexOf('#');
    if (pound > 0)
    {
        activeQuestion = url.substring(pound+2);
        toggleQuestion(activeQuestion);
    }

}