var _ajaxUniqueRequests = new Object;

/*
 *	Load data from server. Arguments:
 *		method		string				GET|POST|HEAD. Default is POST
 *		url			string				Base URL
 *		args		string|hash			Extra arguments either in "name=value&.." format or hash
 *		unique		string				Unique ID. Only one request with specified uid will be performed
 *										simultaneously. Other requests will be cancelled
 *		force		bool				Used only with "unique". Cancel previous unique request.
 *		onwait		func(req)			Waiting handler
 *		onerror		func(reason,req)	Error handler
 *		onready		func(req)			Success handler
 *
 *  @param		args	hash		Arguments
 *  @return		bool
 */
function  ajaxLoadUrl (args)
{
	var req = false;
	
	if ( 'undefined' != typeof( ajaxBaseUrl ) && args['url'].indexOf( '://' ) == '-1' )
		args['url'] = ajaxBaseUrl + args['url'];
	
	try { req = new ActiveXObject("Msxml2.XMLHTTP"); }
	catch (e) { try { req = new ActiveXObject("Microsoft.XMLHTTP"); }
	catch (e) { try { req = new XMLHttpRequest(); }
	catch (e) { req = false; }}}
	if( !req ) 				return alert("Cannot create XMLHttpRequest");
	if( !args['url'] )		return alert("Missing required argument: URL");
	
	try
	{
		args['method'] = args['method'] ? args['method'].toUpperCase() : 'POST';
		if (args['method'] != "POST")
		{
			req.open(args['method'], makeUrl(args['url'],args['args']), true);
			body = null;
		}
		else
		{						
			req.open(args['method'], args['url'], true);
			req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
			body = makeUrl('',args['args']);
		}
		var onWait		= args['onwait'];
		var onError		= args['onerror'];
		var onReady		= args['onready'];
		var unique		= ""+args['unique'];
		req.onreadystatechange = function()
		{
			if (req.readyState != 4)
				return onWait ? onWait(req) : null;
			if( req.status != 200 )
				onError ? onError(req.statusText, req) : alert("There was a problem retrieving data:\n" + req.statusText);
			else
				onReady ? onReady(req) : null;
			if( unique )
				delete _ajaxUniqueRequests[unique];
		};
		if( 'unique' in args )
		{
			if( unique in _ajaxUniqueRequests )
				if( args['force'] )
					_ajaxUniqueRequests[unique].abort();
				else
					return false;
			_ajaxUniqueRequests[unique] = req;
		}
		req.send(body);
	}
	catch(e)
	{
		return alert(e);
	}
	return true;
}

/*
 *	Load XML document from server. Arguments see ajaxLoadUrl.
 *	Extra arguments:
 *		onready		func(xml,req)	Success handler
 *
 *  @param		args	hash		Arguments
 *  @return		void
 */
function  ajaxLoadXml (args)
{
	if( args['onready'] )
	{
		var process = args['onready'];
		var error	= args['onerror'] ? args['onerror'] : function(msg){alert("Error loading xml:" + msg)};
		args['onready'] = function(req)
		{
			if(req.responseXML === null)
				error("No XML data",req);
			else
				process(req.responseXML, req);
		}
	}
	ajaxLoadUrl(args);
}

/*
 *	Load Text document from server. Arguments see ajaxLoadUrl.
 *	Extra arguments:
 *		onready		func(text,req)	Success handler
 *
 *  @param		args	hash		Arguments
 *  @return		void
 */
function  ajaxLoadText (args)
{
	if( args['onready'] )
	{
		var process = args['onready'];
		var error	= args['onerror'] ? args['onerror'] : function(msg){alert("Error loading text:" + msg)};
		args['onready'] = function(req)
		{
			if(req.getResponseHeader("content-type").match(/text\/plain/ ) && req.responseText !== null)
				process(req.responseText, req);
			else
				error("Server returned not a text document!",req);
		}
	}
	ajaxLoadUrl(args);
}

if( "undefined" == typeof ajaxGenShowStatus )
{
	/*
	 *	Generate function to handle loading process ("onwait" handler)
	 *
	 *  @param		el		object		HTML element which will contain loading status
	 *  @return		function(req)
	 */
	ajaxGenShowStatus = function(el)
	{
		return function(req)
		{
			/*if(req.readyState == 1)
				el.innerHTML = "Sending request";
			else if(req.readyState == 2)
				el.innerHTML = "Request is sent";
			else if(req.readyState == 3)
				el.innerHTML = "Loading";
			else
				alert("Oops! readyState == 4!");*/
			el.innerHTML = '<img src="/img/indicator.gif" width="14" height="14" />';
		};
	}
}
