var loadImage;

function constructAjaxObject () {
   var xmlHttp = false;

  // Mozilla, Opera, Safari sowie Internet Explorer 7
   if (typeof(XMLHttpRequest) != 'undefined') {
      xmlHttp = new XMLHttpRequest();
//      xmlHttp.overrideMimeType('text/xml; charset=ISO-8859-1');
   };

   if (!xmlHttp) {
    // Internet Explorer 6 und älter
      try {
	 xmlHttp  = new ActiveXObject("Msxml2.XMLHTTP");
      } catch(e) {
	 try {
	    xmlHttp  = new ActiveXObject("Microsoft.XMLHTTP");
	 } catch(e) {
	    xmlHttp  = false;
	 };
      };
   };
   
   return xmlHttp;
};

/* Konstruktor des ajax-Objekts */
function ajax (thing) {
   this.toolPath = thing;
   this.object = constructAjaxObject();
   this.sendRequest = sendRequest;
   this.handleResponse = handleResponse;
   this.analyzeResponse = analyzeResponse;
   this.loading = false;
};

/* Gibt die Direktive aus line an das Serverscript weiter */
function sendRequest(line) {
//  Sende-Meldung
   ajax = ajaxObj.object;
   if (ajaxObj.loading) {
      todo.push(line);
   } else {
      ajaxObj.loading = true;
      ajax = this.object;
      if (loadImage) {
	 loadImage.src = '/m_images/loading.gif';
      };
      ajax.open('post', this.toolPath, true);
      ajax.onreadystatechange = handleResponse;
      ajax.send(line);
   };
};

function handleResponse() {
   if(ajax.readyState == 4){
      try {
	 if (ajax.status == 200) {
	    var response = ajax.responseXML;
	    ajaxObj.analyzeResponse(response);
	    ajaxObj.loading = false;

	    if (todo[0]) {
	       ajaxObj.sendRequest(todo.shift());
	    };

	 } else {
// Fehler-Meldung
	    window.alert('An error occured.');
	    ajaxObj.loading = false;
	 };
      } catch (e) {
      };
   };
};