function get_month_name(monthAbbr) {
	switch(monthAbbr){
		case "Jan":
			return "January";
			break;
		case "Feb":
			return "February";
			break;
		case "Mar":
			return "March";
			break;
		case "Apr":
			return "April";
			break;
		case "May":
			return "May";
			break;
		case "Jun":
			return "June";
			break;
		case "Jul":
			return "July";
			break;
		case "Aug":
			return "August";
			break;
		case "Sep":
			return "September";
			break;
		case "Oct":
			return "October";
			break;
		case "Nov":
			return "November";
			break;
		case "Dec":
			return "December";
			break;
	}
}

function handle_channels(nodes) {
	var channelDump = "";
	for (n = 0; n < nodes.length; n++) {
		channelDump += handle_items(nodes[n].getElementsByTagName('item'));
	}
	return channelDump;
}
	
function handle_items(nodes) {
	var itemDump = "";
	var itemDate = "";
	for (n = 0; n < nodes.length; n++) {
		itemDump += "<h5><a>";								// Titles in <h3> tags
		itemDump += handle_titles(nodes[n].getElementsByTagName('title'));
		itemDump += "</a></h5>";
		itemDump += "<div>";								// Target for "stretchers"
		itemDump += "<p class=\"date\">";					// Pubdates in <p class="news_pubdate"> tags
		itemDate = nodes[n].getElementsByTagName('pubDate'); 
		itemDump += (itemDate.length==0)?"":("("+handle_pubdates(itemDate)+")");
		itemDump += "</p>";
		itemDump += "<p class=\"news_item\">";						// Descriptions in <p class="news_desc"> tags
		itemDump += "(";
		itemDump += handle_sources(nodes[n].getElementsByTagName('link'));		// The source of the news article
		itemDump += ")<br />";
		itemDump += handle_descriptions(nodes[n].getElementsByTagName('description'));
		itemDump += " <a href=\"";							// Links in <a> tags
		itemDump += handle_links(nodes[n].getElementsByTagName('link'));
		itemDump += "\" target=\"_blank\">View Full Article</a>"
		itemDump += "</p>";
		itemDump += "</div>";
	}
	return itemDump;
}

function handle_titles(nodes) {
	return nodes.item(0).firstChild.nodeValue;
}

function handle_pubdates(nodes) {
	var rawDT = nodes.item(0).firstChild.nodeValue;
	var dateParts = rawDT.split(" ");
	var formatDT = "";
	formatDT += get_month_name(dateParts[2]);
	formatDT += " ";
	formatDT += dateParts[1];
	formatDT += ", ";
	formatDT += dateParts[3];
	return formatDT;
}

function handle_sources(nodes) {
	var source = nodes.item(0).firstChild.nodeValue;
	if (source.indexOf("plus.mcmaster") != -1) return "<b>ACP Journal Club PLUS</b>";
	else if (source.indexOf("topix.com") != -1) return "Topix News";			// Clinician feed
	else if (source.indexOf("dovepress.com") != -1) return "Journal of COPD";		// Clinician feed
	else if (source.indexOf("medicalnewstoday") !=-1) return "Medical News Today";
	else if (source.indexOf("copdnewsoftheday") != -1) return "COPD News of the Day";		// Patient feed
	else if (source.indexOf("sciam.com") != -1) return "Scientific American";			// Patient feed
	else if (source.indexOf("sagepub") != -1) return "Sage Publications";
	else {
		/*var firstSlash = source.indexOf("/", 6);						// 6 = index of second "/" in "http://"
		var source = source.substring(0, firstSlash - 1);					// Remove anything after the first slash
		var dotCount = 0;
		for (var i = source.length - 1; i >= 0; i--) {						// Iterate through the remaining URL backwards
			if (charAt(i) == ".") {
				dotCount++;
			}
			if (dotCount == 2) {								// Upon encountering the second dot in this direction, return anything after the dot
				return source.substring(i + 1, source.length - 1);				
			}
		}*/
		return "COPD News";
	}
}

function handle_descriptions(nodes) {
	var desc = nodes.item(0).firstChild.nodeValue;
	desc = desc.replace("[click link for full article]", "");			// Removes any instances of "[click link for full article]"
	desc = desc.replace("<p>","");
	desc = desc.replace("</p>","");
	desc = desc.replace("<br>","<br />");
	return desc;
}

function handle_links(nodes) {
	return nodes.item(0).firstChild.nodeValue;
}
