//Event.observe(window, 'load', init, false);

function random_int()
{
 return Math.floor(Math.random()*100000);
}

function replyMsg(containerId, toId)
{
  var html = 
  	"<table class=textAreaTable align=center>\n" +
    "   <tr><td align=center><textarea class=textAreaScrap id='"+containerId+"_TextArea' onkeypress=\"return handleKeyStroke(this, event);\"></textarea></td></tr>\n" +
    "   <tr><td align=center>\n" +
    "       <button class=replyButton onclick=\"javascript:postMsg('"+containerId+"_TextArea', '" + toId + "');\">Đăng lên</button> &nbsp;&nbsp; \n" +
    "       <button class=replyButton onclick=\"javascript:if($('"+containerId+"_TextArea').value.strip() != '') {if(confirm('Bạn có muốn bỏ không? Nếu bỏ bạn sẽ mất những gì đang viết dở.')) {$('"+containerId+"').innerHTML='';}} else {$('"+containerId+"').innerHTML='';} \">Hủy Bỏ</button></td></tr>\n" +
    "</table>";
  $(containerId).innerHTML = html;
}

function postMsg(elementId, toId)
{
  var msg = $(elementId).value.strip();
  new Ajax.Request('/my/scrapbook/processScrap',
    {
      method:'post',
      parameters: { cmd:'postMsg', toId:toId, msg:msg, random: random_int() },
      onSuccess: function(transport)
      {
        var response = transport.responseText || "không nhận được reponse của server";

        if(response.match('ERROR') || response.match('error') ||
           response.match("Trở ngại") ||
           response.match("cần sửa") ||
           response.match("Cần Sửa") ||
           response.match("Cần sửa"))
        {
          alert(response);
        }
        else if(response.match('RCP'))
        {
          alert("Xin đăng nhập để đăng tin nhắn");
          window.location = "/my/login?.done="+window.location;
        }
        else
        {
          //$('userInfoTable').innerHTML = response;
          alert(response);
          window.location.reload(true);
        }
      },
      onFailure: function(){ alert("Gặp khó khăn, xin bạn thử lại..."); window.location.reload(true);}
    });
}

function deleteMsg(security, msgNo)
{
  if(!confirm("Bạn muốn xoá thư tín này không?"))
  {
    return false;
  }
  new Ajax.Request('/my/scrapbook/processScrap',
    {
      method:'post',
      parameters: { cmd:'deleteMsg', msgNo:msgNo, security:security, random: random_int() },
      onSuccess: function(transport)
      {
        var response = transport.responseText || "không nhận được reponse của server";

        if(response.match('ERROR') || response.match('error') ||
           response.match("Trở ngại") ||
           response.match("cần sửa") ||
           response.match("Cần Sửa") ||
           response.match("Cần sửa"))
        {
          alert(response);
        }
        else if(response.match('RCP'))
        {
          alert("Xin đăng nhập để xóa thư tín này");
          window.location = "/my/login?.done="+window.location;
        }
        else
        {
          //$('userInfoTable').innerHTML = response;
          alert(response);
          window.location.reload(true);
        }
      },
      onFailure: function(){ alert("Gặp khó khăn, xin bạn thử lại..."); window.location.reload(true);}
    });
}

function spamMsg(security, msgNo)
{
  if(!confirm("Bạn thật muốn đánh dấu thư tín này là thư rác?"))
  {
    return false;
  }
  new Ajax.Request('/my/scrapbook/processScrap',
    {
      method:'post',
      parameters: { cmd:'spamMsg', msgNo:msgNo, security:security, random: random_int() },
      onSuccess: function(transport)
      {
        var response = transport.responseText || "không nhận được reponse của server";

        if(response.match('ERROR') || response.match('error') ||
           response.match("Trở ngại") ||
           response.match("cần sửa") ||
           response.match("Cần Sửa") ||
           response.match("Cần sửa"))
        {
          alert(response);
        }
        else if(response.match('RCP'))
        {
          alert("Xin đăng nhập để đánh dấu thư tín này là thư rác");
          window.location = "/my/login?.done="+window.location;
        }
        else
        {
          //$('userInfoTable').innerHTML = response;
          alert(response);
          window.location.reload(true);
        }
      },
      onFailure: function(){ alert("Gặp khó khăn, xin bạn thử lại..."); window.location.reload(true);}
    });
}