function generateCommentPreview_Heading(){
	var CommentNumber = "11";
	var CommentHeading = "";

	//First, take care of the name part.
	if (document.getElementById("PostComment").AuthorWebsite.value == "")
	{
		//Just fill in the name
		CommentHeading = ". "+document.getElementById("PostComment").CommentAuthor.value;
	}
	else
	{
		CommentHeading = ". <a href=\""+document.getElementById("PostComment").AuthorWebsite.value+"\">"+document.getElementById("PostComment").CommentAuthor.value+"</a>";
	}

	CommentHeading = CommentNumber+CommentHeading;

	document.getElementById("CommentPreviewHeading").innerHTML = CommentHeading;
}

function generateCommentPreview_Gravatar(){
	var http = new createRequestObject();

	//Check the Email address and see if there's a gravatar.
	if (document.getElementById("PostComment").AuthorEmail.value != "")
	{
		http.open("GET", "/getgravatar.php?e="+document.getElementById("PostComment").AuthorEmail.value);
		http.onreadystatechange = showGravatar;
		http.send(null);
	}

	function showGravatar()
	{
		if (http.readyState == 4)
		{
			document.getElementById("CommentPreviewGravatar").src = http.responseText;
		}
	}
}

function generateCommentPreview_Content(){

	//Generate the content of the comment
	var CommentContent = document.getElementById("PostComment").CommentContent.value;
	var CommentContentFinal = "";

	//Break the content into parts based on HTML tags.
	var CommentContentArr = CommentContent.split(/(<[^>]*>)/);
	var CodeTagOpen = false;
	var CodeTagClosed = false;

	//Loop through the array and do some replacin!
	for (i=0; i<CommentContentArr.length; i++)
	{
		//Check for code tags, don't allow any other tags inside code tags.
		if (CommentContentArr[i] == "<code>")
		{
			CodeTagOpen = true;
		}
		else if (CommentContentArr[i] == "</code>")
		{
			CodeTagOpen = false;
		}

		//Replace all < and > characters with their html entities
		CommentContentArr[i] = CommentContentArr[i].replace(/</g,'&lt;');
		CommentContentArr[i] = CommentContentArr[i].replace(/>/g,'&gt;');

		if (CodeTagOpen)
		{
			//If there is a code tag open, treat everything litterally. Just replace line breaks with <br /> tags.

			//Allow code tags.
			//CommentContentArr[i] = CommentContentArr[i].replace(/&lt;code&gt;(.*?)&lt;\/code&gt;/g, "<code>$1</code>");
			CommentContentArr[i] = CommentContentArr[i].replace(/\n/g, "<br />");

			if (CommentContentArr[i] != "&lt;code&gt;")
			{
				CommentContentArr[i] = CommentContentArr[i].replace(/&lt;/g, "&#60;");
				CommentContentArr[i] = CommentContentArr[i].replace(/&gt;/g, "&#62;");
			}
		}

		CommentContentFinal += CommentContentArr[i];
	}

	//Add starting/closing p tags
	CommentContentFinal = "<p>"+CommentContentFinal+"</p>";

	//Replace double line breaks with end/starting p tags
	CommentContentFinal = CommentContentFinal.replace(/\n\n/g, "</p><p>");
	//Replace remaining line breaks with br tags
	CommentContentFinal = CommentContentFinal.replace(/\n/g, "<br />");

	//Allow em tags.
	CommentContentFinal = CommentContentFinal.replace(/&lt;em&gt;(.*?)&lt;\/em&gt;/g, "<em>$1</em>");
	//Allow strong tags.
	CommentContentFinal = CommentContentFinal.replace(/&lt;strong&gt;(.*?)&lt;\/strong&gt;/g, "<strong>$1</strong>");
	//Allow del tags.
	CommentContentFinal = CommentContentFinal.replace(/&lt;del&gt;(.*?)&lt;\/del&gt;/g, "<del>$1</del>");
	//Allow properly formatted a href tags.
	CommentContentFinal = CommentContentFinal.replace(/&lt;a href=\"([a-z]+):\/\/(.*?)\"&gt;(.*?)&lt;\/a&gt;/g, "<a href=\"$1://$2\">$3</a>");
	//Allow blockquote tags.
	CommentContentFinal = CommentContentFinal.replace(/&lt;blockquote&gt;(.*?)&lt;\/blockquote&gt;/g, "<blockquote><p>$1</p></blockquote>");
	//Allow code tags.
	CommentContentFinal = CommentContentFinal.replace(/&lt;code&gt;(.*?)&lt;\/code&gt;/g, "</p><code>$1</code><p>");

	//Clean up redundant tags.
	CommentContentFinal = CommentContentFinal.replace(/<p><code>/g, "<code>");
	CommentContentFinal = CommentContentFinal.replace(/<\/code><\/p>/g, "</code>");
	CommentContentFinal = CommentContentFinal.replace(/<br><code>/g, "</p><code>");
	CommentContentFinal = CommentContentFinal.replace(/<br\/><code>/g, "</p><code>");
	CommentContentFinal = CommentContentFinal.replace(/<br \/><code>/g, "</p><code>");

	CommentContentFinal = CommentContentFinal.replace(/<code><br>/g, "<code>");
	CommentContentFinal = CommentContentFinal.replace(/<code><br\/>/g, "<code>");
	CommentContentFinal = CommentContentFinal.replace(/<code><br \/>/g, "<code>");

	CommentContentFinal = CommentContentFinal.replace(/<br><\/code>/g, "</code>");
	CommentContentFinal = CommentContentFinal.replace(/<br\/><\/code>/g, "</code>");
	CommentContentFinal = CommentContentFinal.replace(/<br \/><\/code>/g, "</code>");

	CommentContentFinal = CommentContentFinal.replace(/<br><\/p>/g, "</p>");
	CommentContentFinal = CommentContentFinal.replace(/<br\/><\/p>/g, "</p>");
	CommentContentFinal = CommentContentFinal.replace(/<br \/><\/p>/g, "</p>");
	CommentContentFinal = CommentContentFinal.replace(/<p><br>/g, "<p>");
	CommentContentFinal = CommentContentFinal.replace(/<p><br\/>/g, "<p>");
	CommentContentFinal = CommentContentFinal.replace(/<p><br \/>/g, "<p>");
	CommentContentFinal = CommentContentFinal.replace(/<p><\/p>/g, "");
	CommentContentFinal = CommentContentFinal.replace(/<p><\/p><blockquote>/g, "<blockquote>");

	//Display the formatted comment content!
	document.getElementById("CommentPreviewContent").innerHTML = CommentContentFinal;
}


document.getElementById("PostComment").CommentAuthor.onkeyup = function() {
	generateCommentPreview_Heading();
}
document.getElementById("PostComment").CommentAuthor.onblur = function() {
	generateCommentPreview_Heading();
}
document.getElementById("PostComment").AuthorWebsite.onkeyup = function() {
	generateCommentPreview_Heading();
}
document.getElementById("PostComment").AuthorWebsite.onblur = function() {
	generateCommentPreview_Heading();
}
document.getElementById("PostComment").AuthorEmail.onblur = function() {
	generateCommentPreview_Gravatar();
}
document.getElementById("PostComment").CommentContent.onkeyup = function() {
	generateCommentPreview_Content();
}
document.getElementById("PostComment").CommentContent.onblur = function() {
	generateCommentPreview_Content();
}


//GLOBAL SCRIPT STUFF
function createRequestObject(){
	var ro;

	try
	{
		ro = new XMLHttpRequest();
	}
	catch (error)
	{
		try
		{
			ro = new ActiveXObject("Microsoft.XMLHTTP"); //For Internet Explorer
		}
		catch (error)
		{
			return false;
		}
	}

	return ro;
}

function submitvote(){

	//Get the PollID
	var PollID = document.getElementById('PollForm').PollID.value;

	var PollAnswer = document.getElementById('PollForm').PollAnswer;
	var SelectedPollAnswer = 0;

	for (var x = 0; x < PollAnswer.length; x++)
	{
		if (PollAnswer[x].checked)
		{
			SelectedPollAnswer = document.getElementById('PollForm').PollAnswer[x].value;
		}
	}

	if (SelectedPollAnswer > 0)
	{
		var http = new createRequestObject();

		http.open("GET", "/answerpoll.php?PollID="+PollID+"&AnswerID="+SelectedPollAnswer+"&AJAX=true");
		http.onreadystatechange = handleVote;
		http.send(null);
	}
	else
	{
		alert('You have not selected a response.');
	}

	function handleVote(){
		
		if (http.readyState == 4)
		{
			var response = http.responseText;
			var answers = new Array();
			var outputText = '';
			var totalVotes = 0;

			if (response.indexOf('#-##-#') != -1)
			{
				answers = response.split('#-##-#');

				for (var i = 0; i < answers.length; i += 3)
				{
					outputText += '<div class="PollAnswer"><p>'+answers[i]+'</p><span class="PollAnswerValue">'+answers[i+2]+'%</span><div class="PollAnswerBar"><div class="PollAnswerColor" style="width: '+answers[i+2]+'%;"></div></div></div>';
					totalVotes += parseInt(answers[i+1]);
				}

				outputText += '<div class="PollTotalVotes"><p><strong>Total Votes: '+totalVotes+'</strong></div>';

				document.getElementById('PollContent').innerHTML = outputText;
			}
		}	
	}
}

window.onload = document.getElementById("PostComment").preview.style.display="none";