// render events on page from XML file

var xmlUrl = "xml/ClientFiles/aaanf/currentEvents.xml";

//This function spits the event title, and date
function renderEvents(displayCount)
{
	var xmlDoc=null;
	var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
	if (window.ActiveXObject)
	{
	// code for IE
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	}
	else if (document.implementation && document.implementation.createDocument && is_chrome == false)
	{
		// code for Mozilla, Firefox, Opera, etc.
		xmlDoc=document.implementation.createDocument("","",null);
	}
	else if (is_chrome == true)
	{
		var xmlhttp = new window.XMLHttpRequest();
		xmlhttp.open("GET", xmlUrl, false);
		xmlhttp.send(null);
		xmlDoc = xmlhttp.responseXML.documentElement;
	}
	else
	{
		alert('Your browser cannot handle this script');
	}
	if (xmlDoc!=null) 
	{
		if (is_chrome == false)
		{
			xmlDoc.async=false;
			xmlDoc.load(xmlUrl);
		}
		var x=xmlDoc.getElementsByTagName("EVENT");
		
		if (displayCount == 0)
		{
			displayCount = x.length;
		}
		
		document.write("<ul>");
		
		for (var i=0;i<displayCount;i++)
		{ 
			document.write("<li>");
			document.write("<a href='event.html?newsid=" + x[i].getElementsByTagName("ID")[0].childNodes[0].nodeValue + "'>");
			document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
			document.write("</a>");
			document.write("<br />");
			document.write(x[i].getElementsByTagName("DATE")[0].childNodes[0].nodeValue);
			document.write("</li>");	
		}
		document.write("</ul>");
	}
}

// Grabbed thes code below from: http://support.mozilla.com/tiki-view_forum_thread.php?comments_parentId=87865&forumId=1
// It works fine in IE6 and 7.
// in FF2 and FF3, it loads and parses the xml fine but it appears to get 
// stuck.  See comments in code below!

// render events on page from XML file
//function renderEvents(displayCount)
//{
//	

//	if (window.XMLHttpRequest) 
//	{
//		//Create XMLHttpRequest object in non-Microsoft browsers
//		request = new XMLHttpRequest();
//	}
//	else if (window.ActiveXObject) 
//	{
//		// Create XMLHttpRequest via MS ActiveX
//		try	
//		{
//			// Try to create XMLHttpRequest in later versions
//			// of Internet Explorer
//			request = new ActiveXObject("Msxml2.XMLHTTP");
//		}
//		catch (e1)	
//		{
//			// Failed to create required ActiveXObject
//			try	
//			{
//				// Try version supported by older versions
//				// of Internet Explorer
//				request = new ActiveXObject("Microsoft.XMLHTTP");
//			} 
//			catch (e2)	
//			{
//			// Unable to create an XMLHttpRequest with ActiveX
//			}
//		}
//	}
//	
//	request.open("GET",xmlUrl,true);
//	request.onreadystatechange = function() 
//	{
//		if (request.readyState == 4) 
//		{
//			xmlDoc = request.responseXML;
//			var x=xmlDoc.getElementsByTagName("EVENT");
//					
//			if (displayCount == 0)
//			{
//				displayCount = x.length;
//			}
//		
//			document.write("<ul>");
//			
//			for (var i=0;i<displayCount;i++)
//			{ 
//				document.write("<li>");
//				document.write("<a href='event.html?newsid=" + x[i].getElementsByTagName("ID")[0].childNodes[0].nodeValue + "'>");
//				document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
//				document.write("</a>");
//				document.write("<br />");
//				document.write(x[i].getElementsByTagName("DATE")[0].childNodes[0].nodeValue);
//				document.write("</li>");	
//			}
//			
//			document.write("</ul>");
//			//*******FF2 and FF3 appears to stop right here*******
//			
//		}
//	}
//	request.send(null);
//} 



//Render events by month
function renderEventsByMonth(month)
{
	var xmlDoc=null;
	var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
	//var xmlhttp = new window.XMLHttpRequest();
	//xmlhttp.open("GET", xmlUrl, false);
	//xmlhttp.send(null);
	//var	xmlDoc = xmlhttp.responseXML.documentElement;
	if (window.ActiveXObject)
	{
	// code for IE
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	}
	else if (document.implementation && document.implementation.createDocument && is_chrome == false)
	{
		// code for Mozilla, Firefox, Opera, etc.
		xmlDoc=document.implementation.createDocument("","",null);
	}
	else if (is_chrome == true)
	{
		var xmlhttp = new window.XMLHttpRequest();
		xmlhttp.open("GET", xmlUrl, false);
		xmlhttp.send(null);
		xmlDoc = xmlhttp.responseXML.documentElement;
	}
	else
	{
		alert('Your browser cannot handle this script');
	}
	if (xmlDoc!=null) 
	{
		if (is_chrome == false)
		{
			xmlDoc.async=false;
			xmlDoc.load(xmlUrl);
		}
		var x=xmlDoc.getElementsByTagName("EVENT");
		
		
		
		
		var eventsForMonth = false;
		
		for (var i=0;i<x.length;i++)
		{ 
			if(x[i].getElementsByTagName("MONTH")[0].childNodes[0].nodeValue == month)
			{
				eventsForMonth = true;
				document.write("<h2>"+month+"</h2>");
				break;				
			}
		}
		
		if(eventsForMonth)
		{
			document.write("<ul class=\"link-list calendar\">");
		
			for (var i=0;i<x.length;i++)
			{ 
				if(x[i].getElementsByTagName("MONTH")[0].childNodes[0].nodeValue == month)
				{
					document.write("<li>");
					document.write("<h4 style=\"margin-bottom:5px; font-size:1em\">"+x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue+"</h4>");
					document.write("<span class=\"padding\" />");
					document.write("<strong style=\"color:#747460\">"+x[i].getElementsByTagName("DATE")[0].childNodes[0].nodeValue+"</strong>");
					document.write("</span>");
					document.write("<p style=\"margin-bottom:0px;\">");
					if(x[i].getElementsByTagName("LOCATION")[0].childNodes[0])
					{
					    document.write("<span class=\"padding\" />");
					    document.write("<strong style=\"color:#747460\">Location: </strong> " + x[i].getElementsByTagName("LOCATION")[0].childNodes[0].nodeValue);
					    document.write("</span>");
					}
					
					if(x[i].getElementsByTagName("HOST")[0].childNodes[0])
					{
					    document.write("<span class=\"padding\" />");					   
					    document.write("<strong style=\"color:#747460\">Host: </strong> " + x[i].getElementsByTagName("HOST")[0].childNodes[0].nodeValue);	
					    document.write("</span>");				
					}
					if(x[i].getElementsByTagName("PRESENTER")[0].childNodes[0])
					{
					    document.write("<span class=\"padding\" />");					   
					    document.write("<strong style=\"color:#747460\">Presenter: </strong> " + x[i].getElementsByTagName("PRESENTER")[0].childNodes[0].nodeValue);
					    document.write("</span>");
					}
					
					document.write("</p>");
					document.write("</li>");
				}
			}
			document.write("</ul>");
		}
	}
}


// renders specific event
function renderEventById(eventId)
{
	var xmlDoc=null;
	if (window.ActiveXObject)
	{
	// code for IE
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	}
	else if (document.implementation && document.implementation.createDocument)
	{
		// code for Mozilla, Firefox, Opera, etc.
		xmlDoc=document.implementation.createDocument("","",null);
	}
	else
	{
		alert('Your browser cannot handle this script');
	}
	if (xmlDoc!=null) 
	{
		xmlDoc.async=false;
		xmlDoc.load(xmlUrl);
		var x=xmlDoc.getElementsByTagName("EVENT");

		for (var i=0;i<x.length;i++)
		{ 
			if (x[i].getElementsByTagName("ID")[0].childNodes[0].nodeValue == eventId)
			{
				document.write("<h1>" + x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue + "</h1>");
				document.write("<p><strong><em>When:</em></strong> " + x[i].getElementsByTagName("DATE")[0].childNodes[0].nodeValue) + "</p>";
				
				//if there's any content in here, then spit it out, else, don't spit out anything
				if(x[i].getElementsByTagName("LOCATION")[0].childNodes[0].nodeValue != "")
				{
					document.write("<p><strong><em>Where:</em></strong> " + x[i].getElementsByTagName("LOCATION")[0].childNodes[0].nodeValue)  + "</p>";
				} 
				
				//if there's any content in here, then spit it out, else, don't spit out anything
				if (x[i].getElementsByTagName("DESCRIPTION")[0].childNodes[0].nodeValue != "")
				{
					document.write("<p>" + x[i].getElementsByTagName("DESCRIPTION")[0].childNodes[0].nodeValue + "</p>");
				}
				
			}
		
		}
	}	
}

