var debug = true;
var iLoad = 0;
var update = "";
var opened;
var iStatus;
var lastResponse = "";
var sSession = "";

function getHTTPObject() {
  var xmlhttp;
  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
     try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
     } catch (E) {
        xmlhttp = false;
     }
  }

  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
      xmlhttp.overrideMimeType('text/html');
    } catch (e) {
      xmlhttp = false;
    }
  }

  return xmlhttp;
}

var objects = new Array();

function getHttp(rid)
{
	if(!objects[rid])
	{
		alert("illegal requestid");
	}
	return objects[rid];
}

function handleHttpResponse(rid,update) 
{
	var http = getHttp(rid);
	//var update = http.doupdate;
  try
  {
	  if (http.readyState == 4) 
	  {
			if(http.status==200)
			{
				//lastResponse = http.responseText;
				//alert(http.responseText);
		  		try
		  		{
		  			if(update!='' && update!='alert_return' && update.indexOf('callback_')==-1)
		  			{
  		    			document.getElementById(update).innerHTML = http.responseText;
  		    		}
  		    		else if(update=='alert_return')
  		    		{
  		    			alert(http.responseText);
  		    		}
  		    		else if(update.indexOf('callback_')==0)
  		    		{
  		    			var abuseParam = http.responseText.replace(/\n/g,"") + '';
  		    			var callbackMethod = "abuseret("+abuseParam+")";
  		    			try
  		    			{
  		    				//eval("abuseret(\'' + abuseParam +'\');");
  		    				//alert(update.substring(update.indexOf("_")+1,update.length));
  		    				eval(update.substring(update.indexOf("_")+1,update.length)+ "(\'' + abuseParam +'\');");
  		    			}catch(e)
  		    			{
							alert(e);
  		    			}
  		    		}
  		    	}
  		    	catch(e)
  		    	{
  		    		if(debug)alert('Kann Element nicht finden ' + update + ' ' + e);
  		    	}
		    }
		    else
		    {
		    	//alert(http.responseText);
		    	document.getElementById(update).innerHTML = http.status;
		    	//alert(http.status);
		    }
			//alert(http.status + http.responseText);
	   }
	   //iStatus = http.status;
	}
	catch(f)
	{
		alert(f);
	}
}

function handleCallbackResponse(rid,func)
{
	var http = getHttp(rid);
	
	if(http.readyState == 4)
	{
		if(http.status == 200)
		{
			var o = new Object();
			o.responseText = http.responseText;
			
			if(func != null)
			{
				o.getResponseObject = function(){
					var resp = eval("(" + o.responseText + ")");
					return resp;
				};
				
				func.call(null,o);
			}
			//callback(func,{args:[o],suppressArgs:true}).call(null, null);
		}
	}
}

function mycall(object)
{
	alert(object.responseText);
}

function doCallbackRequest(sFile,sQuery,func)
{
	var rid = objects.length;
	
	var http = getHTTPObject();
	//http.doupdate = sUpdate;
	
	objects.push(http);
	
	try
	{
		
		theURL = sFile;
		if(theURL.indexOf("?")!=-1)
		{
			theURL = theURL.substring(0,sFile.indexOf("?"));
		}
		i = '1';
		if(sSession!=''&& sFile.indexOf("jsessionid")==-1)
		{
			theURL+=";jsessionid=" + sSession;
		}
		else
		{
			if(theURL.indexOf("?")==-1)
			{
				theURL += "?";
			}
		}
		
		http.onreadystatechange = function() { 
			handleCallbackResponse(rid,func);				
		};	
			
		http.open("POST", theURL, true);
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=UTF-8");
		http.setRequestHeader("Content-length", sQuery.length);
		http.setRequestHeader("Connection", "close");
  		http.send(sQuery);
  	}
  	catch(e)
  	{
  		alert(e + ' ' + i + ' ' + theURL );
  	}
}

function doRequest(sFile, sUpdate, session,query,delay)
{
	var rid = objects.length;
	
	http = getHTTPObject();
	//http.doupdate = sUpdate;
	
	objects.push(http);
	
	document.getElementById(sUpdate).innerHTML = "<span style=color:#ff0000>laden...</span>"
	
	update = sUpdate;
	var i = 0;
	try
	{
		
		theURL = sFile;
		if(theURL.indexOf("?")!=-1)
		{
			theURL = theURL.substring(0,sFile.indexOf("?"));
		}
		i = '1';
		if(session!=''&& sFile.indexOf("jsessionid")==-1)
		{
			theURL+=";jsessionid=" + session;
			//theURL+= "?sDesign=Direct&" + query;
		}
		else
		{
			if(theURL.indexOf("?")==-1)
			{
				theURL += "?";
			}
			//theURL+= "sDesign=Direct&" + query;
		}
		//alert(query);
		i = '2';
		
		//document.write(theURL);
		//var searchQuery = document.getElementById(submit).value;
		i = '3';
			http.onreadystatechange = function() { 
				handleHttpResponse(rid,sUpdate);				
			};	
			
  		i='4';
  		//if(sUpdate!='handlerState')
  		{
  			http.open("POST", theURL, true);
  			http.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=UTF-8");
				http.setRequestHeader("Content-length", query.length);
				http.setRequestHeader("Connection", "close");
  			
  			i = '5';
  		}
  		http.send(query);
  		i = '6'
  		//alert(theURL + ' ' + i);
  	}
  	catch(e)
  	{
  		alert(e + ' ' + i + ' ' + theURL );
  	}
}

/** 
 * 
 * @param {Function} func the callback function 
 * @param {Object} opts an object literal with the following 
 * properties (all optional): 
 * scope: the object to bind the function to (what the "this" keyword will refer to) 
 * args: an array of arguments to pass to the function when it is called, these will be 
 * appended after any arguments passed by the caller * suppressArgs: boolean, whether to supress the arguments passed 
 * by the caller. This default is false. 
 */ 

function callback(func,opts)
{ 
	var cb = function()
	{ 
		var args = opts.args ? opts.args : []; 
		var scope = opts.scope ? opts.scope : this; 
		var fargs = opts.supressArgs === true ? [] : toArray(arguments); 
		func.apply(scope,fargs.concat(args)); 
	} 
	return cb; 
} 

function toArray(arrayLike)
{ 
	var arr = []; 
	for(var i = 0; i < arrayLike.length; i++)
	{ 
		arr.push(arrayLike[i]); 
	} 
	
	return arr; 
}
