/*

To make this work with the defaults, you need:

(1) A div on your page with an ID of "preview_text."  Don't put anything in the DIV -- its contents will be replaced.

(2) An ID on your "Preview" button of "preview_button".

(3) An onclick event on your preview button of "GetCommentPreview();"

(4) A valid entry ID.  It doesn't matter which one, just pick one and change the line of code below:

*/
var entryID = 4106;
/*

Here are some config variables, in case you don't want the defaults. */

var previewDivID = "preview_text";  /* The ID of the DIV where the preview should go. */
var previewButtonID = "preview_button"; /* The ID of the "Preview" button. */
var buttonWorkingText = "Working...";  /* What the button text should change to while the comment preview is being retrieved. */
var buttonRefreshText = "Refresh Preview";  /* What the button text should read after the first preview is retrieved. */
var commentScriptURL = "/cgi-bin/mt/mt-comments.cgi";  /* The URL of your comment preview script. */
var commentTextDivID = "text"; /* The ID of the TEXTAREA where users are entering their comment text. */

/* Make an HTTP object. */
var http = createRequestObject(); 

/* This sends the current contents of the text box to the comment preview script. That script returns whatever I want loaded into the comment preview DIV (control this view the Comment Previewing templates in Movable Type.) */
function GetCommentPreview()
{
	/* Change the value of the button. */
	document.getElementById(previewButtonID).value = buttonWorkingText;

	/* This is the preview script address. */
	http.open('post', commentScriptURL);
	
	/* Define a function to call once a response has been received. */
	http.onreadystatechange = RenderCommentPreview; 

	/*
	Send the data.	
	-- Remember to escape anything you send (thanks Dave for finding this bug...)
	-- Note that with Movable Type, it's the presence of a field called "preview" (the preview button itself) that determines whether or not to post the comment or preview it.  Just make sure you have "preview=[something]" in there.
	*/
	http.send("preview=preview&static=1&entry_id=" + entryID + "&text=" + escape(document.getElementById(commentTextDivID).value));
}


/* This is called when the HTTP object returns the request. */
function RenderCommentPreview()
{
	/* Replace the text of the preview DIV. */
	document.getElementById(previewDivID).innerHTML = http.responseText;

	/* Change the button text (it may be changed already...) */
	document.getElementById(previewButtonID).value = buttonRefreshText;
}

/* I totally stole this code from somewhere... */
function createRequestObject()
{
	var request_o; //declare the variable to hold the object.
	var browser = navigator.appName; //find the browser name
	if(browser == "Microsoft Internet Explorer")
	{
		/* Create the object using MSIE's method */
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		/* Create the object using other browser's method */
		request_o = new XMLHttpRequest();
	}
	return request_o; //return the object
}