// JavaScript Document

var ajaxRequest = function(u, id, m, s)
{
	this.url		= u;
	this.method		= m || "GET";
	this.async		= s || true;
	this.body		= null;
	this.head		= false;
	this.out        = "";
	this.id         = id || null;
	this.msg        = "";
	
	var _this = this;
	
	//------------------------------------------------------------
	try {
		this.request = new XMLHttpRequest();	
	}catch(e){
		try {
			this.request = new ActiveXObject("Microsoft.XMLHTTP");
		}catch(e){
			this.request = false;	
		}
	}
	
	//------------------------------------------------------------
	this.doRequest = function()
	{
		if(!this.url)
		{
			this.onError("keine URL gesetzt");
			return false;
		}
		if(!this.method)
		{
			this.method = "GET";
		}else{
			this.method = this.method.toUpperCase();
		}
		if(!this.request)
		{
			this.onError("kein Verbindungsobjekt gesetzt");
			return false;
		}
		
		this.request.open(this.method, this.url, this.async);
		
		if(this.method == "POST")
		{
			this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
		}
		if(this.head)
		{
			for(var i=0; i<this.head.length; i+=2)
			{
				this.request.setRequestHeader(this.head[i], this.head[i+1]);
			}
		}
		this.request.onreadystatechange = this.checkState;
		this.autosave(this.id); 
		this.request.send(this.body);
	}
	
	//------------------------------------------------------------
	this.onSuccess = function(txt, xml)
	{	
		if(txt != "passt") {
		 document.getElementById('gbError').innerHTML = txt;
		 document.getElementById('gbError').style.display = "block";
		} else {
		 document.getElementById(this.id).submit();
		}
	}
	
	//------------------------------------------------------------
	this.onError = function(msg)
	{
		alert(23);
		alert("Fehler: "+msg);
	}
	
	//------------------------------------------------------------
	this.checkState = function()
	{
		if(_this.request.readyState<4)
		{
			return false;
		}
		if(_this.request.status==200 || _this.request.status == 304)
		{
			_this.onSuccess(_this.request.responseText, _this.request.responseXML);
		}
		else
		{
			_this.onError("Fehler bei der Datenuebertragung");
		}
	}
	
	// -----------------------------------------------------------
	this.urlencode = function(str)
	{
	  var code = "";
      for (var i = 0; i < str.length; i++) {
      if (str.charAt(i) == " ") {
       code += "+";
      } else if (str.charAt(i) == "+") {
       code += "%2B";
      } else if (str.charCodeAt(i) > 127) {
       code += encodeURI(str.charAt(i));
      } else {
       code += escape(str.charAt(i));
      }
     }
     return code; 
	}
	
	// -----------------------------------------------------------	
	this.autosave = function(formid) 
	{
	  var form = document.getElementById(formid);
	  var serialized_form = "";
      for (var i = 0; i < form.elements.length; i++) {
      if (!form.elements[i].disabled && form.elements[i].type != "text/html") {
       serialized_form += this.urlencode(form.elements[i].name) + "=" + this.urlencode(form.elements[i].value);
       serialized_form += "&";
      }
	 } serialized_form = serialized_form.substring(0, serialized_form.length-1);
	  this.body = serialized_form;
	}
	
	// -----------------------------------------------------------
	this.getMsg = function()
	{ 
	 return this.msg;
	}
}
