// chapman.js
//
// This script contains all of the functions needed for rehosting Dr. Chapman's book into HTML
// and give it a little dynamic flexibility

// toc is the table of contents.  The contents of toc[i] is the page number
// of the first page of chapter i.  
var toc = new Array();
toc[0] = 0;
toc[1] = 1;
toc[2] = 21;
toc[3] = 31;
toc[4] = 49;
toc[5] = 67;
toc[6] = 83;
toc[7] = 99;
toc[8] = 113;
toc[9] = 129;
toc[10]= 153;
toc[11]= 175;
toc[12]= 185;
toc[13]= 193;
toc[14]= 219;
toc[15]= 247;
toc[16]= 267;
toc[17]= 283;
toc[18]= 301;
toc[19]= 321;
toc[20]= 331;
toc[21]= 351;
toc[22]= 371;
toc[23]= 385;
toc[24]= 393;
toc[25]= 411;
toc[26]= 415;

function pageToChapter ( pageNum ) {
  var i;
  i = 0;
  while ( toc[i] < pageNum && i < toc.length ) i++;
  if ( i== toc.length ) alert ("Internal S/W error in function pageToChapter"+
        "failed to find the chapter for page " + pageNum +
        "when called from " + window.location + "\nReferrer is "+
        document.referer );
  return i;
 
}

function statusPage ( pageNum ) {
  window.status = "going to page " + pageNum +
        " chapter: " + pageToChapter(pageNum);
  return false;
}

// This function directs the browser to the appropriate web page given a page number from the index
// For more information on the use of this function in an <A HREF> see the ORA Javascript book, 3rd ed., page 277
//
function gotoPage( page ) {
  var chapter;
  chapter = pageToChapter( page );
  chapter = ( chapter < 10 ? "ch0" + chapter : "ch" + chapter ) + ".html";
  alert ( "Going to " + chapter  );
  window.location = chapter;
  return false;         // false -> don't follow the link
}

//  if ( ( page >= 411 ) && ( page < 418 ) ) { window.location="ch15.html"; }
//  else if ( page >= 247 && page < 267 ) { window.location="ch16.html"; }
//  else { 
//    alert ( "Tried to go to page" + page + "and couldn't do it\n");
//  };
//}

function welcome() { window.status = "Welcome!"; }

