var currentElt = null;
var currentParent = null;
var currentCSS = "fancy";
function el(eltId) {
	return document.getElementById(eltId);
}
function addClass(elt, clsname) {
	elt.className += " " + clsname;
}
function remClass(elt, clsname) {
	elt.className = elt.className.replace(clsname,"");
}
function makeCurrent(oldelt,elt) {
	if (oldelt!=null)
		remClass(oldelt,"current");
	currentElt = elt;
	if (elt!=null)
		addClass(elt,"current");
}
function togCurrent(elt) {
	if (currentParent!=null)
		remClass(currentParent, "current");

	if (currentElt==elt)
		makeCurrent(currentElt,null);
	else {
		makeCurrent(currentElt,elt);
		location = "#" + elt.id;
	}
}
function togCurrentChild(parelt,elt) {
	var celt = currentElt;
	if (currentParent!=null)
		makeCurrent(currentParent,null);
	makeCurrent(celt,parelt);
	currentParent = parelt;
	makeCurrent(null,elt);
}

function init() {
	var h = location.hash;
	if (h.indexOf("?")>-1)
		h = h.substr(1,h.indexOf("?"));
	else
		h = h.substr(1);
	
	var s = location.search;
	if (s=="?view=plain") {
		currentCSS = "fancy";
		setActiveStyleSheet("plain");
	}
	else if (isFirefox() || isIE()) {	// Fancy style compatible with IE and Firefox
		currentCSS = "plain";
		setActiveStyleSheet("fancy");
	}
	else {	// Fancy style incompatible with Safari, untested for other browsers
		currentCSS = "fancy";
		setActiveStyleSheet("plain");
	}
	
	if (h!="") {
		location = "#" + h;
		togCurrent(el(h));
	}
	else if (currentCSS=="fancy")
		togCurrent(el('abstract'));	// Show the Abstract section by default
	
	
loadContent('http://inst.eecs.berkeley.edu/~nss/unirand/unirand.php');	
// Random Unicode character in Typography section
	
	if (reply!="")
		getContactInfo();
}
function setActiveStyleSheet(title) {
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if(a.getAttribute("rel").indexOf("style") != -1
        && a.getAttribute("title")) {
       a.disabled = true;
       if(a.getAttribute("title") == title) a.disabled = false;
     }
   }
   
if (!currentCSS) console.log("ha! 1");
   
   el("a"+currentCSS).href = "javascript:location=location.href.replace('?view=" + title + "', '').replace('?view=" + currentCSS + "', '').replace(location.hash,'') + '?view=" + currentCSS + "' + location.hash;";
   currentCSS = title;
   el("a"+currentCSS).className = el("a"+currentCSS).className.replace("disabledLink","");
   el("a"+title).className+=" disabledLink";
   
}

var CAPTCHA_COOKIE_NAME = "captchaAnswer";
var reply = readCookie(CAPTCHA_COOKIE_NAME);
if (reply==null)
	reply = "";

function getTextInput(msg, maxchars) {
	if (currentCSS != "fancy") {
		reply = window.prompt(msg.replace(new RegExp("<i>","gi"),"").replace(new RegExp("</i>","gi"),"")).toUpperCase();
		getContactInfo();
		return;
	}
		
	el("pquestion").innerHTML = msg;
	el("txtinput").maxLength = maxchars;
	el("txtinput").size = maxchars;
	el("acontact").style.display="none";
	el("inputdiv").style.display="inline";
	el("txtinput").focus();
}
function getContactInfo() {
	if (reply.substr(0,3)=="MAM" && reply.substr(3,3)=="MAL") {
		el("inputdiv").style.display="none";
		var eparts = ["", "han", "", "cm", "edu", "nat", "", "", "u.", "@"];
		var acontact = el("acontact");
		acontact.onclick = function () { window.location="mailt" + "o:" + eparts[5] + eparts[1] + eparts[6] + eparts[2] + eparts[7] + eparts[0] + eparts[9] + eparts[3] + eparts[8] + eparts[4]; }
		acontact.innerHTML = eparts[5] + eparts[1] + eparts[6] + eparts[2] + eparts[7] + eparts[0] + eparts[9] + eparts[3] + eparts[8] + eparts[4];
		acontact.style.display="inline";
		var afteremail = '&ensp;&middot;&ensp; 412.268.4950 &ensp;&middot;&ensp; 2602E <a href="http://www.hcii.cmu.edu/about_us/NSH.html">Newell-Simon Hall</a>';
		el("afteremail").innerHTML = afteremail;
		
		createCookie(CAPTCHA_COOKIE_NAME, reply, 100);	// Cookie will store the user's answer for 100 days
	}
	else {
		var msg = "Category including cats and elephants, but not lizards:";
		getTextInput(msg, 6);
	}
}
function setReply() {
	reply = el("txtinput").value.toUpperCase();
	getContactInfo();
}
