var ajaxct = 0;
var ajaxxml = [];
var ajaxtxt = [];

var ajaxdebug = false;

var ajaxrequests = [];

function ajaxrequest(url, callback_function, callback_obj, jsonflag ) 
{ 	
	var http_request = false; 
	var method = 'GET', sendtxt = null;
	if( arguments.length == 6 )
	{
		method = arguments[4];
		sendtxt = arguments[5];
	}

	var ajaxid = ajaxct++;
 	if( ajaxdebug )
	{
		ajaxtxt[ajaxid] = http_request.responseText;
		debug( 'ajax call', ajaxid, '<a href="' + url + '" target="_blank">' + url + '</a>', method, sendtxt );
	}
	if (window.XMLHttpRequest) { // Mozilla, Safari,... 
		 http_request = new XMLHttpRequest(); 
//		 if (http_request.overrideMimeType) { 
//			  http_request.overrideMimeType('text/xml'); 
//		 } 
	} else if (window.ActiveXObject) { // IE 
		 try { 
			  http_request = new ActiveXObject("Msxml2.XMLHTTP"); 
		 } catch (e) { 
			  try { 
					http_request = new ActiveXObject("Microsoft.XMLHTTP"); 
			  } catch (e) {} 
		 } 
	} 
	if (!http_request) { 
		 alert('Unfortunatelly you browser doesn\'t support this feature.'); 
		 return false; 
	} 
	http_request.onreadystatechange = function() { 
		if( !ie && http_request.aborted ) return;
		 if (http_request.readyState == 4) { 
			  if (http_request.status == 200  ) { 
			//  alert(http_request.responseText);
					if( http_request.responseText == '' ) return;
				 	if( ajaxdebug )
					{
						ajaxtxt[ajaxid] = http_request.responseText;
						debug( 'ajax response', ajaxid, '<div class=ajaxreponse id="ajax' + ajaxid + '">' + http_request.responseText + '</div>' );
					}
					if( callback_function != null )
					{
						if( typeof( callback_function ) == 'string' )
							eval(callback_function + '(http_request.responseText,callback_obj,jsonflag)' ); 
						else
							callback_function( http_request.responseText, callback_obj, jsonflag );
					}
			  } else if( http_request.status != 0 ) { 

						if( typeof( callback_function ) == 'string' )
							eval(callback_function + '("",callback_obj,jsonflag)' ); 
						else
							callback_function( '', callback_obj, jsonflag );

//					alert('There was a problem with the request: \n' + url + '\n(Code: ' + http_request.status + ')'); 
			  } 
		 } 
	} 
	http_request.open(method, url, true); 
	if( method == 'POST' ) http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	if( !ie ) http_request.aborted = false;

	http_request.send(sendtxt);
	var ajaxidx = ajaxrequests.length;
	ajaxrequests.push( http_request );
	return ajaxidx;
}

function abort_all_ajax_requests()
{
	for( var i = 0 ; i < ajaxrequests.length ; i++ )
	{
		if( ajaxrequests[i].readyState != 4 )
		{
			if( !ie ) ajaxrequests[i].aborted = true;
			ajaxrequests[i].abort();		
		}
	}
}

// draws the content inside obj 
function resulttxt(txt, obj )
{
	obj.innerHTML = txt;
}

function json( str )
{
	try
	{
		return eval( '(' + str + ')' );
	}
	catch( err )
	{
		debug( 'bad json response:<br><b>' + str + '</b>');
		return new Object;
	}
}


function jsonout( o )
{
	var out = '';
	var isobj = false;
	var isarray = false;

	if( typeof( o ) == 'string' )
	{
		o = o.replace( /"/, '\\"' );
		o = o.replace( /\n/, '\\n' );
		return '"' + o + '"';
	}
	else if( typeof( o ) == 'number' || typeof( o ) == 'boolean' )
		return o;

	for( var v in o )
	{
		try {
			if( v.match( /\./ ) ) var x = eval( 'o[' + v + ']' );
			else var x = eval( 'o.' + v );

			isobj = true;
		}
		catch (e) { isobj = false };
		break;
	}
	if( isdefined( o.length ) )
		isarray = true;
	if( isobj )
	{
		var tmp = [];
		out = '{';
		for( var v in o )
		{
			if( v > '' )
			{
				var val = eval( 'o.' + v );
				if( v > '' && isdefined( val ) )
					tmp.push( '"' + v + '":' + jsonout( val ) );
			}
		}
		out += tmp.join( ',' );
		out += '}';
	}
	else if( isarray )
	{
		var tmp = [];
		out = '[';
		for( var i = 0 ; i < o.length ; i++  )
			tmp.push( jsonout( o[i] ) );
		out += tmp.join( ',' );
		out += ']';
	}
	if( !isarray && !isobj )
		out = '{}';
	return out;
}
