
var xmlHttpAHA;

xmlHttpAHA = GetXmlHttpObject();

startXMLAHA();


//This funciton makes sure a XmlHttpObject has been created and calls the functions needed to parse the XML
function startXMLAHA()
{
	if (xmlHttpAHA==null)
	  {
		alert ("Your browser does not support AJAX!");
		return;
	  }
	
	//setUrl is defined on the page this script is called from
	var url = "index_aha.xml";
	xmlHttpAHA.onreadystatechange=parseXMLAHA;
	xmlHttpAHA.open("GET",url,true);
	xmlHttpAHA.send(null);
}


//This function creates an XmlHttpObject
function GetXmlHttpObject()
{
	var xmlHttpAHA=null;
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttpAHA=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
		{
		xmlHttpAHA=new ActiveXObject("Msxml2.XMLHTTP");
		}
	  catch (e)
		{
		xmlHttpAHA=new ActiveXObject("Microsoft.XMLHTTP");
		}
	  }
	return xmlHttpAHA;
}

//Once the XmlHttpObject is ready and loaded, this script to parse the xml will be called
function parseXMLAHA() 
{ 
		if (xmlHttpAHA.readyState==4)
		{
			var xmlDoc=xmlHttpAHA.responseXML.documentElement;
			var text = "";
			
			text += "<ul>";
			
			var itemNum;
			
			//The short value is defined in the page that is pulling the news headlines
			// short = true; (Limit headlines to 5, truncate long titles)
			// short = false; (Show all headlines, no truncation)
			if(short) {
				itemNum = 5;
			} else {
				itemNum = xmlDoc.getElementsByTagName("item").length;
			}
			
			for(var i=0;i<itemNum;i++) 
			{
				
				var curItem = xmlDoc.getElementsByTagName("item")[i];
				
				var titleContent = curItem.getElementsByTagName("title")[0].childNodes[0].nodeValue;
				var linkContent = curItem.getElementsByTagName("link")[0].childNodes[0].nodeValue;
				var dateContent = formatDateAHA(curItem.getElementsByTagName("pubDate")[0].childNodes[0].nodeValue);
				
				if(short) {
					if(titleContent.length > 65) {
						
						newTitle = titleContent.substring(0,64);
						
						var lastSpace = newTitle.lastIndexOf(" ");
						
						if(lastSpace != -1) {
							titleContent = newTitle.substring(0,lastSpace);
							titleContent = titleContent.concat("...");
						}
						
					}
					
					text += "<li><a href=\"" + linkContent + "\" target=\"_blank\">" + titleContent + "</a></li>";
				
				} else {
					
					text += "" + dateContent + "<br />";
					text += "<a href=\"" + linkContent + "\" target=\"_blank\">" + titleContent + "</a><br /><br />";	
				
				}
				
				
			}
			
			text += "</ul>";
			
			document.getElementById('aha_news').innerHTML = text;
				
		}
}

function formatDateAHA(date) {
	
	var shortDate = date.match(/\d*\s\D\D\D\s\d\d\d\d/);
	
	dateArr = shortDate.toString().split(" ");
	
	var day = dateArr[0];
	var month = dateArr[1];
	var year = dateArr[2];
	
	if(day.search(0) == 0) {
		day = day.charAt(1);	
	}
	
	
	var newDate = "" + month + " " + day + ", " + year;
	
	return newDate;
	
}