var req;
var area;

function Initialize()  {

   if (window.XMLHttpRequest) {
      // Mozilla, Safari,...
      req = new XMLHttpRequest();
      if(req.overrideMimeType) {
         req.overrideMimeType('text/xml');
      }
   } else {
	   try {
	      req = new ActiveXObject("Msxml2.XMLHTTP");
	   }
	   catch(e) {
	      try {
	         req = new ActiveXObject("Microsoft.XMLHTTP");
	      }
	      catch(oc) {
	         req = null;
	      }
	   }
   }

   /**
    * 
    * Für gewöhnlich sollte dieser Block mehr Fehler verursachen als zu helfen.
    * @todo Bitte prüfen...
    * 
    */
   /*
   if(!req && typeof XMLHttpRequest!="undefined") {
      req = new
     
      XMLHttpRequest();
   }
   */
} 
  
function SendQuery(url, target) {

   Initialize(); 

   if(req != null) {
      area = target;

      //elem = document.getElementById(area);
      //elem.innerHTML='<div align="center">'+
      //               '<b style="color:#ff0000">'+
      //               ' *** PLEASE WAIT *** </b></div>';

      req.onreadystatechange = Process;
      req.open("GET", url, true);
      req.setRequestHeader('X_REQUESTED_WITH', 'XmlHttpRequest');
      req.send(null);
   }

}

function SendQuerySynchron(url, target) {
   Initialize(); 

   if(req != null) {
      area = target;
      req.onreadystatechange = Process;
      req.open("GET", url, false);
      req.setRequestHeader('X_REQUESTED_WITH', 'XmlHttpRequest');
      req.send(null);
      Process();
   }

}

function Process() {
   contentArea = area;

   if(req.readyState == 4) {
      if(req.status == 200 && req.responseText != "") { 
        if(!document.getElementById(contentArea)) window.alert('Can not found contentArea: '+contentArea);

         var obj = document.getElementById(contentArea);
         obj.innerHTML = req.responseText;
         obj.style.display = 'inline';
      } else { 
         document.getElementById(contentArea).innerHTML = "There was a problem retrieving data:<br>" + req.statusText;
      }
   }
}

function SendForm(url, target, formId, synchron) {

   area = target;
   asynchron = true;
   if(synchron) asynchron = false;

   parameters = getFormElements(formId);

   Initialize();
   if(!req) {
      alert('Cannot create XMLHTTP instance');
      return false;
   }

   if(req != null) {
      req.onreadystatechange = Process;
      req.open("POST", url, asynchron);
      req.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
      req.setRequestHeader("Content-length", parameters.length);
      req.setRequestHeader("Connection", "close");
      req.setRequestHeader('X_REQUESTED_WITH', 'XmlHttpRequest');
      req.send(parameters);

      if(synchron) Process();
   }
}

function getFormElements(formId)
{
    var parameters = [];

    form = document.getElementById(formId);
    if(!form) window.alert('Form '+formId+' not found');
    
    for(var x = 0; x < form.elements.length; x++) { // select, radio gesondert behandeln !!!!
        var element = form.elements[x];

        if(element.type == "radio" && element.checked == false) continue;

        if(typeof parameters[element.name] == "undefined") {
            parameters[element.name] = [];
        }

        parameters[element.name].push(element.value);
    }
    

    var parametersStr = "";
    
    for(var x in parameters) {
       parametersStr += (parametersStr.length? "&" : "") + 
                        encodeURIComponent(x) + "=" + 
                        encodeURIComponent(parameters[x]);
    }

    return parametersStr;
}

