
function ajax_init()
{
	var x = null;
	
	if (window.XMLHttpRequest)
	{              
		x = new XMLHttpRequest();              
	}
	else
	{          
		var msxmlhttp = new Array('Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0','Msxml2.XMLHTTP','Microsoft.XMLHTTP');
		for(var i = 0; i < msxmlhttp.length; i++)
		{
			try
			{
				x = new ActiveXObject(msxmlhttp[i]);
			}
			catch (e)
			{
				x = null;
			}
		}  		
	} 

	return x;
}

function ajax_call(args, callback, method)
{
	var x, loading_status, send_type, send_data, target;
	var uri = '/ajax.php' + '?';

	target = (typeof(args['target']) == 'undefined') ? null : args['target'];
	delete args['target'];

	if ( typeof(args['self']) == 'undefined' || args['self'] == '' )
	{
		alert('The self name does not empty!');
		return false;
	}
	
	uri += 'self='+ args['self'];
	delete args['self'];

	send_type = ( typeof(method) == 'undefined' || method == '' ) ? 'GET' : ( (method.toUpperCase() != 'POST') ? 'GET' : 'POST' );

	if ( send_type == 'POST' )
	{
        send_data = '';
		for(var i in args)
        {
			send_data += '&'+ i + '=' + encodeURI(args[i]); //encodeURIComponent(args[i]); //escape(args[i]);
        }
	}
	else
	{
        for(var i in args)
        {
            uri += '&'+ i + '=' + encodeURI(args[i]); //encodeURIComponent(args[i]); //escape(args[i]);
        }
        send_data = null;
	}
	
	uri += '&timestamp='+(new Date().getTime()); 

	x = ajax_init();
	
	if (x == null)
	{
		alert('Could not create connection object!');
		return false;
	}
	else
	{
		x.open(send_type, uri, true);
			
        if (send_type == "POST")
        {
            x.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
            x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        } 
		
		loading_status = elm("ajaxloadingstatus");
		loading_status.style.display = 'inline';
		loading_status.style.top = document.body.scrollTop;
		
		x.onreadystatechange = function()
		{			
			if ( x.readyState == 4 )
			{             
				loading_status.style.display = '';
				
				var data = x.responseText.replace(/^\s*|\s*$/g,"");
				
				if (target != null)
				{
					elm(target).innerHTML = data;
				}
				else
				{
					try
					{
						callback(data);
					}
					catch (e)
					{
						alert('Can not use the callback function');
					}				
				}
				x = null;                                         
			}                                                      
		} 
	}
	
	x.send(send_data);
	delete x;
	return true;
}