function addComment()
{
	var myName = document.getElementById("comment_name").value;
	var myEntryId = current_entry_id;
	var myChildId = document.getElementById("comment_child_id").value;
	var myEmail = document.getElementById("comment_email").value;
	var myUrl = document.getElementById("comment_url").value;
	var myBody = document.getElementById("comment").value;
	var mySubscribeThisArticle = 0;
	var mySubscribeNewArticles = 0;
	var mySubscribeReplies = 0;

	if( myEmail == 'Your email address will not be made public' )
		myEmail = '';

	if( document.getElementById("subscribe_this_article").checked )
		mySubscribeThisArticle = 1;
	if( document.getElementById("subscribe_new_articles").checked )
		mySubscribeNewArticles = 1;
	if( document.getElementById("subscribe_comment_reply").checked )
		mySubscribeReplies = 1;

	if( myBody.length > 0 && (myBody != '') )
	{
		params = "name=" + encodeURIComponent(myName);
		params = params + "&entry_id=" + encodeURIComponent(myEntryId);
		params = params + "&child_id=" + encodeURIComponent(myChildId);
		params = params + "&email=" + encodeURIComponent(myEmail);
		params = params + "&url=" + encodeURIComponent(myUrl);
		params = params + "&body=" + encodeURIComponent(myBody);
		params = params + "&subscribe_this_article=" + mySubscribeThisArticle;
		params = params + "&subscribe_new_articles=" + mySubscribeNewArticles;
		params = params + "&subscribe_comment_reply=" + mySubscribeReplies;

		result = ajaxFunction('/ajax_add_comment.php', params, 'POST');

		if( result == "success" )
		{
			document.getElementById("comment_buttons").style.display = 'none';
			document.getElementById("comment_results").style.display = 'block';
			document.getElementById("comment_results").innerHTML = "Thank you for your comment.";
			resetComment();
		}
		else if( result == "success-new-subscribe" )
		{
			document.getElementById("comment_buttons").style.display = 'none';
			document.getElementById("comment_results").style.display = 'block';
			document.getElementById("comment_results").innerHTML = "Thank you. You will receive a confirmation email shortly to complete the subscription process.";
			resetComment();
		}
		else if( result == "success-existing-subscribe" )
		{
			document.getElementById("comment_buttons").style.display = 'none';
			document.getElementById("comment_results").style.display = 'block';
			document.getElementById("comment_results").innerHTML = "Thank you for your comment. You are already subscribed to receive notifications when a new article is posted.";
			resetComment();
		}
		else
		{
				alert(result);
		}
	}
	else
		alert('Please enter a comment before submitting');
}

function resetComment()
{
	document.getElementById("comment_name").value = '';
	document.getElementById("comment_email").value = 'Your email address will not be made public';
	document.getElementById("comment_url").value = '';
	document.getElementById("comment").value = '';
	document.getElementById("comment_character_count").innerHTML = '0';
	document.getElementById("subscribe_this_article").checked = false;
	document.getElementById("subscribe_new_articles").checked = false;
	document.getElementById("subscribe_comment_reply").checked = false;
}

function resetButtons()
{
	document.getElementById("comment_buttons").style.display = 'block';
	document.getElementById("comment_results").style.display = 'none';
}

function countCommentChars()
{
	var count = document.getElementById("comment").value.length;

	if( count > 3000 )
	{
		document.getElementById('comment').value =  document.getElementById('comment').value.substring(0, 3000);
		count = 3000;
	}

	document.getElementById("comment_character_count").innerHTML = count;
}

function replyToComment(id)
{
	document.getElementById('comment_child_id').value = id;
	document.getElementById('hdr_add_comment').innerHTML = "Replying to Comment...";
	resetComment();
	resetButtons();
	document.getElementById('comment_name').focus();
}

function swapLinThread(val)
{
	var entry_id = getEntryId();

	if( val == 'linear' )
	{
		var new_value = "Linear | <a href='javascript:;' onclick=\"swapLinThread('threaded');\">Threaded</a>";
		document.getElementById("linear_thread").innerHTML = new_value;
		swapCommentsPage(entry_id, 1);
	}
	else
	{
		var new_value = "<a href='javascript:;' onclick=\"swapLinThread('linear');\">Linear</a> | Threaded";
		document.getElementById("linear_thread").innerHTML = new_value;
		swapCommentsPage(entry_id, 0);
	}
}

function swapCommentsPage(id, linear)
{
	var initialReplyTo = false;

	// Check to see if comment_id exists in url. If so, anchor the user to that comment in the iframe
	if ( comment_anchor = window.location.href.match(/comment_id=([0-9]*)/) )
	{
		comment_append = '#comment_id_' + unescape(comment_anchor[1]);

		// This was put here because IE has an open bug where it appends the anchor tag to the title bar
		if( is_vlog == 1 )
			setTimeout("document.title = saved_doc_title;", 500);
		else
			setTimeout("document.title = 'BobParsons.me :: Archive Article';", 500);

		// If url contains reply=1, this means the user clicked a direct link to reply to a comment
		if( window.location.href.match(/reply=1/) && (id == initial_entry_id) )
			initialReplyTo = true;
	}
        else
                comment_append = '';

	// Reset the comments iframe and data
	document.getElementById("comment_entry_id").value = id;
	document.getElementById("comment_child_id").value = 0;
	document.getElementById("comment_iframe").src = "/comment.php?id=" + id + "&linear=" + linear + comment_append;
	document.getElementById('hdr_add_comment').innerHTML = "Add a Comment!";

	// If user clicked a link to reply directly to a comment, swap to say reply
	if( initialReplyTo )
		replyToComment(unescape(comment_anchor[1]));

	// Reset all the boxes and submit buttons
	resetComment();
	resetButtons();

	// Update comment count above iframe
	asyncAjaxFunction('/ajax_article.php?id=' + current_entry_id + '&method=comments', '', 'GET', changeCommentsCount);
}

function scrollToComment()
{
	if ( comment_anchor = window.location.href.match(/comment_id=(.*)/) )
	{
		swapCommentsPage(current_entry_id, 0);
	}
}

function clearComments()
{
	//document.getElementById("comment_iframe").src = "";
	window["comment_iframe"].document.body.innerHTML = '';
}

function showCommentsPreview()
{
	var new_comments = ajaxFunction('/comment.php', 'id=' + current_entry_id + '&preview_entries=1', 'GET');
	window["comment_iframe"].document.body.innerHTML = new_comments;
	asyncAjaxFunction('/ajax_article.php?id=' + current_entry_id + '&method=comments', '', 'GET', changeCommentsCount);
}

function getEntryId()
{
	return current_entry_id;
}

function changeCommentsCount(val)
{
	if( val.length > 0 )
	{
		document.getElementById('comments_count').innerHTML = val;
	}
}

// Change form action based on button press.
function OnSubmitForm(url)
{
  if ( document.getElementById("background_draft") )
    document.getElementById("background_draft").value = '';
  if(document.pressed == 'Submit Comment')
  {
    document.serendipity_comment.action = 'comments.php';
    document.serendipity_comment.target="";
  }
  else
  if(document.pressed == 'Done Editing')
  {
    // document.serendipity_comment.action=url;
    document.serendipity_comment.action = 'comments.php';
    document.serendipity_comment.target="";
  }
  else
  if(document.pressed == 'Check Spelling')
  {
    document.serendipity_comment.action ="spellcheck.php";
    window.open('spellcheck.php', 'spellcheck', 'menubar=no,width=540,scrollbars=yes');
    document.serendipity_comment.target ="spellcheck";
  }
  else
  if(document.pressed=='- Check Spelling -')
  {
    document.serendipityEntry.action ="spellcheck.php";
    window.open('spellcheck.php', 'spellcheck', 'menubar=no,width=540,scrollbars=yes');
    document.serendipityEntry.target ="spellcheck";
  }
  else
  if(document.pressed == '- Auto Draft -')
  {
    document.serendipityEntry.action=url;
    document.serendipityEntry.target="saveDraft";
    document.getElementById("background_draft").value = '2';
  }
  else
  if(document.pressed == '- Save Draft -')
  {
    document.serendipityEntry.action=url;
    document.serendipityEntry.target="saveDraft";
    document.getElementById("background_draft").value = '1';
  }
  else
  if(document.pressed == '- Save -' || document.pressed == '- Preview -')
  {
    document.serendipityEntry.action=url;
    document.serendipityEntry.target="";
  }
  return true;
}


// Hint Box
var horizontal_offset="9px" //horizontal offset of hint box from anchor link

/////No further editting needed

var vertical_offset="0" //horizontal offset of hint box from anchor link. No need to change.
var ie=document.all
var ns6=document.getElementById&&!document.all

function getposOffset(what, offsettype) {
  var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
  var parentEl=what.offsetParent;
  while (parentEl!=null){
    totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
    parentEl=parentEl.offsetParent;
  }
  return totaloffset;
}

function iecompattest(){
  return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function clearbrowseredge(obj, whichedge){
  var edgeoffset=(whichedge=="rightedge")? parseInt(horizontal_offset)*-1 : parseInt(vertical_offset)*-1
  if (whichedge=="rightedge"){
    var windowedge=ie && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-30 : window.pageXOffset+window.innerWidth-40
    dropmenuobj.contentmeasure=dropmenuobj.offsetWidth
    if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure)
      edgeoffset=dropmenuobj.contentmeasure+obj.offsetWidth+parseInt(horizontal_offset)
  }
  else{
    var windowedge=ie && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
    dropmenuobj.contentmeasure=dropmenuobj.offsetHeight
    if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure)
      edgeoffset=dropmenuobj.contentmeasure-obj.offsetHeight
  }
  return edgeoffset
}

function showhint(menucontents, obj, e, tipwidth){
  if ((ie||ns6) && document.getElementById("hintbox")){
    dropmenuobj=document.getElementById("hintbox")
    dropmenuobj.innerHTML=menucontents
    dropmenuobj.style.left=dropmenuobj.style.top=-500
    if (tipwidth!=""){
      dropmenuobj.widthobj=dropmenuobj.style
      dropmenuobj.widthobj.width=tipwidth
    }
    dropmenuobj.x=getposOffset(obj, "left")
    dropmenuobj.y=getposOffset(obj, "top")
    dropmenuobj.style.left=dropmenuobj.x-clearbrowseredge(obj, "rightedge")+obj.offsetWidth+"px"
    dropmenuobj.style.top=dropmenuobj.y-clearbrowseredge(obj, "bottomedge")+"px"
    dropmenuobj.style.visibility="visible"
    obj.onmouseout=hidetip
  }
}

function hidetip(e){
  dropmenuobj.style.visibility="hidden"
  dropmenuobj.style.left="-500px"
}

function createhintbox(){
  var divblock=document.createElement("div")
  divblock.setAttribute("id", "hintbox")
  document.body.appendChild(divblock)
}

if (window.addEventListener)
  window.addEventListener("load", createhintbox, false)
else if (window.attachEvent)
  window.attachEvent("onload", createhintbox)
else if (document.getElementById)
  window.onload=createhintbox
