// This file contains a set of routines used in webs created by D+DD
// 1998-01-12 (c) Docu + Design Daube, Zürich
// 2001-12-28 added debug()
// 2002-08-17 added hidemail ()
// 2007-01-09 added generateToc ()

/* navigation -----------------------------------------------------------------
	Purpose: Evaluate and jump to the previous, next and first file
					 of a sequence named abc00, abc01 ... abc77, abc78 (up to abc99).
					 The last file in the sequence must have a <title> ending
					 with a period "."
	Problem: See HTTP://www.fokolar-bewegung.ch/_scripts/ddd_specials.txt
	Credit:  Original idea by ScriptCity
*/ 
var URL = unescape(location.href);
var xstart = 0;
var xend = URL.length;
var digitOnePlace = URL.lastIndexOf('.') - 2;
var digitTwoPlace = URL.lastIndexOf('.') - 1;
var digitOne = URL.charAt(digitOnePlace);
var digitTwo = URL.charAt(digitTwoPlace);
var filePrefix = URL.substring(xstart,digitOnePlace);
var suffixStart = URL.lastIndexOf('.');
var fileSuffix = URL.substring(suffixStart,xend);
var previousFileName = null;
var nextFileName = null;
var firstFileName = null;
var dig1 = null; var dig2 = null;         // Opera 3.0, workaround
dig1 = parseInt(digitOne); dig2 = parseInt(digitTwo);
var islast= false;
var ll= document.title.length;            // opera: value 1 to large
var lix = document.title.lastIndexOf(".");// opera: value OK

function previousFile() {
// alert (document.title+", d1, d2= "+ dig1+","+ dig2);
	if (dig2 == 0 && dig1 == 0) { location.href = URL;
	} else {
		if (dig2 == 0) {
			dig2 = 9; dig1--;
		} else {
			dig2--;
		}
		previousFileName = filePrefix + dig1 + dig2 + fileSuffix;
		location.href = previousFileName;
	}
}

function nextFile() {
	if ((ll - 2)<= lix) islast = true;
	// alert (document.title+", ll,lix,islast= "+ dig1+","+ dig2+","+ ll+", "+lix+", "+ islast);
	if (islast) nextFileName = filePrefix + "00" + fileSuffix;
	else {
		if (dig2 == 9) {
			dig2 = 0; dig1++;
		} else {
			dig2++;
		}
		nextFileName = filePrefix + dig1 + dig2 + fileSuffix;
	}
	location.href = nextFileName;
}

function firstFile() {
	firstFileName = filePrefix + "00" + fileSuffix;
	location.href = firstFileName;
}
  
/* hidemail -------------------------------------------------------------------
	Purpose: hide e-mail address from spam-robots
	Parameter 'what' may be left out when calling - will be "undefined" in the mail
*/
function hidemail(name, address, what) {
	where = name + "@" + address;
	document.location = "mailto:" + where + "?subject=" + what;
}

/* isoupdate -------------------------------------------------------------------
	Purpose: generate lastModified date in ISO format
	Take special care of the strange behaviour of browsers:
		 year   1998, 1999, 2000, 2001
	Opera 3.x:  98,   99,  100,  101
	Opera 4.x:  98,   99,  100,  101
	NS/IE 4.x:  98,   99,    0,    1
	IE 5.x;   1998, 1999, 2000, 2001
*/  
function isoupdate () {
	var indate=new Date(document.lastModified);
	// allow for leading zeroes in mm and dd
	var mm= 101+ indate.getMonth(); var mmm = mm.toString(10).substring(1);
	var dd= 100+ indate.getDate();  var ddd = dd.toString(10).substring(1);
	var yy= indate.getYear(); var year=yy.toString();
	// alert ("Gotten len, year, mmm, ddd: \n"+indate.length +"  "+year+"-"+ mmm+"-"+ddd);
	if (year.length < 4) {
		if (year.length > 2)
			{ year = yy + 1900; year=year.toString();}
		else {
			if (yy > 70) {
				year = yy + 1900; 
				year=year.toString();
			}
			else {
				year = yy + 2000; 
				year=year.toString();
			}
		}
	}
	thedate=year+"-"+ mmm+"-"+ddd;
	return (thedate);
}

/* debug ----------------------------------------------------------------------
  Purpose: open a window the first time we are called,
           or after an existing console window has been closed.
  Usage:
           var _console = null;
           ...
           debug ("whatever you want to display");
*/
function debug(msg) {
  if ((_console == null) || (_console.closed)) {
    _console = window.open("","console","width=600,height=200,resizable");
    _console.document.open("text/plain");
  }
    _console.document.writeln(msg);
}

/* generateToc ----------------------------------------------------------------
  Pupose:     Generate a Table of Contents (TOC) from heading tags.
	Usage:      window.onload = generateToc(2, "Overview");
              This script reference must be located at the end
              of the document, e.g. prior to the /body tag.
	Parameters:
			2      1...n level of <h.> tags to be collected
			"..."  Heading title to be placed above the TOC
	Credit:     This script is based on
	            validweb.nl/artikelen/javascript/documentstructuur-met-javascript/
*/
function generateToc (level, title) {
	levelCode = "h" + level;
																						// check for a capable browser
	if (document.getElementById && document.createElement) {
		b = document.getElementsByTagName("body")[0];
		ul = document.createElement("ul");      // create the TOC list
		b.insertBefore(ul, b.firstChild);
		headers = document.getElementsByTagName(levelCode); // collect headers
		var idNum = 2;                          // start value for unice IDs
		for (var i=0;i<headers.length;i++) {
			var display = headers[i].firstChild.nodeValue;
			var id = display.replace(/\W/g, "-"); // white space to dash
			id = id.replace(/(--+)/g, "-");       // double dash to single one
			if(document.getElementById(id))
				id += "-" + idNum++;                // Unique starting with idnum, idnum+1 etc
			headers[i].setAttribute("id", id);    // apply IDs to header texts
			li = document.createElement("li");    // fill TOC list with items
			ul.appendChild(li);
			a = document.createElement("a");      // fill list items with links
			li.appendChild(a);
			a.setAttribute("href", "#" + id);
			doc = document.createTextNode(display);
			a.appendChild(doc);
		}
		heading = document.createElement("h1"); // Insert TOC at beginning of document
		heading.appendChild(document.createTextNode(title));
		b.insertBefore(heading, b.firstChild);
		b.focus();                              // present start of document
	}
}
