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 = "";
	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
		itemDump += handle_pubdates(nodes[n].getElementsByTagName('pubDate'));
		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 += ") ";
		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(".diabeteshealth.com") != -1) return "Diabetes Health";			// Patient feed
	else if (source.indexOf(".diabetes.org") != -1) return "American Diabetes Association";		// Patient feed
	else if (source.indexOf(".medicalnewstoday.com") != -1) return "Medical News Today";		// Clinician feed
	else if (source.indexOf("health.yahoo.com") != -1) return "Yahoo! Health";			// Clinician feed
	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);				
			}
		}
	}
}

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]"
	return desc;
}

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