var browser = '';
var browserName=navigator.appName;
if (browserName=="Microsoft Internet Explorer") {
	browser = 'ie';
}
else {
	browser='nonie';
}

var pool = new Array();

function ajaxCommunicator(){
	
	if(browser=='ie'){
		function pickRecentProgID(idList, enabledList){
		    // found progID flag
		    var bFound = false;
		    for(var i=0; i < idList.length && !bFound; i++){
		        try{
		            var oDoc = new ActiveXObject(idList[i]);
		            o2Store = idList[i];
		            bFound = true;
		            for(var j=0;j<enabledList.length;j++)
		                if(i <= enabledList[j][1])
		                    Sarissa["IS_ENABLED_"+enabledList[j][0]] = true;
		        }catch (objException){
		            // trap; try next progID
		        };
		    };
		    if (!bFound)
		        throw "Could not retreive a valid progID of Class: " + idList[idList.length-1]+". (original exception: "+e+")";
		    idList = null;
		    return o2Store;
		};
		// pick best available MSXML progIDs
		this._PIM_XMLHTTP_PROGID = pickRecentProgID(["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"], [["XMLHTTP", 4]]);
		// we dont need this anymore
		pickRecentProgID = null;
	}
	
}

ajaxCommunicator.prototype.getXMLHTTP = function(){
	var xmlhttp=null;
	//deba(this);
	if(pool.length>0){
		xmlhttp = pool.shift();
	}
	else{
		xmlhttp = this.createXMLHTTP();
	}
	return xmlhttp;
}
	
ajaxCommunicator.prototype.createXMLHTTP = function(){
	var A = null;
	if(typeof XMLHttpRequest != "undefined") {
		// Mozilla
		A=new XMLHttpRequest();
	}
	if (!A && document.all) {
		// IE
		A=new ActiveXObject(this._PIM_XMLHTTP_PROGID);
	}
	return A;
}
	
ajaxCommunicator.prototype.getData = function(url,callback){
	var xmlhttp = this.getXMLHTTP();
	if (xmlhttp) {
		xmlhttp.open("GET", url, true);
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState == 4) {
				callback(xmlhttp);
				pool[pool.length]=xmlhttp;
//				deba(pool);
			}
		}
		xmlhttp.send(null);
	}
	else{
		throw('No XmlHTTPRequest Object!');
	}
}

ajaxCommunicator.prototype.postData = function(url,data,callback){
	var xmlhttp = this.getXMLHTTP();
	if (xmlhttp) {
		var postdata = new Array;
		for(name in data){
			postdata[postdata.length] = name + '=' + data[name];
		}
		//deba(postdata);
		xmlhttp.open("POST", url, true);
		xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState == 4) {
				callback(xmlhttp);
				pool[pool.length]=xmlhttp;
//				deba(pool);
			}
		}
		xmlhttp.send(postdata.join("&"));
	}
	else{
		throw('No XmlHTTPRequest Object!');
	}
}

var ajax = new ajaxCommunicator();

function updateRight(){
	ajax.getData("requests.php?do=updateRight",function(xmlhttp) {
				if (xmlhttp.status == 200) {
					document.getElementById('right').innerHTML = xmlhttp.responseText;
				}
				else{
					document.getElementById('right').innerHTML = "<p class=\"err\">Server returned error:<br> " + xmlhttp.responseText + "</p>";
				}
			}
		);
}

function autosave(){
	var form=document.forms.material;
	data=new Array();
	for(i=0; i < form.elements.length; i++) {
		data[form.elements[i].name]=form.elements[i].value;
	}
	ajax.postData("requests.php?do=autosave",data,function(xmlhttp) {
				if (xmlhttp.status == 200) {
					document.getElementById('autosavestatus').innerHTML = "autosave ok " + xmlhttp.responseText;
				}
				else{
					document.getElementById('autosavestatus').innerHTML = "autosave error: " + xmlhttp.responseText;
				}
			}
		);
}

	
