/* Copyright Commcam Limited 2008. All rights reserved. (See licence/licence.txt) */

var IE = 1;
var MOZILLA = 2;
var OTHER = 3;
var g_objResponseCache = new Object();

function CAjax()
{
	this.m_objHttp = null;	
	this.m_bCreated = false;
	this.m_iType = 0;
	this.m_strErrorMessage = "";
	this.m_strLastResponse = "";
	this.m_strLastCacheName = "";
	this.create();
}

CAjax.prototype.create = function()
{
	try 
	{
 		this.m_objHttp = new ActiveXObject("Msxml2.XMLHTTP");
		this.m_bCreated = true;
		this.m_iType = IE;
 	} 
	catch (e) 
	{
  		try 
		{
   			this.m_objHttp = new ActiveXObject("Microsoft.XMLHTTP");
			this.m_bCreated = true;
			this.m_iType = IE;
  		}
  		catch (e) 
		{
			this.m_bCreated = false;
		}

	}
	
	
	if (!this.m_bCreated && typeof XMLHttpRequest != 'undefined') 
	{
		try 
		{
			this.m_objHttp = new XMLHttpRequest();
			this.m_bCreated = true;
			this.m_iType = MOZILLA;
		} 
		catch (e) 
		{
			this.m_bCreated = false;
		}
	}
	
	if (!this.m_bCreated && window.createRequest) 
	{
		try 
		{
			this.m_objHttp = window.createRequest();
			this.m_iType = OTHER;
		} 
		catch (e) 
		{
			this.m_bCreated = false;
		}
	}
	
	return this.m_bCreated;
}


CAjax.prototype.open = function(strUrl, bSend, bGet, strCacheName)
{
	this.m_strLastResponse = "";
	this.m_strLastCacheName = strCacheName;

	try {
     pageTracker._trackPageview(strUrl);
	} catch (err){};
	
	strMode = "POST";
	
	if (bGet)
	{
		strMode = "GET";
	}
	
	if (!this.m_strLastCacheName || !g_objResponseCache[this.m_strLastCacheName])
	{
		try
		{
			this.m_objHttp.open(strMode, strUrl, false);
			
			if (bSend)
			{	
				this.m_objHttp.send("");
				this.m_strLastResponse = this.m_objHttp.responseText;
				
				g_objResponseCache[this.m_strLastCacheName] = this.m_strLastResponse;
			}
		}
		catch(e)
		{
			this.m_strErrorMessage = e.name + " - " + e.message;
			alert(this.m_strErrorMessage);
			return false;
		}
	}
	else
	{
		this.m_strLastResponse = g_objResponseCache[this.m_strLastCacheName];
	}
	
	return true;
}

CAjax.prototype.send = function(strContent)
{
	if (!this.m_strLastCacheName || !g_objResponseCache[this.m_strCacheName])
	{
		try
		{
			this.m_objHttp.send(strContent);
			this.m_strLastResponse = this.m_objHttp.responseText;
			g_objResponseCache[this.m_strLastCacheName] = this.m_strLastResponse;
		}
		catch(e)
		{
			this.m_strErrorMessage = e.name + " - " + e.message;
			return false;
		}
	}
	
	
	return true;
}

CAjax.prototype.getError = function()
{
	return this.m_strErrorMessage;
}

CAjax.prototype.getResponse = function()
{
	return this.m_strLastResponse;
}

