/**
 * [c]Copyright: newmagic datensysteme gmbH[/c]
 * [cre]  Datum: 25.09.2008[/cre]
 * [a]    Autor: Matthias Eipeldauer, newmagic datensysteme gmbh[/a]
 * 
 * [desc]
 * Magic Matrix 8.1 Ajax Interaction Functions / Object
 *
 * mm8AxCore ist das statische Objekt, welches die Interaktion mit dem Webserver behandelt.
 * [/desc]
 **/
var handlers = new Object();
 
var mm8AxCore =
{
  axcore:   null,
  sxcore:   null,
  // ----
  // Init
  init: function()
  {
    this.init_s();
    this.init_a();
  },
  // --
  // init async object
  init_a: function()
  {
    if (!window.XMLHttpRequest)
    {
      try 
      {
        this.axcore = new ActiveXObject('Msxml2.XMLHTTP'); // IE 5
      } 
      catch (e) 
      {
        try 
        {
          this.axcore = new ActiveXObject('Microsoft.XMLHTTP'); // IE 6
        } 
        catch (e) {}
      }
    }
    else
    {
      this.axcore = new XMLHttpRequest();
    }
    this.axcore.onreadystatechange = this.HandleResponse;
  },
  // --
  // init sync object
  init_s: function()
  {
    if (!window.XMLHttpRequest)
    {
      try 
      {
        this.sxcore = new ActiveXObject('Msxml2.XMLHTTP'); // IE 5
      } 
      catch (e) 
      {
        try 
        {
          this.sxcore = new ActiveXObject('Microsoft.XMLHTTP'); // IE 6
        } 
        catch (e) {}
      }
    }
    else
    {
      this.sxcore = new XMLHttpRequest();
    }
  },
  // ----
  // Communication
  AGet: function(url)
  {
    url = this.CheckNoCache(url);
    try
    {
      this.axcore.open('GET', url, true);
    }
    catch(ex)
    {
      this.init_a();
      this.axcore.open('GET', url, true);
    }
    this.axcore.onreadystatechange = this.HandleResponse;
    this.axcore.send(null);
  },
  SGet: function(url)
  {
    url = this.CheckNoCache(url);
    try
    {
      this.sxcore.open('GET', url, false);
    }
    catch(ex)
    {
      this.init_s();
      this.sxcore.open('GET', url, false);
    }
    this.sxcore.send(null);
    return this.sxcore.responseText;
  },
  SGetX: function(url)
  {
    url = this.CheckNoCache(url);
    try
    {
      this.sxcore.open('GET', url, false);
    }
    catch(ex)
    {
      this.init_s();
      this.sxcore.open('GET', url, false);
    }
    this.sxcore.send(null);
    return this.sxcore.responseXML;
  },
  APost: function(url, payload)
  {
    url = this.CheckNoCache(url);
    try
    {
      this.axcore.open('POST', url, true);
    }
    catch(ex)
    {
      this.init_a();
      this.axcore.open('POST', url, true);
    }
    this.axcore.onreadystatechange = this.HandleResponse;
    payload = encodeURI(payload);
    this.axcore.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    this.axcore.setRequestHeader("Content-length", payload.length);
    this.axcore.setRequestHeader("Connection", "close");
    this.axcore.send(payload);
  },
  SPost: function(url, payload)
  {
    url = this.CheckNoCache(url);
    try
    {
      this.sxcore.open('POST', url, false);
    }
    catch(ex)
    {
      this.init_s();
      this.sxcore.open('POST', url, false);
    }
    payload = encodeURI(payload);
    this.sxcore.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    this.sxcore.setRequestHeader("Content-length", payload.length);
    this.sxcore.setRequestHeader("Connection", "close");
    this.sxcore.send(payload);
    return this.sxcore.responseText;
  },
  // --
  // handles async responses (axcore), calls registered custom handler pointers
  HandleResponse: function()
  {
  //    debugger;
    xAjax = null;
    if (this.readyState != null) xAjax = this;
    else if (this != null && this.axcore != null)
    {
      xAjax = this.axcore;
    }
    else
    {
      xAjax = mm8AxCore.axcore;
    }
    if (xAjax.readyState == 4)
    {
      try
      {
        responsecontent = xAjax.responseText;
        command = responsecontent.substring(0, responsecontent.indexOf("!!?"));
        payload = responsecontent.substring(responsecontent.indexOf("!!?") + 3);
        if (handlers[command]) handlers[command](payload);
      }
      catch(ex)
      {
        if (handlers["error"]) handlers["error"](content);
      }
    }
  },
  // --
  // Registers a custom handler function for a given command
  RegisterHandler: function(command, pointer)
  {
    handlers[command] = pointer;
  },
  // --
  // Unregisters a handler function
  UnregisterHandler: function(command)
  {
    if (handlers[command]) handlers[command] = null;
  },
  CheckNoCache: function(url)
  {
    if (this.nocache)
    {
      if (url.indexOf("?") > -1)
      {
        url +="&";
      }
      else
      {
        url += "?";
      }
      datNow = new Date();      
      url += "unique=" + datNow.getTime();
    }
    return url;
  }
};



mm8Callback("coreAjax.js");
