
var xmlHttp;

xmlHttp = GetXmlHttpObject();

startXML();


//This funciton makes sure a XmlHttpObject has been created and calls the functions needed to parse the XML
function startXML()
{
	if (xmlHttp==null)
	  {
		alert ("Your browser does not support AJAX!");
		return;
	  }
	
	//setUrl is defined on the page this script is called from
	var url = "index_hbp.xml";
	xmlHttp.onreadystatechange=parseXML;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}


//This function creates an XmlHttpObject
function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
		{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
	  catch (e)
		{
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	  }
	return xmlHttp;
}

//Once the XmlHttpObject is ready and loaded, this script to parse the xml will be called
function parseXML() 
{ 
		if (xmlHttp.readyState==4)
		{
			var xmlDoc=xmlHttp.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 = formatDate(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=\"hc-high-blood-pressure-news-detail.htm?" + linkContent + "\">" + titleContent + "</a></li>";
				
				} else {
					
					text += "" + dateContent + "<br />";
					text += "<a href=\"hc-high-blood-pressure-news-detail.htm?" + linkContent + "\">" + titleContent + "</a><br /><br />";	
				
				}
				
				
			}
			
			text += "</ul>";
			
			document.getElementById('hbp_news').innerHTML = text;
				
		}
}

function formatDate(date) {
	
	var dateArr = date.split("-");
	
	var year = dateArr[0];
	var month = parseInt(dateArr[1],10);
	var day = dateArr[2];
	
	if(day.search(0) == 0) {
		day = day.charAt(1);	
	}
	
	switch(month) {
		
		case 1: 	month = "Jan";
					break;
		case 2:		month = "Feb";
					break;
		case 3:		month = "Mar";
					break;
		case 4:		month = "Apr";
					break;
		case 5:		month = "May";
					break;
		case 6:		month = "Jun";
					break;
		case 7:		month = "Jul";
					break;
		case 8:		month = "Aug";
					break;
		case 9:		month = "Sep";
					break;
		case 10:	month = "Oct";
					break;
		case 11:	month = "Nov";
					break;
		case 12:	month = "Dec";
					break;
		default:	break;
		
	}
	
	var newDate = "" + month + " " + day + ", " + year;
	
	return newDate;
	
}