// JavaScript Document

SendEmail = { 
  currentPopin : "",
  sendEmail : function(formObj)
  {    
    Grouply.ajaxload(SendEmail.currentPopin + '_content_res', 'popin_sendemail.inc.php', SendEmail.getFormParams(formObj), 
    function (){
        captcha = Ext.get('pc_captcha_error');
        if (captcha)
        {
          captcha.remove(); // this to prevent isntances where we have 2 captcha divs          
          Grouply.get('captchapopin').get().on('hide', function(){

            Grouply.unregister('captcha_savebutton');
            Grouply.unregister('user_secword');
            Grouply.currentPopin = SendEmail.currentPopin;
            Ext.get('captchapopin' + '_content').dom.innerHTML = "";
          });

          Grouply.popin.ajaxShow('captchapopin', 'postcomment.captcha.php', '', null,   function() {
            Grouply.renderComponent('captcha_savebutton');
            Grouply.get('captcha_savebutton').setHandler(SendEmail.postCaptcha ,captcha_savebutton_code);
            Grouply.renderComponent('user_secword');
            Grouply.get('user_secword').purgeListeners();
            Grouply.get('user_secword').on('specialkey', function(field, e) {
              if (e.getKey() == 13) {
                  SendEmail.postCaptcha();
              }
            });
          });
        } else
        {
          post_status = Ext.get('pc_status_json');
          if (post_status) {
            post_status_json = Ext.util.JSON.decode(post_status.dom.innerHTML);
            Ext.get('pc_status_json').remove();
            if (post_status_json)
            {
              Grouply.alert(post_status_json['msg'], post_status_json['title']);
                            if (post_status_json['status'] == 1) {
                                Grouply.popin.hide(SendEmail.currentPopin);
                                Ext.get(SendEmail.currentPopin + '_content').dom.innerHTML = "";
                            }
              var post_result_detail = Ext.get('post_result_detail');
              if (post_result_detail) {
                post_result_detail.remove();
              }

              return;
            }
          }
          
          error2 = Ext.get('replyerror_content');

          if (error2)
          {
            Ext.MessageBox.show({
              title : 'Cannot reply through Grouply',
              msg : error2.dom.innerHTML,
              buttons: Ext.MessageBox.OK,
              closable:true,
              progress:false,
              modal:true,
              width:300
              });
            Grouply.messageBox.fixYMapOverlap()
            error2.remove();
            return;
          }

          error = Ext.get('pc_error');
            
          if (!error){
            Grouply.popin.hide(SendEmail.currentPopin);
            Ext.get(SendEmail.currentPopin + '_content').dom.innerHTML = "";
            Grouply.alert('Your message has been sent.', 'Message Sent');
          } else
          {
            Grouply.alert('Unable to post message at this time.', 'System error');    
          }
          
          var post_result_detail = Ext.get('post_result_detail');
          if (post_result_detail) {
            post_result_detail.remove();
          }
        }
      });    
  },
  postCaptcha : function()
  {
    Grouply.ajaxloadform("captchapopin_content", Ext.get("pc_captcha_form").dom, function()
      {
        Grouply.popin.hide('captchapopin');
        
        SendEmail.submitEmailForm();
      });
  },
  clearDefaultText : function (text, el)
  {
    
    if (el.value == text)
    {
      el.value = "";
      el.style.color = "black";
    }
  },
  
  showDefaultText : function (text, el)
  {
    if (el.value == "")
    {
      el.value = text;
      el.style.color = "grey";
    }
  },
  
  openEmailPopin : function(myid, params, customTitle)
  {
    
    if (typeof(CC) != "undefined") { 
      if (!CC.usersOnly()) {
        return;
      }
    }
    
    SendEmail.currentPopin = myid;
    Grouply.currentPopin=myid;
    if (customTitle)
    {
      Grouply.popin.setTitle(myid, customTitle);
    }
    Grouply.get(Grouply.currentPopin).get().on('hide', function(){
      Grouply.unregister('sendemailbutton');
      Grouply.unregister('cancelemailbutton');
      Ext.get(myid+'_content').innerHTML = '';
    });
    
    Grouply.get(Grouply.currentPopin).get().on('show', 
            function(){ 
              Grouply.popin.fixYMapOverlap(Grouply.currentPopin); 
              
              Grouply.renderEditor(Grouply.currentPopin + "_editor");
              Grouply.renderComponent('sendemailbutton');
              Grouply.renderComponent('cancelemailbutton');
              if (el = document.getElementById(Grouply.currentPopin + '_defaultcontent_json'))
              {
                var data = Ext.util.JSON.decode(el.innerHTML);
                Grouply.editor.setValue(Grouply.currentPopin + '_editor', data.data);
              }
              else if (el = document.getElementById(Grouply.currentPopin + '_defaultcontent'))
              {
                var c = el.innerHTML;
                c = c.replace(/&lt;/g, "<");
                c = c.replace(/&gt;/g, ">");
                Grouply.editor.setValue(Grouply.currentPopin + '_editor', c);
              }
              
              Grouply.popin.resizeToContent(Grouply.currentPopin); 
             }, 
             Grouply.get(Grouply.currentPopin).get());

    params = Grouply.addParam(params, 'page', myid);    
      
    Grouply.popin.ajaxShow(myid, Grouply.sitebase + 'popin_sendemail.inc.php', params, null, null, false, document.body, 'c-c');
  },
  
  getFormParams : function(obj) {
    var poststr = "subject=" + encodeURIComponent(obj.subject.value) +
                  "&message=" + encodeURIComponent( Grouply.editor.cleanValue(Grouply.currentPopin+"_editor") ) +
                  "&target_email=" + encodeURIComponent( obj.target_email.value ) +
                  "&group_id=" + encodeURIComponent( obj.group_id.value ) +
                  "&msg_num=" + encodeURIComponent( obj.msg_num.value ) +
                  "&uid=" + encodeURIComponent( obj.uid.value ) +
                  "&page=" + encodeURIComponent( obj.page.value ) +
                  "&sendas=" + encodeURIComponent( eval(Grouply.currentPopin+"_editor"+"_code.style") ) +
                  "&email_messageid=" + encodeURIComponent( obj.email_messageid.value ) +
                  "&orig_comment_title=" + encodeURIComponent( obj.orig_comment_title.value ) +
                  "&submit=1";
    if (obj.coded_email) {
      poststr += "&coded_email=" + encodeURIComponent(obj.coded_email.value);
    }
    if (obj.copyme && obj.copyme.checked) {
      poststr += "&copyme=yes";
    }
    return poststr;
  },
  
  hidePopin : function()
  {
    Grouply.popin.hide(Grouply.currentPopin);
  },
  
  submitEmailForm : function(){
    SendEmail.sendEmail(document.getElementById('sendemailform'));
  },
  
  switchRecipient : function(el){
    if (el && el.options[el.selectedIndex].id == 'send_to_group')
    {
      document.getElementById('sendmessage_subject').disabled=true;
    } else
    {
      document.getElementById('sendmessage_subject').disabled=false;
    }
  }
};
