/**
 * Cross domain AJAX.
 * @author Sławomir Kokłowski {@link http://www.kurshtml.boo.pl}
 * @copyright NIE usuwaj tego komentarza! (Do NOT remove this comment!)
 */

function JSONHttpRequest(charset, timeout, responseName)
{
	this.charset = typeof charset == 'undefined' ? 'utf-8' : charset;
	this.timeout = typeof timeout == 'undefined' ? 0 : timeout;
	this.responseName = typeof responseName == 'undefined' ? 'data' : responseName;
	this.responseText = null;
	this.responseJSON = null;
	this.readyState = 0;
	this.status = 200;
	this.statusText = 'OK';
	this.onreadystatechange = function() {};
}

JSONHttpRequest.prototype.open = function(method, url)
{
	this.url = url;
	this.readyState = 1;
	this.onreadystatechange();
};

JSONHttpRequest.prototype.send = function()
{
	if (document.getElementsByTagName('head').length > 0) var scriptParent = document.getElementsByTagName('head')[0];
	else if (document.getElementsByTagName('body').length > 0) var scriptParent = document.getElementsByTagName('body')[0];
	else throw 'No head nor body element in document';
	window[this.responseName] = null;
	var _this = this;
	var loaded = false;
	var timeoutId = null;
	var script = document.createElement('script');
	script.charset = this.charset;
	script.onload = function()
	{
		window.clearTimeout(timeoutId);
		if (loaded || window[_this.responseName] === null) return;
		loaded = true;
		_this.responseText = '' + window[_this.responseName];
		eval('_this.responseJSON = ' + _this.responseText + ';');
		document.getElementsByTagName('head')[0].removeChild(script);
		delete script;
		window[_this.responseName] = null;
		_this.readyState = 4;
		_this.onreadystatechange();
	};
	script.onreadystatechange = function()
	{
		switch (script.readyState.toLowerCase())
		{
			case 'loaded':
			case 'complete':
				script.onload();
				break;
		}
	};
	script.src = this.url;
	scriptParent.appendChild(script);
	this.readyState = 2;
	this.onreadystatechange();
	if (this.timeout > 0) timeoutId = window.setTimeout(
		function()
		{
			_this.status = 504;
			_this.statusText = 'Gateway Timeout';
			window[_this.responseName] = '{}';
			script.onload();
		},
		this.timeout
	);
};