
/* - ++resource++sta.alerts.js - */
/* Misc sta.alerts related javascript code */

// adds Copy Content button to NewsItem edit form, Alerts tab
function addNewsItemAlertsCopyContentButton() {
  // check if we are on appropriate page to insert button
  if (jq('form#news_item-base-edit').length > 0) {
    var label = jq('div#archetypes-fieldname-mailbody label.formQuestion');
    if (label.length == 0) {
      return;
    }
    
    // insert a button
    jq('<input type="submit" id="copy-alerts-content" class="copyAlertsContent" value="Copy Content" name="copy_alerts_content_button" onclick="javascript:return staAlertsCopyContent();" />').insertAfter(label);
  }
}

// copies content from description and body fields into MailBody of newsitem
function staAlertsCopyContent() {
  // nothing to do if we don't have kupu instances available
  if (typeof(_kupus) == 'undefined') {
    return false;
  }
  
  // check if we got mailbody editor
  if (typeof(_kupus['kupu-editor-mailbody']) == 'undefined') {
    return false;
  }
  
  // preparation
  var value = '';
  
  // get description
  var desc = jq('textarea#description');
  if (desc.length > 0) {
    value = desc.val();
  }
  
  // get text body kupu editor field value
  if (typeof(_kupus['kupu-editor-text']) != 'undefined') {
    var main_body = _kupus['kupu-editor-text'].getHTMLBody();
    if (main_body) {
      if (value) {
        value += '<br />';
      }
    }
    value += main_body;
  }
  
  // set our mailbody if we got any value
  if (value) {
    _kupus['kupu-editor-mailbody'].setHTMLBody(value);
  }
  
  return false;
}

jq(function () {
  // add NewsItem Copy Content button to Alerts tab
  addNewsItemAlertsCopyContentButton();
});


