function replace_div_content(divID,setText){
	document.getElementById(divID).innerHTML = setText;
}

function changeImage(id, new_src)
{
	document.getElementById(id).src = new_src;
}

function email(user, domain)
{
	window.location = "mailto:" + user.replace(/ dot /, ".") + "@" + domain.replace(/ dot /, ".");
}

function toggle_expander(identifier)
{
	if(document.getElementById(identifier).style.display == "none")
	{
		document.getElementById(identifier).style.display = "block";
	}
	else
	{
		document.getElementById(identifier).style.display = "none";
	}
}

function close_expander(identifier)
{
	document.getElementById(identifier).style.display = "none";
}

function open_expander(identifier)
{
	document.getElementById(identifier).style.display = "block";
}

function set_anchor_text(anchor, text)
{
	anchor.firstChild.data = text;
}

function get_display_by_identifier(identifier)
{
	return document.getElementById(identifier).style.display;
}

var request;

function post_entry()
{
	var quit = false;
	if(document.getElementById("name").value == "")
	{
		document.getElementById("name").style.border = "solid 1px red";
		quit = true;
	}
	else
	{
		document.getElementById("name").style.border = "solid 1px white";
	}
	if(document.getElementById("text").value == "")
	{
		document.getElementById("text").style.border = "solid 1px red";
		quit = true;
	}
	else
	{
		document.getElementById("text").style.border = "solid 1px white";
	}
	if(quit == true)
	{
		return;
	}
	if(window.XMLHttpRequest)
	{
		request = new XMLHttpRequest();
		request.onreadystatechange = request_state_changed;
		request.open("POST", "functions/post_entry.php");
		request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		request.send("language=" + escape(document.getElementById("language").value) + "&name=" + escape(document.getElementById("name").value) + "&email=" + escape(document.getElementById("email").value) + "&text=" + escape(document.getElementById("text").value));
	}
	else
	{
		request = new ActiveXObject("Microsoft.XMLHTTP");
		request.onreadystatechange = request_state_changed;
		request.open("POST", "functions/post_entry.php", true);
		request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		request.send("language=" + escape(document.getElementById("language").value) + "&name=" + escape(document.getElementById("name").value) + "&email=" + escape(document.getElementById("email").value) + "&text=" + escape(document.getElementById("text").value));
	}
}

function request_state_changed()
{
	if(request.readyState == 4)
	{
		if(request.status == 200)
		{
			if(request.responseXML.getElementsByTagName("invalid").length == 0)
			{
				window.location.reload();
				// not executed because it does not work yet
				close_expander("add_entry");
				document.getElementById("name").value = "";
				document.getElementById("email").value = "";
				document.getElementById("text").value = "";
				entryElement = document.createElement("div");
				entryElement.setAttribute("class", "entry");
				titleElement = document.createElement("div");
				titleElement.setAttribute("class", "entry-title");
				nameElement = document.createElement("span");
				nameElement.setAttribute("style", "float: left;");
				nameElement.appendChild(document.createTextNode(request.responseXML.getElementsByTagName("name")[0].firstChild.data));
				datetimeElement = document.createElement("span");
				datetimeElement.setAttribute("class", "entry-datetime");
				datetimeElement.appendChild(document.createTextNode(request.responseXML.getElementsByTagName("datetime")[0].firstChild.data));
				titleElement.appendChild(nameElement);
				titleElement.appendChild(datetimeElement);
				textElement = document.createElement("div");
				textElement.setAttribute("class", "entry-text");
				text = request.responseXML.getElementsByTagName("text")[0].firstChild.data;
				text = text.replace(/\\\"/g, "\"");
				text = text.replace(/\\'/g, "'");
				textParts = text.split("\n");
				for(var i = 0; i < textParts.length; ++i)
				{
					textElement.appendChild(document.createTextNode(textParts[i]));
					textElement.appendChild(document.createElement("br"));
				}
				entryElement.appendChild(titleElement);
				entryElement.appendChild(textElement);
				document.getElementById("entries").insertBefore(entryElement, document.getElementById("entries").firstChild);
			}
			// document.getElementById("log").appendChild(document.createTextNode(request.responseText + " / " + request.responseXML.getElementsByTagName("invalid").length));
			// document.getElementById("log").appendChild(document.createElement("br"));
		}
		else
		{
			alert("Error retrieving the server reply!");
		}
	}
}

