function getNextStories(aURL, start, next) {
	var loader = '/' + aURL + '?page=' + start + ',' + next;
	//alert(loader);
	keepURL = aURL;
	keepStart = start;
	keepNext = parseInt(next);
	loadXMLDoc(loader);
	//return false;
}

function getNextPage(aURL) {
	loadXMLDoc('/' + aURL);
}

var offsetStart = 0;
var keepURL = '';
var keepStart = 0;
var keepNext = 0;
var nextText = '';

var req;

function loadXMLDoc(url) {
	req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	if(req) {
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send("");
	}
}

function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            
            offsetStart++;
            if(req.getResponseHeader('Content-Type') == "text/xml") {
	            buildDisplayList();
			} else {
			    eval(req.responseText);
			}
         } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
         }
    }
}

function getElementTextNS(prefix, local, parentElem, index) {
    var result = "";
    if (prefix && isIE) {
        // IE/Windows way of handling namespaces
        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";
    }
}

function clearDisplayList() {
    var unorderedList = document.getElementById("newslist");
    unorderedList.innerHTML = '';
    while (unorderedList.length > 0) {
        unorderedList.remove(0);
    }
}

function clearClass(varse) {
	var myList = document.getElementById('newslist').childNodes;
	for(i=0; i < myList.length; i++) {
		//alert(myList[i].tagName);
		try {
			if(myList[i].firstChild.getAttribute('id') != 'href'+varse) {
				myList[i].firstChild.className = '';	
			} else {
				myList[i].firstChild.className = 'newsactive';
			}
		} catch(e) {
		
		}
	}
	return false;
}

function buildDisplayList() {
	if(nextText == '') {
		nextText = document.getElementById("nextText").innerHTML;
	}
    clearDisplayList();
    var unorderedList = document.getElementById("newslist");
    var stories = req.responseXML.getElementsByTagName("story");
    var blockData = req.responseXML.getElementsByTagName("blockData");
    var more = getElementTextNS("", "more", blockData[0], 0)
    var previousLink = '';
    if (keepNext > 0) {
    //alert('link');
    	var listElementA = document.createElement('li');
    	var anchorA = document.createElement('a');
    	var lastInt = parseInt(keepNext) -1;
    	anchorA.href = "javascript:getNextStories('" + keepURL + "', " + keepStart + ", " + (keepNext -1)   + ");";
    	anchorA.innerHTML = '<strong>Back</strong>';
    	listElementA.appendChild(anchorA);
		unorderedList.appendChild(listElementA);
    }
    // loop through <item> elements, and add each nested
    // <title> element to Topics select element
    for (var i = 0; i < stories.length; i++) {
    	var listElement = document.createElement('li');
    	var anchor = document.createElement('a');
    	anchor.href = getElementTextNS("", "link", stories[i], 0);//links[i];
    	anchor.innerHTML = getElementTextNS("", "title", stories[i], 0);
    	anchor.setAttribute('id', 'href'+getElementTextNS("", "nid", stories[i], 0));
    	
    	listElement.appendChild(anchor);
		unorderedList.appendChild(listElement);
    }
    
    if (more == 'yes') {
    	var listElementA = document.createElement('li');
    	var anchorA = document.createElement('a');
    	var lastInt = parseInt(keepNext) -1;
    	anchorA.href = "javascript:getNextStories('" + keepURL + "', " + keepStart + ", " + (keepNext +1)   + ");";
    	anchorA.innerHTML = '<strong id="nextText">'+ nextText +'</strong>';
    	listElementA.appendChild(anchorA);
		unorderedList.appendChild(listElementA);
    }
}