function addEntry( sectionName, rubricName, pageNr, pageHTML )
{
  var pageArray;

  if ( this.sectionTable[sectionName] )
  {
    pageArray = this.sectionTable[sectionName];
  }
  else
  {
    pageArray = new Array();
    this.sectionTable[sectionName] = pageArray;
  }

  // set first section
  if( !this.sectionName )
    this.sectionName = sectionName;

  infoArray = new Array();
  infoArray[0] = pageHTML;
  infoArray[1] = rubricName;

  pageArray[pageNr] = infoArray;
}

function getEntry( sectionName, pageNr )
{
  var pageArray = this.sectionTable[sectionName];
  var infoArray = pageArray[pageNr]
  if( infoArray )
    return infoArray[0];
  else
    return false;
}

function getRubric( sectionName, pageNr )
{
  var pageArray = this.sectionTable[sectionName];
  var infoArray = pageArray[pageNr]
  return infoArray[1];
}

function getPath( sectionName, pageNr, typePath, extension, no_back )
{
//  alert('getPath ['+sectionName+']['+pageNr+']');
  var pageArray = this.sectionTable[sectionName];
  path = this.entry( sectionName, pageNr );
  if( path )
  {
    rExp = /%PATH%/g;
    path = path.replace(rExp, typePath);
    path += extension;

    this.sectionName = sectionName;
    this.pageNr = parseInt(pageNr);
  }
  else if ( !no_back && (pageNr > 0) & (pageNr < pageArray.length) )
  {
    return this.path( sectionName, pageNr-1, typePath, extension );
  }

  return path;
}

function getFirstPath( typePath, extension )
{
  var path;

  sectionName = this.firstSection();
  firstPage = this.firstPage(sectionName);

  return this.path(sectionName, firstPage, typePath, extension);
}

function getFirstSection()
{
  for( var section in this.sectionTable )
  {
    return section;
  }

  return false;
}

function getFirstPage( sectionName )
{
  var pageArray = this.sectionTable[sectionName];
  var i = 0;

  while ( i < pageArray.length )
  {
    if (pageArray[i])
      return i;

    i++;
  }

  return false;
}

function getNextPath( typePath, extension, sectionName, pageNr )
{
  var path;

  if ( !sectionName )
    sectionName = this.sectionName;
  if ( isNaN(pageNr) )
    pageNr = this.pageNr;

//  alert('getNextPath ['+sectionName+']['+pageNr+']');

  pageNr = pageNr + 1;
  var pageArray = this.sectionTable[sectionName];

  if ( pageNr < pageArray.length )
  {
    path = this.path( sectionName, pageNr, typePath, extension, true );
    if ( !path )
      return this.nextPath(typePath, extension, sectionName, pageNr);
    else
    {
      this.sectionName = sectionName;
      this.pageNr = pageNr;
      return path;
    }
  }
  else
  {
    nextSection = this.nextSection( sectionName );
    if ( nextSection )
    {
      sectionName = nextSection;
      pageNr = 0;
      return this.nextPath(typePath, extension, sectionName, pageNr);
    }
  }
}

function getNextSection( sectionName )
{
  var returnSection = false;
  for( var section in this.sectionTable )
  {
    if ( returnSection )
      return section;
    else if ( section == sectionName )
      returnSection = true;
  }

  return false;
}

function getPreviousPath( typePath, extension, sectionName, pageNr )
{
  var path;

  if ( !sectionName )
    sectionName = this.sectionName;
  if ( isNaN(pageNr) )
    pageNr = this.pageNr;

//  alert('getPreviousPath ['+sectionName+']['+pageNr+']');

  pageNr = pageNr - 1;
  var pageArray = this.sectionTable[sectionName];

  if ( pageNr > 0 )
  {
    path = this.path( sectionName, pageNr, typePath, extension, true );
    if ( !path )
      return this.previousPath(typePath, extension, sectionName, pageNr);
    else
    {
      this.sectionName = sectionName;
      this.pageNr = pageNr;
      return path;
    }
  }
  else
  {
    previousSection = this.previousSection( sectionName );
    if ( previousSection )
    {
      sectionName = previousSection;
      pageNr = this.sectionTable[sectionName].length;
      return this.previousPath(typePath, extension, sectionName, pageNr);
    }
  }
}

function getPreviousSection( sectionName )
{
  var previousSection;
  for( var section in this.sectionTable )
  {
    if ( section == sectionName )
      return previousSection;

    previousSection = section;
  }

  return false;
}

function getLastPath( typePath, extension )
{
  var path;

  sectionName = this.lastSection();
  lastPage = this.lastPage(sectionName);

  return this.path(sectionName, lastPage, typePath, extension);
}

function getLastSection()
{
  var resultSection;
  for( var section in this.sectionTable )
  {
    resultSection = section;
  }

  return (resultSection) ? resultSection : false;
}

function getLastPage( sectionName )
{
  var pageArray = this.sectionTable[sectionName];
  var lastPageNr;
  var i = 0;

  while ( i < pageArray.length )
  {
    if (pageArray[i])
      lastPageNr = i;

    i++;
  }

  return (lastPageNr) ? lastPageNr : false;
}

function printTables( msg )
{
  var pageArray;

  for( var n in this.sectionTable )
  {
    if( msg )
      alert(n);
    else
      document.writeln("<h5>" + n + "</h5>");

    pageArray = this.sectionTable[n];

    for( i=1; i<pageArray.length; i++ )
    {
      if( msg )
        alert("page " + i + " : " + pageArray[i]);
      else
        document.writeln("page " + i + " : " + pageArray[i] + "<br>");
    }
  }
}

function getSectionRange( sectionName )
{
  var range = "";
  var pageArray = this.sectionTable[sectionName];

  var start;
  var end ;

  for( i=1; i<pageArray.length; i++ )
  {
    if( !start && pageArray[i] )
      start = i;
  }

  end = pageArray.length - 1;

  if ( (start > 0) & (end > 0) )
  {
    if (start == end)
      return start;
    else
      return start + ".." + end;
  }
  else
    return "ERROR";
}

function getSectionSize( sectionName )
{
  var pageArray = this.sectionTable[sectionName];
  return pageArray.length - 1;
}

function sections()
{
  // hashtable initialization
  this.sectionTable = {};
  this.sectionName;
  this.pageNr = 1;

  // method declarations
  this.entry = getEntry;
  this.rubric = getRubric;
  this.add = addEntry;
  this.print = printTables;
  this.path = getPath;
  this.firstPath = getFirstPath;
  this.firstSection = getFirstSection;
  this.firstPage = getFirstPage;
  this.nextPath = getNextPath;
  this.nextSection = getNextSection;
  this.previousPath = getPreviousPath;
  this.previousSection = getPreviousSection;
  this.lastPath = getLastPath;
  this.lastSection = getLastSection;
  this.lastPage = getLastPage;
  this.sectionRange = getSectionRange;
  this.sectionSize = getSectionSize;
}