function CConnection()
{
        this.request = undefined;
        this.getContent = get_content;
        this.doRequest = do_request;
        this.doAsyncRequest = do_async_request;
        this.doAsyncRequestWithCallback = do_async_request_with_callback;
        this.parse = parse_;
        this.getRequest = get_request;
        this.setResultFormat = set_result_format;
        this.getResult = get_result;
        this.getLastError = get_last_error;
        this.resultFormat = "ARRAY";
        this.content = "ERROR";
        this.error = "";
        this.arr_result = null;

        this.obj_async = null;

        // Private functions
        this.doAsyncReqWithCallRes = do_async_req_with_call_res;
}

function get_request()
{
        return this.request;
}

function do_request( script, params, method)
{
        if ( this.request == undefined)
                this.request = new XMLHttpRequest();

        if ( method == "POST")
        {
  //      alert( "POST METHOD");
         this.request.open( method, script, false);

         this.request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
                        
         this.request.send( params);
        }
        else
        {
         this.request.open( method, script + "?" + params, false);
         this.request.send( null);
        }

        this.content = this.request.responseText;

//      alert( this.content);

        if ( this.content.substring( 0, 5) == "ERROR")
        {
           this.error = this.content;
           return false;
        }
  
        return this.parse();
}

function do_async_request( script, params, method, callback_func)
{
        if ( this.request == undefined)
                this.request = new XMLHttpRequest();

//        this.request.setTimeouts( 100000);

        if ( method == "POST")
        {
         this.request.open( method, script, true);

         this.request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');

         this.request.send( params);
        }
        else
        {
         this.request.open( method, script + "?" + params, true);
         this.request.send( null);

        }

        this.request.my_this = this;

        this.request.onreadystatechange = function()
        {
            //alert( this.my_this.request.readyState + " : " + this.my_this.request.status);

                if ( this.my_this.request.readyState == 4)
                {
                        if ( this.my_this.request.status == 200)
                        {
                          this.my_this.content = this.my_this.request.responseText;

//                          alert( this.my_this.request.responseText);

                          if ( this.my_this.content.substring( 0, 5) == "ERROR")
                          {
                              this.my_this.error = this.my_this.content;
                              callback_func( true, this.my_this);
                          }
                          else
                          {
                              this.my_this.parse();
                              callback_func( true, this.my_this);
                          }
                        }
                        else
                        {
  //                        callback_func( false, false);
                        }
                }                            
         }

        return true;
}

function do_async_req_with_call_async( success, my_this)
{
//  my_this.obj_async[0] = 1;
  my_this.obj_async[4]( my_this.content)
}

function do_async_req_with_call_res()
{
  if ( my_this.obj_async[0] == 1)
    return;

  result = my_this.doRequest( my_this.obj_async[1],  my_this.obj_async[2],  my_this.obj_async[3]);

  if ( my_this.content.substring(0, 5) == '<END>')
     {
       my_this.obj_async[0] = 1;
       my_this.content = my_this.content.substring(5, my_this.content.length);
     }

//  alert("TOTO:" +  my_this.obj_async[4] + " : " + my_this.content);

  my_this.obj_async[4]( my_this.content);

  if ( my_this.obj_async[0] == 0)
   setTimeout( my_this.doAsyncReqWithCallRes, 10000);
}

function do_async_request_with_callback( script, params, method,
                                         script_res, params_res, method_res,
                                         callback_func)
{
  /* First send the ASYNS function */
  this.obj_async = new Array( 0, script_res, params_res, method_res, callback_func);

  result = this.doAsyncRequest( script, params, method, do_async_req_with_call_async);

  my_this = this; 
  /* Now program the recurent callback */
  setTimeout( this.doAsyncReqWithCallRes, 10000);
}

function get_last_error()
{
        return this.error;
}

function set_result_format( format)
{
        this.resultFormat = format;
}

function get_result()
{
        return this.arr_result;
}

function get_content()
{
        return this.content;
}

function parse_()
{
        if ( this.resultFormat == "ARRAY")
                this.arr_result = eval( this.getContent());

        return true;
}
