function reset_form()
{
  $('comment_content').value="";
  $('comment_author').value="";  	
  refresh_captcha();
}

function refresh_captcha()
{
  $('captcha_image').src='/interact/captcha.php?' + (new Date()).getTime();
  $('comment_captcha').value="";
}

function success(message)
{
  var responsebox = document.getElementById('comment_response');

  responsebox.style.backgroundColor = "#efefef";
  responsebox.style.border = "1px solid #666666";
  responsebox.innerHTML = "<center>" + message + "</center>";
  responsebox.style.visibility = 'visible';
  responsebox.style.color = "#333333";
  setTimeout('hideWrapper()',5000); 	 

}


function hideWrapper()
{ 
  var CW = document.getElementById('CommentWrapper');
  CW.style.display = 'none'; 
  reset_form();      
  var l = $('NewCommentLink'); 
  l.innerHTML='';
  l.focus();
}

function do_error(message)
{
  var responsebox = document.getElementById('comment_response'); 
  responsebox.innerHTML = "<center>" + message + "</center>";
  responsebox.style.visibility = 'visible';
  responsebox.style.color = "#FF0000";
  responsebox.style.backgroundColor = "#efefef";
  responsebox.style.border = "1px solid #666666";
}


function closeCommentForm() 
{ 
  var CW = document.getElementById('CommentWrapper'); 
  var l  = document.getElementById('NewCommentLink');
  CW.style.display = 'none';
  l.innerHTML = 'Submit Your Comment!'; 
  l.style.display = '';
}

function ShowCommentWrapper()
{
  var CW = document.getElementById('CommentWrapper');

  var l = $('NewCommentLink'); 
  CW.style.display = 'block';
  l.innerHTML = ''; 
  l.style.display = 'none';
}



function PostComment()
{
  var b = $('comment_top_box');
  b.focus();
  
  new Ajax.Request('/interact/post_comment.php',
  {
    method: 'post',
    parameters: $('comment_form').serialize(true),
    onSuccess: function(transport) {
      
        var response = transport.responseText || "no response text";
        
       response = response.strip();

       if (response=='SUCCESS')
       {
          success('Thank you for your comment, which has been submitted for moderation. The page will now reload.');
          setTimeout("location.reload(true);", 3000); 
       }
       else
       { 
        if (response=="E_CAPTCHA")
        { 
          do_error("Please Re-type the Captcha Image");
          refresh_captcha();
         }
        if (response=="E_COOKIES")
        {
          do_error("You need cookies enabled to make use of the Comments feature");
        }
        if (response=="E_ALIAS_LENGTH")
        {
          do_error("Your Alias is too short");
        }
        if (response=="E_CONTENT_LENGTH")
        {
          do_error("The comment field is empty - please enter your text");
        }
        if (response=="E_CONTENT_LENGTH")
        {
          do_error("The comment field is empty - please enter your text");
        }
        if (response=="E_INVALID_EMAIL") 
        {
          do_error("Please supply a valid e-mail address"); 
        }
        if (response=="E_TIME")
        {
          do_error("You cannot post more than one comment per article in "+$('comment_delay').value+" minutes");

          refresh_captcha();

        }

        }
      },
        onFailure: function(){ alert('Something went wrong...') }
      });
}


function LoadComments(page,art_id,showall)
{

   var responsebox = document.getElementById('comment_response');
   responsebox.innerHTML = '';
   responsebox.style.visibility = 'hidden';  

  if (showall)
  { 
	showall = 1;
  } 
 else
  { 
	showall = 0;
  } 	
  
  new Ajax.Request('/interact/get_comments.php',
  {
    method: 'post',
    parameters: { art_id: art_id,page: page, showall: showall},
    onSuccess: function(transport) {

    var response = transport.responseText || "no response text";

	$('CommentsGoHere').innerHTML = response;
    $('comment_bottom_box').style.visibility='visible'; // lazy work around for a stupid bug
   $('NewCommentLink').style.visibility='visible';

      },
        onFailure: function(){ alert('Something went wrong...') }
      });



}


function RevealComments()
{
	if ($('NewCommentLink'))
    {
        $('NewCommentLink').style.visibility='visible';
	    $('comment_bottom_box').style.visibility='visible';
    }
}
