function loadGeneric(url){
	var request = new ajaxObject(url, processGenericData);
	request.update();
}

function processGenericData(responseText, isState) {
	if (isState == "200"){
		getRefToElement("errorbox").innerHTML = "";
		eval(responseText);
		var ref = getRefToElement("displaytable");
		removeRows(ref);
		buildTable(ref, resultData);
	}else{
		getRefToElement("displaytable").innerHTML = "";
		getRefToElement("errorbox").innerHTML = "<p class='error'>Fehler: Listings sind nicht vorhanden!<br />Error: The listings are currently not available!</p>";
	}
}

function removeRows(ref){
	var theKids = ref.getElementsByTagName("tr");
	while(theKids.length > 0){
		for(var i=0; i<theKids.length; i++){
			ref.removeChild(theKids[i]);
		}
		theKids = ref.getElementsByTagName("tr");
	}
}

function buildTable(ref, resultData){
	for(x in resultData){
		var nextRow = ref.insertRow(-1);
		nextRow.style.backgroundColor = "#ffffff";
		var theNextArray = resultData[x];
		for (y in theNextArray){
			var nextCell = nextRow.insertCell(-1);
			var theContent = theNextArray[y];
			if (theContent.charAt(0) == "#"){
				nextCell.style.textAlign = "center";
				theContent = theContent.substring(1);
			}else{
				nextCell.style.textAlign = "left";
			}
			nextCell.innerHTML = theContent;
			//nextCell.align = "top";
			nextCell.style.borderBottom = "1px solid #563610";
			if (y == 0){
			}else{
				
			}
		}
	}
}

function loadErgebnisse(file){
	var request = new ajaxObject('/deutsch/workingtest/'+file+'.json', processData);
	request.update();
}

function loadIwtErgebnisse(file){
	var request = new ajaxObject('/deutsch/archiv/'+file+'.json', processData);
	request.update();
}

function processData(responseText, isState) {
	if (isState == "200"){
		getRefToElement("errorbox").innerHTML = "";
		eval(responseText);
		var theData = new resultData.dataSet();
		var ref;
		if (theData.richter){
			ref = getRefToElement("richter")
			removeRows(ref);
			buildTable(ref, theData.richter);
		}
		if (theData.sniffers){
			ref = getRefToElement("sniffers")
			removeRows(ref);
			buildTable(ref, theData.sniffers);
		}
		if (theData.beginners){
			ref = getRefToElement("beginners")
			removeRows(ref);
			buildTable(ref, theData.beginners);
		}
		if (theData.novices){
			ref = getRefToElement("novices")
			removeRows(ref);
			buildTable(ref, theData.novices);
		}
		if (theData.open){
			ref = getRefToElement("open")
			removeRows(ref);
			buildTable(ref, theData.open);
		}
	}else{
		getRefToElement("errorbox").innerHTML = "<p class='error'>Fehler: Ergebnisse nicht vorhanden!<br />Error: The results could not be found!</p>";
	}
}

function makeRequest() {
        var httpRequest;

        if (window.XMLHttpRequest) { // Mozilla, Safari, Opera, Konqueror, MSIE 7
        	try{
            	httpRequest = new XMLHttpRequest();
            } catch (ignore) {
            	httpRequest = false;
            }
        } else if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                   httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (ignore) {
                	httpRequest = false;
                }
            }
        }

        if (!httpRequest) {
            alert('Error : Cannot create an httpRequest instance');
        }
        
        return httpRequest;

    }
	
    function replaceElement(anHttpRequest, anXmlTag, anXmlItem, anElId) {
        if (anHttpRequest.readyState == 4) {
            if (anHttpRequest.status == 200) {
            	var xml_doc = anHttpRequest.responseXML;
				var xml_node = xml_doc.getElementsByTagName(anXmlTag).item(anXmlItem);
       			getRefToElement(anElId).innerHTML = xml_node.firstChild.data;
            } else {
                alert('There was a problem with the request: RC = '+httpRequest.status);
            }
        }
    }
    
    
    function ajaxObject(url, callbackFunction) {
        var that=this;      
        this.updating = false;
        this.abort = function() {
          if (that.updating) {
            that.updating=false;
            that.AJAX.abort();
            that.AJAX=null;
          }
        }
        this.update = function(passData,postMethod) { 
          if (that.updating) { return false; }
          that.AJAX = null; 
          that.AJAX = makeRequest();
          if (that.AJAX==null) {                             
            return false;                               
          } else {
            that.AJAX.onreadystatechange = function() {  
              if (that.AJAX.readyState==4) {             
                that.updating=false;                
                that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);        
                that.AJAX=null;                                         
              }                                                      
            }                                                        
            that.updating = new Date();                              
            if (/post/i.test(postMethod)) {
              var uri=urlCall+'?'+that.updating.getTime();
              that.AJAX.open("POST", uri, true);
              that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
              that.AJAX.setRequestHeader("Content-Length", passData.length);
              that.AJAX.send(passData);
            } else {
              var uri=urlCall+'?'+passData+'&amp;timestamp='+(that.updating.getTime()); 
              that.AJAX.open("GET", uri, true);                             
              that.AJAX.send(null);                                         
            }             
            return true;                                             
          }                                                                           
        }
        var urlCall = url;        
        this.callback = callbackFunction || function () { alert("No Callback Function");};
      }
