/* Do not mess with this code unless you know what you are doing!*/
/* You have been warned. */

/* Globals */
var active;
var collapse;

/* App hook */
window.onload = initializeForm;

/* Picks the correct XMLHTTP object */
var req = null;
this.init = function() {
    try {
        this.req = new XMLHttpRequest();
        return true;
    }catch(e) {
        try {
            this.req = new ActiveXObject("MSXML2.XMLHTTP");
            return true;
        }catch(e) {
            try {
                this.req = new ActiveXObject("Microsoft.XMLHTTP");
                return true;
            }catch(e) {
                return false;
            }
        }
    }
}

/* Switches out the old text for the new one and updates the current link highlighting */
function prepLink( node, col ) {
    if (collapse && col) collapse.className = "state1";
    getForm(node.attributes.getNamedItem("id").nodeValue + ".php");
    active.parentNode.className = active.parentNode.className.replace("active", "");
    active = node;
    active.parentNode.className += " active";
}

function dim( node ) {
    if (node.parentNode.childNodes[3] == null) {
        node.parentNode.childNodes[2].className = "state1";
    }else node.parentNode.childNodes[3].className = "state1";
}

function highlight( node ) {
    /* Expand menu */
    if (collapse) collapse.className = "state1";
    if (node.parentNode.childNodes[3] == null) {
        collapse = node.parentNode.childNodes[2];
    }else collapse = node.parentNode.childNodes[3];
    collapse.className = "state2";

    /* Switch highlight */
    active.className = active.className.replace("active", "");
    active = node;
    active.className += " active";
}

/* Initializes the application */
function initializeForm() {
    /* Install link hooks */
    var index = document.getElementById( 'index' );
    active = index;
    index.href = '#';
    active.className = "active";
    index.onclick = function( evt ) {prepLink(this, true);}

    var program = document.getElementById( 'program' );
    program.href = '#';
    program.onclick = function( evt ) {prepLink(this, true);}

    var speakers = document.getElementById( 'speakers' );
    speakers.href = '#';
    speakers.onclick = function( evt ) {prepLink(this, true);}

    var committee = document.getElementById( 'committee' );
    committee.href = '#';
    committee.onclick = function( evt ) {prepLink(this, true);}
}

/* Fetches an XML document and sends it out for display */
function getForm( url ) {
    if (this.init()) {
        req.onreadystatechange = function() {
            if ( req.readyState == 4)
                displayResult( req, false )
        }
        req.open("GET", "" + url, true );
        req.send( null );
    }else displayResult( "<h3>An error was encountered</h3>", true );
}

/* parses an xml document and displays it in textArea */
function displayResult( req, text ) {
    var textArea = document.getElementsByTagName( 'span' )[0];
    while (textArea.hasChildNodes()) {
        textArea.removeChild(textArea.firstChild);
    }
    if (!text) {
        var xml = document.createElement('div');
        xml.innerHTML = req.responseText;
        xml.setAttribute('id', 'temp');
        document.body.appendChild(xml);
        var doc = document.getElementById( 'temp' );
        try {
            textArea.innerHTML = doc.getElementsByTagName( 'span' )[0].innerHTML;
        }catch(e) {
            textArea.innerHTML = "<h1>An error was encountered</h1>";
        }
        document.body.removeChild(document.getElementById( 'temp' ));
    }else{
        textArea.innerHTML = req;
    }
}

