/***********************************************************************************************
*	Title:				contactlist.js
*	Version:			1.0.0
*
*	Date Created:	12/11/2006
*	Updated On:		05/24/2007
*	
*	Create By: 		Brian Bal
*	Company: 			Washtenaw County Road Commission
*
*	Description:	builds, displays, and controls the staff directory table
*	Requires:			parse-contacts.js v1.0.0
*
*	Changes:		
*								+ 1.0.0 Now displays Department/Section column
*								+ 0.6.0.2 Change display to show hCard microformats
*								+ 0.6.0.1 If email is NA then provide no link
*								+ 0.6.0.0 Multiple departement for one contact
*								+ 0.5.0.0 cleaned up code and bug fixes
*								+ 0.4.1.0 no longer displays page links above table
*
*	Bugs:
*								+ 1.0.0		Fixed: when positions vacant N/A no longer show for
													first name.
*								+ 0.5.0.0 Fixed: dept. jump menu not reseting on page refresh
*								+ 0.4.1.1 Fixed: table even class attribute problem
*								+ 0.4.1.0 Fixed: table display problem in IE
*
***********************************************************************************************/

var xmlDoc;
var req;
var contacts = new Array();

function load(){
	$('department_select').options[0].selected = true;
	xmlDoc = getXMLDocument();
	sendRequest("./contacts.xml");
}


function getXMLDocument(){
	var xDoc=null;
	if (document.implementation && document.implementation.createDocument)
	{
		xDoc=document.implementation.createDocument("","",null);
	}else if (typeof ActiveXObject != "undefined"){
		var msXmlAx=null;
		try{
			msXmlAx=new ActiveXObject("Msxml2.DOMDocument");
		}catch (e){
			msXmlAx=new ActiveXObject("Msxml.DOMDocument");
		}
		xDoc=msXmlAx;
	}
	if(xDoc=null || typeof xDoc.load=="undefined"){
		xDoc=null;
	}
	return xDoc;
}


function getXMLHTTPRequest()
{
	var xRequest=null;
	if(window.XMLHttpRequest){
		xRequest=new XMLHttpRequest();
	}else if (typeof ActiveXObject != "undefined"){
		xRequest=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return xRequest;
}


function sendRequest(url,params,HttpMethod)
{
	if (!HttpMethod){
		HttpMethod="GET";
	}
	if (!params){
		params="";
	}
	req=getXMLHTTPRequest();
	if(req){
		req.onreadystatechange=onReadyStateChange;
		req.open("GET", url, true);
		req.send("");
	}
}


function onReadyStateChange()
{
	var ready=req.readyState;
	if(ready==4){
		if (req.status == 200) {
			var data=null;
			data=req.responseXML;
			loadPage(data);
		}else{
			alert("There was a problem retrieving the XML data:\n" +
                req.statusText);	
		}
	}
}


// this function will load the page and all of its elements
// it also fills the projectsA array with all projects
// calls the update project function with default settings
function loadPage(data)
{
	xmlDoc = data;
	contacts = parseXML();
	contacts.sort(sortByLast);
	updateContactTable("Any", 1);
}
			
			
			
// this function takes two strings as a,b
// returns: -1, 1 or 0 depending on sort
function sortByLast(a,b)
{
	if(a['last'] < b['last']){
		return -1;
	}else if(a['last'] > b['last']){
		return 1;
	}else{
		return 0;
	}
}




// This function will print the projects for a
// given township on the given page.  It will also check to see
// if the current projects only box is checked.  In that case
// will display only projects that are not complete.
// Parameters:
//	twpName - string of the twpName exp -> "Ann Arbor"
//	page - int of the page to display -> 1-n
function updateContactTable(department, page)
{
	// build the list of projects to display
	// this needs to happen first so we know whats up
	var contactsList = new Array();
	for( i = 0; i < contacts.length; i++)
	{
		deptStr = contacts[i]['department'];
		deptStr = deptStr.match(department)
		
		if ( deptStr == department || department == "Any")
		{
			contactsList.push(contacts[i]);
		
		}//end of if
		
	}//end of for i
	
	
	//Initialize variables
	var PerPage = 15;
	var pages = Math.ceil(contactsList.length / PerPage);
	var pageLinks = "";
	var pt = "";
	var lastP = 14;
	var offset = PerPage * (page - 1);
	
	
	// set the title of the page
	if(department == "Any"){
		$('contactlisttitle').innerHTML = "Staff Directory";
	}else{
		$('contactlisttitle').innerHTML = department + " Directory";
	}
	
	
	// build the page links
	pageLinks = "<p class=\"page-links\">Pages ";
	for( i = 0; i < pages; i++)
	{
		fu = i + 1;
		
		// display a link to the page
		// unless it is the current page
		if((i * PerPage) == offset)
		{
			pageLinks = pageLinks + "| " + fu + " ";
		}else{
			pageLinks = pageLinks + "| <a href=\"javascript:updateContactTable(document.contact_frm.department_select.value," + fu + ")\">" + fu + "</a> ";
		}
	}
	pageLinks = pageLinks + "<p>";
	
	
	// start building the table
	var pt = "<table> <tr> <th>Name</th> <th>Title</th> <th>Phone</th> <th>Email</th> <th>Department</th></tr>";
	
	
	// find the index of the last project to display
	lastP = PerPage + offset;
	if(lastP > contactsList.length){
		lastP = contactsList.length;
	}
	
	// variables for row class
	// provides alternating row colors
	var oe = 0;
	var trclass = ""
	
	for( i = offset; i < lastP; i++)
	{
		oe ++;
		trclass = "";
		if((oe % 2) == 0){ trclass = "rel=\"even\""; }
		
		pt = pt + getTableRow(contactsList[i], trclass);
	}
	pt = pt + "</table>";
	$('contactlist').innerHTML = pageLinks + pt + pageLinks;
	
}//end of update project table


// this function will return a string with the html for a table row
// parameters
//	-project - an array that holds all the project info, here it used like a basic class
//	-trclass - is a string that hold the class attribute of the row if any, used to alternate
//			 the colors of the rows for now.
function getTableRow(contact, trclass)
{
	var tr = ""
	tr = tr + "<tr class=\"vcard\" "+ trclass + ">";
	
	if (contact['last'] == 'Vacant') {
		tr = tr + "<td class=\"fn\">" + contact['last'] + "</td>";
	} else {
		tr = tr + "<td class=\"fn\">" + contact['last'] + ", " + contact['first'] + "</td>";
	}
	
	tr = tr + "<td class=\"title\">" + contact['title'] + "</td>";
	tr = tr + "<td class=\"tel\">" + contact['phone'] + "</td>";
	if (contact['email'] == "N/A")
	{
		tr = tr + "<td>" + contact['email'] + "</td>";
	}else{
		tr = tr + "<td class=\"email\">" + "<a href=\"mailto:" + contact['email'] + "\">" + contact['email'] + "</a></td>";
	}
	tr = tr + "<td>" + contact['department'] + "</td>";
	tr = tr + "</tr>";
	return tr;
}


