// ---------------------------------------------------------------------
// JavaScript Functions
// ---------------------------------------------------------------------
// Global JavaScript functions to be used throughout the site.
// ---------------------------------------------------------------------






// ---------------------------------------------------------------------
// prometheusClick                                rdh043000@utdallas.edu
// ---------------------------------------------------------------------
// This functions handles different operations based on where you click
// on the website.
// ---------------------------------------------------------------------
function prometheusClick(element) {
	var element = window.event ? window.event:element;
	var target = window.event ? element.srcElement:element.target;
	if (!target) { return; }
	if (target.nodeType == 3) { target = target.parentNode; }

	// student type drop down box
//	if ((target.id == "studenttype_value") || (target.id == "studenttype_image") || (target.className == "studenttype_hover")) {
//		if (document.getElementById('studenttype_options').style.display == "none") { document.getElementById('studenttype_options').style.display = ''; }
//		else { document.getElementById('studenttype_options').style.display = 'none'; }
//		if (target.className == "studenttype_hover") {
//			document.getElementById("studenttype_value").innerHTML = target.innerHTML;
//			var xhttp = window.XMLHttpRequest ? new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
//			xhttp.open("POST",urlroot+"functions/studenttype.php");
//			xhttp.onreadystatechange=function() { xhttp.close; }
//			xhttp.send("studenttype="+target.innerHTML);
//			document.getElementById('studenttype_options').style.display = 'none';
//		}
//	}

	// search box
	else if ((target.id == "searchbox") && (target.value == "Search Keywords")) { target.value = ''; }

	else { 
		if (document.getElementById('searchbox').value == '') { document.getElementById('searchbox').value = 'Search Keywords'; }
//		document.getElementById('studenttype_options').style.display = 'none'; 
	}
}
// ---------------------------------------------------------------------



// ---------------------------------------------------------------------
// prometheusFooter                               rdh043000@utdallas.edu
// ---------------------------------------------------------------------
// Keeps the footer at the bottom of the page.
// ---------------------------------------------------------------------
window.onload = function() { prometheusFooter(); setInterval('prometheusFooter();',1000); }
window.onresize = function() { prometheusFooter(); }
function wnheight() {
	var wheight = 0;
	if (typeof(window.innerHeight) == 'number') { wheight = window.innerHeight; }
	else {
		if ((document.documentElement) && (document.documentElement.clientHeight)) { wheight = document.documentElement.clientHeight; }
		else if ((document.body) && (document.body.clientHeight)) { wheight = document.body.clientHeight; }
	}
	return wheight;
}
function prometheusFooter() {
	var wheight = wnheight();
	if (wheight > 0) {
		var cheight = document.getElementById('content').offsetHeight;
		var element = document.getElementById('footer');
		var fheight = element.offsetHeight;
		if ((wheight-(cheight+fheight)) >= 0) {
			element.style.position = 'absolute';
			element.style.bottom = '0';
			element.style.width = '100%';
		}
		else { element.style.position = 'static'; element.style.bottom = '0px'; }
	}
}
// ---------------------------------------------------------------------



// ---------------------------------------------------------------------
// prometheusNews                                 rdh043000@utdallas.edu
// ---------------------------------------------------------------------
// News feed.
// ---------------------------------------------------------------------
function prometheusNews(action) {
	var xhttp = window.XMLHttpRequest ? new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
	xhttp.onreadystatechange=function () { xhttp.close; if (xhttp.readyState == 4) { document.getElementById('newsarea').innerHTML = xhttp.responseText; } }
	xhttp.open("POST",urlroot+"functions/newsbar.php");
	if (typeof(action) == 'undefined') { 
		xhttp.send(null);
	}
	else if (action == 'n') { xhttp.send('action=next'); }
	else if (action == 'p') { xhttp.send('action=prev'); }
}
// ---------------------------------------------------------------------



// ---------------------------------------------------------------------
// Toggle Display                                 rdh043000@utdallas.edu
// ---------------------------------------------------------------------
// Toggle's an elements display.
// ---------------------------------------------------------------------
function tdisplay(element) {
	if (document.getElementById(element).style.display == "none") { document.getElementById(element).style.display = ''; }
	else if (document.getElementById(element).style.display == "") { document.getElementById(element).style.display = 'none'; }
}
// ---------------------------------------------------------------------



// ---------------------------------------------------------------------
// Toggle Class                                   rdh043000@utdallas.edu
// ---------------------------------------------------------------------
// Toggle's an elements class.
// ---------------------------------------------------------------------
function tclass(element,class1,class2) {
	var tnmle = document.getElementById(element) ? document.getElementById(element):element;

	if (tnmle.className == class1) { tnmle.className = class2; }
	else if (tnmle.className == class2) { tnmle.className = class1; }
}
// ---------------------------------------------------------------------



// ---------------------------------------------------------------------
// Left Navigation Management                     rdh043000@utdallas.edu
// ---------------------------------------------------------------------
// Manages when left navigation is clicked. Only way to collapse a menu
// is if you click it twice if you are not on that page, once if you 
// are on that page.
// ---------------------------------------------------------------------
function leftnav(element,idnum,ahref,lastclick,lpage) {
	var xhttp = window.XMLHttpRequest ? new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
	xhttp.onreadystatechange=function () { xhttp.close; if (xhttp.readyState == 4) { location.href = ahref; } }
	xhttp.open("POST",urlroot+"functions/leftnav.php");

	if ((element.className == "leftnav_expanded") && (element.id == 'leftnav_'+lastclick)) {
		tdisplay('leftnav_'+idnum+'_subs');
		tclass(element,'leftnav_collapsed','leftnav_expanded');
		xhttp.send("idnum="+idnum+"&toggle=true");
	}
	else if (element.className == "leftnav_expanded") { xhttp.send("idnum="+idnum+"&toggle=false"); }
	else if (element.className == "leftnav_collapsed") {
		tdisplay('leftnav_'+idnum+'_subs');
		tclass(element,'leftnav_collapsed','leftnav_expanded');
		xhttp.send("idnum="+idnum+"&toggle=true");
	}


}
// ---------------------------------------------------------------------



// ---------------------------------------------------------------------
// Top Navigation Sub Menu Positioner             rdh043000@utdallas.edu
// ---------------------------------------------------------------------
// Positions the top navigation sub menus.
// ---------------------------------------------------------------------
function topnav_posit(object,moveelement) {
	var object = document.getElementById(object);
	var curleft = curtop = 0;
	if (object.offsetParent) {
		curleft = object.offsetLeft
		curtop = object.offsetTop
		while (object = object.offsetParent) {
			curleft += object.offsetLeft
			curtop += object.offsetTop
		}
	}
	document.getElementById(moveelement).style.left = curleft+'px';
	document.getElementById(moveelement).style.top = curtop+25+'px';
}
// ---------------------------------------------------------------------



// -----------------------------------------------------------
// AJAX Get Content                     rdh043000@utdallas.edu
//                                            Phoenix Function
// -----------------------------------------------------------
// Get content for dashboard and other items.
// -----------------------------------------------------------
function ajaxf_gcnte(divid,fpath,sendt,dtype) {
	var xhttp = window.XMLHttpRequest ? new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHTTP');
	xhttp.onreadystatechange = function () {
	if (xhttp.readyState == 4) {
		if ((typeof(dtype) == 'undefined') || (dtype == 1)) { document.getElementById(divid).innerHTML = xhttp.responseText; }
		else if (dtype == 2) {
			if (xhttp.responseText.substr(0,4) == 'true') { document.getElementById(divid).checked = true; }
			else { document.getElementById(divid).checked = false; }
			}
			else if (dtype == 3) { document.getElementById(divid).value = xhttp.responseText; }
			xhttp.close;
		}
	}
	xhttp.open('POST',fpath);
	xhttp.send('divid='+divid+'&fpath='+fpath+'&'+sendt);
}
// -----------------------------------------------------------



// -----------------------------------------------------------
// Scrollable Checkbox list             wsm013000@utdallas.edu
// Java script functions
// -----------------------------------------------------------
// Functions to build a scrollable list of checkboxes
// -----------------------------------------------------------


	/*-----------------------------------------------------------+
		 | addLoadEvent: Add event handler to body when window loads |
		 +-----------------------------------------------------------*/
		function addLoadEvent(func) {
			var oldonload = window.onload;
			
			if (typeof window.onload != "function") {
				window.onload = func;
			} else {
				window.onload = function () {
					oldonload();
					func();
				}
			}
		}
		
		/*------------------------------------+
		 | Functions to run when window loads |
		 +------------------------------------*/
		addLoadEvent(function () {
			initChecklist();
		});
		
		/*----------------------------------------------------------+
		 | initChecklist: Add :hover functionality on labels for IE |
		 +----------------------------------------------------------*/
		function initChecklist() {
			if (document.all && document.getElementById) {
				// Get all unordered lists
				var lists = document.getElementsByTagName("ul");
				
				for (i = 0; i < lists.length; i++) {
					var theList = lists[i];
					
					// Only work with those having the class "checklist"
					if (theList.className.indexOf("checklist") > -1) {
						var labels = theList.getElementsByTagName("label");
						
						// Assign event handlers to labels within
						for (var j = 0; j < labels.length; j++) {
							var theLabel = labels[j];
							theLabel.onmouseover = function() { this.className += " hover"; };
							theLabel.onmouseout = function() { this.className = this.className.replace(" hover", ""); };
						}
					}
				}
			}
		}
// -----------------------------------------------------------