com.saltcollective.Phocus.AJAX=function(unique,debug)
{
	this.debug=debug;
	this.clearvariables();
	if(unique!=true)
		phocus.AJAX.ajaxobj=this;
	else
		this.unique=true;
}
$pr=com.saltcollective.Phocus.AJAX.prototype;
$pr.error404=false;
$pr.async=true;
$pr.hackcache=true;
$pr.debug=false;
$pr.method='POST';

$pr.create=function()
{
	if(typeof this.con != 'undefined')
		delete this.con;
	try {
		this.con = new XMLHttpRequest();
	} catch (trymicrosoft) {
	  try {
		this.con = new ActiveXObject("Msxml2.XMLHTTP");
	  } catch (othermicrosoft) {
		try {
		  this.con = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (failed) {
		  this.con = false;
		}
	  }
	}
//	this.con=this.trylist(_try);
	if(!this.con)
		return this.onerror(1);
	
	this.hasconnection=true;
}
$pr.call=function()
{
	if(!this.method || this.method=='')
		return this.onerror(2);
	if(!this.url || this.url=='')
		return this.onerror(3);
	if(this.async=='')
		this.async=false;
	
	try
	{
		this.con.open(this.method,this.url,this.async);
		var p=this;
		this.con.onreadystatechange=function(){p.onstatechange.apply(p)};
		this.assemblepost();
		this.con.send(this.assembledvars);
	} catch(e)
	{
		this.onerror(5);
	}
	
	if(this.async==false)
		return this.con.responseText;
}
/* wrap functionality for a post request
	phocus.AJAX.post(url: String [, variables:Object [, callback:Function/String [, callbackobj:Object [, callbackargs:Array ]]]]);
*/
$pr.post=function(url,variables,callback,callbackobject,callbackargs)
{
	this.error404=false;
	this.clearvariables();
	if(!this.hasconnection)
		this.create();
		
	this.url=url;
	
	if(typeof variables == 'object')
	{
		for(var i in variables)
		{
			var op=variables[i];
			if(typeof op == 'number' || typeof op == 'string')
				this.addvariable(i,op);
		}
	}
	
	if(typeof callback == 'function' || (typeof callback == 'string' && typeof callbackobject == 'object'))
		this.addcallback(callback,callbackobject,callbackargs);
		
	this.method='POST';
	
	this.call();
}
$pr.addcallback=function(methodname,object,vars)
{
	this.callback=methodname;
	this.cbobject=object;
	if(typeof vars == 'Array')
		this.cbvars=vars;
	else
		this.cbvars=[];
}
$pr.addurl=function(_url)
{
	this.url=this.orurl=_url;
}
$pr.addvariable=function(_var,_val)
{
	this.vars[_var]=_val;
}
$pr.clearvariables=function()
{
	this.vars={};
}
$pr.assemblepost=function()
{
	this.assembledvars='';
	_vars=[];
	for(var i in this.vars)
		_vars.push(i+'='+this.vars[i]);
	this.assembledvars=_vars.join('&');
	if(this.method=='POST')
	{
		this.con.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		this.con.setRequestHeader("Content-Length",this.assembledvars.length);
		if(this.hackcache)
			this.url=this.orurl+'?rand='+Math.random();
	}else if(this.method=='GET')
	{
		this.url=this.orurl+'?'+this.assembledvars;
		if(this.hackcache)
			this.url+='&rand='+Math.random();
		this.assembledvars='';
	}
}
$pr.onerror=function(err)
{
	switch(err)
	{
		case 1:
			this.err='Problem initialising XMLHTTP. Problem with browser support?';
			break;
		case 2:
			this.err='Tried to send an XMLHTTP with no method';
			break;
		case 3:
			this.err='Tried to send an XMLHTTP with no url';
			break;
		case 4:
			this.err='Tried to send an XMLHTTP with no async set';
			break;
		case 5:
			this.err='Tried to send an XMLHTTP and failed';
			break;
		default:
			this.err='Unknown error!';
	}
	if(this.debug)
		alert(this.err);
	return false;
}
$pr.onstatechange=function()
{
	var ajax=this;
	var con=ajax.con;
	if(con.readyState == 4 && (con.status == 200 || con.status == 0))
	{
		if(phocus.Util.isinstanceof(ajax.cbvars,Array))
			ajax.cbvars.push(con.responseText);
		if(typeof ajax.callback == 'string')
			ajax.cbobject[ajax.callback].apply(ajax.cbobject,ajax.cbvars)
		else if(typeof ajax.callback == 'function')
			ajax.callback(con.responseText);
	} else if(con.readyState == 4 && (con.status == 404) && !this.error404)
	{
		this.error404=true;
		// pass through to an error page on 404
		this.url='/error.php';
		this.call();
	}
}
com.saltcollective.Phocus.register(com.saltcollective.Phocus.AJAX,'AJAX');

$pr=null;
