function update_select(form) {
  if ((form.action.value == "move") || (form.action.value == "copy")) {
    form.dstfolder.style.display = "";
  }
  else {
    form.dstfolder.style.display = "none";
  }
}

function select_all(form, name) {
  var length = form.elements.length;
  var tocheck = form.selall.checked;
  for (var i = 0; i < length; i++) {
    if (form.elements[i].name.indexOf(name) != -1)
      form.elements[i].checked = tocheck;
  }
}

function attach_on(i,b) {
  var row;
  var but;
  var msie = navigator.appVersion.indexOf("MSIE")  >= 0;
  if (document.getElementById) {
    row = document.getElementById(i);
    but = document.getElementById(b);
    but.style.display = 'none';
    if (msie) row.style.display = 'inline';
    else row.style.display = 'table-row';
  } else if (msie) {
    document.all.item(i).style.display = 'inline';
    document.all.item(b).style.display = 'none';
  }
}

function show_hide(id) {
  if (document.all) {
    img = eval('img_' + id);
	 imgf = eval('imgf_' + id);
	 ext = img.src.substr(img.src.length - 6, 6);
	 subfolder = eval('sub_' + id);
	 if (subfolder.style.display == 'block') {
	   subfolder.style.display =  'none';
	   img.src  = 'gfx/plus' + ext;
	   imgf.src = 'gfx/folder.gif';
    }
    else {
      subfolder.style.display =  'block';
      img.src  = 'gfx/minus' + ext;
      imgf.src = 'gfx/folder_o.gif';
	 }
  }
}

function add_address(o, n) {
  var w=window.open("abookseladdr", "Contact_search", "width=700px, height=450px, resizable=1, scrollbars=1");
  w.focus();
}
var TO_INS = 1;
var TO_DEL = 2;
var CC_INS = 3;
var CC_DEL = 4;
var BC_INS = 5;
var BC_DEL = 6;


function IsEmpty(s)
{
  return s.replace(/ /g, "").length == 0;
}

function jsSelContactFolder(w,sel)
{
var src;
if (w.document.all)
  o = w.ifr.document;
else
  o = w.document.getElementById("ifr").contentDocument;
o.location.href='abookaddrlist?folder='+sel.value;
}

function jsMoveAddr(w, d, cur)
{
  sel = w.document.seladdr.to;
  if (d == TO_INS || d == CC_INS || d == BC_INS) {
    if (w.document.all)
      src = w.ifr.document;
    else
      src = w.document.getElementById("ifr").contentDocument;
    var cbx;
    if (cur)
      cbx = new Array(cur);
    else {
      if (navigator.appVersion.indexOf("MSIE 4")!=-1)
        cbx = src.all.tags("INPUT");
      else
        cbx = src.getElementsByTagName("input");
    }
    for (var i=0; i<cbx.length; i++) {
      if (cbx[i].name == 'selall') {
        cbx[i].checked = false;
        continue;
;      }
      if (cbx[i].name.substr(0,3) == 'chx' && cbx[i].checked) {
        if (cbx[i].disabled == false) {
          var s = cbx[i].getAttribute("addr");
          if (d == TO_INS)
            s = 'To:  ' + s;
          else if (d == CC_INS)
            s = 'Cc:  ' + s
          else if (d == BC_INS)
            s = 'Bcc: ' + s;
          var o = new Option(s,s);
          sel.options[sel.length] = o;
        }
        cbx[i].checked = false;
      }
    }
  }
  else {
    for (var i=sel.length-1; i>=0; i--) {
      if (sel.options[i].selected)
        sel.options[i] = null;
    }
  }
}

function jsSetAddrToMail(w)
{
  if (w.opener == null) return;
  var f = w.opener.document.forms[1];
  if (f == null)
    f = w.opener.document.forms[0];
  if (f == null || f.to == null || f.cc == null || f.bcc == null) return;
  var d = w.document.seladdr;
  var sel = d.to;
  f.to.value = '';
  f.cc.value = '';
  f.bcc.value = '';
  var inp;
  for (var i=0; i<sel.length; i++) {
    var s = sel.options[i].value;
    var type = s.substr(0,5);
    s = s.substr(5);
    if (type == 'To:  ') inp = f.to;
    else if (type == 'Cc:  ') inp = f.cc;
    else inp = f.bcc;
    var t = inp.value;
    if (t.indexOf(s) == -1) {
      if (t.length > 0) t = t + ', ';
      inp.value = t + s;
    }
  }
  window.close();
}

function jsSetAddrToCombo(w)
{
  if (w.opener == null) return;
  if (navigator.userAgent.indexOf('Netscape6')!=-1)
    w.document.getElementById('ifr').style.height='360px';
  var f = w.opener.document.forms[1];
  if (f == null)
    f = w.opener.document.forms[0];
  if (f == null || f.to == null || f.cc == null || f.bcc == null) return;
  var inp = new Array(f.to, f.cc, f.bcc);
  var prefix = new Array('To:  ', 'Cc:  ', 'Bcc: ');
  var d = w.document.seladdr;
  var sel = d.to;
  for (var i=0; i<3; i++) {
    if (IsEmpty(inp[i].value)) continue;
    var in_string = false;
    var is_escape = false;
    var s = inp[i].value;
    var val = '';
    for (var j=0; j<s.length; j++) {
      var c = s.charAt(j);
      switch (c) {
        case '\\':
          val += c;
          is_escape = !is_escape;
          break;
        case '"':
          val += c;
          if (!is_escape)
            in_string = !in_string;
          break;
        case ',':
          if (in_string)
            val += c;
          else {
            val = prefix[i] + val;
            var o = new Option(val, val);
            sel.options[sel.length] = o;
            val = '';
          }
          break;
        case ' ':
          if (!IsEmpty(val))
            val += c;
          break;
        default: val += c;
      }
    }
    if (!IsEmpty(val)) {
      val = prefix[i] + val;
      var o = new Option(val, val);
      sel.options[sel.length] = o;
    }
  }
}

function jsMoveCurrAddr(w, id)
{
  var chx = eval('w.document.forms[0].chx' + id);
  if (chx.disabled) return;
  chx.checked=true;
  jsMoveAddr(w.parent, TO_INS, chx);
}

function jsCheckCurrAddr(w, id)
{
  var chx = eval('w.document.forms[0].chx' + id);
  if (chx.disabled) return;
  chx.checked = !chx.checked;
}

function jsGoToUrl(w,s)
{
  w.open(s.value, "ContactWeb", "menubar=1,toolbar=1,location=1,resizable=1,status=1");
}

function jsCheckContactName(w, msg)
{
  var f = w.document.frm;
  var s = f.givenName.value + f.surName.value + f.commonName.value + f.nickName.value;
  if (IsEmpty(s)) {
    alert(msg);
    return false;
  }
  
  if (IsEmpty(f.commonName.value)) {
    if (IsEmpty(f.givenName.value) && IsEmpty(f.surName.value))
      f.commonName.value = f.nickName.value;
    else
      f.commonName.value = f.givenName.value + " " + f.surName.value;
    f.commonName.value = f.commonName.value.replace(/^ */,"");
  }
  
  return true;
}

function jsFolderMap(w, alias, fld, act)
{
  var url = "edit_map?alias=" + alias + "&folder=" + fld + "&action=" + act;
  w.open(url, "FolderMap", "width=440px, height=200px, menubar=0,toolbar=0,location=0,resizable=1,status=0");
}

function jsLocateContact(name, addr)
{
  window.open("abookedit?locate=1&name="+name+"&addr="+addr, "Contact", "width=600px, height=570px, resizable=yes, scrollbars=yes");
}

function jsLocateContact(name, addr)
{
  window.open("abookedit?locate=1&name="+name+"&addr="+addr, "Contact", "width=600px, height=570px, resizable=yes, scrollbars=yes");
}

function jsEnaInput(o)
{
  var name = o.name.substr(4);
  var f = o.form;
  eval("f." + name + ".disabled=!o.checked");
}

