
	AJAXClient.prototype.constructor               = AJAXClient;
	AJAXClient.prototype.createXMLhttpObject       = AJAXClient_createXMLhttpObject;
	AJAXClient.prototype.attachEventOnStateChange  = AJAXClient_attachEventOnStateChange;
	AJAXClient.prototype.sendPost                  = AJAXClient_sendPost;
	
	
	AJAXClient.prototype.READYSTATE_UNINITIALIZED = 0; 
	AJAXClient.prototype.READYSTATE_OPEN          = 1;	 
	AJAXClient.prototype.READYSTATE_SENT          = 2;
	AJAXClient.prototype.READYSTATE_RECEIVING     = 3;	   
	AJAXClient.prototype.READYSTATE_LOADED        = 4;	
	AJAXClient.prototype.ALL_READYSTATES          = 5; 
	
	AJAXClient.prototype.xmlhttprequest  = null;
	AJAXClient.prototype.sendQueue       = new Array();
	
	AJAXClient.prototype.busy = false;
	AJAXClient.prototype.debug = false;
	AJAXClient.prototype.callcounter = 0;
	

	AJAXClient.prototype.versionBuild = 7;
	AJAXClient.prototype.versionDate = '26Oct2007';
	
	  
	function AJAXClient(url)
	{		

//public properties
	   this.xmlhttprequest = this.createXMLhttpObject();
	   this.url = url;
	   
 //event function objects
	   this.FireOnStateChange = null;
	   this.FireOnStateOpen = null;
	   this.FireOnStateSent = null;
	   this.FireOnStateReceiving = null;
	   this.FireOnStateLoaded = null;  
  
	   
	   //private inner methods  
	}
// public:
	function AJAXClient_createXMLhttpObject()
	{
		var xmlhttp = null;
		if(window.XMLHttpRequest)
		{
			xmlhttp = new XMLHttpRequest();
		}
		else if(window.ActiveXObject)
		{
			try { xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');	}
			catch(e)
			{
				try { xmlhttp = new ActiveXObject("Msxml2.XMLHttp"); }
				catch(e2) { }
			}
		}
		return xmlhttp;
	} 


	function AJAXClient_attachEventOnStateChange(readyState, callback_OnStateChangeFunction)
	{ 
	   if (typeof callback_OnStateChangeFunction != "function")
	      return;
	   
	   //self reference needed for anonymous inner function
	   var self = this;
	   
	   switch(readyState)
	   {
	      case self.READYSTATE_OPEN:       // 1
		    self.FireOnStateOpen  = callback_OnStateChangeFunction;
			break;
			
	      case self.READYSTATE_SENT:      // 2
		    self.FireOnStateSent = callback_OnStateChangeFunction;
			break;
			
	      case self.READYSTATE_RECEIVING: // 3
		    self.FireOnStateReceiving = callback_OnStateChangeFunction; 
			break;
			
	      case self.READYSTATE_LOADED:    // 4 
		    self.FireOnStateLoaded = callback_OnStateChangeFunction;
		    break;
			
		  case self.ALL_READYSTATES: // This will call it all the time
		    self.FireOnStateChange = callback_OnStateChangeFunction;
			break;
	   }
	}
	
	
	function AJAXClient_sendPost(identifier, strQuery)
	{
		if (this.busy) return false;        //already busy
		else this.busy = true;
		
		//array to query string
		if (typeof(strQuery) == 'object')
		{
		   var str = '';
			var count = 0;
			for (i in strQuery)
			{
				if (count > 0) str += '&';

				str += encodeURI(i) + '=' + encodeURI(strQuery[i]);
				count++;
			}
			strQuery = str;
			if (this.debug)
			   alert(str);
		}
		
		if (typeof(strQuery) != 'string') strQuery = '';
	
	    var self = this;
	    if (self.xmlhttprequest === null) return false;
	    
		this.callcounter++; 
		 
	   self.xmlhttprequest.open('POST', self.url, true);  
	  	self.xmlhttprequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	  	
	  	
		  self.xmlhttprequest.onreadystatechange =
		   function() 
		   {
		   
		     if ( typeof(self.FireOnStateChange) == 'function')
			      self.FireOnStateChange(identifier, self.xmlhttprequest);
			   
			   switch ( self.xmlhttprequest.readyState )
			   {			   
				  case self.READYSTATE_OPEN:  
				    if ( typeof(self.FireOnStateOpen) == 'function')
					    self.FireOnStateOpen(identifier, self.xmlhttprequest); 
				    break; 
										
				  case self.READYSTATE_SENT:
				    if ( typeof(self.FireOnStateSent) == 'function' )
					    self.FireOnStateSent(identifier, self.xmlhttprequest); 
					break; 
										
				  case self.READYSTATE_RECEIVING:  
				    if ( typeof(self.FireOnStateReceiving) == 'function' )
					   self.FireOnStateReceiving(identifier, self.xmlhttprequest); 
					break; 
											 
				  case self.READYSTATE_LOADED:
				     self.busy = false;               //no longer busy
				     if ( typeof(self.FireOnStateLoaded) == 'function' )
					      self.FireOnStateLoaded(identifier, self.xmlhttprequest);
					 break;
			   }
		   }
		self.xmlhttprequest.send(strQuery);
		return true;
	}
