var send = create_XMLHttpRequest();

function unhide_box(id)
{
	var el = document.getElementById(id);
	if( el == null )
	{
		// Error with script, allow member to forward to the html version.
		return true;
	}

	el.style.display = 'visible';

	// Script processed ok, stop further action.
	return false;
}

function forum_delete_comment(url)
{
	if( confirm('Are you sure you want to delete this Comment?') )
	{
		location.href = url;
	}
}

function forum_undelete_comment(url)
{
	if( confirm('Are you sure you want to undelete this Comment?') )
	{
		location.href = url;
	}
}


function forum_comment_vote(comment_id, forum_comment_rating)
{
	// If an option has been selected, we submit the vote.
	if( parseInt(forum_comment_rating) > 0 )
	{
		if( send.readyState == 4 || send.readyState == 0 )
		{
			// Set the post parameters.
			var parameters = "comment_id=" + comment_id + "&forum_comment_vote_rating=" + forum_comment_rating;

			// Send data
			send.open('POST', "/ajax/forum_comment_vote/", true);
			send.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			send.setRequestHeader("Content-length", parameters.length);
			send.setRequestHeader("Connection", "close");

			send.onreadystatechange = forum_comment_vote_complete;
			send.send(parameters);
		}
	}
}


function forum_comment_vote_complete()
{
	if (send.readyState == 4)
	{
		// Response code was 200, OK!
		if( send.status == 200 )
		{
			// Get XML data
	 		var xmldoc = send.responseXML;
	 		var comment_id = xml_get_field(xmldoc, "comment_id");
	 		var voting_stars_html = xml_get_field(xmldoc, "voting_stars_html")
	 		var vote_score_box_html = xml_get_field(xmldoc, "vote_score_box_html")
	 		var forum_comment_rating = xml_get_field(xmldoc, "forum_comment_rating");
			var success = xml_get_field(xmldoc, "success");


			// Successfully Voted
	 		if( success.toUpperCase() == "FALSE" )
	 		{
	 			alert('There was an error saving your vote, please try again later.');
			}
			else if( success.toUpperCase() == "TRUE" )
			{
				// Update the vote stars
				var el = document.getElementById("voting_stars_" + comment_id);
				el.innerHTML = voting_stars_html;

				// update the score box
				var em = document.getElementById("voting_score_box_" + comment_id);
				em.innerHTML = vote_score_box_html;

				// Make the image larger.
				for( rating=1; rating<=5; rating++ )
				{
					var el = document.getElementById("voting_stars_" + comment_id + "_rating_" + rating);

					if( rating <= forum_comment_rating )
					{
						el.src = "/images/star_rating_green.gif";
					}
					else
					{
						el.src = "/images/star_rating_empty.gif";
					}
				}

				// Reset the defaults
				forum_default_vote_stars(comment_id);
			}

		}
	}
}




var comment_star_1 = '';
var comment_star_2 = '';
var comment_star_3 = '';
var comment_star_4 = '';
var comment_star_5 = '';

function forum_comment_vote_stars(comment_id, star_number)
{
	forum_default_vote_stars(comment_id);

	for(star=1; star<=5; star++)
	{
		if( star <= star_number )
		{
			// Set the star to gold.
			var star_object = document.getElementById('voting_stars_' + comment_id + '_rating_' + star );
			star_object.src = '/images/star_rating_gold.gif';
		}
		else
		{
			var star_object = document.getElementById('voting_stars_' + comment_id + '_rating_' + star );
			star_object.src = '/images/star_rating_empty.gif';
		}
	}
}

function forum_default_vote_stars(comment_id)
{
	// Set the defaults for on the onmouseout
	comment_star_1 = document.getElementById('voting_stars_' + comment_id + '_rating_1').src;
	comment_star_2 = document.getElementById('voting_stars_' + comment_id + '_rating_2').src;
	comment_star_3 = document.getElementById('voting_stars_' + comment_id + '_rating_3').src;
	comment_star_4 = document.getElementById('voting_stars_' + comment_id + '_rating_4').src;
	comment_star_5 = document.getElementById('voting_stars_' + comment_id + '_rating_5').src;
}

function forum_reset_vote_stars(comment_id)
{
	document.getElementById('voting_stars_' + comment_id + '_rating_1').src = comment_star_1;
	document.getElementById('voting_stars_' + comment_id + '_rating_2').src = comment_star_2;
	document.getElementById('voting_stars_' + comment_id + '_rating_3').src = comment_star_3;
	document.getElementById('voting_stars_' + comment_id + '_rating_4').src = comment_star_4;
	document.getElementById('voting_stars_' + comment_id + '_rating_5').src = comment_star_5;
}