var g_isCommentWorking   = false;
var g_onSuccessComment    = null;
var g_commentCallbackData = null;
var gPrivate			  = false;

function onCommentShow()
{
	var message = document.getElementById("rd0");
    message.focus();
}

function showCommentPopup(actionURL, action, onSuccessComment, userUUID, boxURL, extraParams)
{
	g_onSuccessComment      = onSuccessComment;
	g_commentCallbackData   = userUUID;
	
	var url = actionURL + "?ID=" + userUUID + "&action=" + action + "&boxURL=" + boxURL + extraParams;
	
    showWindow(url, onCommentShow, "Send a message", 410, 340, "es_message_window");
}

function sendComment()
{
    if (g_isCommentWorking == true)
	  return;


	var rb = document.comment_form.radiobutton;
	
	var message = document.getElementById("rd0").value;
	
	for (var i=0; i < rb.length; i++)
    {
	  if (rb[i].checked)
	  {
		  if (rb[i].value != 0)
		    message = document.getElementById("rd" + rb[i].value).innerHTML;
	  }
    }
	
	if (message.length == 0)
	{
		alert('Can not send an empty message');
		return;
	}
	
	gPrivate = document.comment_form.chkPrivate.checked;
	
	var actionSt = "Sending message";
	
	if (gPrivate == false) 
	  actionSt = "Adding comment";
	  
	var commentStatus = document.getElementById("commentStatus");
	commentStatus.innerHTML = "<img hspace='10' align='absmiddle' src='/images/indicator.gif'/> " + actionSt + "...";

	g_isCommentWorking = true;
	
    var url = "/ProfileCommentAction.ns?ts=" + new Date().getTime() + "&action=inline_comment&ID=" + g_commentCallbackData + "&isPrivateMessage=" + gPrivate;
	
    http.open("post", url, true); 
	http.onreadystatechange = handleCommentHttpResponse;
	
	http.send("comment="+ message); 

}

 function handleCommentHttpResponse() {
  
   if (http.readyState != 4)  
     return;
	
   g_isCommentWorking = false;
	
   var response = http.responseText;
   
   var commentStatus = document.getElementById("commentStatus");
   
   if (response == 'ok')
   {
	   if (g_onSuccessComment == null)
	   {
		   var button = document.getElementById("sendMessageButton");
		   button.style.display = 'none';
		   
		   if (gPrivate)
  		     commentStatus.innerHTML = "Message successfully sent to user";	   
		   else	 
  		     commentStatus.innerHTML = "Comment successfully added to user's profile";	   
	   }
	   else
	     g_onSuccessComment(g_commentCallbackData);
   }
   else
   {
	   commentStatus.innerHTML = response;
   }
   
   showEmbeddedObjects();
 }
 
