<%@ page buffer="8kb" autoFlush="true" %>
<%@ page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="zm" uri="com.zimbra.zm" %>
<%@ taglib prefix="app" uri="com.zimbra.htmlclient" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fmt" uri="com.zimbra.i18n" %>
<app:handleError>
    <zm:getMailbox var="mailbox"/>
    <c:choose>
    <c:when test="${not empty mailbox.prefs.locale}">
        <fmt:setLocale value='${mailbox.prefs.locale}' scope='request' />
    </c:when>
    <c:otherwise>
        <fmt:setLocale value='${pageContext.request.locale}' scope='request' />
    </c:otherwise>
    </c:choose>
    <fmt:setBundle basename="/messages/ZhMsg" scope="request"/>
    
    <fmt:message var="title" key="addFromContacts"/>

    <zm:composeUploader var="uploader"/>
    <c:set var="compose" value="${uploader.compose}"/>
    <c:choose>
                <c:when test="${mailbox.features.contactsDetailedSearch}">
                    <c:set var="nameQuery" value="${uploader.contactSearchNameQuery}"/>
                    <c:set var="phoneticQuery" value="${uploader.contactSearchPhoneticQuery}"/>
                    <c:set var="emailQuery" value="${uploader.contactSearchEmailQuery}"/>
                    <c:set var="deptQuery" value="${uploader.contactSearchDeptQuery}"/>
                    <c:set var="query" value="${nameQuery}"/>
                </c:when>
                <c:otherwise>
                    <c:set var="query" value="${uploader.contactSearchQuery}"/>
                </c:otherwise>
    </c:choose>
    <c:choose>
        <c:when test="${uploader.isContactSearch}">
            <c:choose>
                <c:when test="${uploader.contactLocation eq 'gal'}">
                    <zm:searchGal var="searchGal" query="${query}" dept="${deptQuery}" email="${emailQuery}" type="account"/>
                </c:when>
                <c:otherwise>
                    <c:if test="${not empty phoneticQuery}">
                        <c:set var="phoneticQueryString" value="#phoneticFirstName:${phoneticQuery} OR #phoneticLastName:${phoneticQuery}"/>
                        <c:choose>
                            <c:when test="${not empty query}">
                                <c:set var="query" value="(${query} OR ${phoneticQueryString})"/>
                            </c:when>
                            <c:otherwise>
                                <c:set var="query" value="(${phoneticQueryString})"/>
                            </c:otherwise>
                        </c:choose>
                    </c:if>
                    <c:if test="${not empty emailQuery}">
                        <c:set var="query" value="${query} to:${emailQuery}*"/>
                    </c:if>
                    <zm:search var="search" query="${query}" sort="nameAsc" types="contact" field="contact:" start="${0}" end="${0}" timezone="${mailbox.prefs.timeZone}" folderid=""/>
                </c:otherwise>
            </c:choose>
        </c:when>
        <c:when test="${uploader.isContactSearchMore or uploader.isContactSearchPrev}">
            <zm:search var="search" query="${empty query ? 'is:local' : query}" offset="${uploader.isContactSearchMore ? uploader.nextSearchOffset : uploader.prevSearchOffset}" sort="nameAsc" types="contact" field="contact:" start="${0}" end="${0}" timezone="${mailbox.prefs.timeZone}" folderid=""/>
        </c:when>
        <c:otherwise>
            <zm:search var="search" query="is:local" limit="100" sort="nameAsc" types="contact" field="contact:" start="${0}" end="${0}" timezone="${mailbox.prefs.timeZone}" folderid=""/>
            <c:set var="query" value=""/>
        </c:otherwise>
    </c:choose>
</app:handleError>

<app:view mailbox="${mailbox}" title="${title}" selected='compose' folders="true" tags="true" searches="true" context="${null}" keys="false"
        onload="var e=document.getElementById('findField'); if (e) e.focus();">

<c:set var="toolbar">
    <table width=100% cellspacing=0>
        <tr valign='middle'>
            <td class='TbBt'>
                <table cellspacing=0 cellpadding=0 class='Tb'>
                    <tr>
                        <app:button name="actionContactDone" src="common/ImgCheck.png" tooltip="done" text="done"/>
                        <td><div class='vertSep'></div></td>
                        <app:button name="actionContactAdd" src="contacts/ImgContactsPicker.png" tooltip="addSelected" text="addSelected"/>
                        <td><div class='vertSep'></div></td>
                        <app:button name="actionContactCancel" src="common/ImgCancel.png" tooltip="cancel" text="cancel"/>
                    </tr>
                </table>
            </td>
            <c:if test="${uploader.contactLocation ne 'gal'}">
            <td nowrap align=right>
                <table cellspacing=0 cellpadding=0 class='Tb'>
                <tr>
                <app:button id="doSearchPrev" name="actionContactSearchPrev" src="startup/ImgLeftArrow.png" disabled="${search.hasPrevPage ? '' : 'true'}"/>
                <td>
                   <app:searchPageOffset searchResult="${search}"/>
                </td>
                <app:button id="doSearchMore" name="actionContactSearchMore" src="startup/ImgRightArrow.png" disabled="${search.hasNextPage? '' : 'true'}"/>
                </tr>
                </table>
            </td>
            </c:if> 
        </tr>
    </table>
</c:set>

<SCRIPT TYPE="text/javascript">
<!--
var checkEnter = function(f,e) {var k;if (window.event) k = window.event.keyCode;else if (e) k = e.which;else return true;
if (k == 13) { document.getElementById("doSearch").click();return false;} else {return true;}}
//-->
</SCRIPT>

<form action="" method="post" enctype="multipart/form-data" accept-charset="utf-8">
    <table width=100% cellpadding="0" cellspacing="0">
        <tr>
            <td class='TbTop'>
                ${toolbar}
            </td>
        </tr>
        <tr>
            <td class='ZhAppContent'>

            <table width=100%>
                <c:if test="${not (empty compose.to and empty compose.cc and empty compose.bcc and empty uploader.pendingBcc and empty uploader.pendingCc and empty uploader.pendingTo)}">
                    <tr>
                        <td class=MsgHdr>
                                <table width=100% class='ZPropertySheet' cellspacing='6'>
                                    <c:if test="${not (empty compose.to and empty uploader.pendingTo)}">
                                        <tr>
                                            <td class='MsgHdrName'>
                                                <fmt:message key="to"/>:
                                            </td>
                                            <td class='MsgHdrValue'>
                                                <c:out value="${compose.to}"/>
                                                <c:if test="${not empty uploader.pendingTo}">
                                                    <span style='font-weight:bold'>
                                                        <c:if test="${not empty compose.to and not fn:endsWith(fn:trim(compose.to),',')}">
                                                            ,
                                                        </c:if>
                                                        <c:out value="${uploader.pendingTo}"/>
                                                    </span>
                                                </c:if>
                                            </td>
                                        </tr>
                                    </c:if>
                                    <c:if test="${not (empty compose.cc and empty uploader.pendingCc)}">
                                        <tr>
                                            <td class='MsgHdrName'>
                                                <fmt:message key="cc"/>:
                                            </td>
                                            <td class='MsgHdrValue'>
                                                <c:out value="${compose.cc}"/>
                                                <c:if test="${not empty uploader.pendingCc}">
                                                    <span style='font-weight:bold'>
                                                        <c:if test="${(not empty compose.cc) and (not fn:endsWith(fn:trim(compose.cc),','))}">
                                                            ,
                                                        </c:if>
                                                        <c:out value="${uploader.pendingCc}"/>
                                                    </span>
                                                </c:if>
                                            </td>
                                        </tr>
                                    </c:if>
                                    <c:if test="${not (empty compose.bcc and empty uploader.pendingBcc)}">
                                        <tr>
                                            <td class='MsgHdrName'>
                                                <fmt:message key="bcc"/>:
                                            </td>
                                            <td class='MsgHdrValue'>
                                                <c:out value="${compose.bcc}"/>
                                                <c:if test="${not empty uploader.pendingBcc}">
                                                    <span style='font-weight:bold'>
                                                        <c:if test="${not empty compose.bcc and not fn:endsWith(fn:trim(compose.bcc),',')}">
                                                            ,
                                                        </c:if>
                                                        <c:out value="${uploader.pendingBcc}"/>
                                                    </span>
                                                </c:if>
                                            </td>
                                        </tr>
                                    </c:if>
                                </table>
                        </td>
                    </tr>
                </c:if>
                <tr>
                    <td class='SearchBar'>
                            &nbsp;
                            <c:choose>
                                <c:when test="${mailbox.features.contactsDetailedSearch}">

                                    <label for="nameField"><fmt:message key="name"/>:</label>
                                    <input type="text" id="nameField" class="searchField" style='width:16%' maxlength=2048 name='contactSearchNameQuery' value="${fn:escapeXml(nameQuery)}" onkeypress="return checkEnter(this,event)"/>

                                    <span id="phoneticNameDisplay" style="display:${(empty uploader.contactLocation || uploader.contactLocation eq 'personal' || uploader.contactLocation eq 'contact') ? 'inline':'none'}">
                                        <label for="phoneticNameField"><fmt:message key="phoneticName"/>:</label>
                                        <input type="text" id="phoneticNameField" class="searchField" style='width:20%' maxlength=2048 name='contactSearchPhoneticQuery' value="${fn:escapeXml(phoneticQuery)}" onkeypress="return checkEnter(this,event)"/>
                                    </span>

                                    <label for="emailField"><fmt:message key="email"/>:</label>
                                    <input type="text" id="emailField" class="searchField" style='width:16%' maxlength=2048 name='contactSearchEmailQuery' value="${fn:escapeXml(emailQuery)}" onkeypress="return checkEnter(this,event)"/>

                                    <span id="departmentDisplay" style="display:${uploader.contactLocation eq 'gal' ? 'inline':'none'}">
                                        <label for="deptField"><fmt:message key="department"/>:</label>
                                        <input type="text" id="deptField" class="searchField" style='width:20%' maxlength=2048 name='contactSearchDeptQuery' value="${fn:escapeXml(deptQuery)}" onkeypress="return checkEnter(this,event)"/>
                                    </span>

                                </c:when>
                                <c:otherwise>
                                    <label for="findField"><fmt:message key="find"/>:</label>
                                    <input type="text" id="findField" class="searchField" style='width:50%' maxlength=2048 name='contactSearchQuery' value="${fn:escapeXml(query)}" onkeypress="return checkEnter(this,event)">
                                </c:otherwise>
                            </c:choose>
                            <c:if test="${mailbox.features.contactsDetailedSearch}">
                                <script type="text/javascript">
                                    function changeSearchType(value) {
                                        var phonetic = document.getElementById("phoneticNameDisplay");
                                        var department = document.getElementById("departmentDisplay");
                                        if (phonetic) {
                                            phonetic.style.display = (value=="contact") ? "inline":"none";
                                        }
                                        if (department) {
                                            department.style.display = (value=="gal") ? "inline":"none";
                                        }
                                    }
                                </script>
                            </c:if>
                            &nbsp;
                            <fmt:message key="in"/>
                            &nbsp;

                            <select name="contactLocation" <c:if test="${mailbox.features.contactsDetailedSearch}">onchange="changeSearchType(this.value)"</c:if>>
                                <c:if test="${mailbox.features.contacts}">
                                <option
                                        <c:if test="${empty uploader.contactLocation or uploader.contactLocation eq 'personal'}">
                                            selected
                                        </c:if> value="contact"/>
                                <fmt:message key="searchPersonalContacts"/>
                                </c:if>
                                <c:if test="${mailbox.features.gal}">
                                <option
                                        <c:if test="${uploader.contactLocation eq 'gal'}">selected</c:if> value="gal"/>
                                <fmt:message key="GAL"/>
                                </c:if>
                            </select>
                            <input class="SearchButton" type=submit name='actionContactSearch' id="doSearch"
                                   value="<fmt:message key="searchContacts"/>">
                    </td>
                </tr>
                <tr>
                    <td class='List'>
                        <app:contactAddListView searchResult="${search}" searchGalResult="${searchGal}" uploader="${uploader}"/>
                        <input type="hidden" name='nextSearchOffset' id="nextSearchOffset" value="${search.nextOffset}"/>
                        <input type="hidden" name='prevSearchOffset' id="prevSearchOffset" value="${search.prevOffset}"/>
                    </td>
                </tr>
            </table>

            </td>
        </tr>
        <tr>
            <td class='TbBottom'>
                ${toolbar}
            </td>
        </tr>
    </table>
    <input type="hidden" name="sendUID" value="${fn:escapeXml(compose.sendUID)}"/>    
    <input type="hidden" name="to" value="${fn:escapeXml(compose.to)}"/>
    <input type="hidden" name="cc" value="${fn:escapeXml(compose.cc)}"/>
    <input type="hidden" name="bcc" value="${fn:escapeXml(compose.bcc)}"/>
    <input type="hidden" name="pendingTo" value="${fn:escapeXml(uploader.pendingTo)}"/>
    <input type="hidden" name="pendingCc" value="${fn:escapeXml(uploader.pendingCc)}"/>
    <input type="hidden" name="pendingBcc" value="${fn:escapeXml(uploader.pendingBcc)}"/>
    <input type="hidden" name="subject" value="${fn:escapeXml(compose.subject)}"/>
    <c:forEach var="ma" items="${compose.messageAttachments}">
        <input type="hidden" name="messageAttachment" value="${ma.id}:${fn:escapeXml(ma.subject)}"/>
    </c:forEach>
    <c:forEach var="ca" items="${compose.checkedAttachmentNames}">
        <input type="hidden" name="originalAttachment" value="${fn:escapeXml(ca.key)}"/>
    </c:forEach>
    <c:set var="composeformat" value="${mailbox.prefs.composeFormat}" />
    <c:if test="${mailbox.prefs.forwardReplyInOriginalFormat && !empty param.rf && (param.rf eq 'html' || param.rf eq 'text')}">
        <c:set var="composeformat" value="${param.rf}"/>
    </c:if>
    <c:set var="isHtml" value="${composeformat eq 'html'}"/>
    <c:set var="theBody" value="${isHtml ? (empty compose.htmlContent ?  compose.content : compose.htmlContent) : compose.content}"/>
    <input type="hidden" name="body" value="${fn:escapeXml(theBody)}"/>
    <input type="hidden" name="replyto" value="${fn:escapeXml(compose.replyTo)}"/>
    <input type="hidden" name="from" value="${fn:escapeXml(compose.from)}"/>
    <input type="hidden" name="inreplyto" value="${fn:escapeXml(compose.inReplyTo)}"/>
    <input type="hidden" name="messageid" value="${fn:escapeXml(compose.messageId)}"/>
    <input type="hidden" name="replytype" value="${fn:escapeXml(compose.replyType)}"/>
    <input type="hidden" name="draftid"
           value="${fn:escapeXml(empty requestScope.draftid ? compose.draftId : requestScope.draftid)}"/>
    <input type="hidden" name="inviteReplyVerb" value="${fn:escapeXml(compose.inviteReplyVerb)}"/>
    <input type="hidden" name="inviteReplyInst" value="${fn:escapeXml(compose.inviteReplyInst)}"/>
    <input type="hidden" name="inviteReplyAllDay" value="${fn:escapeXml(compose.inviteReplyAllDay)}"/>
    <input type="hidden" name="compNum" value="${fn:escapeXml(compose.compNum)}"/>
    <input type="hidden" name="instCompNum" value="${fn:escapeXml(compose.instanceCompNum)}"/>
</form>
</app:view>
