// Book navigator for Power Direct Marketing
// Bill Blinn 10-8-00
// -----------------------------------------
// Here's the deal
// Power Direct Marketing files all begin with "chp", which will be replaced
// with the actual file name prefix "pdm".
// The next 2 characters will be numbers for the chapter -- "01" thru "10".
// The next character (to improve readability by humans) is a dash "-".
// The next 2 characters are the section number -- "01" thru "21".
// The file names end with ".html".
// Example pdf01-02.html = section 2 of chapter 1.

// Goal: Each section will offer "previous" and "next" section, and
// "table of contents". We will not display "previous" for the
// first section in a chapter or "next" for the last section in a chapter.

// Minimums and maximums
var lowSection    = new Array (10);
var highSection   = new Array (10);
// The book has 10 chapters
// Minimum section is 1 for all chapters
lowSection[1]     =  1;
lowSection[2]     =  1;
lowSection[3]     =  1;
lowSection[4]     =  1;
lowSection[5]     =  1;
lowSection[6]     =  1;
lowSection[7]     =  1;
lowSection[8]     =  1;
lowSection[9]     =  1;
lowSection[10]    =  1;
// Maximum section varies by chapter
highSection[1]    =  7;
highSection[2]    =  6;
highSection[3]    = 12;
highSection[4]    =  7;
highSection[5]    = 21;
highSection[6]    = 17;
highSection[7]    = 13;
highSection[8]    =  4;
highSection[9]    = 11;
highSection[10]   =  9;

// Define variables & constants
var booktitle = "Power Direct Marketing";
// Next and previous chapters
var previousSection = "";
var nextSection     = "";
// This chapter and section
var thisChapter   = "";
var thisSection   = "";
// Lowest and highest sections for this chapter
var lowest        = "";
var highest       = "";
// Flags (Lookie! They're in plain English!)
var advance       = "yes";
var retreat       = "yes";
// File prefix
var chp           = "pdm";

// Here's where the heavy lifting happens
function whereWeAre() {
// What is THIS chapter and section?
	thisChapter = document.URL.slice(document.URL.length-10, document.URL.length-8);
	thisSection = document.URL.slice(document.URL.length-7, document.URL.length-5);
// What are the lowest and highest sections in the chapter?
// Use parseInt to convert string to number.
// Force decimal to avoid assumption of octal based on leading zero
	lowest = lowSection[parseInt(thisChapter, 10)];
	highest = highSection[parseInt(thisChapter, 10)];
// What is the NEXT section?
// Set NEXT section equal to CURRENT section
	nextSection = document.URL.slice(document.URL.length-10);
// Are we at the max?
	if (highest <= parseInt(thisSection, 10)) { advance = "no" };
	if (advance == "yes") {
		mySection = parseInt(thisSection, 10) + 1;
		if (mySection.length != 2) { mySection = "0"+mySection };
		nextSection = chp + thisChapter + "-" + mySection + ".html";
	}
// What is the PREVIOUS section?
// Set PREVIOUS section equal to CURRENT section
	previousSection = document.URL.slice(document.URL.length-10);
// Are we at the min?
	if (lowest >= parseInt(thisSection, 10)) { retreat = "no" };
	if (retreat == "yes") {
		mySection = parseInt(thisSection, 10) - 1;
		if (mySection.length != 2) { mySection = "0"+ mySection };
		previousSection = chp + thisChapter + "-" + mySection + ".html";
	}
// Here's the output
// Always start with the paragraph marker and an option to go to the index
	document.write("<p class='no_indent'>");
	document.write("<a href='index.html'>" + booktitle + " Table of Contents</a>");
	document.write("<br>");
// If we can go to a previous section, offer that option.
	if (retreat == "yes") {
		document.write("<a href='" + previousSection + "'> Previous Section</a>");
	} else {
		document.write("No Previous Section (This is the first section in this chapter.)");
	}
// If we can go to the next section, offer that option.
	document.write("<br>");
	if (advance == "yes") {
		document.write("<a href='" + nextSection + "'> Next Section</a>");
	} else {
		document.write("No Next Section (This is the last section in this chapter.)");
	}
// Close the paragraph
	document.write("</p>");
}

function chapterSection() {
	document.write("<h3>Chapter ");
	document.write(parseInt(document.URL.slice(document.URL.length-10, document.URL.length-8), 10));
	document.write(" Section ");
	document.write(parseInt(document.URL.slice(document.URL.length-7, document.URL.length-5), 10));
	document.write("</h3>");
}
