function checkIndex() {
  return getOptSelValue(formByName('index').elements['index[]']) != '';
}

function checkMailAccount(pMsg, pIndex) {
  doHttpRequest('plain.php?umi=col_messages&page=checkmail.dyn&index=' + pIndex, 'retCheckMailAccount', pMsg, '');
}

function doConfirmActionAccounts(pAction, pMsg, pIndex) {
  loadPageConfirm(pMsg, '&page=accounts&action=' + pAction + '&index=' + pIndex);
}

function doConfirmActionDeleteMessage(pMsg, pFolder, pIndex) {
  loadPageConfirm(pMsg, '&action=delete&folder=' + pFolder + '&index[]=' + pIndex);
}

function doConfirmActionFolders(pAction, pMsg, pIndex) {
  loadPageConfirm(pMsg, '&page=folders&action=' + pAction + '&index=' + pIndex);
}

function doConfirmActionRemoveAttach(pMsg, pSubIndex) {
  if (confirm(pMsg)) {
    formByName('details').elements['subindex'].value = pSubIndex;  
    submitAndAction('details', 'removeattach');
  }
}

function doConfirmActionTemplates(pAction, pMsg, pIndex) {
  loadPageConfirm(pMsg, '&page=templates&action=' + pAction + '&index=' + pIndex);
}

function doIndexAction(pAction) {
  var S = getOptSelValue(formByName('index').elements['index[]']);
  if (S != '')
    if (pAction == 'edit')
      loadPage('&page=edit&index=' + S);
    else if (pAction == 'reply')
      loadPage('&page=edit&template=' + S);
    else if (pAction == 'forward')
      loadPage('&page=edit&template=' + S + '&type=2');
    else
      loadPage('&page=details_message&index=' + S);
}

function doIndexActionMarkRead() {
  if (checkIndex())
    submitAndAction('index', 'markread');
}

function doIndexConfirmActionDelete(pMsg) {
  if (checkIndex() && confirm(pMsg))
    submitAndAction('index', 'delete');
}

function doIndexConfirmActionAccounts(pAction, pMsg) {
  doConfirmActionAccounts(pAction, pMsg, getOptSelValue(formByName('index').elements['index']));
}

function doIndexConfirmActionFolders(pAction, pMsg) {
  doConfirmActionFolders(pAction, pMsg, getOptSelValue(formByName('index').elements['index']));
}

function doIndexConfirmActionTemplates(pAction, pMsg) {
  doConfirmActionTemplates(pAction, pMsg, getOptSelValue(formByName('index').elements['index']));
}

function doPopupActionAdb(pAdrList) {
  var List = getAllOptSelValues(formByIndex(1).elements['contacts']);
  if (List.length > 0) {
    if (!opener.closed)
      with (formByNameOpener('details').elements[pAdrList]) {
        if (value != '')
          value += ', ';
        value += List.join(', ');
      }
    window.close();
  }
}

function doPopupActionMove(pIndex, pFolder) {
  goOpener('index.php?umi=col_messages&action=move&folder=' + pFolder + '&index[]=' + pIndex);
  window.close();
}

function doPopupActionMove2(pFolder) {
  if (!opener.closed) {
    with (formByNameOpener('index')) {
      elements['action'].value = 'move';
      elements['folder'].value = pFolder;
    }
    submitFormOpener('index');
  }
  window.close();
}

function doPopupActionPublishFolder(pMsg, pIndex) {
  if (confirm(pMsg)) {
    goOpener('index.php?umi=col_messages&page=folders&action=publish&index=' + pIndex + '&owner=' + encodeURIComponent(getSelectSelValue(formByIndex(0).elements['selowner'])));
    window.close();
  }
}

function doPopupActionPublishTemplate(pMsg, pIndex) {
  if (confirm(pMsg)) {
    goOpener('index.php?umi=col_messages&page=templates&action=publish&index=' + pIndex + '&owner=' + encodeURIComponent(getSelectSelValue(formByIndex(0).elements['selowner'])));
    window.close();
  }
}

function dragEndMoveMessage(pIndex, pOwner) {
  if (_DragParam != '') {
    var List = _DragParam.split("\n");
    if (pOwner == '_private' || List[1] == '_public' || pOwner == List[1])
      loadPage('&action=move&folder=' + List[0] + '&index[]=' + pIndex);
  }
}

function dragOutMoveMessage(pObj) {
  pObj.style.cursor = 'default';
}

function dragOverMoveMessage(pObj) {
  if (_DragAction != '')
    pObj.style.cursor = 'url(bin/col/messages/img/dragmove.ico),move';
}

function dragStartMoveMessage(pIndex, pOwner, pFlag) {
  _DragAction = "dragEndMoveMessage('" + pIndex + "', '" + pOwner +  "');";
  getElemBody().style.cursor = 'url(bin/col/messages/img/dragmessage' + pFlag + '.ico),move';
}

function dragStopMoveMessage(pIndex, pOwner) {
  _DragParam = pIndex + "\n" + pOwner;
}

function expcolCcBcc() {
  var S = elementById('cc').style.display == 'none' ? 'table-row' : 'none';
  fadeElemById('cc', S);
  fadeElemById('bcc', S);
}

function expcolConvergence(pIndex) {
  if (expcolElem('convergence', 'block', 's') && getHTML('convergence_sub') == '')
    doHttpRequest('plain.php?umi=col_messages&page=convergence.dyn&index=' + pIndex, 'setHTML', 'convergence_sub', '');
}

function getDynParent(pKeyPress) {
  var Form = formByName('details').elements;
  var S = Form['dyn_parent'].value;
  if (pKeyPress) {
    Form['parent'].value = '';
    displayElemById('ok', S == '' ? 'inline' : 'none');
  }
  doHttpRequest('plain.php?umi=col_messages&page=parent.dyn', 'showDynParent', '', 'index=' + Form['index'].value + '&name=' + encodeURIComponent(S));
}

function getDynPreviewMessage(pIndex) {
  if (elementById('index_preview').style.display != 'none')
    doHttpRequest('plain.php?umi=col_messages&page=preview_message.dyn&index=' + pIndex, 'setHTML', 'index_preview', '');
}

function getDynPreviewTemplate(pIndex) {
  if (elementById('index_preview').style.display != 'none')
    doHttpRequest('plain.php?umi=col_messages&page=preview_template.dyn&index=' + pIndex, 'setHTML', 'index_preview', '');
}

function getDynTemplate(pObj) {
  var S = getSelectSelValue(pObj);
  if (S != '')
    doHttpRequest('plain.php?umi=col_messages&page=inserttemplate.dyn&index=' + S, 'setDynTemplate', '', '');
  pObj.selectedIndex = 0;
}

function goParentFolder() {
  var S = formByName('details').elements['parent'].value;
  if (S != '')
    loadPage('&page=details_folder&index=' + S);
}

function hideDynParent() {
  window.setTimeout("displayElemById('dyn_parent', 'none');", 300);
}

function popupAdb(pAdrList) {
  popupWindow('plain.php?umi=col_messages&page=adb.pop&adrlist=' + pAdrList, 320, 420);
}

function popupHistoryAccount(pIndex) {
  popupWindow('plain.php?page=history.pop&entity=col_messages_accounts&index=' + pIndex, 240, 500);
}

function popupHistoryFolder(pIndex) {
  popupWindow('plain.php?page=history.pop&entity=col_messages_folders&index=' + pIndex, 240, 500);
}

function popupHistoryMessage(pIndex) {
  popupWindow('plain.php?page=history.pop&entity=col_messages_messages&index=' + pIndex, 240, 500);
}

function popupHistoryTemplate(pIndex) {
  popupWindow('plain.php?page=history.pop&entity=col_messages_templates&index=' + pIndex, 240, 500);
}

function popupMove(pIndex) {
  popupWindow('plain.php?umi=col_messages&page=move.pop&index=' + pIndex, 280, 220);
}

function popupMove2(pFolder) {
  if (checkIndex())
    popupWindow('plain.php?umi=col_messages&page=move2.pop&folder=' + pFolder, 280, 220);
}

function popupPublishFolder(pIndex) {
  popupWindow('plain.php?umi=col_messages&page=publish_folder.pop&index=' + pIndex, 100, 220);
}

function popupPublishTemplate(pIndex) {
  popupWindow('plain.php?umi=col_messages&page=publish_template.pop&index=' + pIndex, 100, 220);
}

function retCheckMailAccount(pValue, pParam) {
  var List = pValue.split("\n");
  _CheckMailCountMsg += parseInt(List[0]);
  setHTML(List[1], 'checkmail_name');
  _CheckMailCount--;
  if (_CheckMailCount == 0) {
    window.setTimeout("displayElemById('checkmail', 'none');", 1000);
    if (_CheckMailCountMsg > 0)
      loadPageConfirm(pParam.replace(/%s/, _CheckMailCountMsg), '');
  }
}

function setDynParent(pIndex, pName) {
  var Form = formByName('details').elements;
  Form['parent'].value = pIndex;
  Form['dyn_parent'].value = pName;
  displayElemById('ok', 'inline');
}

function setDynTemplate(pValue, pParam) {
  insertText(formByName('details').elements['text'], pValue, '');
}

function showDynParent(pValue, pParam) {
  setHTML(pValue, 'dyn_parent');
  fadeElemById('dyn_parent', pValue == '' ? 'none' : 'block');
}

function updateProtocol(pObj, pMsg) {
  with (pObj) {
    if (selectedIndex == options.length - 1) {
      var S = prompt(pMsg, options[selectedIndex].value);
      if (S == null)
        selectedIndex = options.defaultSelected;
      else
        options[length - 1] = new Option(S, S, false, true);
    }
    formByName('details').elements['port'].value = options[selectedIndex].value == 'imap' ? '60' : '110';
  }
}