// JavaScript Document
function makeRequest(url, getpost, senddata, wait, success) {
	if(getpost == "undefined" || getpost != "POST" || getpost != "GET") {
		getpost = "GET";
	} 
	var http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	
	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	
	http_request.onreadystatechange = function() {
		if (http_request.readyState == 4 && http_request.status == 200) {	
			success(http_request)
		} else {
			wait(http_request);
		}
	};
	
	http_request.open(getpost, url, true);

	if(getpost == "POST") {
		http_request.send(senddata);
	} else {
		http_request.send(null);
	}
}

function addVariableToUrl(url,varname,value) {
		if (url.indexOf('?')>-1)
			return (url + "&" + varname + "=" + value);
		else
			return (url + "?" + varname + "=" + value);
}

function TimerRequest(sName, sUrl, sGetPost, sSendData, fWait, fSuccess, iDelay, bNow) {
	this.name=sName;
	this.url=sUrl;
	this.getpost=sGetPost;
	this.senddata=sSendData;
	this.wait=fWait;
	this.success=fSuccess;
	this.delay=iDelay;
	
	this.tick = function() {
		var myurl=this.url + "?timestamp=" + (new Date()).toUTCString();
		makeRequest(myurl, this.getpost, this.senddata, this.wait, this.success);
		window.setTimeout(this.name+".tick();",this.delay);
	};	
	
	if (!this.wait)
		this.wait = function (httpReq) {};

	if (!this.success)
		this.success = function (httpReq) {};
		
	window[this.name]=this;
	
	if (bNow)
		this.tick();
	else
		setTimeout(this.name+".tick();", this.delay);
		
}
