var ajax_instances = [] ;

var str_searching_msg = 'searching...' ;

function CAjaxHandler()
{
	var xmlHttp = null ;
	
	var call_back_function_name = '' ;
	
	//index in ajax_instances of this particular object
	var id = -1 ;

	/**
	 * @constructor
	 * Inits variables of the class, inits this.xmlHttp
	 **/
	this.__constructor = function()
	{
		if ( window.ActiveXObject)
		{//if it's IE with it's ActiveX Ajax
			//create object as ActiveXObject
			this.xmlHttp = new ActiveXObject( 'Microsoft.XMLHTTP' ) ;
			
		}
		else if ( window.XMLHttpRequest )
		{//if it's standard object
			//create it in standard fashion
				this.xmlHttp = new XMLHttpRequest() ;
		}
	
		//register this class in ajax_instances, so we can set back-call-functions
		this.id = ajax_instances.length ;
		
		ajax_instances[ this.id ] = this ;			
	}//__constructor
	
	
	/** 
	 * used as call-back function to process results from requests
	 **/
	this.CallBack = function() 
	{
	
		if ( 4 == this.xmlHttp.readyState )
		{
			if ( 200 == this.xmlHttp.status )
			{			
				window.status = '' ;//make it empty, we've finished search 
			
				var str_result = this.xmlHttp.responseText ;
				var xml_result = this.xmlHttp.responseXML  ;
				var callback_func = eval ( this.call_back_function_name ) ;			
				callback_func( str_result, xml_result ) ;
			}
		}
	}//CallBack
	
	this.GetXmlResult = function()
	{
		return this.xmlHttp.responseText ; 
	}
	
	this.GetStrResult = function()
	{
		return this.xmlHttp.responseText ; 
	}
	
	/**
	 * Makes request to specified url with specified method, sending specified fields and setting specified back-call-function
	 **/
	this.MakeRequest = function( method, url, fields, call_back_function_name, is_in_bg )
	{		
		//escape all fields
		var escaped_fields = new Array( fields.length ) ;
		for ( var field_name in fields )
			escaped_fields[field_name] = escape( fields[ field_name ] ) ;	

		//if it's GET-request and we have some fields
		if ( 'GET' == method && escaped_fields.length != 0 )
		{
			//add ? delimiter to url if it is not present already there
			if ( -1 == url.indexOf( '?' ) )
				url += '?' ;			
			//add specified fields to url, 
			for ( field_name in escaped_fields )
				url += '&' + field_name + '=' + escaped_fields[ field_name ] ;
			//make fields to send as an empty array
			escaped_fields = null ;
		}

//alert( url ) ;		
		//open request
		this.xmlHttp.open( method, url, is_in_bg ) ;
		//set back-call function if any
		if ( undefined != call_back_function_name )
		{
			//remember back-call-function-name
			this.call_back_function_name = call_back_function_name ;

			eval( "var call_back_function = function() { ajax_instances[" + this.id + "].CallBack() ; } ; " ) ;
		
			this.xmlHttp.onreadystatechange = call_back_function ;
		}
		//make request
		this.xmlHttp.send( escaped_fields ) ;
		//display about this in status-bar
		window.status = str_searching_msg ;

	}//makeRequest
	
	//init the object
	this.__constructor() ;
}//CAjaxHandler