/**********************************************************

Functions for controlling login-specific parts of the page.
NOTE currently all we can tell is that the user is logged in or not
(and even that is spoofable pretty easily).  But that's okay, because
the serverside checks the actual user data before doing anything with it.

**********************************************************/

function initUser(cookieName) {

	var loginresult = getQueryVal("loginresult");
	if (loginresult == -1) {
		alert("No account with that username and password was found; please try again.");
	} else if (loginresult == 2) {
		// alert("You have logged out.");  // (too annoying)
	} else if (loginresult == 3) {
		alert("Your account has been created. Welcome!");
	} else if (loginresult == -2) {
		alert("That doesn't match any current online workshop."); // trying to register for workshop
	} else if (loginresult == -3) {
		alert("You are already registered in that workshop; just click its name to enter (you don't have to enter the secret phrase every time)."); // trying to register for workshop
	} else if (loginresult == 4) {
		alert("You have successfully registered in the online workshop; click its name to enter.");
	}

	var userhash = getCookie(cookieName);
	if (userhash) {
		// replace *User spans with cookie data.  Ooh, fancy.
		var userdata = getCookie(cookieName + "User").split("\|");
		for (var i = 0; i < userdata.length; i++) {
			var n = userdata[i].substring(0,userdata[i].indexOf("="));
			var v = userdata[i].substring(userdata[i].indexOf("=")+1, userdata[i].length);
			replaceByClass("span", n + "User" , v);
		}
		// show the user divs
		setStyleByClass("div","loggedin","visibility","visible");
		setStyleByClass("div","loggedin","display","block");
		setStyleByClass("div","notloggedin","visibility","hidden");
		setStyleByClass("div","notloggedin","display","none");
	} else {
		// user is definitely not logged in
		setStyleByClass("div","loggedin","visibility","hidden");
		setStyleByClass("div","loggedin","display","none");
		setStyleByClass("div","notloggedin","visibility","visible");
		setStyleByClass("div","notloggedin","display","block");
	}
}





/* COMMON FUNCTIONS: should pull these out into a separate file eventually */

function getCookie(cookieName) {
	var myCookie = document.cookie;
	var prefix = cookieName + "=";
	var begin = myCookie.indexOf("; " + prefix);
	if (begin == -1) {
		begin = myCookie.indexOf(prefix);
		if (begin != 0) return null;
	} else {
		begin += 2;
	}
	
	var end = myCookie.indexOf(";",begin);
	if (end == -1) end = myCookie.length;
	
	var returnString = unescape(myCookie.substring(begin + prefix.length, end));
	if (returnString) {
		return returnString;
	} else {
		return false;
	}
}

function getCookieVal(cookieName, key) {
	// parses a pipe-separated list of key=value pairs, returns the value of key or null
	if (getCookie(cookieName)) {
		var Array = getCookie(cookieName).split("\|");
		var testKey, testVal;
		for (var i = 0; i < Array.length; i++) {
			testKey = Array[i].substring(0,Array[i].indexOf("="));
			if (testKey == key) {
				return(Array[i].substring(Array[i].indexOf("=")+1,Array[i].length));
			}
		}
	}
	return "";
}


/* thank you, apple.com: */

function setStyleByClass(tagName,className,propertyName,newValue){
	var elements;
	if(tagName == '*') {
		// '*' not supported by IE/Win 5.5 and below
		elements = (document.all) ? document.all : document.getElementsByTagName('*');
	} else {
		elements = document.getElementsByTagName(tagName);
	}
	for(var i = 0; i < elements.length; i++) {
		var node = elements.item(i);
		for(var j = 0; j < node.attributes.length; j++) {
			if(node.attributes.item(j).nodeName == 'class') {
				// need to allow multi classes here
				var nodeClasses = node.attributes.item(j).nodeValue.split(" ");
				for (k=0; k < nodeClasses.length; k++) {
					if(nodeClasses[k] == className) {
						eval('node.style.' + propertyName + " = '" + newValue + "'");
					}
				}
			}
		}
	}
}

// experimental: replace nodes matching the tagName with the new string.
function replaceByClass(tagName,className,newValue){
	var elements;
	if(tagName == '*') {
		// '*' not supported by IE/Win 5.5 and below
		elements = (document.all) ? document.all : document.getElementsByTagName('*');
	} else {
		elements = document.getElementsByTagName(tagName);
	}
	for(var i = 0; i < elements.length; i++) {
		var node = elements.item(i);
		for(var j = 0; j < node.attributes.length; j++) {
			if(node.attributes.item(j).nodeName == 'class') {
				// need to allow multi classes here
				var nodeClasses = node.attributes.item(j).nodeValue.split(" ");
				for (k=0; k < nodeClasses.length; k++) {
					if(nodeClasses[k] == className) {

						node.innerHTML = newValue;  // yeah, yeah, should use the DOM model here. But it's a pain in the ass, and this works.

					}
				}
			}
		}
	}
}
