function promptForComment(pid, parent_cid)
{
	var prompt = document.getElementById('commentPrompt');
	var author = document.getElementById('commentPrompt_author')
	var email = document.getElementById('commentPrompt_email')
	var webpage = document.getElementById('commentPrompt_webpage')
	var subject = document.getElementById('commentPrompt_subject')
	var comment = document.getElementById('commentPrompt_comment')
	var submit = document.getElementById('commentPrompt_submit')
	var code = document.getElementById('commentPrompt_code')
	
	author.value = email.value = webpage.value = subject.value = comment.value = '';
	submit.onclick = function () {
		if (author.value.match(/^\s*$/))
		{
			alert ("Name field is required.")
			return
		}
		else if (comment.value.match(/^\s*$/))
		{
			alert ("Comment field is required.")
			return
		}
		
		var callback = function(ret) {
			if (ret)
			{
				updateComments(pid)
				hideCommentPrompt()
			}
			else
				alert("Comment could not be posted.")
		} 
		postComment(pid, parent_cid, author.value, email.value, webpage.value, subject.value, comment.value, code.value, callback);
	}
	
	prompt.style.display = 'block';
}
function hideCommentPrompt()
{
	var prompt = document.getElementById('commentPrompt');
	prompt.style.display = 'none';
}

function postComment(pid, parent_cid, author, email, webpage, subject, comment, code, callback)
{
	vars 	= 'pid=' + escape(pid) + '&' +
			  'parent_cid=' + escape(parent_cid) + '&' +
			  'author=' + escape(author) + '&' +
			  'email=' + escape(email) + '&' +
			  'webpage=' + escape(webpage) + '&' +
			  'subject=' + escape(subject) + '&' +
			  'comment=' + escape(comment) + '&' +
			  'code=' + escape(code);
	
	var commentDiv = document.getElementById('comments_'+pid);
	if (commentDiv)
	{
		var f = function(response) {
			callback(response.match(/1/))
		};
		ajax('/ajax/postcomment.php', vars, f);
	}
}
function showComments(pid)
{
	var commentDiv = document.getElementById('comments_'+pid)
	if (commentDiv) {
		if (commentDiv.innerHTML.length == 0)
			updateComments(pid)
		else
			commentDiv.innerHTML = ''
	}
}
function updateComments(pid)
{
	var commentDiv = document.getElementById('comments_'+pid)
	if (commentDiv)
		ajax('/ajax/comments.php', 'pid='+pid, function (commentsHTML) { commentDiv.innerHTML = commentsHTML })
}
function updatePostPreview(pid) {
	var postPreview = document.getElementById('postPreview');
	if (postPreview)
		ajax('/ajax/blogposts.php', 'pid='+pid, function (postHTML) { postPreview.innerHTML = postHTML; })
}
function updatePostPreviewCustom(content, topic, music) {
	var postPreview = document.getElementById('postPreview');
	if (postPreview)
		ajax('/ajax/blogposts.php', 'content='+escape(content)+'&topic='+escape(topic)+'&music='+escape(music), function (postHTML) { postPreview.innerHTML = postHTML; })
}
function ajax(url, vars, callbackFunction)
{ 
	var request = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
	
	request.open("POST", url, true);
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	
	request.onreadystatechange = function(){
		if (request.readyState == 4 && request.status == 200) {
			if (request.responseText){	
				callbackFunction(request.responseText);
			}
		}
	};
	
	request.send(vars);
}	
