/***********************************************************************************************
*	Title:				parse-roadwork.js
*	Version:			1.0
*
*	Date Created:	10/24/2006
*	Updated On:		04/19/2007
*	
*	Create By: 		Brian Bal
*	Company: 			Washtenaw County Road Commission
*
*	Description:	takes global xmlDoc var and parses it into an array that is returned
*	Works With:		projectmap.js v1.x and projectlist.js v1.x
*
*	Changes:
*								+ 0.9.2 added id to attributes
*
*	Bugs:					
*								+ fixed problem with desc and description tags
*								+ fixed multiple township bug with version 0.9.1
*
***********************************************************************************************/

//this function will take the xmlDoc and parse it into an array
//returns: the array of parseed xml
function parseXML()
{
	// initiate variables
	var projects = xmlDoc.getElementsByTagName("project");
	projectsArray = new Array();
	
	//build the array of projects
	for ( i = 0; i < projects.length; i++)
	{
		var tempArray = new Array();
		
		//get the attributes
		tempArray['id'] = projects[i].getAttribute("id");
		tempArray['title'] = projects[i].getAttribute("title");
		tempArray['onmap'] = projects[i].getAttribute("onmap");
		tempArray['complete'] = projects[i].getAttribute("complete");
		tempArray['lat'] = projects[i].getAttribute("lat");
		tempArray['lng'] = projects[i].getAttribute("lng");
		
		// search for specific tags
		var townships = projects[i].getElementsByTagName("township");
		var locations = projects[i].getElementsByTagName("location");
		var descs = projects[i].getElementsByTagName("desc");
		var descriptions = projects[i].getElementsByTagName("description");
		var schedules = projects[i].getElementsByTagName("schedule");
		var status = projects[i].getElementsByTagName("status");
		var contacts = projects[i].getElementsByTagName("contact");
		var emails = projects[i].getElementsByTagName("email");
		var phones = projects[i].getElementsByTagName("phone");
		var advisory = projects[i].getElementsByTagName("advisory");
		var update = projects[i].getElementsByTagName("update");
		var detours = projects[i].getElementsByTagName("detour");
		
		//
		// add the node value to the array with the node name
		//
		
		// loop through the township nodes list and add them to its own array
		if(townships.length > 0){
			if(townships[0].childNodes.length > 0)
			{
				tempArray['township'] = townships[0].childNodes[0].nodeValue;
			}else{
				tempArray['township'] = "Not Available";
			}
		}else{
			tempArray['township'] = "Not Available";
		}
		
		if(locations.length > 0){
			if(locations[0].childNodes.length > 0)
			{
				tempArray['location'] = locations[0].childNodes[0].nodeValue;
			}else{
				tempArray['location'] = "Not Available";
			}
		}else{
			tempArray['location'] = "Not Available";
		}
		
		if(descs.length > 0){
			if(descs[0].childNodes.length > 0)
			{
				tempArray['desc'] = descs[0].childNodes[0].nodeValue;
			}else{
				tempArray['desc'] = "Not Available";
			}
		}else{
			tempArray['desc'] = "Not Available";
		}
		
		if(descriptions.length > 0){
			if(descriptions[0].childNodes.length > 0)
			{
				tempArray['description'] = descriptions[0].childNodes[0].nodeValue;
			}else{
				tempArray['description'] = "Not Available";
			}
		}else{
			tempArray['description'] = "Not Available";
		}
		
		if(schedules.length > 0){
			if(schedules[0].childNodes.length > 0)
			{
				tempArray['schedule'] = schedules[0].childNodes[0].nodeValue;
			}else{
				tempArray['schedule'] = "Not Available";
			}
		}else{
			tempArray['schedule'] ="Not Available";
		}
					
		if(status.length > 0){
			if(status[0].childNodes.length > 0)
			{
				tempArray['status'] = status[0].childNodes[0].nodeValue;
			}else{
				tempArray['status'] = "Not Available";
			}
		}else{
			tempArray['status'] = "Not Available";
		}
		
		if(contacts.length > 0){
			if(contacts[0].childNodes.length > 0)
			{
				tempArray['contact'] = contacts[0].childNodes[0].nodeValue;
			}else{
				tempArray['contact'] = "Not Available";
			}
		}else{
			tempArray['contact'] = "Not Available";
		}
		
		if(emails.length > 0){
			if(emails[0].childNodes.length > 0)
			{
				tempArray['email'] = emails[0].childNodes[0].nodeValue;
			}else{
				tempArray['email'] = "Not Available";
			}
		}else{
			tempArray['email'] = "Not Available";
		}
		
		if(phones.length > 0){
			if(phones[0].childNodes.length > 0)
			{
				tempArray['phone'] = phones[0].childNodes[0].nodeValue;
			}else{
				tempArray['phone'] = "Not Available";
			}
		}else{
			tempArray['phone'] = "Not Available";
		}
		
		if(advisory.length > 0){
			if(advisory[0].childNodes.length > 0)
			{
				tempArray['advisory'] = advisory[0].childNodes[0].nodeValue;
			}else{
				tempArray['advisory'] = "Not Available";
			}
		}else{
			tempArray['advisory'] = "Not Available";
		}
		
		if(update.length > 0){
			if(update[0].childNodes.length > 0)
			{
				tempArray['update'] = update[0].childNodes[0].nodeValue;
			}else{
				tempArray['update'] = "Not Available";
			}
		}else{
			tempArray['update'] = "Not Available";
		}
		
		if(detours.length > 0){
			if(detours[0].childNodes.length > 0)
			{
				tempArray['detours'] = detours[0].childNodes[0].nodeValue;
			}else{
				tempArray['detours'] = "Not Available";
			}
		}else{
			tempArray['detours'] = "Not Available";
		}
		
		//add the temp array to the projectsArray 
		projectsArray.push(tempArray);
	}//end of for ( i = 0; i < projects.length; i++)
	return projectsArray;
}// end of parseXML

