function ajaxrefresh(elementid, url, interval){
this.elementid = elementid;
this.interval = interval * 1000;
this.dorefresh = false;
this.req = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
this.seturl(url);


var refreshinstance=this;

//if (window.addEventListener) //run onload in DOM2 browsers
//	window.addEventListener("unload", function(){refreshinstance.stop()}, false);
//else if (window.attachEvent) //run onload in IE5.5+
//	window.attachEvent("onunload", function(){refreshinstance.stop()});
//else if (document.getElementById) //if legacy DOM browsers, just start scroller after 0.5 sec
//setTimeout(function(){refreshinstance.stop()}, 500)
}

ajaxrefresh.prototype.seturl = function(u)
{
	this.url = combineurls(location.href, u);
}

ajaxrefresh.prototype.refreshloop = function()
{
	if(this.dorefresh == true)
	{
		var refreshinstance = this;
		this.refresh();

		if(this.interval > 0)
			setTimeout(function() {refreshinstance.refreshloop()}, this.interval);
	}
}

ajaxrefresh.prototype.refresh = function()
{
	//alert('refreshing element ' + this.elementid + ' with url ' + this.url);
	//return;
	
	var elem = document.getElementById(this.elementid);
	if(elem)
	{
		this.req.open('GET', this.url, false);
		this.req.send(null);
		var resp = this.req.responseText;
		
		elem.innerHTML = resp;
	}
}

ajaxrefresh.prototype.stop=function() {
	this.dorefresh = false;
}

ajaxrefresh.prototype.start =function() {
	this.refresh();
	this.dorefresh = true;

	var refreshinstance = this;
	if(this.interval > 0)
		setTimeout(function() {refreshinstance.refreshloop()}, this.interval);
	
}

function combineurls(url1, url2)
{
	var uo1 = parseUri(url1);
	var uo2 = parseUri(url2);
	
	if(url2 == '')
		return url1;

	if(url2.substr(0,7) == 'http://')
		return url2;

	if(url2.substr(0,1) == '/')
		return uo1.protocol + '://' + uo1.authority + url2;
		
	//if(url2.substr(0,1) == '.')
	//	return uo1.protocol + uo1.authority + uo1.directoryPath + url2;
	
	return uo1.protocol + '://' + uo1.authority + uo1.directoryPath + url2;
		
}

/* parseUri JS v0.1.1, by Steven Levithan <http://stevenlevithan.com>
Splits any well-formed URI into the following parts (all are optional):
----------------------
- source (since the exec method returns the entire match as key 0, we might as well use it)
- protocol (i.e., scheme)
- authority (includes both the domain and port)
  - domain (i.e., host; can be an IP address)
  - port
- path (includes both the directory path and filename)
  - directoryPath (supports directories with periods, and without a trailing backslash)
  - fileName
- query (does not include the leading question mark)
- anchor (i.e., fragment) */
function parseUri(sourceUri){
	var uriPartNames = ["source","protocol","authority","domain","port","path","directoryPath","fileName","query","anchor"],
		uriParts = new RegExp("^(?:([^:/?#.]+):)?(?://)?(([^:/?#]*)(?::(\\d*))?)((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[\\?#]|$)))*/?)?([^?#/]*))?(?:\\?([^#]*))?(?:#(.*))?").exec(sourceUri),
		uri = {};
	
	for(var i = 0; i < 10; i++){
		uri[uriPartNames[i]] = (uriParts[i] ? uriParts[i] : "");
	}
	
	/* Always end directoryPath with a trailing backslash if a path was present in the source URI
	Note that a trailing backslash is NOT automatically inserted within or appended to the "path" key */
	if(uri.directoryPath.length > 0){
		uri.directoryPath = uri.directoryPath.replace(/\/?$/, "/");
	}
	
	return uri;
}

