/**
 * Dateiname       : javascript/AjaxHandler.js
 * Erzeugungsdatum : 12.12.2007
 * Autor           : Carsten Kube
 * Version         : 1.7
 * Letzte Akt.     : 06.10.2009
 *
 * (c) Copyright SECRA GmbH
 */

/*jslint bitwise: true, browser: true, eqeqeq: true, immed: true, newcap: true, nomen: true, plusplus: true, regexp: true, undef: true*/
/*global ActiveXObject, XMLHttpRequest, window */

var secra, Geometry, Handler;
if(!secra) {secra = {};}
else if(typeof secra !== "object") {throw new Error("secra ist kein Objekt!");}
if(secra.AjaxHandler) {
  //throw new Error("secra.AjaxHandler existiert bereits!");
}

secra.AjaxHandler = function(name)
{
  this.x = null;
  this.method = "";
  this.url = "";
  this.body = null;
  this.name = name;
  this.async = true;
  this.json = false;

  if(window.XMLHttpRequest)
  {
    try
    {
      this.x = new XMLHttpRequest();
      //this.name += " XMLHttpRequest";
    }
    catch(e1)
    {
    }
  }
  else if(window.ActiveXObject)
  {
    try
    {
      this.x = new ActiveXObject("Msxml2.XMLHTTP");
      //this.name += " ActiveXObject";
    }
    catch(e2)
    {
      try
      {
        this.x = new ActiveXObject("Microsoft.XMLHTTP");
        //this.name += " ActiveXObject";
      }
      catch(e3)
      {
      }
    }
  }
  var thisObj = this;
  this.x.onreadystatechange = function()
  {
    thisObj.callback();
  };
};

secra.AjaxHandler.prototype.open = function(method, url, async)
{
  this.method = method.toUpperCase();
  this.url = url;
  if(typeof async === "boolean")
  {
    this.async = async;
  }
  this.x.open(this.method, this.url, this.async);
};

secra.AjaxHandler.prototype.submit = function(){
  this.x.open(this.method, this.url, this.async);
  this.send(this.body);
};

secra.AjaxHandler.prototype.send = function(body) {
  if(this.method === "POST")
  {
    this.body = body;
  }
  this.x.send(this.body);
  if(!this.async)
  {
    if(typeof this.state4 === "function")
    {
      this.state4(this);
    }
    return this;
  }
  return null;
};

secra.AjaxHandler.prototype.postFormdata = function(url, formdata, async) {
  this.open("POST", url, async);
  this.x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  return this.send(this.encodeFormdata(formdata));
};

secra.AjaxHandler.prototype.postJSON = function(url, data, async) {
  this.open("POST", url, async);
  this.x.setRequestHeader("Content-Type", "text/plain");
  return this.send(JSON.stringify(data));
};

secra.AjaxHandler.prototype.abort = function() {
  this.state4 = null;
  this.x.abort();
};

secra.AjaxHandler.prototype.encodeFormdata = function(data) {
  var pairs = [], pair, name, value;
  var regexp = new RegExp("%20", "g");

  for(name in data) {
    if(data.hasOwnProperty(name)) {
      value = data[name] === null ? null : data[name].toString();
      pair = encodeURIComponent(name).replace(regexp, "+") + "=" +
        encodeURIComponent(value).replace(regexp, "+");
      pairs.push(pair);
    }
  }

  return pairs.join("&");
};

secra.AjaxHandler.prototype.encodeJSON = function(data) {
  var value, regexp = new RegExp("%20", "g");

  value = JSON.stringify(data);

  return "json=" + encodeURIComponent(JSON.stringify(data)).replace(regexp, "+");
};

secra.AjaxHandler.prototype.registerCallback = function(p, returnType) {
  for(var key in p)
  {
    if (typeof p[key] === "function")
    {
      this[key] = p[key];
    }
  }
  if(returnType === "json") {
    this.json = true;
  }
};

secra.AjaxHandler.prototype.callback = function() {
  if(this.x.readyState === 0)
  {
    if(typeof this.state0 === "function")
    {
      this.state0(this);
    }
  }
  else if(this.x.readyState === 1)
  {
    if(typeof this.state1 === "function")
    {
      this.state1(this);
    }
  }
  else if(this.x.readyState === 2)
  {
    if(typeof this.state2 === "function")
    {
      this.state2(this);
    }
  }
  else if(this.x.readyState === 3)
  {
    if(typeof this.state3 === "function")
    {
      this.state3(this);
    }
  }
  else if(this.x.readyState === 4)
  {
    if(typeof this.state4 === "function")
    {
      this.state4(this);
    }
  }
};

secra.AjaxHandler.prototype.toString = function() {
  return "[XMLHttpObject " + this.name + "]";
};

secra.AjaxHandler.prototype.getXML = function() {
  return this.x.responseXML;
};
secra.AjaxHandler.prototype.getText = function() {
  return this.x.responseText;
};
secra.AjaxHandler.prototype.getJSON = function() {
  var t;
  try {
    t = JSON.parse(this.x.responseText);
  } catch(ex) {
    t = {
      "parsererror":  this.addslashes(this.x.responseText),
      "error":        true
    };
  }

  return t;
};

secra.AjaxHandler.prototype.addslashes = function(str) {
  return (str+"").replace(/([\\"'])/g, "\\$1").replace(/\u0000/g, "\\0");
};


