<script language="JavaScript" type="text/javascript">
<!--

var display = "<?php echo (!empty($display) ? $display : 'name') ?>";

function changeDisplay()
{
    var listDisplay = document.contacts.display;

    if (listDisplay.options[listDisplay.selectedIndex].value != display) {
        display = listDisplay.options[listDisplay.selectedIndex].value;

        s = document.contacts.search_results;
        for(var i = 0; i < s.length; i++) {
            var temp = s.options[i].value;
            if (temp == "") { continue; }

            s.options[i].value = escape(s.options[i].text);
            s.options[i].text = unescape(temp);
        }

        d = document.contacts.selected_addresses;
        for(var i = 0; i < d.length; i++) {
            var temp = d.options[i].value;
            if (temp == "") { continue; }

            d.options[i].value = escape(d.options[i].text);
            d.options[i].text = unescape(temp);
        }
    }
}

function passAddresses()
{
    var list = document.contacts.selected_addresses;
    var sa = '';

    for (var i = 0; i < list.length; i++) {
        var item = list.options[i];
        if (item.value == '') {
            continue;
        }
        sa += item.value + '|' + item.text + '|';
    }
    document.contacts.sa.value = sa;
}

function addAddress()
{
    var s = document.contacts.search_results;
    var d = document.contacts.selected_addresses;

    if (s.selectedIndex < 0) {
        alert('<?php echo addslashes(_("You must select an address first.")) ?>');
        return false;
    } else {
        for (var i = 0; i < s.length; i++) {
            var item = s.options[i];
            if (item.value == "") { continue; }
            if (item.selected) {
                d.options[d.length] = new Option(item.text, item.value);
            }
        }
    }
}

function updateMessage()
{
    if (parent.opener.closed) {
        alert('<?php echo addslashes(_("The Edit Attendees screen is no longer present. Exiting.")) ?>');
        this.close();
        return;
    }

    if (!parent.opener.document.attendeesForm) {
        alert('<?php echo addslashes(_("You can only use this form from the Edit Attendees screen.")) ?>');
        this.close();
        return;
    }

    var list = document.contacts.selected_addresses;
    var field = parent.opener.document.attendeesForm.newAttendees;
    for (var i = 0; i < list.length; i++) {
        if (list.options[i].value == '') {
            continue;
        }
        if (display == 'name') {
            var address = unescape(list.options[i].value);
        } else {
            var address = list.options[i].text;
        }

        if (field.value) {
            field.value = field.value + ', ' + address;
        } else {
            field.value = address;
        }
    }
    this.close();
}

function removeAddress()
{
    var list = document.contacts.selected_addresses;
    for (var i = list.length - 1; i > 0; i--) {
        if (list.options[i].selected) {
            list.options[i] = null;
        }
    }
}

//-->
</script>
<form method="post" name="contacts" onsubmit="passAddresses();" action="contacts.php?nocache=<?php echo md5(mt_rand()) ?>">
<input type="hidden" name="sa" />
<?php Util::pformInput() ?>
<table border="0" align="center" cellpadding="0" cellspacing="1">
<tr height="25">
  <td class="header">
    <b><?php echo _("Address Book") ?></b>
  </td>
</tr>

<tr>
  <td>
<table border="0" width="100%" cellpadding="0">
<tr>
  <td class="light" align="left" nowrap="nowrap"><b><?php echo _("Find") ?></b>
    <input value="<?php echo $search ?>" name="search" style="width:150px"></td>
<?php if (count($source_list) > 1): ?>
    <td class="light" align="left" nowrap="nowrap"><b><?php echo _("from") ?></b>
    <select name="source">
    <?php foreach ($source_list as $key => $select): ?>
    <option value="<?php echo $key ?>"<?php if ($key == $source) echo ' selected="selected"' ?>><?php echo htmlspecialchars($select) ?></option>
    <?php endforeach; ?>
    </select></td>
<?php else: ?>
    <td class="light"><input name="source" type="hidden" value="<?php echo key($source_list) ?>" /></td>
<?php endif; ?>
    <td class="light" align="left" nowrap="nowrap"><input type="submit" class="button" value="<?php echo _("Search") ?>" />
  </td>
</tr>
</table>

<tr>
  <td>
<table border="0" width="100%" cellpadding="0">
<tr>
  <td width="45%"><select name="search_results" multiple="multiple" size="10" width="200" onchange="document.contacts.search_results[0].selected=false" ondblclick="addAddress()">
    <option value=""><?php echo _("* Please select address(es) *") ?></option>
    <?php foreach ($addresses as $addr): ?>
      <?php if ($display == 'email'): ?>
        <option value="<?php echo rawurlencode($addr['name']) ?>"><?php echo $addr['email'] ?></option>
      <?php else: ?>
        <option value="<?php echo rawurlencode($addr['email']) ?>"><?php echo $addr['name'] ?></option>
      <?php endif; ?>
    <?php endforeach; ?>
    </select>
  </td>
  <td width="10%" valign="top">
    <input type="button" class="button" style="width:60px" onclick="addAddress();" value="<?php echo _("Add") . " &gt;&gt;" ?>" /><br />&nbsp;<br />
  <td width="45%" colspan="2">
    <select name="selected_addresses" multiple="multiple" size="10" width="200" onchange="document.contacts.selected_addresses[0].selected=false">
      <option value=""><?php echo _("* Add these by clicking Add *") ?></option>
    <?php foreach ($selected_addresses as $value => $text): ?>
      <option value="<?php echo $value ?>"><?php echo $text ?></option>
    <?php endforeach; ?>
    </select>
  </td>
</tr>
<tr>
  <td nowrap="nowrap" align="left" class="light">
    <b><?php echo _("Display") ?></b>
    <select name="display" onchange="changeDisplay();">
      <option value="name"<?php echo $display == "name" ? " selected=\"selected\"" : "" ?>><?php echo _("Name") ?></option>
      <option value="email"<?php echo $display == "email" ? " selected=\"selected\"" : "" ?>><?php echo _("Email Address") ?></option>
    </select>
  </td>
  <td>&nbsp;</td>
  <td colspan="2">
    <input type="button" class="button" onclick="removeAddress()" value="<?php echo _("Delete") ?>" />
  </td>
</tr>
</table>
  </td>
</tr>

<tr>
  <td class="header" align="right">
    <input type="button" class="button" onclick="updateMessage();" value="<?php echo _("Add") ?>" />
    <input type="button" class="button" onclick="window.close();" value="<?php echo _("Cancel") ?>" />
    <?php echo Help::link('kronolith', 'invite-addressbook') ?>
  </td>
</tr>

</table>
