var htmlConnection;
var browserIE;
var method;
var data_to_send;
var debug = false;



function AjaxClass() 
{
  this.method = null;
  this.data_to_send = null;
  this.url = null;
  this.show_loading = false;
  var show_loading = false;
  var loading_id;
  var obj;
  
  this.setLoadingId = setLoadingId;
  this.showLoading = showLoading;
  this.hideLoading = hideLoading;
  this.setMethod = setMethod;
  this.setUrl = setUrl;
  this.setDataToSend = setDataToSend;
  this.createXMLHttp = createXMLHttp;
  this.sendData = sendData;

  this.onCompleted = onCompleted;
  this.onFailed = onFailed;
  this.onForbidden = onForbidden;
  this.onLoading = onLoading;
  this.onLoaded = onLoaded;
  this.getResponseText = getResponseText;
  this.getResponseXML = getResponseXML;
}
  
  function setLoadingId(id)
  {
    loading_id = id;
    this.loading_id = id;
    show_loading = true;
    this.show_loading = true;
  }

  function setMethod(method)
  {
    this.method = method;
  }
  
  function setUrl(url)
  {
    this.url = url;
  }
  
  function setObj(obj)
  {
    this.obj = obj;
  }

  function setDataToSend(data)
  {
    this.data_to_send = data;
  }

  function createXMLHttp()
  { 
    var ret = null;
    browserIE = false;
    if ( window.XMLHttpRequest )
    {
      ret = new XMLHttpRequest();
      if ( navigator.appName == 'Microsoft Internet Explorer' ) 
        browserIE = true;
    }
    else if ( window.ActiveXObject )
    {
      ret = new ActiveXObject('Microsoft.XMLHTTP');
      browserIE = true;
    }

    return ret;
  }

  function sendData(data_to_send)
  {
    this.htmlConnection = htmlConnection = this.createXMLHttp();
        
    if ( !this.method )
      this.setMethod('GET');

    if ( htmlConnection!=null )
    {
      if ( browserIE == true )
      {
      
        htmlConnection.onreadystatechange = stateHandler;
        htmlConnection.open(this.method, this.url, true);
        htmlConnection.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        if ( this.data_to_send )
          htmlConnection.send(this.data_to_send);
        else
          htmlConnection.send();
      }
      else
      {

				htmlConnection.onreadystatechange = this.stateHandler;

        htmlConnection.open(this.method, this.url, true);
        htmlConnection.overrideMimeType('text/xml');
        if ( data_to_send )
          htmlConnection.send(data_to_send);
        else
          htmlConnection.send(null);
      }
    }
  }
  
  function getResponseText()
  {
    if ( this.htmlConnection.readyState == 4 )
      return this.htmlConnection.responseText;
    else
      return '';
  }
  
  function getResponseXML()
  {
    if ( this.htmlConnection.readyState == 4 )
      return this.htmlConnection.responseXML;
    else
      return '';
  }
  
  function stateHandler()
  {
  
    if( !this.htmlConnection )
      return;

    // 1 - Loading
    if ( this.htmlConnection.readyState == 1 )
    {
      this.showLoading();
    
    // 2 - Loaded
    }
    else if ( this.htmlConnection.readyState == 2 )
    {
      this.onLoaded();
       
    // 3 - Receiving
    }
    else if ( this.htmlConnection.readyState == 3 )
    {
      this.onReceiving();
        
    // 4 - Completed
    }
    else if ( this.htmlConnection.readyState == 4 )
    {
      if ( htmlConnection.status == 200 )
      {
        this.onCompleted();
      }
      else if ( this.htmlConnection.status == 403 )
      {
        this.onForbidden();
      } else if ( this.htmlConnection.status == 404 )
      {
        this.onFailed();
      }
      window.setTimeout('hideLoading()', 200);
     
    // NONE OF THEM
    }
    else
    {
      ;
    }
  }
    
  function onCompleted()
  {     
    if ( debug )
    {
      alert(this.htmlConnection.responseText);
    }
    
    if ( this.show_loading )
      window.setTimeout('hideLoading()', 300);
      
    success();
  }
    
  function onFailed()
  {
    if ( debug )
      alert('failed');
  }
    
  function onReceiving()
  {
    if ( debug )
      alert('receiving');
  }
    
  function onForbidden()
  {
    if ( debug )
      alert('Error 404 - access forbidden.');
  }
    
  function onLoading()
  {
    if ( debug )
      alert('loading');
    if ( this.show_loading ) 
      showLoading();
  }
    
  function onLoaded()
  {
    if ( debug )
      alert('loaded');
  }
      
  function showLoading()
  {
    if ( !this.show_loading )
      return false;
      
    if ( loading_id && $(loading_id) )
      $(loading_id).className = $(loading_id).className.replace('hidden', '');
    if ( this.loading_id && $(this.loading_id) )
      $(this.loading_id).className = $(this.loading_id).className.replace('hidden', '');
  }
  
  function hideLoading()
  {
    if ( !this.show_loading )
      return false;
      
    if ( loading_id && $(loading_id) )
      $(loading_id).className += ' hidden ';
    if ( this.loading_id && $(this.loading_id) )
      $(this.loading_id).className += ' hidden ';
  }
