Ajax=function(addrandomnumber, filetype)
{
	this.basedomain='http://'+location.hostname;
	this.filetype=(filetype!=undefined ? filetype : 'txt');
	this.addrandomnumber=(addrandomnumber!=undefined ? addrandomnumber : false);
	
	this.ajaxobj=false;
	if (window.ActiveXObject) // if IE
	{
		try{
			this.ajaxobj=new ActiveXObject("Msxml2.XMLHTTP");
		}catch (err)
		{
			try{
				this.ajaxobj=new ActiveXObject("Microsoft.XMLHTTP");
			}catch (err) {}
		}
	}
	else if (window.XMLHttpRequest) // if Mozilla, Safari etc
	{
		this.ajaxobj=new XMLHttpRequest();
		if (this.ajaxobj.overrideMimeType);
			this.ajaxobj.overrideMimeType(this.filetype=='xml' ? 'text/xml' : 'text/plain');
	}
}

Ajax.prototype.get=function(url, callbackfunc)
{
	if (this.addrandomnumber) // To stop IE caching
		var url=url+(url.indexOf('?')!= -1 ? '&' : '?')+"ts="+new Date().getTime();
	if (this.ajaxobj)
	{
		this.ajaxobj.onreadystatechange=callbackfunc;
		this.ajaxobj.open('GET', url, true);
		this.ajaxobj.send(null);
	}
}

Ajax.prototype.post=function(url, parameters, callbackfunc)
{
	if (this.ajaxobj)
	{
		this.ajaxobj.onreadystatechange = callbackfunc;
		this.ajaxobj.open('POST', url, true);
		this.ajaxobj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		this.ajaxobj.setRequestHeader("Content-length", parameters.length);
		this.ajaxobj.setRequestHeader("Connection", "close");
		this.ajaxobj.send(parameters);
	}
}

Ajax.prototype.request=function(url, parameters, calltype, callbackfunc)
{
	if(calltype.toLowerCase()=='post')
		this.post(url, parameters, callbackfunc);
	else
		this.get(url+'?'+parameters, callbackfunc);
}

Ajax.prototype.ready=function()
{
	return (this.ajaxobj.readyState == 4);
}

Ajax.prototype.status=function()
{
	return (this.ajaxobj.status);
}

Ajax.prototype.response=function()
{
	return (this.filetype=='txt' ? this.ajaxobj.responseText : this.ajaxobj.responseXml);
}

function handleAjaxForm(form, onsuccess, onfailure)
{
	var useAIM=false;
	for(var i=0; i<form.elements.length; i++)
	{
		if(form.elements[i].type=='file')
			useAIM=true;
	}
	
	if(useAIM && (!!AIM)) // Make sure the AIM Object is loaded; if not, AJAX is all you get.
	{
		form.enctype='multipart/form-data'; // Just in case.
		return AIM.submit(form,
		{
			onComplete: function(response)
			{
				resp=JSON.parse(response);
				if(resp.success)
				{
					if(!!onsuccess)
						onsuccess();
				}
				else
				{
					if(!!onfailure)
						onfailure();
					if(!!resp.msg)
						alert('Error: '+resp.msg);
				}
			}
		});
	}
	else
	{
		var ajax=new Ajax(true, 'txt');
		
		var params='';
		for(var i=0; i<form.elements.length; i++)
		{
			if(form.elements[i].name != '')
				params += escape(form.elements[i].name)+'='+escape(form.elements[i].value).replace(/\+/g, '%2B')+'&';
		}
		
		ajax.request(form.action, params, form.method, function()
		{
			if(ajax.ready() && ajax.status()==200)
			{
				var resp=JSON.parse(ajax.response());
				if(resp.success)
				{
					if(!!onsuccess)
						onsuccess();
				}
				else
				{
					if(!!onfailure)
						onfailure();
					if(!!resp.msg)
						alert('Error: '+resp.msg);
				}
			}
		});
	}
	
	return false;
}
