function postReturnContent(data, url, elementToUpdate) {
	request(data, url, function(req) {
		document.getElementById(elementToUpdate).innerHTML = req.responseText;
	});
}

function request(data, url, callback) {
	var req = new XMLHttpRequest();
	req.open("POST", url);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	function stateChange() {
		if (req.readyState == 4) {				callback(req);            };
	}
    req.onreadystatechange = stateChange;    req.send(data);	
}

function actOnKeyPress(event, keyCode, funcName) {
	if(keyPressed(event, keyCode)) {
		funcName();
		return false;
	}
	return true;
}
function keyPressed(event, keyCode) {    if(event.keyCode == keyCode) {        return true;    }    if(event.which == keyCode) {        return true;    }    return false;}

function moveBlob() {
	var now = new Date();
	var minutesInDay = now.getUTCMinutes() + (now.getUTCHours() * 60);
	var blob = document.getElementById("blob");
	var rightPos = (minutesInDay/1440) * 100;
	blob.style.right = rightPos + "%";
	setTimeout("moveBlob()", 60000);
}

function modifySelection(textArea, func) {
	if (typeof(textArea.selectionStart) != "undefined") { 		var begin = textArea.value.substr(0, textArea.selectionStart); 		var selection = textArea.value.substr(textArea.selectionStart, textArea.selectionEnd - textArea.selectionStart); 		var end = textArea.value.substr(textArea.selectionEnd); 		textArea.value = begin + func(selection) + end; 	}
}

function listFromSelection(textAreaId) {
	modifySelection(document.getElementById(textAreaId), makeList);
}

function boldSelection(textAreaId) {
	modifySelection(document.getElementById(textAreaId), makeBold);
}

function headlineSelection(textAreaId) {
	modifySelection(document.getElementById(textAreaId), makeHeadline);
}

function makeList(text) {
	if((text == '') || (text.indexOf(", ") == -1)) {
		return text;
	}
	text = wrap("<ul>\n<li>", text, "</li>\n</ul>");
	text = text.replace(/, /g, "</li>\n<li>");
	return text;
}

function makeBold(text) {
	return wrap("<b>", text, "</b>");
}

function makeHeadline(text) {
	return wrap("\n<h2>", text, "</h2>\n");
}

function wrap(start, text, end) {
	if(text == '') {
		return text;
	}
	return start + text + end;
}

function showFormatHelp() {
	alert("Bold and headline wrap selected text in said HTML tags. List turns comma separated selected text into HTML list.");
}

function toggleMenus() {
	var oSheet = document.styleSheets[1];
	if( oSheet.insertRule ) {
	  oSheet.insertRule('.menu { display: inline; }',oSheet.cssRules.length);
	} else if( oSheet.addRule ) {
	  oSheet.addRule('.menu','display: inline;');
	}
	document.getElementById('menuLink').style.display = "none";
}