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

function processRequest(cmd)
{
  var name = $('name').value;
  var email = $('email').value;
  var hint = $('hint').value;

  if(name.strip() == '')
  {
    alert("Cần sửa: Xin điền tên người đó");
    return;
  }
  if(email.strip() == '')
  {
    alert("Cần sửa: xin điền vào điạ chỉ điện thư.");
    return;
  }
  $('statusTd').innerHTML = "<span class=colorGreen>Đang gởi lời ngỏ ý đi ...<img src='/images/sending-in-progress.gif'/></span>";
  
  new Ajax.Request('/my/breakice/processRequest',
    {
      method:'post',
      parameters: { name: name, email:email, cmd:cmd, hint:hint, 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') || response.match('Sign In') || response.match('SIGN-IN'))
        {
          if(document.cookie.indexOf('user') != -1)
          {
            alert("Xin đăng nhập để sử dụng công dụng này");
            window.location = "/my/login/?.done="+window.location;
          }
          else
          {
            alert("Xin bạn tham gia hôm nay để sử dụng công dụng này");
            window.location = "/breakice/register";
          }
        }
        else
        {
          //alert(response);
          if(cmd=='preview')
          {
            blurrBackground();
            previewTxt = response;
            setTimeout('showPreview()', 250);
            //window.location.reload(true);
            return;
          }
          else
          {
            //alert(response);
            hidePopup();
            $('statusTd').innerHTML = "<span class=colorGreen>"+response+"</span>";
          }
        }
      },
      onFailure: function(){ alert("Gặp khó khăn, xin bạn thử lại..."); window.location.reload(true);}
    });
}

var previewTxt = '';
function showPreview(response)
{
  html = '\
  <table class=popupTable width=100%> \
    <tr><td class="popupTd bottomSpc"></td></tr>\
    <tr><td align=left>'+previewTxt+'</td></tr>\
    <tr><td align=center><button class=cuteButton onclick="javascript:processRequest(\'submit\'); return false;">Gởi đi</button></td></tr>\
  </table>';

  createFormDiv('previewButton', html, 600, 0, -40, -250);
}
