/**
 *  Description: A CMyAJAX class.
 *  Author: Copyright (c) EZi Software.
 *  Version: v1.0.0
 *  ChangeLog: 
 *  2006-08-23 Tomaz Zveglic  <tzveglic@gmail.com>
 *  dbmysql.inc.php: Project started. 
 */
function CMyAJAX()
{
  var m_xmlHttp = 0;
  var m_jsExecuteAtRequest = '';
  var m_jsExecuteAtResponse = '';
  var m_jsExecuteAtProcess = '';
  
  this.GetXmlHttpObject = GetXmlHttpObject;
  this.GetMSXmlHttp = GetMSXmlHttp;
  this.CreateXmlHttp = CreateXmlHttp;
  this.SendXmlHttpRequest = SendXmlHttpRequest;
  this.ExecuteCall = ExecuteCall;
  this.CallbackMethod = CallbackMethod;
  this.GetAJAXObject = GetAJAXObject;
  this.EncodeURL = EncodeURL;
  this.Set_JSOnRequest = Set_JSOnRequest;
  this.Set_JSOnResponse = Set_JSOnResponse;
  this.Set_JSOnProcess = Set_JSOnProcess;
  
  function GetXmlHttpObject(handler)
  { 
    var objXmlHttp = null;
    objXmlHttp = GetAJAXObject();
    objXmlHttp.onreadystatechange = handler;
    return objXmlHttp; 
  } 

  function GetMSXmlHttp()
  {
    var xmlHttp = null;
    var clsids = ["Msxml2.XMLHTTP", "Microsoft.XMLHTTP", 
                  "Msxml2.XMLHTTP.6.0","Msxml2.XMLHTTP.5.0",
                  "Msxml2.XMLHTTP.4.0","Msxml2.XMLHTTP.3.0", 
                  "Msxml2.XMLHTTP.2.6","Microsoft.XMLHTTP.1.0", 
                  "Microsoft.XMLHTTP.1","Microsoft.XMLHTTP"];
    for(var i=0; i<clsids.length && xmlHttp == null; i++)
    {
      xmlHttp = CreateXmlHttp(clsids[i]);
    }
    return xmlHttp;
  }

  function CreateXmlHttp(clsid)
  {
    var xmlHttp = null;
    try
    {
      xmlHttp = new ActiveXObject(clsid);
      lastclsid = clsid;
      return xmlHttp;
    }
    catch(e) {}
  }
  function GetAJAXObject()
  {
    var C=null;
    try{
      C=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
      try
      {
        C=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(sc)
      {
        C=null;
      }
    }
    if(!C&&typeof XMLHttpRequest!="undefined")
    {
      C=new XMLHttpRequest();
    }
    return C;
  }
  function SendXmlHttpRequest(xmlhttp, url)
  {
    xmlhttp.open('GET', url, true); 
    xmlhttp.send(null); 
  }

  function ExecuteCall(url)
  {
    try 
    {
      if(m_jsExecuteAtRequest!='')
      {
        eval(m_jsExecuteAtRequest);
      }
      xmlHttp = GetXmlHttpObject(CallbackMethod); 
      SendXmlHttpRequest(xmlHttp, url); 
    }
    catch(e)
    {
	    if(m_jsExecuteAtResponse!='')
      {
        eval(m_jsExecuteAtResponse);
      }
	  } 
  } 

  function CallbackMethod() 
  { 
    try
    {
      if (xmlHttp.readyState == 4 || 
          xmlHttp.readyState == 'complete')
      {
        if(m_jsExecuteAtResponse!='')
        {
          eval(m_jsExecuteAtResponse);
        }
          
        var response = xmlHttp.responseText; 
        if (response.length > 0)
        {
          if(m_jsExecuteAtProcess!='')
          {
            eval(m_jsExecuteAtProcess+"('"+response+"');");
          }
          else
          {
            eval(response);
          }
        }
      }
    }
    catch(e){}
  }
  function EncodeURL(sKeyWord)
  {
    if(encodeURIComponent)
      return encodeURIComponent(sKeyWord);
    if(escape)
      return escape(sKeyWord);
    return sKeyWord;
  }
  function Set_JSOnRequest(sJSCode)
  {
    m_jsExecuteAtRequest = sJSCode;
  }
  function Set_JSOnResponse(sJSCode)
  {
    m_jsExecuteAtResponse = sJSCode;
  }
  function Set_JSOnProcess(sJSCode)
  {
    m_jsExecuteAtProcess = sJSCode;
  }
}
