function CAjax()
{
    this.http             = null;
    this.modeXML          = false;
    this.after            = null;
    this.progressForm     = null;

    var me = this;

    this.setResponseXML = function(val)
    {
    	this.modeXML = val ? true : false;
	    if (window.ActiveXObject && this.http && this.http.overrideMimeType)
	        this.http.overrideMimeType(this.modeXML ? 'text/xml' : 'text/html');
    };

    this.init = function()
    {
        if (window.XMLHttpRequest)
        {
            this.http = new XMLHttpRequest();
        }
        else if (window.ActiveXObject)
        {
            try
            {
                this.http = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch(e)
            {
                try { this.http = new ActiveXObject("Microsoft.XMLHTTP"); }
                catch(e){}
            }
		    if (this.http && this.modeXML && this.http.overrideMimeType)
		        this.http.overrideMimeType('text/xml');
   	    }
	};

	this.req = function(args/*hash || form*/, after/*function*/, url/*script url*/, post/*method*/, nocache)
	{
        this.showProgressForm();
		if (this.http == null)
		{
    	    this.init();
    	    if(this.http == null)
    	    {
    		    alert('Use another browser, please!');
		        return false;
	        }
        }
        this.after = after ? after : null;
        var method = args.method ? args.method : post ? 'post' : 'get';
        var qs = null;
		if (method=='post')
		{
	        qs = this.getArgs(args);
	        url = args.action;
    	    if (!nocache) url += '?rand='+Math.random();
    	}
    	else
    	{
    	    url += '?'+this.getArgs(args);
    	    if (!nocache) url += '&rand='+Math.random();
        }
        this.http.open(method, url);
   		if (method=='post' && typeof(this.http.setRequestHeader)!="undefined")
   		{
            this.http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        }
        if (this.after)
        {
       	    this.http.onreadystatechange = function()
     	    {
    			if (me.http.readyState==4)
        		{
   		            me.after(this.modeXML ? me.http.responseXML : me.http.responseText);
                    me.hideProgressForm();
        		}
     	    };
        }
        else
        {       	    this.http.onreadystatechange = function()
     	    {
                if (me.http.readyState==4) me.hideProgressForm();
     	    };
        }
        this.http.send(qs);
        return false;
	};

    this.getArgs = function(args)
    {
        var url = '';
        if (args)
        {
              if (args.tagName=='FORM')
              {
                  var elem = args.getElementsByTagName('INPUT');
                  var len = elem.length;
                  for(var j=0;j<len;j++)
                  {
                      if (!elem[j].name) continue;
                      switch (elem[j].type.toUpperCase())
                      {
                          case 'CHECKBOX': case 'RADIO':
                          {
                          	if (!elem[j].checked) continue;	break;
                          }
                          case 'BUTTON': case 'SUBMIT': case 'RESET': case 'IMAGE': case 'FILE':
                          {
                          	continue; break;
                          }
                      }
                      if (url.length>0) url += '&';
                      url += encodeURIComponent(elem[j].name)+'='+encodeURIComponent(elem[j].value);
                  }
                  elem = args.getElementsByTagName('TEXTAREA');
                  len = elem.length;
                  for(var j=0;j<len;j++)
                  {
                      if (!elem[j].name) continue;
                      if (url.length>0) url += '&';
                      url += encodeURIComponent(elem[j].name)+'='+encodeURIComponent(elem[j].value);
                  }
                  elem = args.getElementsByTagName('SELECT');
                  len = elem.length;
                  for(var j=0;j<len;j++)
                  {
                      if (!elem[j].name) continue;
                      if (elem[j].multiple)
                      {
                          var opt = elem[j].options;
                          {
                              var optlen = opt.length;
                              for(var k=0;k<optlen;k++)
                              {
                              	if (opt[k].selected)
                              	{
                                    if (url.length>0) url += '&';
                                    url += encodeURIComponent(elem[j].name)+'['+k+']='+encodeURIComponent(opt[k].value);
                              	}
                              }
                          }
                      }
                      else
                      {
                          if (url.length>0) url += '&';
                          url += encodeURIComponent(elem[j].name)+'='+encodeURIComponent(elem[j].value);
                      }
                  }
              }
              else
              {
                  if(typeof(args)=='object') for(var i in args)
                  {
                      if (url.length>0) url += '&';
                      url += encodeURIComponent(i)+'='+encodeURIComponent(args[i]);
                  }
                  else url = args;
              }
        }
        return url;
    };

    this.showProgressForm = function()
    {
        if (!this.progressForm) return;
        var x = parseInt(getClientWidth() / 2) + document.body.scrollLeft;
        var y = parseInt(getClientHeight() / 2) + document.body.scrollTop;
        if (!this.progressForm.offsetWidth)
        {
           this.progressForm.style.left = x+'px';
           this.progressForm.style.top = y+'px';
           this.progressForm.style.display = 'block';
           this.progressForm.style.left = (x - parseInt(this.progressForm.offsetWidth/2))+'px';
           this.progressForm.style.top = (y - parseInt(this.progressForm.offsetHeight/2))+'px';

        }
        else
        {
           this.progressForm.style.left = x - parseInt(this.progressForm.offsetWidth/2)+'px';
           this.progressForm.style.top = y - parseInt(this.progressForm.offsetHeight/2)+'px';
           this.progressForm.style.display = 'block';
        }
    };

    this.hideProgressForm = function()
    {
        if (me.progressForm) me.progressForm.style.display = 'none';
    };

   	this.setProgressForm = function(innerHtml)
   	{
        if (!this.progressForm)
   		{
   		     if (!$('ajax_progress'))
   		     {
   		         var e = $$('DIV');
                 e.style['display'] = 'none';
                 e.style['position'] = 'absolute';
                 e.id = 'ajax_progress';
                 document.body.appendChild(e);
             }
             this.progressForm = $('ajax_progress');
   		}
   		if (innerHtml) this.progressForm.innerHTML = innerHtml;
   	};

   	this.dropProgressForm = function()
   	{
        this.progressForm = null;
   	};
}





