// general.js functions
var UNDEFINED;

function showSubList(node) {
	node.parentNode.className="expanded";
	node.onclick=function() { hideSubList(node); }
}

function hideSubList(node) {
	node.parentNode.className="collapsed";
	node.onclick=function() { showSubList(node); }
}

function show(id) {
	_(id).style.visibility = "visible";
}
function hide(id) {
	_(id).style.visibility = "hidden";
}
function highlight(obj, bool) {
	if (bool == true || bool == UNDEFINED)
		obj.style.backgroundColor = '#FF0';
	else
		obj.style.backgroundColor = '#FFF';
}
function focusDefault(obj, txt)
{
	if (typeof(obj) == "string")
		obj = _(obj);

	if (obj.value == txt)
	{
		obj.value = '';
		if (obj.className == "default")
			obj.className = '';
	}
}
function blurDefault(obj, txt)
{
	if (typeof(obj) == "string")
		obj = _(obj);

	if (obj.value == '')
	{
		obj.value = txt;
		if (obj.className == '')
			obj.className = 'default';
	}
}

function showEmail(id) {
	/*var img = document.getElementById('email' + id);
	img.parentNode.parentNode.replaceChild(img, img.parentNode);
	img.src = "/people/?email=" + id;*/
	makerequest('http://www.iue.edu/people/', getEmail, 'POST', 'email=' + id);
}

function getEmail(str) {
	var o = JSON.parse(str);
	if(o) {
		var i = _('email' + o.id);
		var a = i.parentNode;
		//i.parentNode.parentNode.replaceChild(i, i.parentNode);
		i.src = "/people/?email=" + o.id;
		i.alt = o.alt;
		a.onclick = null;
		a.href = "mailto:" + o.alt;
	}
}

function showActiveList(pageId) {
		var theId=pageId;
		if (theId=='') return;
		theId='nav-' + theId;
		obj=_(theId);
		if (!obj) return;
//		alert(obj.parentNode.parentNode.innerHTML);
		obj.parentNode.parentNode.parentNode.className='expanded';
}

function _(elementId) {
	return document.getElementById(elementId);
}

// There are some problems with IE6 and AJAX -- force redirect if they're using IE6.
function isIE6(){
	var browser = navigator.appName;
	var b_version = navigator.appVersion;
	var version = parseFloat(b_version);
	if ((browser == "Microsoft Internet Explorer") && (version < 7))
		return true;
	else
		return false;
}

// from the ajax_lite file
<!-- Code r0x0red by IU East Web Team -->
function GetXHR()
{
  var XHR=null;
  try
    {
    // Firefox, Opera 8.0+, Safari
    XHR=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      XHR=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      XHR=new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
  return XHR;
}

function makerequest(serverPage, obj, getOrPost, str)
{
	// These three lines allow legacy functionality with older
	// versions of the functions, which didn't use the 3rd and
	// 4th parameters. Old uses still need to now pass the 
	// actual object instead of just the ID of the object.
	var undefined;
	if (str == undefined) { str = null; }
	if (getOrPost == undefined || getOrPost.toUpperCase() != "POST") 
	{ getOrPost = "GET"; }
	
	var XHR;
	
	if(XHR = GetXHR()) {
		XHR.open (getOrPost, serverPage, true);
		if (getOrPost.toUpperCase() == "POST")
		{
			XHR.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
			XHR.setRequestHeader("Content-Length", str.length);
			XHR.setRequestHeader("Connection", "close");
		}
		XHR.onreadystatechange = function() {
			if (XHR.readyState == 4 && XHR.status == 200 && obj != null)
			{
				switch(typeof(obj)) {
					// Allows for AJAX chaining //
					case "function":
						obj(XHR.responseText);
						break;
					case "object":
						if (obj.innerHTML)	
						{ 
							obj.innerHTML = XHR.responseText;
						}
						else obj.src = XHR.responseText;
						break;
					
					default:
						obj = XHR.responseText; 
						break;
				}
	
			}
		}
		XHR.send(str);
	}
}


/* parses JSON objects quickly and cleanly */
var JSON = {
	"re": /^(\s|[,:{}\[\]]|"(\\["\\bfnrtu]|[^\x00-\x1f"\\])*"|-?\d+(\.\d*)?([eE][+-]?\d+)?|true|false|null)+$/, 
	"parse": function (text) {
				return this.re.test(text) ? eval('(' + text + ')') : undefined;
			 }
};

/* bug reporting functions */
//for stupid ie
//getKeyCode=function(e){try{return e.which;}catch(err){return event.keyCode;}}
//grabs keystrokes & checks for=='bug' on \n
//var kbb=null;
//document.onkeypress=function(e){if(!_('bug-background')){if(getKeyCode(e)!=13){if(kbb==null){kbb='';}kbb+=String.fromCharCode(getKeyCode(e));}else{makerequest('/pub/php/',displayBugReport,'post',kbb+'=');kbb=null;}}}
submitbugreport=function(){
	var submitter=escape(_('bugreportname').value.replace(/\+/g,"\&#43;"));
	var desc=escape(_('bugreportdesc').value.replace(/\+/g,"\&#43;"));
	var title=escape(document.getElementsByTagName('title')[0].innerHTML.replace(/\+/g,"\&#43;"));
	if(submitter=='Name')submitter='';
	if(desc=='Description')desc='';
	makerequest("http://www.iue.edu/pub/php/bug-reporter.php",submitted,"POST","submitter="+submitter+"&desc="+desc+"&title="+title);
	return false;
}
submitted=function(str){var obj=JSON.parse(str);if(obj){alert(obj.msg);}clearBugReport();}
getBugs=function(){makerequest("http://www.iue.edu/pub/php/bug-reporter.php?bugs",displayBugs,"GET");}

displayBugs=function(str) {
	var o=(str?JSON.parse(str):null);
	if(o&&_('bug-background')) {
		if(o.existing) {
			var ex=o.existing;
			var s='<p><strong>'+(ex.bugs.length>1?'Some bugs have':'A bug has')+' already been reported for this page. Please check below to make sure you\'re not re-reporting a bug!'+'</strong></p><dl id="buglist">';
			for(var i=0,e=ex.bugs[i];i<ex.bugs.length;e=ex.bugs[++i])
			{
				//s+='<li'+(e.fixed!=null?' style="text-decoration:line-through;" title="Fixed '+(e.fixed==0?'the same day':(e.fixed==1?'the next day':'after '+e.fixed+' days'))+'"':'')+'>
				s += '<dt><a title="'+e.ip+'">['+e.submitter+']</a></dt><dd>'+e.desc+' - <em>Reported '+(e.daysago==0?'Today':e.daysago+' days ago')+'</em></dd>';
			}
			_('existing-bugs').innerHTML=s+'</ul>';
		}
	}
}
displayBugReport=function(str) {
	var bg=document.createElement('div'),wrap=document.createElement('div');
	bg.id='bug-background';wrap.id='bug-wrapper';
	wrap.innerHTML = str;
	document.body.appendChild(bg);document.body.appendChild(wrap);
	window.location.hash="bug-reporter";
	getBugs();
}
clearBugReport=function(){var b=_('bug-wrapper');b.parentNode.removeChild(b);b=_('bug-background');b.parentNode.removeChild(b);}