/*
Copyright (C) 2003 Media Net Link, Inc. Danville, CA 94526 USA

This file is part of AthenaRMS.

AthenaRMS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

AthenaRMS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

function update_parent_window() {
  var f = document.f;
  var i;
  var re = new RegExp("(to|b?cc)_(.*)","i");
  var to = new Array();
  var cc = new Array();
  var bcc = new Array();
  var parent_form;
  var parent_to;
  var parent_cc;
  var parent_bcc;

  for (i = 0; i < f.elements.length; i++) {
    e = f.elements[i];

    /* Skip non-checkboxes and unchecked checkboxes. */
    if (e.type != 'checkbox' || !e.checked) continue;

    e.name.search(re);
    action = RegExp.$1;
    email = RegExp.$2;

    /* Sadly, Mac IE doesn't support Array.push()! */
    switch (action) {
      case 'to':
	to[to.length] = email;
	break;
      case 'cc':
	cc[cc.length] = email;
	break;
      case 'bcc':
	bcc[bcc.length] = email;
	break;
    }
  }

  parent_form = window.opener.document.email_form;
  parent_to = parent_form.to;
  parent_cc = parent_form.cc;
  parent_bcc = parent_form.bcc;

  if (to.length != 0) {
    if (parent_to.value.length != 0) {
      parent_to.value = parent_to.value + ',' + to.join(',');
    } else {
      parent_to.value = to.join(',');
    }
  }

  if (cc.length != 0) {
    if (parent_cc.value.length != 0) {
      parent_cc.value = parent_cc.value + ',' + cc.join(',');
    } else {
      parent_cc.value = cc.join(',');
    }
  }

  if (bcc.length != 0) {
    if (parent_bcc.value.length != 0) {
      parent_bcc.value = parent_bcc.value + ',' + bcc.join(',');
    } else {
      parent_bcc.value = bcc.join(',');
    }
  }

  window.close();
}

function check_all() {
  var f = document.f;
  var i;
  var re = new RegExp("to_","i");

  for (i = 0; i < f.elements.length; i++) {
    e = f.elements[i];

    /* Skip non-checkboxes */
    if (e.type != 'checkbox') continue;

    if (e.name.search(re) != -1) {
      e.checked = true;
    }
  }
}

function uncheck_all() {
  var f = document.f;
  var i;

  for (i = 0; i < f.elements.length; i++) {
    e = f.elements[i];

    /* Skip non-checkboxes */
    if (e.type != 'checkbox') continue;

    e.checked = false;
  }
}
