function displayError(message, url, lineno){
	alert("JavaScript error detected!\n\nError: " + message + "\nLine: " + lineno + "\nURL: " + url + "\nBrowser: " + navigator.userAgent);
	return false;
}
// window.onerror = displayError;

function init() {
	// warn people about their very old browsers
	if (is.IEapp && is.version < 5.5 || (is.NSapp && is.version < 5))
		idOpen("browserwarn");
	if (typeof(local_init) == "function")
		local_init();
	//showheadings();
}
window.onload = init;

function detectBrowser(){
	this.IE=0;
	this.NS=0;
	this.NSapp=(navigator.appName=="Netscape") ? 1 : 0;
	this.IEapp=(navigator.appName=="Microsoft Internet Explorer") ? 1 : 0;
	this.appver=navigator.appVersion;
	if(this.IEapp)
		this.IE=(typeof(document.all)=="object") ? 1 : 0;
	else if(this.NSapp)
		this.NS=(typeof(document.layers)=="object") ? 1 : 0;
	if(this.NSapp)
		this.version=parseFloat(this.appver);
	else
		this.version=parseFloat(this.appver.substr(this.appver.indexOf("MSIE") + 5, 3));
}
var is=new detectBrowser();

function idGet(id) {
	if (typeof(document.getElementById) != "undefined") {
		return document.getElementById(id);
	} else {
		return document.all[id];
	}
}

function idOpen(id) {
	var x = idGet(id);
	if (!x)
		return;
	if (x.style.display == "" || x.style.display == "none")
		x.style.display = "block";
	else
		x.style.display = "none";
}

function getCookie(name) {
	var index = document.cookie.indexOf(name + "=");
	if (index == -1)
		return null;
	index = document.cookie.indexOf("=", index) + 1;
	var endstr = document.cookie.indexOf(";", index);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(index, endstr));
}

function setCookie(name, value) {
	var expiry = new Date(new Date().getTime() + 360 * 24 * 60 * 60 * 1000);
	if (value != null && value != "")
		document.cookie=name + "=" + escape(value) + "; expires=" + expiry.toGMTString() + "; domain=gemal.dk; path=/";
}

function obj(o,ot) {
	if (ot) {
		if (typeof(o) == t) {
			return 1;
		} else {
			return 0;
		}
	} else {
		if (typeof(o) != "undefined") {
			return 1;
		} else {
			return 0;
		}
	}
}

function addSidebar(n, u, c) {
	if ((typeof window.sidebar == "object") && (typeof window.sidebar.addPanel == "function")) {
		try {
			window.sidebar.addPanel(n, u, c);
		} catch(e) {
			alert("Unable to add sidebar. Make sure that sidebars are active (via View -> Show/Hide -> Sidebar or press F9.");
			if (u == "http://gemal.dk/sidebar.html") {
				document.location = "http://gemal.dk/archives/000122.html";
			} else {
				document.location = "http://gemal.dk/archives/000228.html";
			}
		}
	} else {
		alert("Sidebars are only available to Gecko based browsers such as Mozilla and Netscape 6/7.");
		if (u == "http://gemal.dk/sidebar.html") {
			document.location = "http://gemal.dk/archives/000122.html";
		} else {
			document.location = "http://gemal.dk/archives/000228.html";
		}
	}
}

function extensionUpdate(extname, version_latest) {
	if (location.search) {
		var re = /ver=(.*)$/;
		re.test(location.search);
		var version_current = RegExp.$1;
		if (version_current) {
			document.writeln("You have " + extname + " version <strong>" + version_current + "</strong> installed!");
			if (version_latest > version_current) {
				document.writeln('<div class="warning content-code-box">' + extname + ' version <strong>' + version_latest + '</strong> is available! You should upgrade to the newest version!</div>');
			} else if (version_latest == version_current) {
				document.writeln('You already have the latest version!');
			}
		}
	} else if (typeof(InstallTrigger) == "object") {
		version_current = InstallTrigger.getVersion(name.toLowerCase());
		if (version_current) {
			re = /(.*?)\.(\d+)$/;
			re.test(version_current);
			if (RegExp.$1 && RegExp.$2) {
				document.writeln("You have " + extname + " version <strong>" + RegExp.$1 + "</strong> build " + RegExp.$2 + " installed!");
			} else {
				document.writeln("You have " + extname + " version <strong>" + version_current + "</strong> installed!");
			}
			if (version_latest > version_current) {
				document.writeln('<div class="warning content-code-box">' + extname + ' version <strong>' + version_latest + '</strong> is available! You should upgrade to the newest version!</div>');
			} else if (version_latest == version_current) {
				document.writeln('You already have the latest version!');
			}
		}
	}
}

function showheadings() {
	var h;
	// Support both Gecko and IE
	try {
		h = document.getElementsByTagNameNS("*", "h2");
	} catch(err) {
		h = document.getElementsByTagName("h2");
	}
	// if there are some headings
	if (h.length) {
		idOpen("nav-headings");
		var headings = idGet("nav-headings");
		for (var i = 0; i < h.length; i++) {
			// get the text of the heading and insert it
			var txt;
			if (h[i].childNodes[0]) {
				if (h[i].childNodes[0].nodeValue)
					txt = h[i].childNodes[0].nodeValue;
				else
					txt = h[i].childNodes[0].childNodes[0].nodeValue;
				if (txt) {
					var txts = txt;
					if (txt.length > 25) {
						txts = txt.substring(0,25);
						txts += "...";
					}
					headings.innerHTML += "<div><a href=\"#" + i + "\" title=\"" + txt + "\">" + txts + "</a></div>";
				}
			}

			// now create a href ala <a name="1"></a>
			var hl = document.createElement("A");
			hl.setAttribute("name", i);
			h.item(i).insertBefore(hl, null);
		}
	}
}

function install(aEvent, extName, iconURL) {
	var params = [];
	params[extName] = {
		URL: aEvent.target.href,
		IconURL: iconURL,
		toString: function () {return this.URL;}
	};
	InstallTrigger.install(params);
	return false;
}

