//
// DoComments.js
// (c) 2008 Johnnie Rose, Jr. (johnnie@jerrata.com)
// http://johnnie.jerrata.com/software.php?product=DoComments
//
// Javascript file to ensure a smooth user experience by animating
// the interface and providing instant feedback.
//
// Questions? Comments? Feel free to contact me at the email address
// above.

//==================================================================
//==================================================================
//==================================================================
// WEBSITE-SPECIFIC VARIABLES
// Change the following settings to customize DoComments to your
// website.

// Set DOCOMMENTS_LOCATION to the place where you put DoComments.php
// and DoComments.js, ensuring that there is a slash at the end.
var DOCOMMENTS_LOCATION = "http://johnnie.jerrata.com/bin/";

//==================================================================
//==================================================================
//==================================================================
// PROGRAM CODE BEGINS HERE

function isAnonymousPost () {
	if ( document.DoCommentsForm.is_anonymous[0].checked )
		return document.DoCommentsForm.is_anonymous[0].value;
	else
		return document.DoCommentsForm.is_anonymous[1].value;
}

function reflectChangeInAnonymity () {
	if ( isAnonymousPost () == "yes" ) {
		document.DoCommentsForm.name.value = "Anonymous";
		document.DoCommentsForm.email.value = "";
		document.DoCommentsForm.web.value = "";
		document.DoCommentsForm.name.disabled = true;
		document.DoCommentsForm.email.disabled = true;
		document.DoCommentsForm.web.disabled = true;
	} else {
		document.DoCommentsForm.name.value = "";
		document.DoCommentsForm.name.disabled = false;
		document.DoCommentsForm.email.disabled = false;
		document.DoCommentsForm.web.disabled = false;
		document.DoCommentsForm.name.focus();
	}
}

function affectThePostDiv ( currentHeight, increment, cbFuncName ) {
	currentHeight += increment;
	if ( currentHeight > 0 && currentHeight < 350 ) {
		document.getElementById ( "thePostDiv" ).style.height = currentHeight;
		setTimeout ( "affectThePostDiv ( " + currentHeight + ", " + increment + ", " + cbFuncName + " )", 15 );
	} else {
		if ( cbFuncName != null )
			cbFuncName ();
	}
}

function revealThePostDiv () {
	document.getElementById ( "thePostDiv" ).style.display = "block";
	affectThePostDiv ( 0, 5, null );
}

function hideThePostDiv () {
	affectThePostDiv ( 350, -5, "hideThePostDivCallback" );
}

function hideThePostDivCallback () {
	document.getElementById ( "thePostDiv" ).style.display = "none";
}

function disableForm () {
	document.DoCommentsForm.is_anonymous[0].disabled = true;
	document.DoCommentsForm.is_anonymous[1].disabled = true;
	document.DoCommentsForm.name.disabled = true;
	document.DoCommentsForm.email.disabled = true;
	document.DoCommentsForm.web.disabled = true;
	document.DoCommentsForm.rating.disabled = true;
	document.DoCommentsForm.comment_text.disabled = true;
	document.DoCommentsForm.cancel.disabled = true;
	document.DoCommentsForm.submitStatus.disabled = true;
}

function recomputeNumberCharactersRemaining () {

	var numCharactersRemaining = 500 - document.DoCommentsForm.comment_text.value.length;

	// stop accepting input if user's gone over the limit
	if ( document.DoCommentsForm.comment_text.value.length > 500 )
		document.DoCommentsForm.comment_text.value = document.DoCommentsForm.comment_text.value.substring ( 0 , 500 );

	if ( numCharactersRemaining < 0 )
		numCharactersRemaining = 0;

	document.getElementById ( "commentCharacterCount" ).innerHTML = "<strong>My comment</strong>: <small>(" + numCharactersRemaining + " characters remaining; no HTML, please.)</small>";
}

function validateForm () {

	if ( document.DoCommentsForm.is_anonymous[1].checked ) {

		// did user enter name?
		if ( document.DoCommentsForm.name.value.length == 0 ) {
			window.alert ( "Please enter your name." );
			return false;
		}

		// did user enter valid email address?
		if ( document.DoCommentsForm.email.value.length > 0 ) {

			if ( document.DoCommentsForm.email.value.indexOf ( "@" ) == -1 ) {
				window.alert ( "Please enter your valid email address." );
				return false;
			}
		}

		// did user enter valid web address?
		if ( document.DoCommentsForm.web.value.length > 0 ) {

			if ( document.DoCommentsForm.web.value.indexOf ( "http" ) == -1 ) {
				window.alert ( "Please enter your full web address with an http:// or https:// prefix." );
				return false;
			}
		}
	}
	if ( document.DoCommentsForm.comment_text.value.length == 0 ) {
		window.alert ( "Please enter your comment." );
		return false;
	}
	if ( document.DoCommentsForm.comment_text.value.length > 500 ) {
		window.alert ( "Your comment has exceeded the 500 character limit!" );
		return false;
	}
	return true;
}

function createHTMLForNewPost ( timestamp ) {

	var html = "<div><font style=\"font-family:Georgia;font-size:32px;font-weight:bold;line-height:24px;\">You just wrote...</font><div style=\"border:1px dashed #000000;background-color:#ffffff;width:800px;opacity:0.75;filter:alpha(opacity='75');\"><div style=\"padding:15px;\"><em>&quot;" + document.DoCommentsForm.comment_text.value + "&quot;</em> ";

	if ( document.DoCommentsForm.rating.value != 0 )
		html += "and rated this at <strong>" + document.DoCommentsForm.rating.value + " / 5</strong>.";

	html += "</div></div><div style=\"width:800px;text-align:right;padding-bottom:2px;\"><font style=\"font-family:Helvetica;font-size:0.7em;color:#d6cc9d;\">Posted on " + timestamp;

	// connecting phrase one
	if ( document.DoCommentsForm.email.value.length > 0 || document.DoCommentsForm.web.value.length > 0 )
		html += "; ";

	// contact link
	if ( document.DoCommentsForm.email.value.length > 0 )
		html += "<a href=\"mailto:" + document.DoCommentsForm.email.value + "\">contact author</a>";

	// connecting phrase two
	if ( document.DoCommentsForm.email.value.length > 0 && document.DoCommentsForm.web.value.length > 0 )
		html += ", or ";

	// visit link
	if ( document.DoCommentsForm.web.value.length > 0 )
		html += "<a href=\"" + document.DoCommentsForm.web.value + "\">visit author</a>";

	html += ".</font></div></div><p>";

	return html;
}

// thanks to: http://developer.apple.com/internet/webcontent/XMLHttpRequestExample/example.html
// thanks to: http://www.devx.com/DevX/Tip/17500
function postMessageViaXMLHTTPRequest () {

	// branch based on browser
	if ( window.XMLHttpRequest )
		req = new XMLHttpRequest ();
	else
		req = new ActiveXObject ( "Microsoft.XMLHTTP" );

        req.onreadystatechange = handleXMLHTTPRequestResult;
	// accesses global var doCommentsTopic
        req.open("POST", DOCOMMENTS_LOCATION + "DoComments.php?topic=" + doCommentsTopic + "&action=post", true);
	req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        req.send("is_anonymous="+isAnonymousPost ()+"&name="+document.DoCommentsForm.name.value+"&email="+document.DoCommentsForm.email.value+"&web="+document.DoCommentsForm.web.value+"&rating="+document.DoCommentsForm.rating.value+"&comment_text="+document.DoCommentsForm.comment_text.value);

	disableForm ();
	document.DoCommentsForm.submitStatus.value = "Sending Comment...";
}

// thanks to: http://developer.apple.com/internet/webcontent/XMLHttpRequestExample/example.html
function handleXMLHTTPRequestResult () {

    // only if req shows "loaded"
    if (req.readyState == 4) {

        // only if "OK"
        if (req.status == 200) {

		// server-side response is...
		if ( req.responseText.substring ( 0, 2 ) == "ok" ) {
			document.DoCommentsForm.submitStatus.value = "Comment Posted!";
			document.getElementById ( "theReadDiv" ).innerHTML = createHTMLForNewPost ( req.responseText.substring ( 2 ) ) + document.getElementById ( "theReadDiv" ).innerHTML;
			setTimeout ( "hideThePostDiv ()", 3000 );
		} else {
			document.DoCommentsForm.submitStatus.value = "Error: " + req.responseText.substring ( 2 );
			window.alert ( "An error occurred while posting your comment: " + req.responseText.substring ( 2 ) );
		}
         } else {
		document.DoCommentsForm.submitStatus.value = "Error: " + req.statusText;
		window.alert("An error occurred while posting your comment:\n" + req.statusText);
         }
    }
}

// did user find comment helpful or not helpful?
function postCommentHelpfulVote ( comment_id, opinion )
{
	// branch based on browser
	if ( window.XMLHttpRequest )
		req = new XMLHttpRequest ();
	else
		req = new ActiveXObject ( "Microsoft.XMLHTTP" );

        req.onreadystatechange = handleCommentHelpfulVoteResult;

        req.open("POST", DOCOMMENTS_LOCATION + "DoComments.php?topic=none&action=recordHelpfulVote", true);
	req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        req.send("comment_id="+comment_id+"&is_helpful="+opinion);
}

function registerCommentHelpful ( comment_id )
{
	postCommentHelpfulVote ( comment_id, "helpful" );
}

function registerCommentNotHelpful ( comment_id )
{
	postCommentHelpfulVote ( comment_id, "not_helpful" );
}

function handleCommentHelpfulVoteResult ()
{
    // only if req shows "loaded"
    if (req.readyState == 4)
    {
        // only if "OK"
        if (req.status == 200)
	{
		// server-side response is...
		if ( req.responseText.substring ( 0, 2 ) == "ok" )
		{
			window.alert ( "Thanks, your vote was recorded!" );
		}
		else
		{
			document.DoCommentsForm.submitStatus.value = "Error: " + req.responseText.substring ( 2 );
			window.alert ( "An error occurred while recording your vote: " + req.responseText.substring ( 2 ) );
		}
	}
	else
	{
		document.DoCommentsForm.submitStatus.value = "Error: " + req.statusText;
		window.alert("An error occurred while recording your vote:\n" + req.statusText);
	}
    }
}
