/*
 * Copyright 1999 Netscape Communications Corp, a subsidiary of America Online
 */

var PAB_GRP_ALL = 1, PAB_GRP_UNFILED = 2, helpid = '';
var PAB_ONLY_GRP = 3
var globalArray = new Array()


function setFrames() {
  toolFrame = window.frames[0]
  listFrame = window.frames[1]
}

function setUp() {
  if (main.isRefreshing())
    return;
  // If the pabFrame isn't setup correctly, fix that!
  if (!main.pabSrtIdx) {
    main.state = 'pabinit';
    main.fetchPab();
  }
}

function getToolbar() {
  return '<table border="0" cellpadding="0" cellspacing="0">\n' +
    '<TR><TD colspan=42><IMG src="imx/spacer.gif" width="1" height="8"></TD></TR>'+
    '<tr align="center">\n' +
    '<td width="10">&nbsp;</td>\n' +  '<td nowrap>' + main.font(1) + 
    main.folderSelectionBreadCrumbs('folderList','main.selectMbox(options[selectedIndex].value);selectedIndex=0',
        i18n['select folder'],false,'','',main.cfgFrame.sub,'') + '</td>\n' +

    '<td><img src="imx/spacer.gif" width="6" height="1"></td>\n' +
    '<td rowspan="3" bgcolor="#999999"><img src="imx/spacer.gif" width="1" height="1">' +
    '<td><img src="imx/spacer.gif" width="6" height="1"></td>\n' +

    main.WMtoolbar(
      i18n_pab['update'], 'main.state=\'pabinit\';main.fetchPab()', 'imx/pab-pull.gif" alt="' + i18n['pab update'], 27, 25, true,
      null,null,'imx/spacer.gif',3,1,false,
      i18n['compose'], 'parent.composeFromAddr()', 'imx/compose.gif" alt="' + i18n['msg compose'], 24, 24, true,
      null,null,'imx/spacer.gif',3,1,false,
      i18n_pab["new entry"], 'parent.addEntry()', 'imx/pab-new_entry.gif" alt="' +  i18n['pab new'], 24, 24, true,
      null,null,'imx/spacer.gif',3,1,false,
      i18n_pab["new group"], 'parent.addGroup()', 'imx/pab-new_group.gif" alt="' + i18n['pab group'], 24, 24, true,
      null,null,'imx/spacer.gif',3,1,false)+
    
    '<td><img src="imx/spacer.gif" width="1" height="1"></td>\n' +
    '<td rowspan="3" bgcolor="#999999"><img src="imx/spacer.gif" width="1" height="1">' +
    '<td><img src="imx/spacer.gif" width="6" height="1"></td>\n' +

    '<td align=left nowrap>' + main.font(1) + nbsp +
    emitGroupList() + '</td>\n' +
    
    '<td><img src="imx/spacer.gif" width="6" height="1"></td>\n' +
    '<td rowspan="3" bgcolor="#999999"><img src="imx/spacer.gif" width="1" height="1">' +
    '<td><img src="imx/spacer.gif" width="3" height="1"></td>\n' +
   
    main.WMtoolbar(
      null,null,'imx/spacer.gif',3,1,false,
      i18n['search'], 'parent.search()', 'imx/pab_search.gif" alt="' + i18n['pab search'], 24, 24, true) +
    (IE > 0 ? '</tr><TR><TD colspan=42><IMG src="imx/spacer.gif" width="1" height="8"></TD></TR></TABLE>' : '</tr></table>')


}

function toolFrameHTML() {
  if (main.isRefreshing())
    return '';
  return main.toolFrame();
}

function getSortHeaderbak(str, srt, img, w, h) {
  var s = '';

  if (img == null & srt == main.pabSrtBy)
    s = '<table border=0 cellpadding=0 cellspacing=0 width=100%>\n<tr><td nowrap>';
  s += nbsp + '<a id="black" href="javascript:main.sortPab(' + 
    srt + ')" onMouseOver="return hint(\'' + main.i18n_squote(str) +
    '\')" onMouseOut="return hint()">';
  if (img)
    s += '<image src="' + img + '" width=' + w + ' height=' + h + ' border=0>';
  else
    s += main.font(null, '000000') + html(str) + '</font>';
  s += '</a>';
  if (img == null & srt == main.pabSrtBy) {
    s += '</td>\n<td align=right><img src="' +
      (main.pabSrtAsc ? "imx/sort_dn.gif" : "imx/sort_up.gif") +
      '" border=0 width=8 height=7></td></tr>\n</table>\n';
  }
  return s;
}

function getSortHeader(str, sort_str, img, w, h)
{
  var s = ''
  s += main.font(null, '000000') + html(str) + '</font>'
  if(sort_str == main.pabSrtBy) {
   s += '&nbsp;<a id="black" href="javascript:main.sortPab(\'' + 
    sort_str + '\')" onMouseOver="return hint(\'' + main.i18n_squote(str) +
    '\')" onMouseOut="return hint()"><img src="' +
     (main.pabSrtAsc ? "imx/sort_dn.gif" : "imx/sort_up.gif") + 
      '" border=0 width=16 height=16 alt="'
   switch(sort_str)
   {
      case main.BY_NAME : s += main.pabSrtAsc ? i18n['name dn'] : i18n['name up'] ; break;
      case main.BY_MAIL : s += main.pabSrtAsc ? i18n['mail dn'] : i18n['mail up'] ; break;
   }
   s += '"></a>'
  }
  else {
   s += '&nbsp;<a id="black" href="javascript:main.sortPab(\'' + 
    sort_str + '\')" onMouseOver="return hint(\'' + main.i18n_squote(str) +
    '\')" onMouseOut="return hint()"><img src="imx/notselected.gif" border=0 width=16 height=16 alt="'
    
   switch(sort_str)
   {
      case main.BY_NAME : s += i18n['name up'] ; break;
      case main.BY_MAIL : s += i18n['mail up'] ; break;
   }
   s += '"></a>'
  }
  return s
}


function listFrameHTML() {
  if (main.isRefreshing() || !main.pabSrtIdx)
    return '';
  var addrs
  var mycount
  var i, a = curViewIdx()/* main.pabSrtIdx */, f = main.pabFrame, u = f.people, g = f.groups, step = parseInt(main.cfg.meViewSize);
  var ugs = '', s = main.tableStart + '<FORM name=form><TR bgcolor=' + main.chrome2 + '>';
  var mark, tmp;

  globalArray = new Array()

   //reinitialize the head of the page if the number of entries < display size (preference)
  if( a.length < step )
     main.pabPageHead = 0;

  if (main.lastModUn) {
    for (i = 0; i < a.length; i++) {
      tmp = a[i].t == main.ENTRY ? u : g;
      if (tmp[a[i].i].un == main.lastModUn && (i + 1 > parseInt(main.cfg.meViewSize) || i < main.pabPageHead)) {
        main.pabPageHead = i % parseInt(main.cfg.meViewSize);
        main.pabPageHead = i - main.pabPageHead;
      }
    }
  }
  // column headers
//s += '<TD width=2% valign=bottom><A href="javascript:main.sortPab(' + main.BY_TYPE + ')"><IMG src="' + main.srcHREF + 'imx/pab-edit.gif" width=16 height=16 border=0></A></TD>' +
    s += '<td width=2% align=center>' + main.font() + i18n_pab['to'] + '</font></td>\n' +
    '<td width=2% align=center>' + main.font() + i18n_pab['cc'] + '</font></td>\n' +
    '<td width=2% align=center>' + main.font() + i18n_pab['bcc'] + '</font></td>\n' +
    '<TD>' + getSortHeader(i18n_pab['name'],main.BY_NAME) + '</TD>' +
    '<TD width=1% nowrap>'+ getSortHeader(i18n_pab['mail'],main.BY_MAIL) + '</TD>' +
    '<TD width=1% nowrap><NOBR>' + main.font() + i18n_pab['work'] + '</FONT></NOBR></TD>' +
    '<TD width=1% nowrap><NOBR>' + main.font() + i18n_pab['home'] + '</FONT></NOBR></TD></TR>';
   
  // pabPageHead and pabPageTail are the beginning and end indexes for a page.
  // Make pabPageTail equal pabPageHead and then increment it until we've
  // reached either the end of this page (step == 0) or pabPageTail is greater
  // than the total number of entries.
  main.pabPageTail = main.pabPageHead;
  // step begins the while loop equal to main.cfg.meViewSize, which represents
  // the number of entries one would like to see per page. It is then decre-
  // mented until it reaches zero. If it reaches zero, then we know that we are
  // looking at a full page of entries.

  addrs = 0

  while (main.pabPageTail != a.length && step > 0) {
    // since pabPageTail starts the loop equal to pabPageHead, then we can use
    // pabPageTail as an index into the current view. 
    // a[main.pabPageTail]    is the PabSortObj for the sort order and page
    // a[main.pabPageTail].t  is the type (entry or group)
    // a[main.pabPageTail].i  is the index into the list of entries or groups
    // 
    // if a[main.pabPageTail].t == main.ENTRY, then:
    //   u[a[main.pabPageTail].i] is the current entry object
    //
    // if a[main.pabPageTail].t == main.GROUP, then:
    //   g[a[main.pabPageTail].i] is the current group object

    // Make sure there is a un mark that we can pass to modEntry.
    // This mark will substitute for lastModUn if a modified entry
    // is deleted.
    if (main.pabPageTail + 1 < a.length) {
      if (a[main.pabPageTail + 1].t == main.ENTRY)
        mark = u[a[main.pabPageTail + 1].i].un;
      else
        mark = g[a[main.pabPageTail + 1].i].un
    } else if (main.pabPageTail - 1 > 0) {
      if (a[main.pabPageTail - 1].t == main.ENTRY)
        mark = u[a[main.pabPageTail - 1].i].un;
      else
        mark = g[a[main.pabPageTail - 1].i].un;
    } else
      mark = 0;
    // Entries
    if (a[main.pabPageTail].t == main.ENTRY) {
    mycount = main.pabPageTail
      ugs += '<TR' + main.base_line + ' ' + main.cellBgString + '>'
  	if(u[a[main.pabPageTail].i].mail && u[a[main.pabPageTail].i].mail[0]) {
  		ugs += '<td width=2% align=center>' +  '<input type=checkbox name=t value="' +
  		mycount + '" onClick="parent.radioType(this,' + addrs + ')"></td>\n';
  		ugs += '<td width=2% align=center>' +  '<input type=checkbox name=c value="' +
  		mycount + '" onClick="parent.radioType(this,' + addrs + ')"></td>\n';
  		ugs += '<td width=2% align=center>' + '<input type=checkbox name=b value="' +
  		mycount + '" onClick="parent.radioType(this,' + addrs + ')"></td>\n';
	 
		globalArray[globalArray.length] = u[a[main.pabPageTail].i]
		addrs ++
  	}
	else ugs += '<td width=2% align=center>&nbsp;</td><td width=2% align=center>&nbsp;</td><td width=2% align=center>&nbsp;</td>'
        // Entry/Group Icon
       	ugs += '<TD width=20% valign=bottom nowrap><NOBR><A href="javascript:parent.modEntry(\'' + u[a[main.pabPageTail].i].un + '\',\'' + mark + '\')"><IMG src="' + main.srcHREF + 'imx/pab-entry.gif" width=16 height=16 border=0 alt="' + i18n_pab['edit entry'] + '"></A></NOBR>' + '&nbsp;' +
       // Display Name
        '<A href="javascript:parent.modEntry(\'' + u[a[main.pabPageTail].i].un + '\',\'' + mark + '\')">' + main.font() + main.unescape_crlf(u[a[main.pabPageTail].i].cn) + '</FONT></A></TD>' +
        // Email
        '<TD width=20% nowrap><NOBR>' + main.font() + 
         (u[a[main.pabPageTail].i].mail.length ? 
          s_EmailEntry(u[a[main.pabPageTail].i].cn, 
                         u[a[main.pabPageTail].i].mail.join('; ')) : 
          nbsp) + '</FONT></NOBR></TD>' +
        // Work Phone
        '<TD width=20% nowrap><NOBR>' + main.font() + (u[a[main.pabPageTail].i].telephonenumber ? main.unescape_crlf(u[a[main.pabPageTail].i].telephonenumber) : nbsp) + '</FONT></NOBR></TD>' +
        // Home Phone
        '<TD width=20% nowrap><NOBR>' + main.font() + (u[a[main.pabPageTail].i].homephone ? main.unescape_crlf(u[a[main.pabPageTail].i].homephone) : nbsp) + '</FONT></NOBR></TD></TR>';
      if (step == parseInt(main.cfg.meViewSize))
        main.lastModUn = u[a[main.pabPageTail].i].un;
    // Groups
    } else {
	 mycount = main.pabPageTail
     ugs += '<TR' + main.base_line + ' ' + main.cellBgString + '>' 
	 
  	ugs += '<td width=2% align=center>' +  '<input type=checkbox name=t value="' +
  		mycount + '" onClick="parent.radioType(this,' + addrs + ')"></td>\n';
  	ugs += '<td width=2% align=center>' +  '<input type=checkbox name=c value="' +
  		mycount + '" onClick="parent.radioType(this,' + addrs + ')"></td>\n';
  	ugs += '<td width=2% align=center>' +  '<input type=checkbox name=b value="' +
  		mycount + '" onClick="parent.radioType(this,' + addrs + ')"></td>\n';

	globalArray[globalArray.length] = g[a[main.pabPageTail].i]

	addrs ++
  	 
	  ugs += '<TD width=20% valign=bottom nowrap><NOBR><A href="javascript:parent.modGroup(\'' + g[a[main.pabPageTail].i].un + '\',\'' + mark + '\')"><IMG src="' + main.srcHREF + 'imx/pab-group.gif" width=16 height=16 border=0 alt="'+ i18n_pab['edit group'] + '"></A></NOBR>' + '&nbsp;' +
      main.font() + '<A href="javascript:parent.modGroup(\'' + g[a[main.pabPageTail].i].un + '\',\'' + mark + '\')">' + main.unescape_crlf(g[a[main.pabPageTail].i].cn) + '</A>' + '</FONT></TD>' +
      '<TD width=20% valign=bottom nowrap><NOBR>' + main.font() + s_EmailGroup(main.unescape_crlf(g[a[main.pabPageTail].i].cn), g[a[main.pabPageTail].i].un) + '</FONT></NOBR></TD>'+
      '<TD width=20%  valign=bottom nowrap><NOBR>' + main.font() + nbsp + '</FONT></NOBR></TD>'+
      '<TD width=20% valign=bottom nowrap><NOBR>' + main.font() + nbsp + '</FONT></NOBR></TD></TR>'; 
  	if (step == parseInt(main.cfg.meViewSize))
        main.lastModUn = g[a[main.pabPageTail].i].un;
    }
    // Adjust pabPageTail and step to ensure that we stay within the bounds of
    // the current group view and page.
    main.pabPageTail++;
    step--;

  }
  if (!ugs)
    s += '<TR' + main.base_line + ' ' + main.cellBgString + '>' +
    '<TD align=center colspan=7>' + main.font() + i18n_pab['not found'] +
    '</TD></TR>';
  s += ugs + '<TR bgcolor=' + main.chrome2 + '><TD colspan=7>' + main.font() + nbsp +
    '</FONT></TD></TR>' + main.tableEnd + emitPageCtrl(a.length) + '</FORM>';
  return main.getBody(main.white, true) + s;
}

/* curViewIdx -----------------------------------------------------------------
 * main.pabSrtIdx is a unified sorted index into the groups and people arrays
 * returned by PAB_CMD_EDIT. Each group view, however, is a different window
 * onto this composite. curViewIdx returns an array subset of main.pabSrtIdx,
 * which indexes the current view only.
 */
function curViewIdx() {
  // The ALL view does not need to be filtered
  if (main.curPabGrp == 1)
    return main.pabSrtIdx;

  var i, a = main.pabSrtIdx, v = new Array(), u = main.pabFrame.people, g = main.pabFrame.groups;
  for (i = 0; i < a.length; i++) {
    // Only include those entries that belong in the current PAB group
    // If the current group un is 2, assume UNFILED
    // Otherwise, see if the un is part the current entry's group list.
    if (a[i].t == main.ENTRY && main.curPabGrp != 3 &&
      ((main.curPabGrp == 2 && !u[a[i].i].memberofpabgroup) ||
        ((isNaN(main.curPabGrp)? true : main.curPabGrp > 2) && indexOfGroup(u[a[i].i], main.curPabGrp) > -1)))
      v[v.length] = a[i];
    if(a[i].t == main.GROUP && main.curPabGrp == PAB_ONLY_GRP)
      v[v.length] = a[i];
  }
  return v;
}

function listFrameInit() {
  listFrame.focus();
}

function radioType(fe, i) {

  return ; // do nothing now.


  var t = listFrame.form.elements['t'], c = listFrame.form.elements['c'], b = listFrame.form.elements['b'];
  if (!isArray(t)) {
    t = new Array(t);
    c = new Array(c);
    b = new Array(b);
  }
  if (fe.checked) {
    if (fe.name == 't') {
      c[i].checked = false;
      b[i].checked = false;
    } else if (fe.name == 'c') {
      t[i].checked = false;
      b[i].checked = false;
    } else {
      t[i].checked = false;
      c[i].checked = false;
    }
  }
}

function emitGroupList() {
  var g = main.pabFrame.groups;
  // Set up the SELECT object, including the onChange handler.
  // After a change: If the selected option does not have a value, loop through
  // the OPTIONS array until we've found the current group (main.curPabGrp).
  // Otherwise, call selGroup().
  var s = '<SELECT name=groupSel onChange="if(!this.options[this.selectedIndex].value){for (var i=0;i<this.options.length;i++){if(this.options[i].value==main.curPabGrp)this.options[i].selected=true;}}else{parent.selGroup(this.options[this.selectedIndex].value);}">';

  s += '<OPTION>' + i18n_pab['view logo'] + '</option><option value="' + PAB_GRP_ALL + '"' + (main.curPabGrp == PAB_GRP_ALL ? ' selected' : '') + '>' + i18n['list indent'] + i18n_pab['all group'] + '</OPTION>';
  s += '<option value="' + PAB_ONLY_GRP + '"' + (main.curPabGrp == PAB_ONLY_GRP ? ' selected' : '') + '>' + i18n['list indent'] + i18n_pab['only groups'] + '</OPTION>';

  if (g) {
    for (var i = 0; i < g.length; i++) {
      s += '<OPTION value="' + g[i].un + '"' + (main.curPabGrp == g[i].un ? ' selected' : '') + '>' + i18n['list indent'] + main.unescape_crlf(g[i].cn) + '</OPTION>';
    }
  }
  return s + '<OPTION value="' + PAB_GRP_UNFILED + '"' + (main.curPabGrp == PAB_GRP_UNFILED ? ' selected' : '') + '>' + i18n['list indent'] + i18n_pab['unfiled group'] + '</OPTION></SELECT>';
}

function indexOfGroup(e, n) {
  if (!e.memberofpabgroup)
    return -1;
  for (var i = 0; i < e.memberofpabgroup.length; i++) {
    if (e.memberofpabgroup[i] == n)
      return i;
  }
  return -1;
}

function selGroup(g) {
  // If we are currently viewing the ALL or UNFILED group and we switch to
  // a user defined group, or vice versa, reflow the toolFrame.
  var tool = ((isNaN(g)? true : g > 2) && (isNaN(main.curPabGrp)? false : main.curPabGrp < 3)) || ((isNaN(g)? false : g < 3) && (isNaN(main.curPabGrp)? true : main.curPabGrp > 2));
  // Make sure we track the current group un
  if(main.curPabGrp == 3 || g == 3) tool = true
  main.curPabGrp = g;
  // Reset main.pabPageHead so we see the first page of the newly selected
  // group view.
  main.pabPageHead = main.pabPageTail = main.lastModUn = 0;
  if (tool)
    reload(toolFrame);
  reload(listFrame);
}

function refresh() {
  reload(toolFrame);
  reload(listFrame);
}

function afterSort() {
  reload(listFrame);
}


function composeFromAddr()
{
  var tempData;
  var mailData
  var data = new Array()
  data = globalArray

  /* i ... iterator
   * j ... iterator
   * a ... an array of checkboxes
   * s ... the string of emails to be added to the message's envelope
   * t ... the type of address (t - to, c - cc, b - bcc)
   */
  var i, j, a, t, s = '';

  if(globalArray.length == 0)
  {
     main.compose('new',s);
     return
  }
    
  // We don't want to write this code three times over so iterate,
  // making t the appropriate type each pass.
  for (i = 0; i < 3; i++) {
    t = (i == 0 ? 'b' : (i == 1 ? 'c' : 't'));
    s += (s ? '&' : '') + t + '=';
    a = listFrame.form.elements[t];
    if (!isArray(a)) {
      a = new Array(a);
    }
    for (j = 0; j < a.length; j++) {
      if (a[j].checked) {
        if (data[j].objectclass == 'pabperson') {
			mailData = (isArray(data[j].mail) ? data[j].mail[0] : split(data[j].mail, ',')[0]);
			s += (s.charAt(s.length-1) == '=' ? '' : ',') + s_EmailEntry(data[j].cn,
				mailData, true)
		}
        else
          s += (s.charAt(s.length-1) == '=' ? '' : ',') + s_EmailGroup(data[j].cn, data[j].un,true)
        a[j].checked = false;
   }
  }
 }
  main.compose('new', s);
}

