
function unescapeAndReplace(str)
{
    var val = str.replace(/\+/g, " ");
    return unescape(val);
}


////////////////////////////////////////////////////////////////////////////////
// The code below for ETOC is much like that of the Curriculum
// Standards page.
////////////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////////////////////
//
// This function toggles the +/- tree for Publication List.
//
// Typically executed from a <div> element (<div class="std" id="dALC-447301">).
// The <div> id has a 'd' appended to the <li> parent element id.  The
// <li> should be toggled as well as the first child <ul> with a previous
// className.  <ul> elements that should not be toggled will have no previous
// className.
//
// <li id="ALC-447301" class="open/close">
// 	<div class="std" id="dALC-447301">
// 	<ul class="open/close">
//
////////////////////////////////////////////////////////////////////////////////
function toggleTableOfContentsList()
{
    var id = this.id.substring(1);
    var element = document.getElementById(id);
    if ( element == null )
    {
        return;
    }
    if (element.className == null || element.className == "" || element.className == "close")
    {
        element.className = "open";
    } else
    {
        element.className = "close";
    }

    var foundChild = false;
    var uls = element.getElementsByTagName("ul");
    for (var index = 0; index < uls.length; index++)
    {
        var ul = uls[index];
        if (ul.className != null && ul.className != "" && !foundChild)
        {
            if (ul.className == "close")
            {
                ul.className = "open";
                foundChild = true;
            } else if (ul.className == "open")
            {
                ul.className = "close";
                foundChild = true;
            }
        }
    }
}

////////////////////////////////////////////////////////////////////////////////
//
// This function initially sets the Publication List.
//
//
// The collapseAll method is added to the <a> elements with class 'collapseAll'.
//
// <select id="gradeSelect">
// 	<option value="6">Grade 6</option>
// 	...
//
// The togglePublicationList method is added to any <div> elements with className 'std'
// that have <ul> elements with className 'close'.  Furthermore, the <div> id
// is added with an appending 'd' to the parent <li> id.  Otherwise, parent <li>
// elements have a className 'leaf' added.  All <li> elements have className
// 'close' added.
//
// <ul id="standards">
// 	<li id="ALC-447301">
//              <div class="std" id="dALC-447301">
// 		<ul class="close">
// 	<li id="ALC-447302" class="leaf">
//              <div class="std" id="ALC-447301">
// 		<ul class="close">
//
////////////////////////////////////////////////////////////////////////////////
function setupTableOfContentsList()
{
    var tableOfContents = document.getElementById("tableOfContents");
    if(isValidObject(tableOfContents)) {
	    var mainUl = tableOfContents.getElementsByTagName("ul")[0];
	    var divs = mainUl.getElementsByTagName("div");
	    for (var i = 0; i < divs.length; i++)
	    {
	        var element = divs[i];
	        if (element.id != null && element.id != "")
	        {
	            element.onclick = toggleTableOfContentsList;
	        }
	    }
	}
}

function getVolumeList(event)
{
    var etocUrl = getElementById("tableOfContents-volumeList").href;
    
    var selectedVolumeIndex = document.etocSelector.selectedEtocId.selectedIndex;
    var volumeNumber = document.etocSelector.selectedEtocId.options[selectedVolumeIndex].text.split(" ")[0];
    
    var etocId = document.etocSelector.selectedEtocId.value;
    if (etocId.length > 0)
    etocUrl = etocUrl.replace('&etocId=', '&etocId=' + etocId)
            + "&isDownLoadOptionDisabled=false" + "&volume=" + volumeNumber;
    window.location.href = etocUrl.replace('&tabID=', '&tabID=' + CURRENT_TABID);
   cancelEvent(event);
   return false;
}


/***************************************** Table Of Contents Event Binding ******************************************/

function attachTableOfContentsEvents(){
	setupTableOfContentsList();
	//addClickEventForId("navigation", goBacktoPreviousPage);
    attachPopup("globalTools-infomark", openInfomarkWindow);
//	attachPopup("globalTools-print", openPrintWindow);
	attachPopup("docTools-print", openPrintWindow);
	attachPopup("docTools-citation", openGaleChildWindow);
	addClickEventForId("tableOfContents-volumeList", getVolumeList);
	addClickEventForId("iteratorBar-markDocument", markPageCheckBox);
}

addWindowLoadEvent(attachTableOfContentsEvents);
