window.captureEvents(Event.KEYPRESS);
window.onkeypress = handleKey;

var counter = 0;
var d;
var browser = "NS";
var DHTML = 0;

function initPage() {
    getBrowser();
    d = getDelayedElements();
}

function getBrowser() {
    if (window.opera) {
        browser = "OP";
    }
    if(document.getElementById) {
       DHTML = 1;
       browser = "DOM";
    }
    if(document.all && !OP) {
        DHTML = 1;
        browser = "IE";
    }
    if (window.netscape && window.screen && browser != "DOM" && 
      browser != "OP") {
        DHTML = 1;
        browser = "NS";
    }
}

function openTOC() {
  window.open('toc.html', 'toc',
    'locationbar=no,menubar=no,status=no,width=' + String(screen.availWidth) +
    ',height=' + String(screen.availHeight));
  return false;
}

function getDelayedElements() {
    var a = new Array();
    var el = document.getElementsByTagName("*");
    for (i=0; i < el.length; i++) {
        if (el[i].className == "delayed") {
            a.push(el[i]);
        }
    }
    return a
}

function showNextDelayed() {
    if (counter < d.length) {
        d[counter].style.visibility = "visible";
        counter++;
        return true;
    }
    else {
        return false;
    }
}

function handleKey(e) {
    //alert("Key pressed! ASCII-value: " + e.which);
    var code;

    switch (browser) {
        case "IE":
            var code = e.keyCode;
            break;
        case "NS":
        case "DOM":
        case "OP":
            var code = e.which;
            break;
    }
    switch (code) {
        case 32: // space
            if (! showNextDelayed()) {
                if (document.links["forward"]) {
                    window.location.href = document.links["forward"];
                }
            }
            return false;
        case 8: // BS
            if (document.links["back"]) {
                window.location.href = document.links["back"];
                return false;
            }
            if (document.links["toc"]) {
                window.location.href = document.links["toc"];
                return false;
            }
            return false;
        case 113: // q
            window.close();
    }
    window.routeEvent(e)
    return true;
}
