/**
 * @author marc.zahn - mzahn@magix.net
 */
function AjaxApp(ident)
{
	this.ident			= ident;
	this.params			= new Object;
	this.options		= new Object;
	//method (POST/GET)
	//url
	this.reqObj			= null;
	this.reqSent		= false;
	this.responseXml	= null;
	this.responseText	= null;
	this.createHttpRequestObject();
}

AjaxApp.prototype.createHttpRequestObject = function()
{
	var tmp_xHttp;
	try
	{
		tmp_xHttp = new ActiveXObject('Msxml2.XMLHTTP');
	}
	catch(e)
	{
		try
		{
    		tmp_xHttp = new ActiveXObject('Microsoft.XMLHTTP');
		}
		catch(e)
		{
    		tmp_xHttp = false;
		}
	}
	if(!tmp_xHttp && typeof XMLHttpRequest != 'undefined')
	{
		tmp_xHttp = new XMLHttpRequest();
	}
	this.reqObj = tmp_xHttp;
}

AjaxApp.prototype.setOption = function(key, value)
{
	this.options[key] = value;
}

AjaxApp.prototype.setParam = function(key, value)
{
	this.params[key] = value;
}

AjaxApp.prototype.getUrlString = function()
{
	var urlString = this.options['url'] + '?ajax_app=' + this.ident;
	if (this.options['method'] == 'GET')
	{
		urlString += this.getParamString();
		return urlString + '?' + urlString;
	}
	else
	{
		return urlString;
	}
}

AjaxApp.prototype.getParamString = function()
{
	var paramString = '';
	for (index in this.params)
	{
		paramString += '&' + index + '=' + this.params[index];
	}
	return paramString;
}

AjaxApp.prototype.sendRequest = function()
{
	if (this.reqSent)
	{
		return false;
	}
	this.reqSent 		= true;
	this.responseXml	= null;
	this.responsetext	= null;
	
	if (this.options['synchron'])
	{
		var reqTmp = this;
		this.reqObj.onreadystatechange = this.handleResponse(reqTmp);
	}
	if (this.options['method'] == 'POST')
	{
		var params = this.getParamString();
		this.reqObj.open('POST', this.getUrlString(), this.options['synchron']);
		this.reqObj.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		this.reqObj.setRequestHeader('Content-length', params.length);
		this.reqObj.send(params);
	}
	else
	{
		this.reqObj.open('GET', this.getUrlString(), this.options['synchron']);
		this.reqObj.send(null);
	}
	if (!this.options['synchron'])
	{
		this.handleResponse();
	}
}

AjaxApp.prototype.handleResponse = function()
{
	if (this.reqObj.readyState == 4)
	{
		if (this.reqObj.status == 200)
		{
			this.responseXML	= this.reqObj.responseXML;
			this.responseText	= this.reqObj.responseText;
		}
		else
		{
			this.responseXML	= false;
			this.responseText	= false;
		}
		this.reqSent = false;
	}
}

AjaxApp.prototype.getResponseXml = function()
{
	return this.responseXML;
}

AjaxApp.prototype.getResponseText = function()
{
	return this.responseText;
}