
/*
	Who'sWho? - people / employee renderer v1.0, by Martin Latter (2004) ~ http://www.copysense.co.uk/
	This application was developed for an intranet system - quickness in sorting, not data security.
	Thanks to Alex Vincent for the function cleanWhiteSpace() - for Gecko browsers.
	innerHTML used for fast rendering speed with big lumps of data (see http://www.quirksmode.org/).

	This application is licensed under the Creative Commons License Attribution 2.0 License (http://creativecommons.org/licenses/by/2.0/) - basically it means you are free to enhance this code to meet your needs but please credit me as the original author; and for any reuse or distribution you need to make clear to others the license terms.
*/

var xmlFile = "http://www.wright.edu/biology/department/directory/directory.xml";
var imgDir = "http://www.wright.edu/biology/department/directory/";  // directory path for images

/* ************************************************** */


var personArr = new Array();

function loadXML()
{
	var xmlDoc;

	try
	{
		if (window.ActiveXObject)
		{
			xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
			xmlDoc.onreadystatechange = function() { if (xmlDoc.readyState==4) grabPeopleData(xmlDoc, 0); }
			xmlDoc.load(xmlFile);
		}

		else if (document.implementation && document.implementation.createDocument)
		{
			xmlDoc = document.implementation.createDocument('','doc',null);
			xmlDoc.onload = function() { grabPeopleData(xmlDoc, 1); }
			xmlDoc.load(xmlFile);
		}

		else alert('Sorry, this browser is not XML-compliant and cannot render the XML data.');
	}

	catch(e)
	{
		alert ('There was an error (' + e + ') attempting to load the XML document');
	}
}

function grabPeopleData(xmldoc, f)
{
	var i = 0;
	var people = xmldoc.getElementsByTagName('people')[0];

	if (f) cleanWhiteSpace(people);
	// Alex Vincent's cleanWhiteSpace function is a slow method (other methods are twice+ as fast), but it's neat and highly suitable in situations like this where users may want to alter the XML nodes being parsed.

	var firstName = '', lastName = '', position = '',  dept = '', role = '', image = '';
	var numPeople = people.childNodes.length;

	for (; i < numPeople; i++)
	{
		firstName = people.childNodes[i].childNodes[0].firstChild.nodeValue;
		lastName = people.childNodes[i].childNodes[1].firstChild.nodeValue;
		position = people.childNodes[i].childNodes[2].firstChild.nodeValue;
		dept = people.childNodes[i].childNodes[3].firstChild.nodeValue;
		role = people.childNodes[i].childNodes[4].firstChild.nodeValue;
		image = people.childNodes[i].childNodes[5].firstChild.nodeValue;

		personArr[i] = new person(firstName, lastName, position, dept, role, image);
	}
}

function person(firstName, lastName, position, dept, role, image)
{
	this.firstName = firstName;
	this.lastName = lastName;
	this.position = position;
	this.dept = dept;
	this.role = role;
	this.image = image;
}

function cleanWhiteSpace(node)
{
	// function by Alex Vincent

	var notWS = /\S/;
	var cN, i;

	for (i=0; i< node.childNodes.length; i++)
	{
		cN=node.childNodes[i];

		if ((cN.nodeType == 3) && (!notWS.test(cN.nodeValue)))
		{
			node.removeChild(node.childNodes[i]);
			i--;
		}

		if (cN.nodeType == 1) cleanWhiteSpace(cN);
	}
}

function sortit(n)
{
	switch (n)
	{
		case 1:
			personArr.sort(sortFname);
			displayPeople();
			break;

		case 2:
			personArr.sort(sortLname);
			displayPeople();
			break;
	}
}

function sortFname(a,b)
{
	var x = a.firstName.toLowerCase();
	var y = b.firstName.toLowerCase();
	if (x < y) return -1;
	else if (x > y) return 1;
	else return 0;
}

function sortLname(a,b)
{
	var x = a.lastName.toLowerCase();
	var y = b.lastName.toLowerCase();
	if (x < y) return -1;
	else if (x > y) return 1;
	else return 0;
}

function displayPeople(depstr)
{
	var numPeople = personArr.length;
	var i = 0;
	var os = '<p>';
	var temp = '';

	if (!depstr)
	{
		for (; i< numPeople; i++)
		{
			os += '<a href="javascript:personDisplay('+i+')" class="personlink">' + personArr[i].firstName + ' ' + personArr[i].lastName + '<\/a>';
		}
	}

	else
	{
		// sortit(2);
	
		for (; i< numPeople; i++)
		{
			if (personArr[i].dept == depstr)
			{
				os += '<a href="javascript:personDisplay('+i+')" class="personlink">' + personArr[i].firstName + ' ' + personArr[i].lastName + '<\/a>';
			}
		}
	}

	os += '<\/p>';
	document.getElementById('indexWin1').innerHTML = os;
}

function personDisplay(n)
{
	var os = '';
	os += '<p><strong>' + personArr[n].firstName + ' ' + personArr[n].lastName + '<\/strong><br>';
	os += '<em>' + personArr[n].position + '<\/em><br>';

	if (personArr[n].image.indexOf(".") > 1)
	{
		os += '<img src="' + imgDir + personArr[n].image + '" alt="' + personArr[n].firstName + ' ' + personArr[n].lastName + '"><br>';
	}

	os += personArr[n].role;
	os += '<\/p>';
	document.getElementById('personWin1').innerHTML=os;
}

function retern()
{
	document.getElementById('indexWin1').style.visibility='visible';
	document.getElementById('personWin1').style.visibility='visible';
}

function extwin(f)
{
	document.getElementById('indexWin1').style.height = h + 'px';
	document.getElementById('personWin1').style.height = h + 'px';
}

function init()
{
	loadXML();
	window.setTimeout("displayPeople('Tenured')", 500);
}

window.onload = init;