
/* XMLHTTP objects used for POSTing the comment data, */
/* and for updating the comments after a successful comment submission  */
var g_oRequest_Comment_Submit = Create_XMLHttpRequest_Object();
var g_oRequest_Comment_Display = Create_XMLHttpRequest_Object();



/* Create browser-specific XMLHTTP object */
function Create_XMLHttpRequest_Object()
{
	if (window.XMLHttpRequest)
	{
		/* Create XMLHttpRequest object for Firefox, Opera, Ssfari... */
		return new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		/* Create XMLHTTP object for Internet Explorer */
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
		return 0;
}




/* This function is called when a comment submission is attempted */
function Comment_Submit(fCommentForm)
{
	/* Disable the Submit button while we process the submission */
	document.getElementById('comments_submit_button').disabled = true;
	
	/* Create the string that will be POSTed to the comment-processor file, process_comment.php */
	var sPOST = fCommentForm.comments_captcha.name + "=" + encodeURIComponent(fCommentForm.comments_captcha.value) +
		"&" + fCommentForm.comments_name.name + "=" + encodeURIComponent(fCommentForm.comments_name.value) +
		"&" + fCommentForm.comments_email.name + "=" + encodeURIComponent(fCommentForm.comments_email.value) +
		"&" + fCommentForm.comments_website.name + "=" + encodeURIComponent(fCommentForm.comments_website.value) +
		"&" + fCommentForm.comments_comment.name + "=" + encodeURIComponent(fCommentForm.comments_comment.value);
	
	/* Make sure readyState is finished or not started */
	if ((g_oRequest_Comment_Submit.readyState == 4) || (g_oRequest_Comment_Submit.readyState == 0))
	{
		
		/* Set our status div to show that we're attempting to submit the comment */
		document.getElementById('comments_info').innerHTML = "<div class=\"comment_submit_outer\"><div style=\"padding: 1px;\"><div class=\"comment_submit_inner\"><img style=\"width: 64px; height: 24px; float: left; margin: 3px 12px 0 3px;\" alt=\"Progress\"src=\"/images/ani.gif\"><p style=\"margin: 8px 0 0 8px;\"><strong>Submitting your comment...</strong></p></div></div></div><br style=\"clear: both;\"><div style=\"float: left;\"class=\"separator\"></div>";
	
		/* Open asyncronous connection to the processor */
		g_oRequest_Comment_Submit.open("POST", './process_comment.php', true);
	
		/* Set the callback function for this object */
		g_oRequest_Comment_Submit.onreadystatechange = Comment_Submit_Callback; 

		/* Setup header */
		g_oRequest_Comment_Submit.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		g_oRequest_Comment_Submit.setRequestHeader("Content-length", sPOST.length);
		g_oRequest_Comment_Submit.setRequestHeader("Connection", "close");

		/* Send the POST */
		g_oRequest_Comment_Submit.send(sPOST);
	}   

}


/* This is called when the comment submission object receives a response from the processor */
function Comment_Submit_Callback()
{
	/* Check if the request was completed */
	if (g_oRequest_Comment_Submit.readyState == 4)
	{
		/* Make sure we got an "OK" response */
		if (g_oRequest_Comment_Submit.status == 200)
		{
			/* Make sure data returned is at least long enough to hold a return code */
			if(g_oRequest_Comment_Submit.responseText.length >= 3)
			{
				/* Check what type of response we got */
				if(g_oRequest_Comment_Submit.responseText.substr(0, 3) == '100')
				{
					/* We got an error code (100) */
					/* process_comments.php successfully read in the POST data, */
					/* however, something the user entered was invalid, so let's tell the user know about that */
					
					/* Set the content of the response div to the error message we got */
					/* We need to strip the return code first, though */
					
					document.getElementById('comments_info').innerHTML = "<div class=\"comment_error_outer\"><div style=\"padding: 1px;\"><div class=\"comment_error_inner\"><p style=\"margin: 5px 0 0 10px;\">" + g_oRequest_Comment_Submit.responseText.substr(3) + "</p></div></div></div><br style=\"clear: both;\"><div style=\"float: left;\"class=\"separator\"></div>";

					/* Clear the CAPTCHA field, as it is now invalid */
					document.getElementById('comments_captcha').value = '';
				}
				/* Check if we got return code 200, the Success code */
				else if(g_oRequest_Comment_Submit.responseText.substr(0, 3) == '200')
				{
					/* Show a message telling them their comment was successfully submitted */
					document.getElementById('comments_info').innerHTML = "<div class=\"comment_success_outer\"><div style=\"padding: 1px;\"><div class=\"comment_success_inner\"><p style=\"margin: 5px 0 0 10px;\">" + g_oRequest_Comment_Submit.responseText.substr(3) + "</p></div></div></div><br style=\"clear: both;\"><div style=\"float: left;\"class=\"separator\"></div>";

					/* Clear textfields */
					document.getElementById('comments_name').value = '';
					document.getElementById('comments_email').value = '';
					document.getElementById('comments_website').value = '';
					document.getElementById('comments_comment').value = '';
					document.getElementById('comments_captcha').value = '';
					
					/* Refresh the comments section on the post page to show the newly-added comment */
					Comments_Update();
				}
			}

			/* Update the CAPTCHA every time */
			
			/* Get the IMG element for the CAPTCHA */
			img = document.getElementById('imgCaptcha'); 
			
			/* Create a new CAPTCHA image and display it */
			img.src = '/captcha_image.php?' + Math.floor(Math.random()*999999999999999);
		}
	}
	
	document.getElementById('comments_submit_button').disabled = false;
	
}


function Comments_Update()
{
		/* Set up the connection to the comment updater */
		g_oRequest_Comment_Display.open("POST", './update_comments.php', true);
		
		/* Set the callback function that will handle the response */
		g_oRequest_Comment_Display.onreadystatechange = Comments_Update_Callback; 

		var sPOST = "action=" + encodeURIComponent('update');
		
		g_oRequest_Comment_Display.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		g_oRequest_Comment_Display.setRequestHeader("Content-length", sPOST.length);
		g_oRequest_Comment_Display.setRequestHeader("Connection", "close");

		/* Send the POST */
		g_oRequest_Comment_Display.send(sPOST);

}


/* This function refreshes the comments div, to display the newly-added comment, */
/* as well as any other changes that occured (other users adding comments/comments being deleted) */
function Comments_Update_Callback()
{
	if (g_oRequest_Comment_Display.readyState == 4)
	{
		if (g_oRequest_Comment_Display.status == 200)
		{
			/* Refresh the comments div to show the new comment */
			document.getElementById('comments').innerHTML = g_oRequest_Comment_Display.responseText;
		}
	}
	
}
