//Redirect users with non-compliant browsers (NN4, Mac IE, No-DOM)
if (document.layers || (document.all && !window.print) || !document.getElementById) {
	location.href = "?s=badbrowser";
} //end document.layers || (document.all && !window.print) || !document.getElementById IF

//PHP Function Implementations
function implode(glue,array) {
	var string = "";
	for (i = 0; i < array.length; i++) {
		string += (i > 0 ? glue : "")+array[i];
	} //end i = 0; i < array.length; i++ FOR
	return (string);
} //end implode

//DOM Core
function emptyElement(target) {
	while (target.hasChildNodes()) {
		target.removeChild(target.firstChild);
	} //end target.hasChildNodes() WHILE
} //end emptyElement
function makeStrong(text) {
	var strong = document.createElement("strong");
	strong.appendChild(document.createTextNode(text));
	return (strong);
} //end makeStrong
function makeBr() {
	var br = document.createElement("br");
	return (br);
} //end makeBr

//AJAX Core
function HttpClient(method,async,xmlhttp,result) {
	this.requestType = method;
	this.isAsync = async;
	this.xmlhttp = xmlhttp;
	this.requestResult = result;
} //end HttpClient
HttpClient.prototype = {
	callback:false,
	onSend:false,
	onLoad:false,
	onError:function(error) {
		alert(error.message);
	}/*onError:function(error)*/,
	init:function() {
		try {
			this.xmlhttp = new XMLHttpRequest();
		} catch (e) {
			var XMLHTTP_IDS = new Array('MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP');
			var success = false;
			for (var i = 0; i < XMLHTTP_IDS.length && !success; i++) {
				try {
					this.xmlhttp = new ActiveXObject(XMLHTTP_IDS[i]);
					success = true;
				} catch (e) {}
			} //end var i = 0; i < XMLHTTP_IDS.length && !success; i++ FOR
			if (!success) {
				throw new Error('Unable to create XMLHttpRequest.');
			} //end !success IF
		} //end try/catch
	} /*end init:function()*/,
	makeRequest:function(url,payload) {
		if (!this.xmlhttp) {
			this.init();
		} //end !this.xmlhttp IF
		this.xmlhttp.open(this.requestType,url,this.isAsync);
		var self = this;
		if (this.callback != false) {
			this.xmlhttp.onreadystatechange = function() {self._readyStateChangeCallback();}
		} //end this.callback != false IF
		if (this.requestType == "POST") {
			this.xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
		} //end this.requestType == "POST" IF
		this.xmlhttp.send(payload);
		if (!this.isAsync) {
			return this.xmlhttp.responseText;
		} //end !this.isAsync IF
	} /*end makeRequest:function(url,payload)*/,
	_readyStateChangeCallback:function() {
		switch(this.xmlhttp.readyState) {
			case 2:
				if (this.onSend != false) {
					this.onSend();
				} //end this.onSend != false IF
			break;
			case 4:
				if (this.onLoad != false) {
					this.onLoad();
				} //end this.onLoad != false IF
				if (this.xmlhttp.status == 200) {
					if (this.requestResult == 'XML') {
						this.callback(this.xmlhttp.responseXML);
					} else {
						this.callback(this.xmlhttp.responseText);
					} //end this.requestResult == 'XML' IF
				} else {
					this.onError(new Error('HTTP Error Making Request: ['+this.xmlhttp.status+'] '+this.xmlhttp.statusText));
				} //end this.xmlhttp.status == 200 IF
			break;
		} //end this.xmlhttp.readyState SWITCH
	} //end _readyStateChangeCallback:function()
} //end HttpClient.prototype

//Fade Core
function fade(target,direction) {
	targetElement = target;
	dir = direction;
	o = (dir == "out" ? 1 : 0);
	if (document.all) {
		o = (o == 1 ? 100 : 0);
		targetElement.style.filter="alpha(opacity="+o+")";
		timer = setInterval("fadeIE()",30);
	} else if (document.getElementById && !document.all) {
		timer = setInterval("fadeGecko()",30);
	} //end document.all || (document.getElementById && !document.all) IFs
} //end fade
function fadeGecko() {
	target = targetElement;
	direction = dir;
	if (o <= 1.05 && direction == "in") {
		target.style.MozOpacity = o;
		target.style.KhtmlOpacity = o;
		target.style.opacity = o;
		o += 0.05;
	} else if (o >= -0.05 && direction == "out") {
		target.style.MozOpacity = o;
		target.style.KhtmlOpacity = o;
		target.style.opacity = o;
		o -= 0.05;
	} else {
		clearInterval(timer);
		if (direction == "out") {
			emptyElement(target);
			target.style.width = "0px";
		} //end direction IF
	} //end (o < 1.05 && direction == "in") && (o > -0.05 && direction == "out") IFs
} //end fadeGecko
function fadeIE() {
	target = targetElement;
	direction = dir;
	if (o <= 120 && direction == "in") {
		target.filters.alpha.opacity = o;
		o += 20;
	} else if (o >= -20 && direction == "out") {
		target.filters.alpha.opacity = o;
		o -= 20;
	} else {
		clearInterval(timer);
		if (direction == "out") {
			emptyElement(target);
			target.style.width = "0px";
		} //end direction IF
	} //end (o < 120 && direction == "in") || (o > -20 && direction == "out") IFs
} //end fadeIE

//Tracking Core
var x = 0;
var y = 0;
var o = 0;
var sW = 0;
var sH = 0;
function getMouseCoords(e) {
	if (document.all) {
		x = event.clientX + document.body.scrollLeft;
		y = event.clientY + document.body.scrollTop;
	} else {
		x = e.pageX;
		y = e.pageY;
	} //end document.all IF
	return true;
} //end getMouseCoords
document.onmousemove = getMouseCoords;
function getScreenRes() {
	if (self.innerWidth) {
		sW = self.innerWidth;		
		sH = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientWidth) {
		sW = document.documentElement.clientWidth;
		sH = document.documentElement.clientHeight;
	} else if (document.body) {
		sW = document.body.clientWidth;
		sH = document.body.clientHeight;
	} //end self.innerWidth || (document.documentElement && document.documentElement.clientWidth) || document.body IFs
	window.onresize = getScreenRes;
} //end getScreenRes
getScreenRes();

//Details Core
function showDetails(category,id,width) {
	//Prevent details box from displaying off-screen
	if ((x+width) > sW) {
		x = sW-width-50;
	} //end (x+width) > sW IF
		
	//Fetch data
	fetchData(category,id);
	
	//Position details box
	var target = document.getElementById("details");
	target.style.left = x+"px";
	target.style.top = y+"px";
	target.style.width = width+"px";
} //end showDetails
function fetchData(category,id) {
	var http = new HttpClient("POST",true,false,"XML");
	http.callback = function(xmlDoc) {
		var target = document.getElementById("details");
		emptyElement(target);
		
		if (xmlDoc.getElementsByTagName("error").length > 0) {
			var data = xmlDoc.getElementsByTagName("error")[0];
			
			var header = document.createElement("div");
			header.setAttribute("id","details_header");
			header.appendChild(document.createTextNode(data.getAttribute("title")));
			target.appendChild(header);
		
			var closeBox = document.createElement("div");
			closeBox.setAttribute("id","details_close");
			if (document.all) {
				closeBox.setAttribute("onclick",hideDetails);
			} else {
				closeBox.setAttribute("onclick","hideDetails();");
			} //end document.all IF
			closeBox.setAttribute("title","Hide Error Message");
			closeBox.appendChild(document.createTextNode("X"));
			target.appendChild(closeBox);
		
			var content = document.createElement("div");
			content.setAttribute("id","details_content");
			content.setAttribute("class","error");
			content.appendChild(document.createTextNode(data.getAttribute("message")));
		} else {
			var target = document.getElementById("details");
			emptyElement(target);
			
			switch (category) {
				case "e":
					var data = xmlDoc.getElementsByTagName("event")[0];
			
					var header = document.createElement("div");
					header.setAttribute("id","details_header");
					header.appendChild(document.createTextNode(data.getAttribute("name")));
					target.appendChild(header);
					
					var closeBox = document.createElement("div");
					closeBox.setAttribute("id","details_close");
					if (document.all) {
						closeBox.setAttribute("onclick",hideDetails);
					} else {
						closeBox.setAttribute("onclick","hideDetails();");
					} //end document.all IF
					closeBox.setAttribute("title","Hide Details");
					closeBox.appendChild(document.createTextNode("X"));
					target.appendChild(closeBox);
					
					var content = document.createElement("div");
					content.setAttribute("id","details_content");
					
					content.appendChild(makeStrong("When: "));
					content.appendChild(document.createTextNode(data.getAttribute("when")));
					content.appendChild(makeBr());
					content.appendChild(makeStrong("Doors Open at: "));
					content.appendChild(document.createTextNode(data.getAttribute("doors")));
					content.appendChild(makeBr());
					content.appendChild(makeStrong("Where: "));
					content.appendChild(document.createTextNode(data.getAttribute("where")));
					content.appendChild(makeBr());
					
					var prices = data.getElementsByTagName("price");
					for (i = 0; i < prices.length; i++) {
						content.appendChild(makeStrong(prices[i].getAttribute("name")+": "));
						content.appendChild(document.createTextNode((prices[i].getAttribute("amount") == 0 ? "FREE" : "$"+prices[i].getAttribute("amount"))));
						content.appendChild(makeBr());
					} //end i = 0; i < prices.length; i++ FOR
					
					if (data.getAttribute("ticketswhere") != "NULL") {
						content.appendChild(makeStrong("Tickets on sale at: "));
						content.appendChild(document.createTextNode(data.getAttribute("ticketswhere")));
						content.appendChild(makeStrong(" starting: "));
						content.appendChild(document.createTextNode(data.getAttribute("ticketswhen")));
						content.appendChild(makeBr());
					} //end data.getAttribute("ticketswhere") != "NULL" IF
					
					if (data.getAttribute("signupswhere") != "NULL") {
						content.appendChild(makeStrong("Signup at: "));
						content.appendChild(document.createTextNode(data.getAttribute("signupswhere")));
						content.appendChild(makeStrong(" starting: "));
						content.appendChild(document.createTextNode(data.getAttribute("signupswhen")));
						content.appendChild(makeBr());
					} //end data.getAttribute("signupswhere") != "NULL" IF
					
					content.appendChild(makeStrong("Interpreted/Subtitled: "));
					if (data.getAttribute("interpreted") == 1) {
						var span = document.createElement("span");
						span.setAttribute("class","yes");
						span.appendChild(document.createTextNode("Yes"));
						content.appendChild(span);
					} else {
						var span = document.createElement("span");
						span.setAttribute("class","no");
						span.appendChild(document.createTextNode("No "));
						content.appendChild(span);
						var requestLink = document.createElement("a");
						requestLink.setAttribute("href","https://www.ntid.rit.edu/AccessServices/index.cfm?Info=Service");
						requestLink.setAttribute("target","_blank");
						requestLink.setAttribute("title","Request Interpreter");
						requestLink.appendChild(document.createTextNode("Request Interpreter"));
						content.appendChild(requestLink);
					} //end data.getAttribute("interpreted") == 1 IF
					content.appendChild(makeBr());
			
					content.appendChild(makeStrong("Food Offered: "));
					var span = document.createElement("span");
					span.setAttribute("class",(data.getAttribute("food") == 1 ? "yes" : "no"));
					span.appendChild(document.createTextNode((data.getAttribute("food") == 1 ? "Yes" : "No")));
					content.appendChild(span);
					content.appendChild(makeBr());
					
					content.appendChild(makeStrong("Details:"));
					content.appendChild(makeBr());
					var fragments = data.getAttribute("details").split("::BR::");
					for (i = 0; i < fragments.length; i++) {
						content.appendChild(document.createTextNode(fragments[i]));
						content.appendChild(makeBr());
					} //end i = 0; i < fragments.length; i++ FOR
					
					if (data.getAttribute("division") == "tncs") {
						content.appendChild(makeStrong("Movie Synopsis:"));
						content.appendChild(makeBr());
						var fragments = data.getAttribute("synopsis").split("::BR::");
						for (i = 0; i < fragments.length; i++) {
							content.appendChild(document.createTextNode(fragments[i]));
							content.appendChild(makeBr());
						} //end i = 0; i < fragments.length; i++ FOR
					} //end data.getAttribute("division") == "tncs" IF
					
					var cosponsors = data.getElementsByTagName("cosponsor");
					if (cosponsors.length > 0) {
						content.appendChild(makeStrong("Co-Sponsored by: "));
						content.appendChild(makeBr());
						for (i = 0; i < cosponsors.length; i++) {
							content.appendChild(document.createTextNode(cosponsors[i].getAttribute("name")));
							content.appendChild(makeBr());
						} //end i = 0; i < cosponsors.length; i++ FOR
					} //end cosponsors.length > 0 IF
					
					var websites = data.getElementsByTagName("website");
					if (websites.length > 0) {
						content.appendChild(makeStrong("For more information, visit: "));
						content.appendChild(makeBr());
						for (i = 0; i < websites.length; i++) {
							var webLink = document.createElement("a");
							webLink.setAttribute("href",websites[i].getAttribute("url"));
							webLink.setAttribute("target","_blank");
							webLink.appendChild(document.createTextNode(websites[i].getAttribute("url")));
							content.appendChild(webLink);
							content.appendChild(makeBr());
						} //end i = 0; i < websites.length; i++ FOR
					} //end websites.length > 0 IF
					
					var divisionLink = document.createElement("a");
					divisionLink.setAttribute("class","small");
					divisionLink.setAttribute("href","?s=events&d="+data.getAttribute("division"));
					divisionLink.appendChild(document.createTextNode("Learn more about "+data.getAttribute("divisionname")+" »"));
					content.appendChild(divisionLink);
				break;
				case "s":
					var data = xmlDoc.getElementsByTagName("staff")[0];
					
					//Place all the divisions this person works in into an array so that we can call the implode() function for display
					var divisions = new Array();
					var divs = data.getElementsByTagName("division");
					for (i = 0; i < divs.length; i++) {
						divisions.push(divs[i].getAttribute("name").replace(/amp;/,""));
					} //end i = 0; i < divs.length; i++ FOR
			
					var header = document.createElement("div");
					header.setAttribute("id","details_header");
					header.appendChild(document.createTextNode(data.getAttribute("name")));
					target.appendChild(header);
					
					var closeBox = document.createElement("div");
					closeBox.setAttribute("id","details_close");
					if (document.all) {
						closeBox.setAttribute("onclick",hideDetails);
					} else {
						closeBox.setAttribute("onclick","hideDetails();");
					} //end document.all IF
					closeBox.setAttribute("title","Hide Details");
					closeBox.appendChild(document.createTextNode("X"));
					target.appendChild(closeBox);
					
					var content = document.createElement("div");
					content.setAttribute("id","details_content");
					
					content.appendChild(document.createTextNode((data.getAttribute("category") == 2 ? "Event Manager - "+implode(", ",divisions) : data.getAttribute("position"))+" | "));
					var emailLink = document.createElement("a");
					emailLink.setAttribute("href","mailto:"+data.getAttribute("email"));
					emailLink.setAttribute("title","Send an email to "+data.getAttribute("name"));
					emailLink.appendChild(document.createTextNode(data.getAttribute("email")));
					content.appendChild(emailLink);
					content.appendChild(makeBr());
					
					if (data.getAttribute("year") != "NULL") {
						content.appendChild(makeStrong("Year: "));
						content.appendChild(document.createTextNode(data.getAttribute("year")));
						content.appendChild(makeBr());
					} //end data.getAttribute("year") != "NULL" IF
					if (data.getAttribute("major") != "NULL") {
						content.appendChild(makeStrong("Major: "));
						content.appendChild(document.createTextNode(data.getAttribute("major")));
						content.appendChild(makeBr());
					} //end data.getAttribute("major") != "NULL" IF
					if (data.getAttribute("hometown") != "NULL") {
						content.appendChild(makeStrong("Hometown: "));
						content.appendChild(document.createTextNode(data.getAttribute("hometown")));
						content.appendChild(makeBr());
					} //end data.getAttribute("hometown") != "NULL" IF
					if (data.getAttribute("interests") != "NULL") {
						content.appendChild(makeStrong("Interests:"));
						content.appendChild(makeBr());
						var fragments = data.getAttribute("interests").split("::BR::");
						for (i = 0; i < fragments.length; i++) {
							content.appendChild(document.createTextNode(fragments[i]));
							content.appendChild(makeBr());
						} //end i = 0; i < fragments.length; i++ FOR
					} //end data.getAttribute("interests") != "NULL" IF
				break;
			} //end category SWITCH
		} //end xmlDoc.getElementsByTagName("error").length > 0 IF
		target.appendChild(content);
		
		//Fade in
		fade(target,"in");
	} //end http.callback = function(response)
	http.makeRequest("ajax/details.php","for="+category+"&id="+id);
} //end fetchData
function hideDetails() {
	var target = document.getElementById("details");
	fade(target,"out");
} //end hideDetails

//Forum Emoticon Core
function addContent(what,target) {
	document.getElementById(target).value += what;
} //end addContent