/*///////////////////////////////////////////////////////////////////////
//Ajax.js
//Written by Electric Solutions GbR
//Copyright 2002-2009 by Electric Solutions GbR
//Authors: R. Grosseck
//elso@p-link.de
//www.electric-solutions.de
///////////////////////////////////////////////////////////////////////*/
function Ajax (params, callback){
	if(params.indexOf('http://') >= 0 || params.indexOf('https://') >= 0){
		var up =  params.split("?");
		this.url = up[0];
		this.params = up[1];
	}
	else {
  	this.url = url+"/ajax.php";
  	this.params = params+'&sid='+sid;
  }
  if(callback !== null)
  	this.callback = callback;
  	this.create ();
  if(callback !== null)
  	this.req.onreadystatechange = this.dispatch (this);
}

Ajax.prototype.dispatch = function (ajax) {	
	function funcRef()
	{
		if (ajax.req.readyState == 4) {
			if (ajax.callback) {
				ajax.callback (ajax.req);
			}
		}
	}
	
	return funcRef;
}

Ajax.prototype.request = function (methode) {
	if(methode){
		this.req.open("GET", this.url + "?" + this.params, true);
		this.req.send (null);
	}
	else {
		this.req.open("POST", this.url, true);
		this.req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		this.req.send (this.params);
	}
	
}

Ajax.prototype.post_form = function (formObj) {
	this.req.open("POST", this.url, true);
	this.req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	for (var i = 0; i < formObj.length; i++) {
		switch (formObj.elements[i].type) {
			case "select-multiple":
				for(var s=0; s<formObj.elements[i].options.length; s++) {
					if (formObj.elements[i].options[s].selected) {
						this.params+='&'+formObj.elements[i].id+'['+s+']'+'='+formObj.elements[i].options[s].value;						
					}
				}                
			break;
			
			case "checkbox":
				if(formObj.elements[i].checked == true)
				this.params+='&'+formObj.elements[i].name+'='+formObj.elements[i].value;
			break;

			default:
				this.params+='&'+formObj.elements[i].name+'='+formObj.elements[i].value;
			break;
		}		
	}
	this.req.send(this.params);
}

Ajax.prototype.create = function () {

  var http_request;

  if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();			
	} 
	else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) {
				alert('Giving up :( Cannot create an XMLHTTP instance');
				return false;	
			}
		}
	}
  this.req = http_request;
}
