function readCookie(name) {
	var cks = document.cookie.split(';');
	for(var i=cks.length-1; i >= 0; i--) {
		var c = cks[i];
		while(c.charAt(0)==' ') c = c.substring(1,c.length);
		if(c.indexOf(name+"=")==0) return c.substring(name.length+1,c.length);
	}
	return null;
}
function TabSet(id) {
	this.id = id;
	this.current = null;

	if(document.getElementById) {
		// var activeCk = "urldefined";
		 var activeCk = readCookie("tabset-" + id); // used to keep tab state on back button
		if(location.href.indexOf("tab=") >= 0) activeCk = "urldefined";
		var tabset = document.getElementById(id);
		var tabs =  tabset.getElementsByTagName("h3");
		for(var i = tabs.length-1; i >= 0 ; i--) {
			var tab = document.createElement("div");
			tab.tabset = this;
			tab.panel = tabs[i].parentNode;
			tab.className = "tab-head";
			tabs[i].style.display = "none";
			var txt = tabs[i].firstChild.nodeValue;
			tab.appendChild(document.createTextNode(txt));
			tabset.insertBefore(tab,tabset.firstChild);
			tab.onclick = onClickTab;

			if((location.href.indexOf("tab="+i) >= 0) || (activeCk == null && i == 0) || (activeCk != null && activeCk == txt)) {
				tab.className += " selected";
				this.current = tab;
			}
			else tab.panel.style.display = "none";
		}
	}

	function onClickTab() {
		if(this.className.indexOf("selected") < 0) {
			var current = this.tabset.current;
			if(current != null) {
				current.panel.style.display = "none";
				current.className = current.className.replace(" selected","");
			}
			this.panel.style.display = "block";
			this.className += " selected";
			this.tabset.current = this;
			document.cookie = "tabset-" + this.tabset.id + "=" + this.firstChild.nodeValue;
		}
	}
}