var oXmlHttp;

function editComment(id, story_id) {
	oXmlHttp = GetXmlHttpObject();

	if (oXmlHttp == null) {
		return false;
	}

	var url = "/comment/id/" + id;
	if (story_id) {
		url += "/story/" + story_id;
	}
	url += "/edit";

	var oComments = document.getElementById("comments");
	if (oComments.edit_id != undefined) {
		tinyMCE.execCommand('mceRemoveControl', false, 'txaComment_' + oComments.edit_id);

		var oOldComment = document.getElementById('comment_' + oComments.edit_id);
		oOldComment.innerHTML = oComments.oldComment;
		oComments.oldComment = '';
	}
	oComments.edit_id = id;

	oXmlHttp.open("GET", url, true);
	oXmlHttp.onreadystatechange = function() {//Call a function when the state changes.
		if(oXmlHttp.readyState == 4 && oXmlHttp.status == 200) {
			var oComments;
			var oEditComment;
			if (oComments = document.getElementById("comments")) {
				if (oEditComment = document.getElementById('comment_' + oComments.edit_id)) {
					oComments.oldComment = oEditComment.innerHTML;
					oEditComment.innerHTML = oXmlHttp.responseText;
					tinyMCE.execCommand('mceAddControl', false, 'txaComment_' + oComments.edit_id);
				}
			}
		}
	}
	oXmlHttp.send('');
	
}

function postComment(form) {
	tinyMCE.formSubmit(form);
	oXmlHttp = GetXmlHttpObject();

	if (oXmlHttp == null) {
		return false;
	}

	var url = "/comment";
	var params = "mode=ajax";

	if (form.elements['name'].value == '') {
		alert('Please enter your name');
		form.elements['name'].focus();
		return true;
	}

	if (form.elements['email'].value == '') {
		alert('Please enter your email');
		form.elements['email'].focus();
		return true;
	}

	if (form.elements['comment'].value == '') {
		alert('Please enter a comment');
		form.elements['comment'].focus();
		return true;
	}

	for (key in form.elements) {
		params += "&" + form.elements[key].name + "=" + escape(form.elements[key].value);
	}

	oXmlHttp.open("POST", url, true);

	//Send the proper header information along with the request
	oXmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	oXmlHttp.setRequestHeader("Content-length", params.length);
	oXmlHttp.setRequestHeader("Connection", "close");

	oXmlHttp.onreadystatechange = function() {//Call a function when the state changes.
		if(oXmlHttp.readyState == 4 && oXmlHttp.status == 200) {
			var oComments;
			var oNewComment;
			if (oComments = document.getElementById("comments")) {
				if (oComments.edit_id) {
					tinyMCE.execCommand('mceRemoveControl', false, 'txaComment_' + oComments.edit_id);

					if (oNewComment = document.getElementById('comment_' + oComments.edit_id)) {
						oNewComment.innerHTML = oComments.oldComment;
						oComments.oldComment = '';
					}
				}
				if (oComments.edit_id == 0) {
					oNewComment = document.createElement('div');
					oNewComment.setAttribute('class', 'comment');
					oComments.insertBefore(oNewComment, oComments.childNodes[2]);
				}

				oNewComment.innerHTML = oXmlHttp.responseText;

				oComments.edit_id = undefined;
			}
		}
	}
	oXmlHttp.send(params);

	return true;
}

function deleteComment(id) {
	oXmlHttp = GetXmlHttpObject();

	if (oXmlHttp == null) {
		return false;
	}

	var url = "/comment/id/" + id;
	url += "/delete";

	var oComments = document.getElementById("comments");
	oComments.delete_id = id;

	oXmlHttp.open("GET", url, true);
	oXmlHttp.onreadystatechange = function() {//Call a function when the state changes.
		if(oXmlHttp.readyState == 4 && oXmlHttp.status == 200) {
			var oComments;
			var oDeleteComment;
			if (oComments = document.getElementById("comments")) {
				if (oDeleteComment = document.getElementById('comment_' + oComments.delete_id)) {
					if (oComments.id == oDeleteComment.parentNode.id) {
						oComments.removeChild(oDeleteComment);
					}
					else {
						oComments.removeChild(oDeleteComment.parentNode);
					}
				}
			}
		}
	}
	oXmlHttp.send('');
	
}

function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}
