/*	BysMoo WCMS, Copyright (c) ByYourSite since 2008. All rights reserved.

	This source file is free software; you can redistribute it and/or
	modify it under the terms of the MOO Public License as published
	by the MOO Development Group; either version 1.1 of the License, or
	(at your option) any later version.
*/


function Ajax()
{

	var cb = null;
	function getTransport() {
		try { return new XMLHttpRequest(); }catch(e){}
		try { return new ActiveXObject('Msxml2.request'); }catch(e){}
		try { return new ActiveXObject('Microsoft.request'); }catch(e){}
		return false;
	};

	this.onStateChange = function() {
		if (this.request.readyState == 4) {
			if (typeof cb == 'function') try { cb(this); }catch(e){}
			stripClassName(document.body, "ajax_loading");
		}
	};

	this.get = function(url, callback) {
		if (!this.request) { return false; }
		addClassName(document.body, 'ajax_loading');
		cb = callback;
		this.request.open('get', url, true);
		this.request.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
		this.request.send(null);
		return true;
	};

	this.post = function(url, data, callback) {
		if (!this.request) { return false; }
		addClassName(document.body, 'ajax_loading');
		cb = callback;
		with (this.request) {
			open('post', url, true);
			setRequestHeader('X-Requested-With', 'XMLHttpRequest');
			setRequestHeader('Accept', (document.documentElement.namespaceURI?'application/xhtml+xml':'text/html')+',application/xml;q=0.9,*/*;q=0.8');
			setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
			setRequestHeader('Content-Length', data.length);
			send(data);
		}
		return true;
	};

	this.request = getTransport();
	if (!this.request) { return; }
	this.request.onreadystatechange = this.onStateChange.bind(this);

}