function getXmlHttpRequestObject() 
{	
	if (window.XMLHttpRequest) return new XMLHttpRequest(); // Not IE	 
	else if(window.ActiveXObject) return new ActiveXObject('Microsoft.XMLHTTP'); //IE	
	else write('Your browser does not support the XmlHttpRequest object.');	
}
function answer(req)
{	
	if (req.readyState != 4) return;
			
	var answer = req.responseText;
	//write(answer);		
	
	// xml
	if (answer.substr(0,1) == '<'){
		mailAvailableAnswer(req);
		return;
	}
	
	var param = answer.split(',');	
	if (!param[0] || !param[1]) return; // response incorrect
	else if (param[0] == 'editor') receiveEditor(req.responseText.split('***html***'));
	else if (param[0] == 'start') logon(param);
	else {
		var functionArray = param[1].split('_');
		if (!functionArray[1]) window[functionArray[0]](param); 	
		else window[functionArray[0]](param,functionArray[1]); // with detail
	}
}
function getData(modul,table,selectArray,whereArray,valueArray,whileBool,sortArray,sortDirection,header,orWhereArray,orValueArray)
{
	var uri = file + '/?action=getDataAjax';
	var post = 'modul=' + modul;
	post += '&table=' + table;
	if (selectArray) post += '&selectArray=' + selectArray.join('*');
	else whileBool = true;
	if (whereArray) post += '&whereArray=' + whereArray.join('*');
	if (valueArray) post += '&valueArray=' + valueArray.join('*');
	if (whileBool) post += '&while=' + whileBool;
	if (sortArray) post += '&sortArray=' + sortArray.join('*');
	if (sortDirection) post += '&sortDirection=' + sortDirection;
	if (header) post += '&header=' + header;
	if (orWhereArray) post += '&orWhereArray=' + orWhereArray.join('*');
	if (orValueArray) post += '&orValueArray=' + orValueArray.join('*');
	var req = request(uri,post);
	return req;	
}
function saveData(modul,table,insertArray,valueArray)
{
	var uri = file + '/?action=saveDataAjax';
	var post = 'modul=' + modul;
	post += '&table=' + table;
	post += '&insertArray=' + insertArray.join('*');
	post += '&valueArray=' + valueArray.join('*');
	request(uri,post);	
}
function changeData(modul,table,setArray,newArray,whereArray,valueArray)
{
	var uri = file + '/?action=changeDataAjax';
	var post = 'modul=' + modul;
	post += '&table=' + table;
	post += '&setArray=' + setArray.join('*');
	post += '&newArray=' + newArray.join('*');
	post += '&whereArray=' + whereArray.join('*');
	post += '&valueArray=' + valueArray.join('*');
	request(uri,post);	
}
function delData(modul,table,whereArray,valueArray)
{
	var uri = file + '/?action=delDataAjax';
	var post = 'modul=' + modul;
	post += '&table=' + table;
	post += '&whereArray=' + whereArray.join('*');
	post += '&valueArray=' + valueArray.join('*');
	request(uri,post);	
}
function checkData(modul,table,whereArray,valueArray)
{
	var uri = file + '/?action=checkDataAjax';
	var post = 'modul=' + modul;
	post += '&table=' + table;
	post += '&whereArray=' + whereArray.join('*');
	post += '&valueArray=' + valueArray.join('*');
	request(uri,post);	
}
function sendMail(modul,address,text,subject,mailfrom,action)
{
	if (!address) address = getObject('mailAddress').value;
	if (!text) text = getObject('mailText').innerHTML;
	if (!subject) subject = 'Weitersagen';
	
	if (action) var uri = file + '/?action='+action;
	else var uri = file + '/?action=sendMail';
	var post = 'modul=' + modul;
	post += '&address=' + address;
	post += '&text=' + encrypeText(text);
	post += '&subject=' + subject;
	if (mailfrom) post += '&mailfrom=' + mailfrom;
	request(uri,post);
}
function sendUpload(name)
{
	uri = file + '/?action=upload&name=' + name;
	request(uri);
}
function request(uri,post)
{
	if(!post) post = '';
	var index;
	
	for (var i=0; i<reqArray.length; i++) if (reqArray[i].readyState == 4 || reqArray[i].readyState == 0) index = i;	
	
	// when all objects in the array are busy create new
	if (index == null){
		reqArray.push(getXmlHttpRequestObject());
		index = reqArray.length-1;
	}
	else reqArray[index] = getXmlHttpRequestObject(); // add cause of problems in IE

	reqArray[index].onreadystatechange = function() { answer(reqArray[index]); };
	reqArray[index].open('POST',uri,true);
	reqArray[index].setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
	reqArray[index].setRequestHeader("Content-length", post.length);
	reqArray[index].setRequestHeader("Connection", "close");
	reqArray[index].send(post);
	
	return reqArray[index];
}