var today = new Date();
var seed = today.getTime();
var httpcallback = function() {};

function rnd() {
	seed = (seed * 9301 + 49297) % 233280;
	return seed/233280;
}

function rand(range) {
	return Math.ceil(rnd() * range);
}

function getXMLHttpRequest() {
	if(window.XMLHttpRequest) {
		try {
			return new XMLHttpRequest();
		} catch(e) {
		}
	} else if(window.ActiveXObject) {
		var msxmls = [
		      'Msxml2.XMLHTTP.5.0',
		      'Msxml2.XMLHTTP.4.0',
		      'Msxml2.XMLHTTP.3.0',
		      'Msxml2.XMLHTTP',
     		  'Microsoft.XMLHTTP'
     	];
     	for (var i=0; i<msxmls.length; i++) {
			try {
				return new ActiveXObject(msxmls[i]);
			} catch(e) {
			}
		}
	}
	alert('couldnt create http request object');
	return null;
}

function http(responseType, params, chain) {
	this.params = params;
	this.req = getXMLHttpRequest();
	this.responseType = responseType;
	this.chain = chain ? chain : null;
	
	this.execute = function() {
		if (!this.req) return;

		var txt = '';
		for (var i in this.params) txt = txt + '&' + this.params[i].n + '=' + this.params[i].v;

		var obj = this;
		this.req.onreadystatechange = function() { obj.statechange(); };
		var url = 'atomicdox2.php?v=' + rand(16777214) + txt;
		this.req.open('GET', url, true);
		this.req.send('');
	};

	this.statechange = function() {
		switch(this.req.readyState) {
			case 4:
				if (this.req.status != 200) {
					alert('The server responded with a bad status code:' + this.req.status + '\n' + this.req.responseText);
					return false;
				} else {
					switch (this.responseType) {
						case 'text':	httpcallback(this.req.responseText); break;
						case 'xml':		httpcallback(this.req.responseXML); break;
					}
				}

				// sometimes we need to chain a command after an async http request is done
				if (this.chain) this.chain();
				break;
		}
	};
}
