
var xmlhttp;

function loadXMLDoc(url) {
	xmlhttp=null;
	if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }  // code for IE7, Firefox, Mozilla, etc.
	else if (window.ActiveXObject) { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } // code for IE5, IE6
	if (xmlhttp!=null) {
		xmlhttp.onreadystatechange = podResponse;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	} else document.write("Your browser does not support XMLHTTP.");
}

function podResponse() {
	if (xmlhttp.readyState!=4) return;
	if (xmlhttp.status!=200) {
		document.write("Problem retrieving XML data");
		return;
	}
	x=xmlhttp.responseXML.documentElement.getElementsByTagName("item");
	
	function getXML(tagName,j) {
		// Returns only first child node--won't work for multi-node tags
		// Inexplicably doesn't work when tag name contains colon -- see itunes:subtitle below for workaround without this function
		try { theValue = x[j].getElementsByTagName(tagName)[0].firstChild.nodeValue; } catch (er) { theValue = "\""+tagName+"\":"+er; }
//		try { theValue = x[j].getElementsByTagName(tagName)[0].childNodes[0].nodeValue; } catch (er) { theValue = "#ERROR#"; }
		return theValue;
	}

	var theHTML = "";
	for (i=0;i<x.length;i++) {
//	for (i=0;i<1;i++) {
		theTitle = getXML("title",i);
		pubDate = getXML("pubDate",i).split(" ");  // <pubDate>Wed, 2 Apr 2008 19:00:00 CDT</pubDate>
		shortPubDate = pubDate[1]+" "+pubDate[2]+" "+pubDate[3];
		txt = "<table class=\"pod\">\n<tr style=\"vertical-align:top\">";
		txt += "<td style=\"width:75%\">";
			txt += "<strong>"+theTitle+"</strong><br />\n";
//			txt += "<em>"+getXML("itunes:subtitle")+"</em><br />\n";  // problem: it seems not to work when tag name has a colon
			txt += "<em>"+getXML("gssubtitle",i)+"</em><br />\n";
//			txt += "<em>"+x[i].getElementsByTagName("itunes:subtitle")[0].childNodes[0].nodeValue+"</em><br />\n";  // line kills Safari
			var gsLinkList=x[i].getElementsByTagName("gslink");
			if (gsLinkList.length>0) {
				txt += "<div style=\"font-size:0.9em; margin-top:6px;\">\nRelated resources:<br />\n<ul style=\"padding-bottom:0px;\">\n";			
				for (j=0; j<gsLinkList.length; j++) {
					theChild = gsLinkList[j];
					theAttributes = theChild.attributes;
					theUrl = theAttributes.getNamedItem("url");
					txt += '<li><a href="'+theUrl.nodeValue+'">'+theChild.childNodes[0].nodeValue+'</a></li>\n';
				}
				txt += "</ul>\n</div>\n";
			}
		txt += "</td>\n<td style=\"width:25%; padding-left:30px; text-transform:uppercase; font-size:0.8em; text-align:right;\">";
//			txt += x[i].getElementsByTagName("itunes:duration")[0].childNodes[0].nodeValue + " min /";  // line prevents Safari from rendering whole thing
			txt += getXML("gsduration",i) + " min /";
			var filesize="-";
				var enclosure=x[i].getElementsByTagName("enclosure");
				for (j=0; j<enclosure.length; j++) {
					theChild = enclosure[j];
					theAttributes = theChild.attributes;
					thisAttribute = theAttributes.getNamedItem("length");
					filesize = (Math.round(10*thisAttribute.nodeValue/(1024*1024))/10)+' MB';
				}		
			txt += filesize+"<br />\n";
	//		txt += "Published "+shortPubDate+"<br />\n";
		txt += "</td>";
		txt += "</tr></table>\n\n";
//		document.write(txt);
		theHTML+=txt;
	}
	document.getElementById('podtarget').innerHTML=theHTML;

}

