phocus.AJAX=function(debug)
{
	this.debug=debug;
	this.clearVariables();
	phocus.AJAX.ajaxobj=this;
}
$pr=phocus.register(phocus.AJAX);
$pr.async=true;
$pr.hackcache=true;
$pr.debug=false;
$pr.method='POST';

$pr.create=function()
{
/*	var _try = [
		function () { return new XMLHttpRequest(); },
		function () { return new ActiveXObject('Msxml2.XMLHTTP'); },
		function () { return new ActiveXObject('Microsoft.XMLHTTP'); },
		function () { return new ActiveXObject('Msxml2.XMLHTTP.4.0'); }
		];*/
	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);
}
$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);
		this.con.onreadystatechange=this.onStateChange;
		this.assemblePost();
//		alert(this.assembledvars);
		this.con.send(this.assembledvars);
	} catch(e)
	{
		this.onError(5);
	}
	
	if(this.async==false)
		return this.con.responseText;
}
$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=phocus.AJAX.ajaxobj;
	var con=ajax.con;
	if(con.readyState == 4 && (con.status == 200 || con.status == 0))
	{
		ajax.cbvars.push(con.responseText)
		if(typeof ajax.callback == 'string')
			ajax.cbobject[ajax.callback].apply(ajax.cbobject,ajax.cbvars)
		else
			ajax.callback(con.responseText);
	}
}
