/* usemedia.com . joes koppers . 05.2006 [rev 06.2007] */
/* thnx for reading this code */


//generic AJAX obj constructor

/*	usage: (php)
	var ajax = XHR.init([base_url])
		ajax.get(filename, callback, [parameter pairs])
		
	alt usage:
	var ajax = new XHR.XMLObj() 
		ajax.done_action = function(resp)
		[ajax.init_action = function()]
		[ajax.fail_action = function(resp)]
		[ajax.post_vars = '']
		ajax.httpRequest(url) 	*/

var XHR =
{
	init: function(base_url)
	{
		this.srv = base_url || '';
		return this;
	},

	//get request with optional parameters (key,value pairs)
	get: function(cmd,callback)
	{
		var ajax = new this.XMLObj(this.srv);
		var obj = this;
		ajax.done_action = function(resp) { obj.parse(resp,callback) }

		var url = cmd +'.php?';
		for (var i=2; i<arguments.length; i+=2)
		{
			if (i>2) url+= '&';
			url+= arguments[i] +'='+ escape(arguments[i+1]);
		}
		//add session key
// 		if (login.id)
// 		{
// 			if (url.substr(url.length-1)!='?') url+= '&';
// 			url+= 'key='+login.id;
// 		}
		//add timestamp
		if (url.substr(url.length-1)!='?') url+= '&';
		url+= 't='+new Date().getTime();

		//add base url
		if (this.srv!='') url = (this.srv=='/')? '/'+url:this.srv+'/'+url;

		ajax.httpRequest(url);
	},
	
	parse: function(resp,callback)
	{
		eval('var resp = '+resp);

		//check server response
		if (resp.result=='OK') callback(resp);
		else
		{	
			//-> add decent error handling here
			alert('XHR error:\nmsg='+resp.msg);
		}
	},

	/* connection */

	XMLObj: function(srv)
	{
		this.srv = srv || '';
		this.req = false;
		this.post_vars = false;
		this.init_action = false;
		this.done_action = false;
		this.fail_action = false;

		this.httpRequest = function(url)
		{
			this.req = false;
			// branch for native XMLHttprequest object
			if (window.XMLHttpRequest) {
				try {
					this.req = new XMLHttpRequest();
				} catch(e) {
					this.req = false;
				}
			// branch for IE/Windows ActiveX version
			} else if (window.ActiveXObject) {
				try {
					this.req = new ActiveXObject("Msxml2.XMLHTTP");
				} catch(e) {
					try {
						this.req = new ActiveXObject("Microsoft.XMLHTTP");
					} catch(e) {
						this.req = false;
					}
				}
			}
			if (this.req) {
				var obj = this;
				this.req.onreadystatechange = function() { obj.rsc() };
	
				if (this.post_vars)
				{
					this.req.open('POST',url,true); 
					this.req.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 
					this.req.setRequestHeader("Content-length",this.post_vars.length); 
					this.req.setRequestHeader("Connection","close"); 
					this.req.send(this.post_vars);
				}
				else
				{
					this.req.open('GET',url,true);
					this.req.send("");
				}
			}
			else alert('error creating xml connection');
		}
		
		this.rsc = function()
		{
			//handler for state=loading
			if (this.req.readyState==1 && this.init_action) this.init_action();
		
			//handler for state=done
			if (this.req.readyState==4)
			{
				if (this.req.status==200) this.done_action(this.req.responseText);
				else if (this.fail_action) this.fail_action(this.req.responseText);
				else alert('XHR connection error');
			}
		}
	}
}