/*
 * ------------------------------------------------------------------------
 * Common
 * ------------------------------------------------------------------------
 */

function getDataElem () {
    return document.getElementById ("fp_data");
}

function getHeaderElem () {
    return document.getElementById ("fp_header");
}

function getMenuElem () {
    return document.getElementById ("fp_menu");
}

/*
 * Is the browser the Internet Explorer?
 */
function isExplorer () {
    return navigator.appName.indexOf ("Explorer") != -1;
}


/*
 * Remove leading and trailing whitespaces.
 */
function trim (str) {
    return str.replace (/^\s+/, "").replace (/\s+$/, "");
}

/*
 * Returns the value of the named parameter from the query line, that
 * means from the line starting with "http://"
 */
function getQueryValue (name) {
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regex = new RegExp ("[\\?&]" + name + "=([^&#]*)");
    var values = regex.exec (window.location.href);
    if (values == null)
	return "";
    else
	return values[1];
}

/**
 * Add the class "odd" or "even" to the "tr" elements of all tables in
 * the document.
 */
function initTables () {
    var tables = document.getElementsByTagName ("table");
    for (var i = 0; i < tables.length; i++) {
	var linetype = "odd";
	var tbody = tables[i].getElementsByTagName ("tbody");
	tbody = tbody ? tbody[0] : tables[i];
	var trs = tbody.getElementsByTagName ("tr");
	for (var j = 0; j < trs.length; j++) {
	    if (trs[j].getElementsByTagName ("td").length > 0) {
		trs[j].className = linetype;
		linetype = linetype == "odd" ? "even" : "odd";
	    }
	}
    }
}

/*
 * ------------------------------------------------------------------------
 * Cookies
 * ------------------------------------------------------------------------
 */

/*
 * Writes a cookie with (nearly unlimited) usability
 */
function writeCookie (name, value) {
    document.cookie = name + "=" + value + "; expires=30-Dec-2100 12:00:00 GMT";
}

/*
 * Reads and returns the value of the named cookie, if not found
 * returns null.
 */
function readCookie (name) {
    var a = document.cookie.split (";");
    for (var i = 0; i < a.length; i++) {
	var anv = trim (a[i]).split ("=");
	if (anv[0] == name) {
	    return trim (anv[1]);
	}
    }
    return null;
}

/*
 * Returns the visible window area height
 */
function getVisibleHeight() {
    var h = 0;
    if (typeof (window.innerHeight) == 'number') {
	h = window.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
	h = document.documentElement.clientHeight;
    } else if (document.body && document.body.clientHeight) {
	h = document.body.clientHeight;
    }
    return h;
}

function getVisibleWidth () {
    var w = 0;
    if (typeof (window.innerWidth) == 'number') {
	w = window.innerWidth;
    } else if (document.documentElement && document.documentElement.clientWidth) {
	w = document.documentElement.clientWidth;
    } else if (document.body && document.body.clientWidth) {
	w = document.body.clientWidth;
    }
    return w;
}

/*
 * The first element in the given elem should always have the same
 * marginTop - but nodes without text (or getText() calls) will get a
 * top and bottom margin 0.
 */
function setTopMarginOfFirstElem (parent) {
    if (parent) {
	var elems = parent.getElementsByTagName ("*");
	for (var i = 0; i < elems.length; i++) {
	    var tid = "-" + elems[i].tagName + "-";
	    if (elems[i].className == "numbering") {
	    }
	    else if ("-H1-".indexOf (tid) >= 0) {
		elems[i].style.marginTop = "10px";
		return true;
	    }
	    else if ("-H2-".indexOf (tid) >= 0) {
		elems[i].style.marginTop = "12px";
		return true;
	    }
	    else if ("-OBJECT-APPLET-".indexOf (tid) >= 0) {
		return true;
	    }
	    else if ("-STYLE-SCRIPT-".indexOf (tid) < 0) {
		elems[i].style.marginTop = "12px";
		return true;
	    }
	}
    }
    return false;
}

/*
 * On load and resize set the height of the data and menu area
 * automatically
 */

if (window.attachEvent) {
    window.attachEvent ("onresize", resize);
    window.attachEvent ("onload", resize);
}
else if (window.addEventListener) {
    window.addEventListener ("resize", resize, false);
    window.addEventListener ("load", resize, false);
}

function resize () {
    var hdiff = 0;
    var elem = getHeaderElem ();
    if (elem) {
	hdiff = elem.offsetHeight + elem.offsetTop;
    }

    var height = getVisibleHeight() - hdiff;

    getDataElem ().style.height = (height - 12) + "px";
    getMenuElem ().style.height = (height - 2) + "px";

    setTopMarginOfFirstElem (getDataElem ());
    setTopMarginOfFirstElem (getMenuElem ());
}


if (window.attachEvent) {
    window.attachEvent ("onload", initTables);
}
else if (window.addEventListener) {
    window.addEventListener ("load", initTables, false);
}

