/***********************************************************************************************
*	Title:				projectlist.js
*	Version:			1.0.0
*
*	Date Created:	10/24/2006
*	Updated On:		03/19/2007
*	
*	Create By: 		Brian Bal
*	Company: 			Washtenaw County Road Commission
*
*	Description:	builds, displays, and controls the project list
*	Requires:			parse-roadwork.js v1.0.0
*
*	Changes:		
*								+ 0.9.2.2 - added id attribute
*								+ 0.9.2.1 - add path param to load()
*								+ 0.9.2.1 - link to project page pass year as well
*
*	Bugs:		
*								+ fixed multiple township bug with version 0.9.2
*
***********************************************************************************************/

var xmlDoc;
var req;
var projectsA = new Array();
var year;

function load(path, theyear){
	year = theyear;
	xmlDoc = getXMLDocument();
	sendRequest(path);
}


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;
	projectsA = parseXML();
	projectsA.sort(sortByTitle);
	updateProjectTable("Any", 1);
}
			
			
			
// this function takes two strings as a,b
// returns: -1, 1 or 0 depending on sort
function sortByTitle(a,b)
{
	if(a['title'] < b['title']){
		return -1;
	}else if(a['title'] > b['title']){
		return 1;
	}else{
		return 0;
	}
}




/****************************************************************
*	Function: updateProjectTable(twpName, page)
*	Parameters:
*		twpName - string of the twpName exp -> "Ann Arbor"
*		page - int of the page to display -> 1-n
*	Description:	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.
****************************************************************/
function updateProjectTable(twpName, page)
{	
	// build the list of projects to display
	// this needs to happen first so we know whats up
	var projects = new Array();
	for( i = 0; i < projectsA.length; i++)
	{
		tbar = false;
		matchStr = projectsA[i]['township'];
		
		// cycle through all of the townships for this project to see if
		// it could be a match
		if (matchStr.match(twpName) || twpName == "Any")
		{
			if(document.twp_form.twp_current.checked){
				if(projectsA[i]['complete'] != "yes"){
					tbar = true;
				}
				}else{
					tbar = true;
				}
				
		}//end of if
		
		if(tbar == true){
			projects.push(projectsA[i]);
		}
		
	}//end of for i
	
	
	
	//Initialize variables
	var projectsPerPage = 15;
	var pages = Math.ceil(projects.length / projectsPerPage);
	var pageLinks = "";
	var pt = "";
	var lastP = 14;
	var offset = projectsPerPage * (page - 1);
	
	
	// get the title of the page
	if(twpName == "Any"){
		$('projectlist_name').innerHTML = "<h1>" + year + " Project List</h1>";
	}else{
		$('projectlist_name').innerHTML = "<h1>" + twpName + "Twp. " + year + " Project List</h1>";
	}
	
	
	// 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 * projectsPerPage) == offset)
		{
			pageLinks = pageLinks + "| " + fu + " ";
		}else{
			pageLinks = pageLinks + "| <a href=\"javascript:updateProjectTable(document.twp_form.twp_select.value," + fu + ")\">" + fu + "</a> ";
		}
	}
	pageLinks = pageLinks + "<p>";
	
	
	// start building the table
	var pt = "<table><tr>" +
	"<th>Title</th>" +
	"<th>Description</th>" +
	"<th>Township</th>" +
	"<th>Schedule</th></tr>";
	
	
	// find the index of the last project to display
	lastP = projectsPerPage + offset;
	if(lastP > projects.length){
		lastP = projects.length;
	}
	
	// init var oe use for add and even toggles
	var oe = 0;
	var trclass = ""
	
	
	for( i = offset; i < lastP; i++)
	{
		oe ++;
		trclass = "";
		if((oe % 2) == 0){ trclass = " class=\"even\""; }
		
		pt = pt + getTableRow(projects[i], trclass);
	}
	pt = pt + "</table>";
	$('projectlist').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(project, trclass)
{
	var pt = ""
	pt = pt + "<tr" + trclass + ">";
	pt = pt + "<td> <a href=\"projectpage.htm?year=" + year + "&project=" + project['id'] + "\">" +
	project['title'] + "</a></td>";
	pt = pt + "<td>" + project['desc'] + "</td>";
	pt = pt + "<td>" + project['township'] + "</td>";
	pt = pt + "<td>" + project['schedule'] + "</td>";
	pt = pt + "</tr>";
	return pt;
}



