//function Holder(){}
//Holder.prototype.CACHandler = {};
//aHolder = new Holder();

var debug = 0;
var caller;

function CAC()  //initialization
{
  var XMLHTTP = false;
  try //FF, Opera
  {
    XMLHTTP = new XMLHttpRequest();
  }
  catch (e) //IE
  {
    _arrVersions = ['MSXML2.XMLHTTP.6.0',
                    'MSXML2.XMLHTTP.5.0',
                    'MSXML2.XMLHTTP.4.0',
                    'MSXML2.XMLHTTP.3.0',
                    'MSXML2.XMLHTTP',
                    'Microsoft.XMLHTTP'];
    for(i=0; i<_arrVersions.length && !XMLHTTP; i++)
    {
      try
      {
        XMLHTTP = new ActiveXObject(_arrVersions[i]);
      }
      catch (e) {}
    }
  }

  if (!XMLHTTP)
  {
    if(debug)
    {
      alert('Cannot use AJAX!');
    }
  }
  else
  {
    this.XMLHTTP = XMLHTTP;
//    aHolder.CACHandler = this;
  }
}

CAC.prototype =
{
  XMLHTTP: {},
  async: true,
  method: "GET",
  request_URL: "",
  request_params: {},
  request_user: "",
  request_pass: "",
  callback_function: null,
  responseXML: "",
  responseText: "",

  checkMethod: function()
  {
    this.method = this.method.toUpperCase();
    switch(this.method)
    {
      case "POST":
      case "HEAD":
      case "GET" : break;
      default    : this.method = "POST";
    }
    return this.method;
  },

  sendRequest: function()
  {
    this.checkMethod();
    this.responseXML = "";
    this.responseText = "";

    if(this.XMLHTTP && (4 == this.XMLHTTP.readyState || 0 === this.XMLHTTP.readyState))
    {
      if("POST" == this.method)
      {
        this.XMLHTTP.open(this.method, this.request_URL, true/*this.async, this.request_user, this.request_pass*/);
        this.XMLHTTP.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      }
      else
      {
        this.XMLHTTP.open(this.method, this.request_URL+"&"+this.formURLString(this.request_params), true/*, this.request_user, this.request_pass*/);
      }

      if(null !== this.callback_function)
      {
        caller = this;
        this.XMLHTTP.onreadystatechange = function()
          {
            if(caller)
            {
              if(4 == caller.XMLHTTP.readyState && caller.callback_function)
              {
                if("" !== caller.XMLHTTP.responseText)
                {
                  caller.responseText = caller.XMLHTTP.responseText;
                  //alert('answer: ' + caller.responseText);
                  caller.callback_function(caller);
                }
                else
                  if(debug)
                  {
                    alert("responseText is NULL");
                  }
              }
            }
            else
              if(debug)
              {
                alert("XMLHTTP is NULL");
              }
          };
      }
      try
      {
        this.XMLHTTP.send(this.formURLString(this.request_params));
      }
      catch(c)
      {
        if(debug)
        {
          alert("XMLHttpSend failed "+c.toString()+"\n"+c.stack);
          throw c;
        }
      }
    }
    else
      if(debug)
      {
        alert("Cannot use AJAX");
      }
  },

  //goes through an object and makes URL line from object's "attribute=value" pairs
  formURLString: function (obj)
  {
    var str = [];
    for(i in obj)
    {
      str[str.length] =  escape(i) + "=" + escape(obj[i]);
    }
    str = str.join("&");
    return str;
  }
};

/* **************************************************** */

function showData(obj, id_container)
{

  if(obj && "" !== obj.responseText)
  {
    displayText(id_container, obj.responseText);
  }
  else
  {
    displayText(id_container, "No data");
  }
};

function new_request(obj, id_container)
{
  if(null !== obj)
  {
    displayText(id_container,
      '<table align="center"><tr><td class="td_wait">'+_loading_text+'...</td></tr></table>');
    obj.sendRequest();
  }
};

function displayText(id_container, text)
{
  var container = document.getElementById(id_container);
  if(null !== container)
  {
    container.innerHTML = text;
    //alert(container.innerHTML);
  }
};

