
function showComments(quote_id) {
  var randNum = Math.floor(Math.random()*5);
  var url = script_name + '?do=show_comments&quote_id='+quote_id+'&rnd='+randNum;
  $('c' + quote_id).src = '/iquotes/rates/loading.gif';
  getUrl(url, true, execOnSuccess(handleShowComments));
}

function spam(comment_id,quote_id){

        var randNum = Math.floor(Math.random()*5);
	var url = script_name + '?do=report_comment&c='+comment_id+'&q='+quote_id;
        getUrl(url, true, execOnSuccess(function(req){alert((req.responseText.split('<SEP>'))[2]);}));
        displayShowCommentLink(comment_id);
	hideSpam(comment_id);
}

function handleShowComments(req) {
  
  var response_str = req.responseText;
  var responses = response_str.split('<SEP>');
  var response_code = responses[0];
  var quote_id = responses[1];
  var response_html = responses[2];
  $('quote' + quote_id).innerHTML = response_html;
  $('dt'+quote_id).className = 'quoteSelected';
  $('c' + quote_id).src = '/qimg/message_edit.png';
  $display('quote'+quote_id); 
}

function hideCommentForm(quote_id, notAll) {

      $('dt'+quote_id).className = 'quoteNormal';
      var toHide =  'quote';
      $nodisplay(toHide+quote_id); 
}

function showCommentForm(quote_id, nbr_comments) {

var str = '<div class="gray_div" style="width:450px"><h3 class="blue" onclick="$display(\'comment_form'+quote_id+'\')">Ajouter votre commentaire</h3><form name="comment_form'+quote_id+'" id="comment_form'+quote_id+'" method="POST" action="<_SCRIPT_NAME_>" style="display:none"><input type="hidden" name="quote_id" value="'+quote_id+'"><input type="hidden" name="comment_type" value="V"><table style="width:100%"><tr><td>Pseudo</td><td><input type="text" style="width:300px" name="pseudo" value="" id="pseudo'+quote_id+'"></td></tr><tr><td>Commentaire</td><td><textarea style="width:300px" name="comment" cols="30" rows="3" id="comment'+quote_id+'"></textarea></td></tr><tr><td colspan="2" style="margin-top:10px; text-align:right"><input align="right" type="button" class="resetButton" name="discard_comment_button" value="Annuler" onclick=\'hideCommentForm("'+quote_id+'","1");\'> &nbsp; &nbsp; <input class="submitButton" align="right" type="button" name="add_comment_button" value=" Envoyer " onclick="postComment(\'comment_form'+quote_id+'\');" id="comment_submit'+quote_id+'"></td></tr></table><input type="hidden" name="do" value="comment"></form></div>';

var comments = '';

if ( nbr_comments > 0 ) {

  var randNum = Math.floor(Math.random()*5);
  var url = script_name + '?do=show_comments&quote_id='+quote_id+'&rnd='+randNum;
  $('c' + quote_id).src = '/iquotes/rates/loading.gif';
  getUrl(url, true, execOnSuccess(function(req) { $('quote' + quote_id).innerHTML = '<div id="commentsDiv">'+ (req.responseText.split('<SEP>'))[2] + str+'</div>';} ));

} else {
      $('quote' + quote_id).innerHTML = str;
      $display('comment_form'+quote_id); 
      //$focus('pseudo'+quote_id); 
}

      $('dt'+quote_id).className = 'quoteSelected';
      $('c' + quote_id).src = '/qimg/message_edit.png';
      $display('quote'+quote_id); 
}

function postThreadedComment(comment_form_id) {

	var form = document.forms[comment_form_id];

	if (threadedCommentHandler(form)) {
		
	  var add_button = form.add_comment_button;
	  add_button.value = "Envoi en cours...";
	  add_button.disabled = true;
	} 
}
function threadedCommentHandler(comment_form)
{
        var pseudo = comment_form.pseudo;
        var comment = comment_form.comment;
		
        var comment_button = comment_form.comment_button;

        if (pseudo.value.length == 0 || pseudo.value == null)
        {
                alert("Vous devez saisir un pseudonyme!");
                pseudo.focus();
                return false;
        }

        if (comment.value.length == 0 || comment.value == null)
        {
                alert("Vous devez saisir un commentaire!");
                comment.focus();
                return false;
        }

        if (comment.value.length > 255)
	  {
                alert("Votre commentaire ne doit pas dépasser les 255 charact&egrave;res!");
                comment.focus();
                return false;
        }
	
	postFormByForm(comment_form, true, threadedCommentResponse);
	return true;
}

function displayShowCommentLink(comm_id){

var comment_body_div = "comment_body_"+comm_id;
var span_hide_id="hide_link_"+comm_id;
var span_show_id="show_link_"+comm_id;

$nodisplay(comment_body_div);
$hide(span_hide_id);
$visible(span_show_id);
}

function displayHideCommentLink(comm_id){

var comment_body_div="comment_body_"+comm_id;
var span_hide_id="hide_link_"+comm_id;
var span_show_id="show_link_"+comm_id;
$display(comment_body_div);
$visible(span_hide_id);
$hide(span_show_id);
}

	function hideSpam(cid) {

		if ($('reply_comment_form_id_'+cid)) {
			$('reply_comment_form_id_'+cid).style.display = 'none';
			
		}
		if ($('comment_body_'+cid)) {
			$('comment_body_'+cid).style.display = 'none';
		}
		if ($('comment_spam_bug_'+cid)) {
			$('comment_spam_bug_'+cid).style.display = 'inline';
		}
	}

function threadedCommentResponse(req) {

  var theDiv =  $('response_div');
  
  if( req.readyState == 4) { // request ready
    
    //$nodisplay('wait_div');

    if (req.status == 200) { // request succeed
      
      var response_str = req.responseText;
      var responses = response_str.split('<SEP>');
      var response_code = responses[0];
      var quote_id = responses[1];
      var response_html =  responses[2];

      if (response_code == 'OK' ) { // request okay
	     
	$('quote'+quote_id).innerHTML = 'Merci! ' + response_html;

	setTimeout(function() { 
	  $('dt'+quote_id).className = 'quoteNormal';
	  $('quote'+quote_id).innerHTML = ''}, 5000);

	 
      } else {   // request failed // there is errors 
	
	alert('Corrigez ces erreurs:\n'+ response_html);	          
	$enabled('comment_submit' + quote_id);
	$('comment_submit' + quote_id).value = 'J\'ai corrigé !';
	
      }

    } else {
      alert("Erreur Interne:\n" + req.statusText);
      $enabled('comment_submit'+quote_id);
    }
    
  } else {
    
    //$display('wait');
  }
}

function showCommentReplyForm(form_id, quote_id, is_main_comment_form) {

		var div_id = "div_" + form_id;
		var reply_id = "reply_" + form_id;
		var reply_comment_form = "comment_form" + form_id;

		if (is_main_comment_form)
			discard_visible="style='display: none'";
		else
			discard_visible="";

		var innerHTMLContent = '\
		<form name="' + reply_comment_form + '" id="' + reply_comment_form + '" onSubmit="return false" method="post" action="<_SCRIPT_NAME_>" >\
		<input type="hidden" name="do" value="comment">\
		<input type="hidden" name="comment_id" value="' + form_id + '">\
                <table style="width:95%; border:1px solid #ccc; padding:10px; margin-top:15px"><tr><td>Pseudo</td><td><input type="text" name="pseudo" value="" id="pseudo'+quote_id+'" style="width:300px"></td></tr><tr><td>Réponse</td><td>\
<input type="hidden" name="quote_id" value="' + quote_id + '">\
			<textarea   style="width:300px" name="comment" \
			cols="35" rows="3" \
			></textarea>\
			</td></tr><tr><td colspan="2" style="text-align:right">\
				<input style="margin-right:15px" class="resetButton" type="button" name="discard_comment_button"\
								value="Abandonner" ' + discard_visible + '\
								onclick="hideCommentReplyForm(\'' + form_id + '\',false);">\
								<input class="submitButton" type="button" name="add_comment_button" \
								value="Publier ma réponse" \
								onclick="postThreadedComment(\'' + reply_comment_form + '\');">\
			</td></tr></table>\
			</form>';
		
$nodisplay(reply_id);
$display(div_id);
	$(div_id).innerHTML = 	innerHTMLContent;

	}


function hideCommentReplyForm(form_id){

var div_id=$("div_"+form_id);
var reply_id=$("reply_"+form_id);

        $display(reply_id);
        $nodisplay(div_id);


}

