		
function callContent(cId, opt) {
	var url = "index.php";
	anfrage.open("POST", url, true);
	anfrage.onreadystatechange =	function () {
																	requestReturnContent(opt);
																}
	anfrage.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
	anfrage.send(	"ajaxEnabled=1" +
								"&callContent=" + cId +
								"&opt=" + opt);
}

function requestReturnContent(opt) {
		
	if (anfrage.readyState == 4) {
		if (anfrage.status == 200) {
			var antwort = anfrage.responseText;
			buildNewContent(antwort, opt);
		}
		else {
			var meldung = anfrage.getResponseHeader("Status");
			if ((meldung.legth == null) || (meldung.legth <= 0)) {
				//alert ("Fehler! Anfragestatus ist " + anfrage.status + " (Meldung: " + meldung + ")");
			}
			else {
				//alert (meldung);
			}
		}
	}
	else {
		
		//alert('komisch... ' + anfrage.readystate + "\n :: " + anfrage.getResponseHeader("Status"));
	}
}

function removeOldContent (opt) {
	var corpus = document.getElementById("corpus");
	var content = document.getElementById("content");
	var contentText = document.getElementById("contentText");
	
	if (opt == 2)
		content.removeChild(contentText);
	else
		corpus.removeChild(content);
	return true;
}

function buildNewContent (antwort, opt) {
	var corpus = document.getElementById("corpus");
	var content = document.getElementById("content");
	if ( opt == 2 )
		content = document.getElementById("contentText");
	if ( removeOldContent(opt) != true)
		return false;
	var newContent = document.createElement("div");
//	var contentId = document.createAttribute("id");
//	contentId.value = "content";
	var contentId = "content";
	if ( opt == 2 )
	//	contentId.value = "contentText";
		var contentId = "contentText";
	//newContent.setAttributeNode(contentId);
	newContent.setAttribute("id", contentId);
	
	if (opt == 2) {
		var footNotes = document.getElementById('footNotes');
		var corpus = document.getElementById("content");
		var textarea = document.createElement("textarea");
		//var textareaId = document.createAttribute("id");
		//textareaId.value = "editText";
		//textarea.setAttributeNode(textareaId);
		textarea.setAttribute("id", "editText");
		
		corpus.appendChild(newContent);
		document.getElementById("content").insertBefore(textarea, footNotes);
		textarea.innerHTML = antwort;		
	}
	else {
		corpus.appendChild(newContent);
		newContent.innerHTML = antwort;
		
		makeAjaxLinks();
	}
}

// Edits
function editTitle (cId) {
//	alert ('editTitle');
	var title = document.getElementById('contentTitle');
	var button = document.getElementById('editTitleLink');
	var divCont = title.parentNode;
	if ( title.hasChildNodes() )
		var titleText = title.firstChild.nodeValue;
	else
		var titleText = "";
	
	var newInput = document.createElement("input");
//	var inputId = document.createAttribute("id");
//	inputId.value = "editTitle";
//	newInput.setAttributeNode(inputId);
	newInput.setAttribute("id", "editTitle");
	newInput.value = titleText;
	
//	var newHref = document.createAttribute("href");
	var aTitle = ('einreichen');
	button.firstChild.replaceData(0, button.firstChild.length, aTitle);
//	newHref.value = "javascript:updateTitle(" + cId + ")";
	button.setAttribute("href", "javascript:updateTitle(" + cId + ")");


	divCont.replaceChild(newInput,title);
}

function editContent (cId) {
	var button = document.getElementById('editContentLink');
//	var newHref = document.createAttribute("href");
	var aTitle = ('einreichen');
	button.firstChild.replaceData(0, button.firstChild.length, aTitle);
//	newHref.value = "javascript:updateContent(" + cId + ")";
//	button.setAttributeNode(newHref);
	button.setAttribute("href", "javascript:updateContent(" + cId + ")");
	callContent (cId, 2);
}

function updateTitle (cId) {
	var url = "index.php";
	var newContent = encodeURIComponent(document.getElementById("editTitle").value);

	anfrage.open("POST", url, true);
	anfrage.onreadystatechange = function () { 
																	if (anfrage.readyState == 4) {
																		if (anfrage.status == 200) {
																			var antwort = anfrage.responseText;
																			callContent (cId);
																		}
																		else {
																			var meldung = anfrage.getResponseHeader("Status");
																			if ((meldung.legth == null) || (meldung.legth <= 0)) {
																				//alert ("Fehler! Anfragestatus ist " + anfrage.status + " (Meldung: " + meldung + ")");
																			}
																			else {
																				//alert (meldung);
																			}
																		}
																	}
																	else {
																		
																		//alert('komisch... ' + anfrage.readystate + "\n :: " + anfrage.getResponseHeader("Status"));
																	}
																}
	anfrage.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
	anfrage.send(	"ajaxEnabled=1" +
								"&updateContentTitle=1" +
								"&cid=" + cId +
								"&newContentTitle=" + newContent);

}

function updateContent (cId) {
	var url = "index.php";
	var newContent = encodeURIComponent(document.getElementById("editText").value);

	anfrage.open("POST", url, true);
	anfrage.onreadystatechange = function () { 
																	if (anfrage.readyState == 4) {
																		if (anfrage.status == 200) {
																			var antwort = anfrage.responseText;
																			callContent (cId);
																		}
																		else {
																			var meldung = anfrage.getResponseHeader("Status");
																			if ((meldung.legth == null) || (meldung.legth <= 0)) {
																				//alert ("Fehler! Anfragestatus ist " + anfrage.status + " (Meldung: " + meldung + ")");
																			}
																			else {
																				//alert (meldung);
																			}
																		}
																	}
																	else {
																		
																		//alert('komisch... ' + anfrage.readystate + "\n :: " + anfrage.getResponseHeader("Status"));
																	}
																}
	anfrage.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
	anfrage.send(	"ajaxEnabled=1" +
								"&updateContentText=1" +
								"&cid=" + cId +
								"&newContentText=" + newContent);

}

function makeAjaxLinks() {
	var nonAjaxLink = document.getElementsByTagName('a');
	for ( var i=0; i<nonAjaxLink.length; i++) {
		var org = nonAjaxLink[i].getAttribute("href");
		if (org != null && org.match (/.+contentId=/) != null) {
			var id = org.replace (/.+contentId=/, "");
			var js = "javascript:callContent(" + id + ")";
			// FIXED: Auskommentiertes wurde durch nachfolgende Zeile ersetzt. Grund Safari konnte href.value nicht setzen.
			//var href = document.createAttribute("href");
			//href.value = js;
			//alert (href.value);
			//nonAjaxLink[i].setAttributeNode(href);
			nonAjaxLink[i].setAttribute("href", js);
		}
		else {
		}
	}
}