AjaxClient = function ()
{
	this.params = new Array();
};

AjaxClient.prototype =
{
	url: &quot;&quot;,
	async: true,
	mimeType: &quot;text/plain&quot;,
			
	errorMsg: false,
	HttpRequest: null,
	rensponseText: null,
	rensponseXml: null,
	rensponseError: null,
	params: null,
	
	onLoading: function(obj){},
    onLoad: function(obj){},
	onSuccess: function(obj){},
    onAbort: function(obj){},
	onError: function(obj){alert(&quot;error&quot;)},
	
	createXMLHttpRequest: function()
	{
		try
		{
			return new XMLHttpRequest();
		}
		catch(Err1)
		{
			try
			{
				return new ActiveXObject(&quot;Msxml2.XMLHTTP&quot;);
			}
			catch(Err2)
			{
				try
				{
					return new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);
				}
				catch(Err3)
				{
					return null;
				}
			}
		}
	},
    
    abort: function()
    {
        alert(&quot;insideaborting&quot;);
        this.HttpRequest.abort();
    },
    
    send: function(method, url, skipRewrite)
	{		
        if(!skipRewrite &amp;&amp; url.indexOf(&quot;http://&quot;) != 0)
        {
            url = setting.siteRootUrl + '/index.php' + url;
        }
        
		this.HttpRequest = this.createXMLHttpRequest();
        this.aborted = false;

		//this.HttpRequest.overrideMimeType(this.mimeType);
		
		var that = this;
        
        if(this.timeout)
            setTimeout(function ()
            {
                that.aborted = true;
                that.HttpRequest.abort();
                delete that.HttpRequest['onreadystatechange'];
                that.HttpRequest = null; 
                
            }, this.timeout); 
            
		if(this.async)
        this.HttpRequest.onreadystatechange = function()
		{
             
			switch(that.HttpRequest.readyState)
			{
				case 1: that.onLoading(that); break;
				case 2: that.onLoad(that); break;
				case 4:

                    if(that.aborted)
                    {
                        that.onAbort(that);
                        return;
                    }
                    
					if(that.HttpRequest.status == 200)
					{
						if(that.mimeType.indexOf('html'))
						{
							that.responseText = that.HttpRequest.responseText;
						}
						else if(that.mimeType.indexOf('xml'))
						{
							that.responseXml = that.HttpRequest.responseXML;
						}
						
						that.onSuccess(that);
					}

					break;
			}			
        	
		}
		
        //this.params[&quot;dumm&quot;] = Math.random();
        
		var queryStr = &quot;&quot;;
		
		for(var key in this.params)
		{
			if(queryStr != &quot;&quot;)
				queryStr += &quot;&amp;&quot;;

			queryStr += encodeURIComponent(key) + &quot;=&quot; + encodeURIComponent(this.params[key]);
		}
        
        this.clearParams();
        
//alert(&quot;query &quot;+ queryStr);
		if(url != null)
		{
			try
			{
				if(method == &quot;get&quot; &amp;&amp; queryStr.length &gt; 0)
				{
					url += (url.indexOf(&quot;?&quot;) != -1 ? &quot;&amp;&quot; : &quot;?&quot;) + queryStr;
				}
				
				this.HttpRequest.open(method, url, this.async);
				
				if(method == &quot;post&quot;)
				{
					this.HttpRequest.setRequestHeader(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;);
					this.HttpRequest.send(queryStr);	
				}
				else
				{
					this.HttpRequest.send(null);
				}
                
                if(this.HttpRequest.status == 200)
                {
                    if(this.mimeType.indexOf('html'))
                    {
                        this.responseText = this.HttpRequest.responseText;
                    }
                }
				
			}
			catch(e)
			{
				this.ErrorMsg = e;
				return 0;
			}
		}		
	},
    
    parseJSON: function(text)
    {
        if(!text)text = this.responseText;
        
        return eval(&quot;(&quot; + text + &quot;)&quot;);
    },
	
	get: function(url)
	{
		this.send(&quot;get&quot;, url);
	},

	post: function(url)
	{
		this.send(&quot;post&quot;, url);
	},
	
	/**
	 * @return {ajaxClient}
	 */
	addParam: function(name, value)
	{
		//alert(name+&quot; &quot;+value);
		this.params[name] = value;
		return this;	
	},
    
    clearParams: function()
    {
        this.params = new Array();
        return this;    
    },
	
	set: function(name, value)
	{
		this[name] = value;
		return this;	
	},
	
	fillForm: function(form, data)
	{
		form = $(form);	
		var elements = form.elements;
		
		for(var nr = 0; nr &lt; elements.length; nr++)
		{
			var e = elements[nr];
            
            switch(e.type)
            {
                case &quot;radio&quot;:
                case &quot;checkbox&quot;:
                    
                    if(data[e.name] != undefined)
                    {
                        e.checked = (data[e.name] == e.value) ? true : false;
                    }
                    
                    break;
                
                default:
                    if(data[e.name] != undefined)e.value = data[e.name]; 

            }

		}
	},
	
	sendForm: function(form, url, method)
	{
		this.form = $(form); 
                                          
		url = url || this.form.action || this.url;
		method = method || this.form.method || &quot;post&quot;;

		var elements = this.form.elements;
	
		for(var nr = 0; nr &lt; elements.length; nr++)
		{
			var e = elements[nr];
			if(e.disabled)continue;
			
			switch(e.type)
			{
				case &quot;checkbox&quot;:
				case &quot;radio&quot;:
					if (e.checked)
					this.addParam(e.name, e.value);
					break;
				
				case &quot;select-one&quot;:
					if(e.selectedIndex &gt;= 0)
					this.addParam(e.name, e.options[e.selectedIndex].value);
					break;
				
				case &quot;select-multiple&quot;:
					for(var nr2 = 0; nr2 &lt; e.options.length; nr2++)
					if(e.options[nr2].selected)
					this.addParam(e.name, e.options[nr2].value);
					break;
					
				case &quot;button&quot;:
				    break;
				
				default:
					this.addParam(e.name, e.value);

			}
	    }
		
		this.send(method, url, true);
  
    },
    
    selectAll: function(field)
    {       
        if(field.length)
        { 
            for(i = 0; i &lt; field.length; i++)
            {
                field[i].checked = true ;
            }
        }
        else
        {
            field.checked = true;
        }
    }
}