addOnLoad(initFeeds);

function addOnLoad(newFunction) {
    var oldOnload = window.onload;

    if (typeof oldOnload == 'function') {
        window.onload = function() {
            if (oldOnload) {
                oldOnload();
            }
            newFunction();
        }
    } else {
        window.onload = newFunction;
    }
}

var xhr = false;
var target;

function initFeeds() {
	load_feeds("feeds_rss.php", "middlepiece"); 
}

function load_feeds(url, thetarget) {

	target = thetarget;
	
	document.getElementById(target).innerHTML = 'Fetching data....';
	
	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	} else { 
		if (window.ActiveXObject) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				document.getElementById(target).innerHTML = "This unsupported in this browser...";
			}
		}
	}
	
	if (xhr) {
		xhr.onreadystatechange = function() { load_feeds_done(); };
		xhr.open("GET", url, true);
// needed for safari bug?
//		xhr.setRequestHeader('If-Modified-Since', 'Wed, 15 Nov 1995 00:00:00 GMT');
		xhr.send(null);
	} else {
		document.getElementById(target).innerHTML = "Sorry, couldn't load data.....";
	}
}

function load_feeds_done() {
	if (xhr.readyState == 4) {
		if (xhr.status == 200) {
			document.getElementById(target).innerHTML = xhr.responseText;
		} else {
			document.getElementById(target).innerHTML = xhr.status + ":There was a problem loading the data....";
		}
	}

}

