var randomLoader=null;

function randomLoad(targetElement, urlArray) {
	_randomLoader=this;
	
	this.httpRequest=null;
	this.targetElement=targetElement;
	if (typeof this.targetElement == "string") {
		this.targetElement=document.getElementById(this.targetElement);
	}
	this.urlArray=urlArray;
	
	this.requestRandom();
}

randomLoad.prototype.response = function() {
	if (this.httpRequest.readyState == 4) {
		if (this.httpRequest.status == 200) {
			this.targetElement.innerHTML=this.httpRequest.responseText;
		} else {
			this.targetElement.innerHTML="Error loading file. Response: "+this.httpRequest.status;
		}
	}
}
	
randomLoad.prototype.request = function(url) {
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		this.httpRequest = new XMLHttpRequest();
		if (this.httpRequest.overrideMimeType) {
			this.httpRequest.overrideMimeType('text/xml');
			// See note below about this line
		}
	} 
	else if (window.ActiveXObject) { // IE
		try {
			this.httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) {
			try {
				this.httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) {}
		}
	}
	
	if (!this.httpRequest) {
		alert('Giving up - Cannot create an XMLHTTP instance');
		return false;
	}
	
	_randomLoader=this;
	
	this.httpRequest.onreadystatechange = function() { _randomLoader.response(); };
	this.httpRequest.open('GET', url, true);
	this.httpRequest.send('');
}

randomLoad.prototype.requestRandom=function() {
	pos=Math.floor(Math.random()*(this.urlArray.length));
	this.request(this.urlArray[pos]);
}
