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


//generic AJAX obj constructor

/*	usage:
	var ajax = XHR.init([base_url],[file_extension])
		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,extension,loginobj)
	{
		this.srv = base_url || '';
		this.ext = extension!=undefined? extension:'.php';
		this.login = loginobj;
		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 + this.ext +'?';
		for (var i=2; i<arguments.length; i+=2)
		{
			if (i>2) url+= '&';
			url+= arguments[i] +'='+ escape(arguments[i+1]);
		}
		//add session key
		if (this.login && this.login.session)
		{
			if (url.substr(url.length-1)!='?') url+= '&';
			url+= this.login.session.key+'='+this.login.session.value;
		}
		//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.isError) callback(resp);
		else
		{	
			//-> add decent error handling here\
			alert(resp.errorMsg || 'Error!\n\nplease try again');
		}
	},

	/* connection */

	XMLObj: function(srv)
	{
		this.messages =
		{
			create:'Sorry, an error has occured,\nplease try again.\n\n(error creating xhr connection)',
			notfound:'Sorry, an error has occured,\nplease try again.\n\n(xhr error, file not found)',
			unspecified:'Sorry, an error has occured,\nplease try again.\n\n(xhr unspecified error)'
		}

		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(this.messages.create);
		}
		
		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 if (this.req.status==404) alert(this.messages.notfound);
				else alert(this.messages.unspecified)
			}
		}
	}
}