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

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

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

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

        s = window.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 = window.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 = window.document.contacts.selected_addresses;
    var action = "";

    for (var i = 0; i < list.length; i++) {
        var item = list.options[i];
        if (item.value == "") { continue; }
        action += '&sa' + i + '=' + escape(item.value + "|" + item.text);
    }
    if (window.document.contacts.action.indexOf("?") < 0) {
        action = window.document.contacts.action + "?" + action.substring(1,action.length);
    } else {
        action = window.document.contacts.action + "&" + action;
    }
    window.document.contacts.action = action;
}

function addAddress(f)
{
    var s = window.document.contacts.search_results;
    var d = window.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(f + ": " + item.text, f + ": " + item.value);
            }
        }
    }
}

function updateMessage()
{
    if (parent.opener.closed) {
        alert('<?php echo addslashes(_("The message being composed has been closed.  Exiting.")) ?>');
        this.close();
        return;
    }

    if (!parent.opener.document.compose) {
        alert('<?php echo addslashes(_("This window must be called from compose window")) ?>');
        this.close();
        return;
    }

    var list = window.document.contacts.selected_addresses;

    for (var i = 0; i < list.length; i++) {
        if (display == "name") {
            var address = unescape(list.options[i].value);
            var fullname = list.options[i].text;
        } else {
            var address = list.options[i].text;
            var fullname = unescape(list.options[i].value);
        }
        var pos     = address.indexOf(":");
        var f       = address.substring(0, pos);
        var field   = null;

        fullname = fullname.substring(pos + 2, fullname.length);
        fullname = fullname.replace(/\\/g, '\\\\');
        fullname = fullname.replace(/"/g, '\\"');
        if (address.substring(pos + 2, address.length).indexOf(",") > 0) {
            address = fullname + ': ' + address.substring(pos + 2, address.length) + ";";
        } else {
            address = '"' + fullname + '" <' + address.substring(pos + 2, address.length) + ">";
        }
        if (f == "<?php echo _("To") ?>") {
            field = parent.opener.document.compose.to;
        } else if (f == "<?php echo _("Cc") ?>") {
            field = parent.opener.document.compose.cc;
        } else if (f == "<?php echo _("Bcc") ?>") {
            field = parent.opener.document.compose.bcc;
        } else {
            continue;
        }

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

function saClear() {
    window.document.contacts.selected_addresses.length = 1;
}

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

//-->
</script>
