var submit_button;

function setError(text) {
	alert(text);
}
	
function postComment(result) {
	var box = document.getElementById("input_text");
	var newcomment = document.createElement("div");
	var comments = document.getElementById("comments");
	newcomment.innerHTML = result;
	box.value = "";
	comments.appendChild(newcomment);
}

function restoreSubmitButton(result) {
	document.getElementById("comment-form").appendChild(submit_button);
}

function updateCommentBody(result) {
	commentid = result.substring(0,result.indexOf(':'));
	comment_body = document.getElementById("comment" + commentid + "_body");
	comment_body.innerHTML = result.substring(result.indexOf(':')+1);
}	

function deleteResults(result) {
	commentid = result.substring(0,result.indexOf(':'));
	result = result.substring(result.indexOf(':')+1);
	if (result == "") {
		comment = document.getElementById("comment" + commentid);
		comment.parentNode.removeChild(comment);
	} else {
		splat("comment" + commentid + "_body:" + result);
	}				
}	
			
function sendNewComment() {
	var poststr = "parent_type=post"
	+ "&parent_id=" + document.getElementById("post_id").value
	+ "&text=" + document.getElementById("input_text").value
	+ "&post=post";
	
	submit_button = document.getElementById("submit-button");
	submit_button.parentNode.removeChild(submit_button);
 	sendData("/ablog/postcomment.php", poststr, postComment, restoreSubmitButton);
}

function getEditForm(commentid) {
	sendData("/ablog/editcomment.php", "getform=true&commentid=" + commentid, updateCommentBody, dull);	
}

function sendEditForm(commentid) {
	var poststr = "getform=false&commentid=" + commentid
	+ "&text=" + document.getElementById("input_comment" + commentid + "_text").value;

	submit_button = document.getElementById("input_comment" + commentid + "_submit");
	submit_button.parentNode.removeChild(submit_button);
	sendData("/ablog/editcomment.php", poststr, updateCommentBody, dull);
}

function getCommentBody(commentid) {
	sendData("/ablog/editcomment.php", "getform=false&commentid=" + commentid, updateCommentBody, dull);
}

function deleteComment(commentid, mode) {
	if (mode != "soft" && mode != "hard") {
		return;
	}
	var message = "Really delete this comment?";
	if (mode == "hard") {
		message += " This comment and all its child comments will be permanently deleted!";
	}
	if (confirm(message)) {
		var poststr = "commentid=" + commentid + "&mode=" + mode;
		sendData("/ablog/deletecomment.php", poststr, deleteResults, dull);
	}
}
	
	
	
