﻿// Gets all child elements and creates an array of all loaded sections and articles withing them
function GetChildObjects(objParent, childTagName)
{
    var i, cnt = 0;
    var childObjects = new Array();
    for(i=0; i<objParent.childNodes.length; i++)
    {
        if(objParent.childNodes[i].nodeName==childTagName)
            childObjects[cnt++] = objParent.childNodes[i];
    }
    return childObjects;
}

var navigationIdList = new Array();
var sectionNavigationList = new Array();

var qsTopicId = getQueryString("topicId");
var qsarticleId = getQueryString("articleId");

var sectionId;
var iteratorId = 1;
var articleNumbers = 0;
var currentArticle;

do {
    var objUL = document.getElementById("section"+iteratorId);
    var childLiObjects = GetChildObjects(objUL, "LI");
    var nodeId;
    
    for(i=0; i<childLiObjects.length; i++)
    {
        nodeId = childLiObjects[i].id.replace("article", "-");
        navigationIdList[articleNumbers] = nodeId;
        articleNumbers ++;
    }
    iteratorId++;
}
while (document.getElementById("section"+iteratorId) != null)            


function expandSection(currentSection) {
    var i = 1;
    
    if(!currentSection){
        if(qsTopicId){
            sectionId = qsTopicId;
        } else {
            sectionId = 1;
        }
    } else {
        sectionId = currentSection;
        currentArticle = 1;
    }
    do {
        document.getElementById("section"+i).style.display = "none";
        document.getElementById("sectionTitle"+i).style.fontWeight = "normal";
        i++;
    }
    while (document.getElementById("section"+i) != null)
    
    document.getElementById("sectionTitle"+sectionId).style.fontWeight = "bold";
    document.getElementById("section"+sectionId).style.display = "block";
    
    document.getElementById("UCFaqTopicList_LbttnPreviousArticle").className = "previousDisabledLink";

    directSelect(sectionId, currentArticle);
}

if(qsarticleId){
    currentArticle = qsarticleId;
} else {
    currentArticle = 1;
}

expandSection();


function directSelect(sectionId, articleId) {
    var i = 1;
    var currentIdPosition = 0;
    sectionNavigationList = new Array();
    
    do {
        document.getElementById(sectionId + "article" + i).className = "";
        i++;
    }
    while (document.getElementById(sectionId + "article" + i) != null)

    //highlight selected node
    document.getElementById(sectionId + "article" + articleId).className = "selected";
    
    // builds sectionNavigationList array which stores ids for current section
    for(j=0; j<navigationIdList.length; j++) {
        sectionNavigationArray = navigationIdList[j].split("-");
        if(sectionNavigationArray[0] == sectionId){
            sectionNavigationList[currentIdPosition] = sectionNavigationArray[1];
            currentIdPosition++;
        }
    }
    
    for (j=0; j < sectionNavigationList.length; j++){
        if(articleId == sectionNavigationList[j]){
            currentArticle = j+1;
        }
    }
    
        document.getElementById("UCFaqTopicList_LbttnNextArticle").className = "nextLink";
        document.getElementById("UCFaqTopicList_LbttnPreviousArticle").className = "previousLink";
        
    if (currentArticle == 1){
        document.getElementById("UCFaqTopicList_LbttnPreviousArticle").className = "previousDisabledLink";
    }
    
    if (currentArticle == sectionNavigationList.length){
        document.getElementById("UCFaqTopicList_LbttnPreviousArticle").className = "previousLink";
        document.getElementById("UCFaqTopicList_LbttnNextArticle").className = "nextDisabledLink";
    }
    
}

function moveToNextArticle(){
    if (currentArticle < sectionNavigationList.length){
        currentArticle++;
    }
    
    var i = 1;
    do {
        document.getElementById(sectionId + "article" + i).className = "";
        i++;
    }
    while (document.getElementById(sectionId + "article" + i) != null)
    
    document.getElementById(sectionId + "article" + currentArticle).className = "selected";
    
    document.getElementById("UCFaqTopicList_LbttnPreviousArticle").className = "previousLink";
    
    if (currentArticle == sectionNavigationList.length){
        document.getElementById("UCFaqTopicList_LbttnNextArticle").className = "nextDisabledLink";
    }
}

function moveToPreviousArticle(){
    if (currentArticle > 1){
        currentArticle--;
    }
    
    if (currentArticle > 0){
        var i = 1;
        do {
            document.getElementById(sectionId + "article" + i).className = "";
            i++;
        }
        while (document.getElementById(sectionId + "article" + i) != null)
    }
    
    document.getElementById(sectionId + "article" + currentArticle).className = "selected";
    
    document.getElementById("UCFaqTopicList_LbttnNextArticle").className = "nextLink";
    
    if (currentArticle == 1){
        document.getElementById("UCFaqTopicList_LbttnPreviousArticle").className = "previousDisabledLink";
    }
}