
//==================================================================================
//==================================================================================
//
// Ajax-MODUL Projektbezogene Methoden und Objekte
// OO-PROGRAMMIERUNG
// AUTOR: DF
// ERSTELLT: 31.01.2007
//
//==================================================================================
//==================================================================================

/*
XMLHttpRequest-Properties:
open
onreadystatechange
send
readyState
channel
responseXML
responseText
status
statusText
abort
getAllResponseHeaders
getResponseHeader
setRequestHeader
overrideMimeType
multipart
onload
onerror
onprogress
addEventListener
removeEventListener
dispatchEvent
getInterface
*/

var undefined;

function Ajax(id) {
  this.id;
  this.req = undefined;
  this.url = '';
  this.asynchron = true;
  this.parameter = {};
  this.parameterLength = [];
  this.mimeType = 'text/xml';
  this.requestHeader = 'application/x-www-form-urlencoded';
  this.callBackFunction = undefined;
  this.method = 'POST';

  this._setID(id);
}

Ajax.prototype._setID = function (str) {
  if (arguments.length != 1) {
    focus();
    throw new Error("Ajax->_setID: Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("Ajax->_setID: Argument str ist nicht vom Typ String!");
  }
  this.id = str;
}

Ajax.prototype.createRequest = function () {
  var req;
  if (window.ActiveXObject) {
    try {
      req = new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e) {
      req = new ActiveXObject("Msxml2.XMLHTTP");
    }
  } else if (typeof XMLHttpRequest != undefined) {
    req = new XMLHttpRequest();
    /*
    if (this.mimeType){
      req.overrideMimeType(this.mimeType);
    }*/
  } else {
    focus();
    throw new Error("Ajax->createRequest: Der Browser unterstützt die Methode nicht!");
  }
  return req;
}

Ajax.prototype.addParameter = function (strKey, strValue) {
  if (arguments.length != 2) {
    focus();
    throw new Error("Ajax->addParameter: Falsche Anzahl von Argumenten!");
  }
  if (typeof strKey != "string") {
    focus();
    throw new Error("Ajax->addParameter: Argument strKey ist nicht vom Typ String!");
  }
  if (typeof strValue != "string") {
    focus();
    throw new Error("Ajax->addParameter: Argument strValue ist nicht vom Typ String!");
  }
  this.parameter[strKey] = strValue;
  this.parameterLength.push(strKey);
}

Ajax.prototype.setRequestHeader = function (str) {
  if (arguments.length != 1) {
    focus();
    throw new Error("Ajax->setRequestHeader: Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("Ajax->setRequestHeader: Argument str ist nicht vom Typ String!");
  }
  this.requestHeader = str;
}

Ajax.prototype.setAsynchron = function (b) {
  if (arguments.length != 1) {
    focus();
    throw new Error("Ajax->setAsynchron: Falsche Anzahl von Argumenten!");
  }
  if (typeof b != "boolean") {
    focus();
    throw new Error("Ajax->setAsynchron: Argument str ist nicht vom Typ Boolean!");
  }
  this.asynchron = b;
}

Ajax.prototype.setMethod = function (str) {
  if (arguments.length != 1) {
    focus();
    throw new Error("Ajax->setMethod: Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("Ajax->setMethod: Argument str ist nicht vom Typ String!");
  }
  if (str!='GET' && str!='POST' && str!='HEAD'){
    focus();
    throw new Error("Ajax->setMethod: Argument str muss Wert GET oder POST oder HEAD haben!");
  }
  this.method = str;
}

Ajax.prototype.setURL = function (str) {
  if (arguments.length != 1) {
    focus();
    throw new Error("Ajax->setURL: Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("Ajax->setURL: Argument str ist nicht vom Typ String!");
  }
  this.url = str;
}

Ajax.prototype.setCallBackFunction = function (func) {
  if (arguments.length != 1) {
    focus();
    throw new Error("Ajax->setCallBackFunction: Falsche Anzahl von Argumenten!");
  }
  if (typeof func != "function") {
    focus();
    throw new Error("Ajax->setCallBackFunction: Argument func ist nicht vom Typ Function!");
  }
  this.callBackFunction = func;
}

Ajax.prototype.open = function () {
  this.req = this.createRequest();
  if (this.method == 'POST'){
    if (! this.requestHeader){
      focus();
      throw new Error("Ajax->open: Mehtod=POST requires requestHeader !");
    }
  }
  try {
    this.req.open(this.method, this.url, this.asynchron);
    if (this.method == 'POST'){
      this.req.setRequestHeader("Content-type",  this.requestHeader);
    }
    if (this.callBackFunction){
      this.req.onreadystatechange = this.callBackFunction;
    }
  } catch(e){
    Ajax._getNavigatorPrivilege(this);
  }
}

Ajax.prototype.sendAll = function () {
  var _req = '';
  for (var i in this.parameter){
    if (Ajax.getCharset() == 'utf-8' || Ajax.getCharset() == 'utf-16'){
      _req += '&' + i + '=' + encodeURIComponent(this.parameter[i]);
    } else {
      _req += '&' + i + '=' + escape(this.parameter[i]);
    }
  }
  _req = _req.replace(/^\&/,'');
  this.req.send(_req);
}

Ajax.prototype.sendNull = function () {
  this.req.send(null);
}

Ajax.prototype.send = function (strKey, strValue) {
  if (arguments.length != 2) {
    focus();
    throw new Error("Ajax->send: Falsche Anzahl von Argumenten!");
  }
  if (typeof strKey != "string") {
    focus();
    throw new Error("Ajax->send: Argument strKey ist nicht vom Typ String!");
  }
  if (typeof strValue != "string") {
    focus();
    throw new Error("Ajax->send: Argument strValue ist nicht vom Typ String!");
  }
  if (Ajax.getCharset() == 'utf-8' || Ajax.getCharset() == 'utf-16'){
    this.req.send(strKey + '=' + encodeURIComponent(strValue));
  } else {
    this.req.send(strKey + '=' + escape(strValue));
  }
}

Ajax.prototype.getReadyState = function () {
  if (this.req.readyState == undefined){
    return undefined;
  }
  return this.req.readyState;
}

Ajax.prototype.getStatus = function () {
  if (this.req.status == undefined){
    return undefined;
  }
  return this.req.status;
}

Ajax.prototype.getResponseXML = function () {
  return this.req.responseXML;
}

Ajax.prototype.getResponseText = function () {
  return this.req.responseText;
}

Ajax.prototype.getXML = function (str) {
  str = Ajax.stripSpace(str);
  if (arguments.length != 1) {
    focus();
    throw new Error("Ajax->getXML: Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("Ajax->send: Argument str ist nicht vom Typ String!");
  }
  var xml = '';
  if (window.DOMParser){
    var parser = new DOMParser();
    xml = parser.parseFromString(str, "text/xml");
  } else {
    xml = new ActiveXObject("Microsoft.XMLDOM");
    xml.async="false";
    xml.loadXML(str);
  }
  return xml;
}

Ajax.prototype.getReadableXMLNode = function () {
  if (! this.req.responseXML){
    focus();
    throw new Error("Ajax->getReadableXMLNode: INVALID XML, SERVER-REQUEST-STATUS: " + this.getStatus());
    return undefined;
  }
  return this.req.responseXML.documentElement;
}

Ajax.prototype.getSingleNodeValue = function (name) {
  if (arguments.length != 1) {
    focus();
    throw new Error("Ajax->getSingleNodeValue: Falsche Anzahl von Argumenten!");
  }
  if (typeof name != "string") {
    focus();
    throw new Error("Ajax->getSingleNodeValue: Argument name ist nicht vom Typ String!");
  }
  var xml = this.getReadableXMLNode();
  if (! xml){
    return undefined;
  }
  for (var i=0; i<xml.childNodes.length; i++) {
    var node = xml.childNodes[i];
    if (node.nodeType != 1){
      continue;
    }
    if (node.nodeName.toLowerCase() != name){
      continue;
    }
    return this.getValueFromXMLNode(node);
  }
  return undefined;
}

Ajax.prototype.getValueFromXMLNode = function (node) {
  if (arguments.length != 1) {
    focus();
    throw new Error("Ajax->getValueFromXMLNode: Falsche Anzahl von Argumenten!");
  }
  return node.firstChild.nodeValue;
}

Ajax.prototype.getIdenticalNodeArray = function (name) {
  if (arguments.length != 1) {
    focus();
    throw new Error("Ajax->getIdenticalNodeArray: Falsche Anzahl von Argumenten!");
  }
  if (typeof name != "string") {
    focus();
    throw new Error("Ajax->getIdenticalNodeArray: Argument name ist nicht vom Typ String!");
  }
  var xml = this.getReadableXMLNode();
  if (! xml){
    return undefined;
  }
  return xml.getElementsByTagName(name);
}

Ajax._increment = [];
Ajax._registerInstance = {};
Ajax._registerInstanceLength = [];

Ajax._getNavigatorPrivilege = function (obj) {
  if (arguments.length != 1) {
    focus();
    throw new Error("Ajax._getNavigatorPrivilege: Falsche Anzahl von Argumenten!");
  }
  if (! (obj instanceof Ajax)) {
    focus();
    throw new Error("Ajax._getNavigatorPrivilege: Argument obj ist keine Instance von Ajax!");
  }
  if (obj.req) {
    if (typeof netscape != 'undefined' && typeof netscape.security != 'undefined' && typeof netscape.security.PrivilegeManager != 'undefined') {
      netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
      obj.open();
    } else {
      focus();
      throw new Error("Ajax._getNavigatorPrivilege: Das Ajax-Progamm läuft in diesem Kontext nicht. bitte wenden Sie sich an Ihren Administrator!");
    }
  }
}

Ajax.stripSpace = function(attr){
  var result = '';
  if (attr){
    attr = new String(attr);
    result = attr.replace(/^\s+/,'');
    result = result.replace(/\s+$/,'');
  }
  if (result.match(/^\s+/) || result.match(/\s+$/)){
    return Ajax.stripSpace(result);
  }
  return result;
}

Ajax.getInstance = function(id) {
  if (arguments.length!=1) {
    throw new Error("Falsche Anzahl von Argumenten!");
  }
  if (! (Ajax._registerInstance[id])){
    focus();
    throw new Error("Es ist keine Ajax.Instance mit id=" + id + " registriert!");
  }
  return Ajax._registerInstance[id];
}

Ajax.createInstance = function(id) {
  if (!arguments.length) {
    id = 'Ajax' + Ajax._increment.length;
    Ajax._increment.push(1);
  }
  if (! (Ajax._registerInstance[id])){
    Ajax._registerInstance[id] = new Ajax(id);
    Ajax._registerInstanceLength.push(id);
  } else {
    focus();
    throw new Error("Ajax.createInstance: id schon vorhanden!");
  }
  return Ajax.getInstance(id);
}

Ajax.cursorWait = function () {
  if (! document.getElementsByTagName('body').length){
    return;
  }
  document.getElementsByTagName('body')[0].style.cursor = 'wait';
}

Ajax.cursorFinished = function () {
  if (! document.getElementsByTagName('body').length){
    return;
  }
  document.getElementsByTagName('body')[0].style.cursor = 'auto';
}

Ajax.getCharset = function () {
  if (document.charset){
    // IE
    return document.charset.toLowerCase();
  } else if (document.characterSet) {
    return document.characterSet.toLowerCase();
  } else {
    return undefined;
  }
}




