// global flag
var isIE = false;

// global request and XML document objects
var req;

var currDoc = null;


function loadDoc(evt) {
	
	document.getElementById("news9").innerHTML = '<center><img src="loading-circ.gif" width="200" height="15" border="0" alt="" />&nbsp; Fetching XML Data...</center></div>';
	
	// equalize W3C/IE event models to get event object
    var XMLurl = "catch3.php";
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", XMLurl, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", XMLurl, true);
            req.send();
        }
    }
}

function loadXMLDoc(url) {
	// retrieve XML document (reusable generic function);
	// parameter is URL string (relative or complete) to
	// an .xml file whose Content-Type is a valid XML
	// type, such as text/xml; XML source must be from
	// same domain as HTML file
	
	currDoc = url;
	
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send(null);
	}
	// branch for IE/Windows ActiveX version
	else if (window.ActiveXObject) {
		isIE = true;
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			req.onreadystatechange = processReqChange;
			req.open("GET", url, true);
			req.send();
		}
	}
}

function processReqChange() {
	// handle onreadystatechange event of req object
	// only if req shows "loaded"
	if (req.readyState == 4) {
	// only if "OK"
		if (req.status == 200) {
			buildTopicList();
		}
		else
			alert("There was a problem retrieving the XML data:\n" + req.statusText);
	}
}

// fill Topics select list with items from
// the current XML document
function buildTopicList() {
	var select = document.getElementById("topics");
	var items = req.responseXML.getElementsByTagName("item");
	// loop through <item> elements, and add each nested
	// <title> element to Topics select element
	
	var str = "";
	
	for (var i = 0; i < items.length; i++){
		var item = items[i];
		var title = getElementTextNS("", "title", item, 0)
		var intro = getElementTextNS("content", "encoded", item, 0);
		if(intro == "n/a")
			intro = getElementTextNS("", "description", item, 0);
		
		var linkout = getElementTextNS("", "guid", item, 0);
	
		//appendToSelect(select, i,document.createTextNode(title));
		
		//str += "<a class='arttitle'>"+ getElementTextNS("", "title", items[i], 0) +"</a>"+intro;
		if(linkout != "n/a")
			str += "<ul>" + "<li><b><a href='"+ linkout +"' target='_blank'>"+ getElementTextNS("", "title", items[i], 0) +"</b></a>"+intro+"</li>" + "</ul>";
		else
			str += "<li>"+ getElementTextNS("", "title", items[i], 0) +"</div><div class='desc'>"+intro+"</li>";	
	}

	// clear detail display
	document.getElementById("news9").innerHTML = str;
}

// retrieve text of an XML document element, including
// elements using namespaces
function getElementTextNS(prefix, local, parentElem, index) {
	var result = "";
	
	// IE/Windows way of handling namespaces
	if (prefix && isIE)
		result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
	else {
		// the namespace versions of this method 
		// (getElementsByTagNameNS()) operate
		// differently in Safari and Mozilla, but both
		// return value with just local name, provided 
		// there aren't conflicts with non-namespace element
		// names
		result = parentElem.getElementsByTagName(local)[index];
	}

	if (result) {
		// get text, accounting for possible
		// whitespace (carriage return) text nodes 
		if (result.childNodes.length > 1)
			return result.childNodes[1].nodeValue;
		else
			return result.firstChild.nodeValue;
	}
	else
		return "n/a";
}