//********************************************************
// ParseCourseDescription.js
// Version 2 9/29/01
// 3 levels of navigation working 
// Version 1.2
// updated 8/26/01
// added gSectionFirstSubSectionIndex array
// changed debug printout to print in tables
// updated 4/29/01
// updated to parse 4 levels of organization: Chapters:Sections:Subsections:Pages
// Version 1.1
// updated 4/25/01
// updated to use relative nav instead of site relative nav
//********************************************************

// This file contains

// Global variables
// function ParseCourseDescription() - reads the global variable from the file
//                                     courseDescription and creates the 
//									   global variables that are used for nav
//									   in the whole site
// function DebugCourseDescription() - will print the results of ParseCourseDescription

// variable and function naming standard
  // global variable  - gClassnamePropertyName
  // local variable   - lowerCase
  // function         - StartsWithUpperCase


//********************************************************
// Global Variables
//********************************************************

  // Chapter Variables
     var gChapterName         = new Array ()
	 var gChapterURL          = new Array ()
	 var gChapterNoSections   = new Array ()
	 var gChapterFirstSectionIndex = new Array ()
	 var gChapterCurrentIndex = 0

  // Section Variables
     var gSectionName          = new Array ()
	 var gSectionURL           = new Array ()
	 var gSectionNoSubSections = new Array ()
	 var gSectionFirstSubSectionIndex = new Array ()
	 var gSectionCurrentIndex  = 0

  // SubSection Variables
     var gSubSectionName         = new Array ()
	 var gSubSectionURL          = new Array ()
	 var gSubSectionStartAtPage  = new Array ()
	 var gSubSectionNoPages      = new Array ()
	 var gSubSectionCurrentIndex = 0

  // Page Variables
     var gPageName         = new Array ()
     var gPageURL          = new Array ()
	 var gPageCurrentIndex = 0
	 
  // Relative Path from current page to root level of course
	 var gRelativePath  = ""
	 var hrefString 
	 var pageString 


//********************************************************
// function ParseCourseDescription
//********************************************************

function ParseCourseDescription()
{
  //** Declare Local Variables

    // Chapter Variables
       var lastChapter  = ""
	   var chapterIndex = 0

    // Section Variables
       var lastSection = ""
       var noSections  = 0
	   var sectionIndex = 0

	// SubSection Variables
       var lastSubSection = ""
       var noSubSections  = 0
	   var subSectionIndex = 0
	   
    // Page Variables
       var pageIndex = 0
	   var noPages = 0
	   
   // Relative Path variable
	   var folderNames = ""

    // Counter Variables
	   var i = 0

  //** Parse Course Description

    // Loop through the courseDescription array one "row" at a time

    for(i=0; i< gCourseDescription.length; i+=5)
	{
      // Parse Page Information
	      pageIndex ++
		  gPageName[pageIndex] = gCourseDescription[i+3]
		  gPageURL[pageIndex] = gCourseDescription[i+4]

       // Parse Chapter Information
	      if(gCourseDescription[i] != lastChapter)
		  {
		    lastChapter = gCourseDescription[i]
		    chapterIndex ++
			// set lastSection to null so that it triggers a new section
			lastSection = ""
			// store next section index
			gChapterFirstSectionIndex[chapterIndex] = sectionIndex + 1
			gChapterName[chapterIndex] = gCourseDescription[i]
			gChapterURL[chapterIndex] = gCourseDescription[i+4]

            // if not the first chapter, record related section info
            if(chapterIndex !=1)
			{
			  gChapterNoSections[chapterIndex-1] = noSections
			  noSections = 0
			} // end if not first chapter
		  }

       // Parse Section Information
	      if(gCourseDescription[i+1] != lastSection)
		  {
		    lastSection = gCourseDescription[i+1]
			sectionIndex ++
			noSections ++
			// set lastSubSection to null so that it triggers a new subsection
			lastSubSection = ""
			gSectionFirstSubSectionIndex[sectionIndex] = subSectionIndex + 1
			gSectionName[sectionIndex] = gCourseDescription[i+1]
			gSectionURL[sectionIndex] = gCourseDescription[i+4]

			// if not the first section, record related subsection info
            if(sectionIndex !=1)
			{
			  gSectionNoSubSections[sectionIndex-1] = noSubSections
			  noSubSections = 0
			}
		  }

	   // Parse SubSection Information
	      if(gCourseDescription[i+2] != lastSubSection)
		  {
		    lastSubSection = gCourseDescription[i+2]
			subSectionIndex ++
			noSubSections ++
			gSubSectionName[subSectionIndex] = gCourseDescription[i+2]
			gSubSectionURL[subSectionIndex] = gCourseDescription[i+4]
			gSubSectionStartAtPage[subSectionIndex] = pageIndex

			// if not the first section, record related page info
            if(subSectionIndex !=1)
			{
			   noPages = pageIndex - gSubSectionStartAtPage[subSectionIndex -1]
               gSubSectionNoPages[subSectionIndex - 1] = noPages
			}
		  }
		  
       // Find the Current Chapter, Section, SubSection, and Page

		  hrefString = top.location.href.toLowerCase()
		  pageString = gCourseDescription[i+4].toLowerCase()
		  
		  //check to make sure that the browser assumes the name is default.htm
		  
		  if(hrefString.indexOf(".htm") < 1)
		  {
		    hrefString = hrefString + "default.htm"
		  }

          if (hrefString.indexOf(pageString) != -1)
          {
	          gChapterCurrentIndex    = chapterIndex
			  gSectionCurrentIndex    = sectionIndex
			  gSubSectionCurrentIndex = subSectionIndex
			  gPageCurrentIndex       = pageIndex
			  
			  //calculate the relative path from the current page to the root folder
			  folderNames = gCourseDescription[i+4].split("/")
			  gRelativePath = ""
			  if (folderNames.length > 0)
			  {
			    for (k=1; k<folderNames.length; k++)
				{
				  gRelativePath += "../"
				}
			  }
	       }


	} //end for i
	
	// finish calculations for number of sections and no of pages for last chapter and section
	gChapterNoSections[chapterIndex] = noSections
	gSectionNoSubSections[sectionIndex] = noSubSections
	noPages = pageIndex - gSubSectionStartAtPage[subSectionIndex -1] - gSubSectionNoPages[subSectionIndex-1]+1
    gSubSectionNoPages[subSectionIndex] = noPages


}

function DebugCourseDescription()
{
   //debug print to see if parser is working correctly
     var i = 0
	 
   //Print Course Description Information
     document.writeln("Length of course description array : " +  gCourseDescription.length + "<br>")
	 document.writeln("Length of chapter description array : " +  gChapterName.length + "<br>")
	 document.writeln("Length of section description array : " +  gSectionName.length + "<br>")
	 document.writeln("Length of subsection description array : " +  gSubSectionName.length + "<br>")
	 document.writeln("Length of page description array : " +  gPageName.length + "<br>")
   
   //Print Course Description information
     document.writeln("<hr><P>Course Description: <br>")
	 document.writeln("<table border =1><tr><td>Chapter</td>")
	 document.writeln("<td>Section</td>")
	 document.writeln("<td>SubSection</td>")
	 document.writeln("<td>Page</td>")
	 document.writeln("<td>URL</td></tr>")
     for(i=0; i< gCourseDescription.length; i+=5)
     {
       document.writeln("<tr><td>" + gCourseDescription[i]+"</td>")
       document.writeln("<td>" + gCourseDescription[i+1]+"</td>")
	   document.writeln("<td>" + gCourseDescription[i+2]+"</td>")
	   document.writeln("<td>" + gCourseDescription[i+3]+"</td>")
       document.writeln("<td>" + gCourseDescription[i+4]+"</td></tr>")
     }
	 document.writeln("</table>")
	 
   //Print Chapter information
     document.writeln("<hr><P>List of all Chapters: <br>")
	 document.writeln("<table border =1><tr><td>Name</td>")
	 document.writeln("<td>URL</td>")
	 document.writeln("<td>Index of First Section</td>")
	 document.writeln("<td> No of Sections</td></tr>")
     for(i=1; i< gChapterName.length;i++)
     {
       document.writeln("<tr><td>" + gChapterName[i]+"</td>")
       document.writeln("<td>" + gChapterURL[i]+"</td>")
	   document.writeln("<td>" + gChapterFirstSectionIndex[i]+"</td>")
       document.writeln("<td>" + gChapterNoSections[i]+"</td></tr>")
     }
	 document.writeln("</table>")


   //Print Section information
     document.writeln("<hr><P>List of all sections: <br>")
	 document.writeln("<table border =1><tr><td>Name</td>")
	 document.writeln("<td>URL</td>")
	 document.writeln("<td>Index of First SubSection</td>")
	 document.writeln("<td>No of SubSections</td></tr>")

     for(i=1; i< gSectionName.length;i++)
     {
       document.writeln("<tr><td>" + gSectionName[i]+"</td>")
       document.writeln("<td>" + gSectionURL[i]+"</td>")
	   document.writeln("<td>" + gSectionFirstSubSectionIndex[i]+"</td>")
       document.writeln("<td>" + gSectionNoSubSections[i]+"</td></tr>")
     }
     document.writeln("</table>")

   //Print SubSection information
     document.writeln("<hr><P>List of all subsections: <br>")
	 document.writeln("<table border =1><tr><td>Name</td>")
	 document.writeln("<td>URL</td>")
	 document.writeln("<td>Index of First Page</td>")
	 document.writeln("<td> No of Pages</td></tr>")

     for(i=1; i< gSubSectionName.length;i++)
     {
       document.writeln("<tr><td>" + gSubSectionName[i]+"</td>")
       document.writeln("<td>" + gSubSectionURL[i]+"</td>")
       document.writeln("<td>" + gSubSectionStartAtPage[i]+"</td>")
       document.writeln("<td>" + gSubSectionNoPages[i]+"</td></tr>")
     }
     document.writeln("</table>")
	 
   //Print Page information
     document.writeln("<hr><P>List of all Pages: <br>")
	 document.writeln("<table border =1><tr><td>Name</td>")
	 document.writeln("<td> URL</td></tr>")

     for(i=1; i< gPageName.length;i++)
     {
       document.writeln("<tr><td>" + gPageName[i]+"</td>")
       document.writeln("<td>" + gPageURL[i]+"</td></tr>")
     }
     document.writeln("</table>")


   //Print Current settings
     document.writeln("<hr><P>Current Indexs: <br>")
     document.writeln("Chapter: " + gChapterCurrentIndex + " : " + gChapterName[gChapterCurrentIndex] + "<br>")
     document.writeln("Section: " + gSectionCurrentIndex + " : " + gSectionName[gSectionCurrentIndex] + "<br>")
	 document.writeln("SubSection: " + gSubSectionCurrentIndex + " : " + gSubSectionName[gSubSectionCurrentIndex] + "<br>")
	 document.writeln("Page: " + gPageCurrentIndex + " : " + gPageName[gPageCurrentIndex] + "<br>")
	 document.writeln("URL: " + gPageURL[gPageCurrentIndex] + "<br>")
	 document.writeln("Relative Path to course root: " + gRelativePath + "<br>")
     document.writeln("</P>")

}