function getXmlHttp() {
	var xmlHttp = null;
	try {
		xmlHttp = new XMLHttpRequest();
	}
	catch (e) {
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	if (xmlHttp == null)
		alert("Your browser doesn't support AJAX. Please update to a browser that does, preferably Firefox or Safari.");
	return xmlHttp;
}

function preview(comment) {
	document.getElementById("c_name").style.display='none';
	document.getElementById("c_web").style.display='none';
	document.getElementById("c_test").style.display='none';
	document.getElementById("c_text").style.display='none';
	xmlHttp = getXmlHttp();
	if (xmlHttp == null)
		return;
	document.getElementById("preview").innerHTML = '<p>Loading preview...</p>';
	url = "/elements/code/user.preview.php";
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				document.getElementById("preview").innerHTML = xmlHttp.responseText;
				if (document.getElementById("error")) {
					document.getElementById("c_name").style.display='block';
					document.getElementById("c_web").style.display='block';
					document.getElementById("c_test").style.display='block';
					document.getElementById("c_text").style.display='block';
				}
				else
					document.getElementById("c_push").innerHTML = '<input type="button" value="Edit comment" onclick="edit()" /><input type="button" value="Post comment" onclick="post(c_form)" />';
			}
			else {
				document.getElementById("preview").innerHTML = '<p><b>Failed comment preview: Status ' + xmlHttp.status + '</b></p>';
				editComment();
			}
		}
	}
	xmlHttp.open("POST", url, true);
	content = comment.content.value.replace(/&/g, "#amp#");
	content = content.replace(/\+/g, "#plus#");
	params = "path=" + comment.path.value.split('/')[1] + "&name=" + comment.name.value + "&website=" + comment.website.value + "&test=" + comment.test.value + "&content=" + content;
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
}

function edit() {
	document.getElementById("c_name").style.display='block';
	document.getElementById("c_web").style.display='block';
	document.getElementById("c_test").style.display='block';
	document.getElementById("c_text").style.display='block';
	document.getElementById("c_push").innerHTML = '<input type="button" value="Preview comment" onclick="preview(c_form)" />';
}

function post(comment) {
	xmlHttp = getXmlHttp();
	if (xmlHttp == null)
		return;
	document.getElementById("c_push").innerHTML = "Posting comment...";
	url = "/elements/code/user.post.php";
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200)
				cache(comment.path.value, xmlHttp.responseText);
			else {
				document.getElementById("preview").innerHTML = '<p><b>Failed posting comment: Status ' + xmlHttp.status + '</b></p>';
				edit();
			}
		}
	}
	xmlHttp.open("POST", url, true);
	content = comment.content.value.replace(/&/g, "#amp#");
	content = content.replace(/\+/g, "#plus#");
	params = "path=" + comment.path.value.split('/')[1] + "&name=" + comment.name.value + "&website=" + comment.website.value + "&test=" + comment.test.value + "&content=" + content;
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
}

function cache(path, result) {
	xmlHttp = getXmlHttp();
	if (xmlHttp == null)
		return;
	fullpath = path;
	url = "/elements/code/comments.php?request=" + path.split('/')[1];
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200)
				refresh(fullpath, result);
			else
				cleanup(result + '<p><b>Failed comments cache & refresh: Status ' + xmlHttp.status + '</b></p>');
		}
	}
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function refresh(path, result) {
	xmlHttp = getXmlHttp();
	if (xmlHttp == null)
		return;
	url = "/elements/code/user.refresh.php?path=" + path;
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				document.getElementById("forRefresh").innerHTML = xmlHttp.responseText;
				cleanup(result);
			}
			else
				cleanup(result + '<p><b>Failed comments refresh: Status ' + xmlHttp.status + '</b></p>');
		}
	}
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function cleanup(result) {
	document.c_form.name.value="";
	document.c_form.website.value="http://";
	document.c_form.test.value="";
	document.c_form.content.value="";

	document.getElementById("c_name").style.display='block';
	document.getElementById("c_web").style.display='block';
	document.getElementById("c_test").style.display='block';
	document.getElementById("c_text").style.display='block';

	document.getElementById("c_push").innerHTML = '<input type="button" value="Preview comment" onclick="preview(c_form)" />...or view the <a href="/">homepage</a>';
	document.getElementById("preview").innerHTML = result;
}