<%--
/*
 * ***** BEGIN LICENSE BLOCK *****
 * Zimbra Collaboration Suite Web Client
 * Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Synacor, Inc.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software Foundation,
 * version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with this program.
 * If not, see <https://www.gnu.org/licenses/>.
 * ***** END LICENSE BLOCK *****
 */
--%>
<%@ 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="session"/>
    
    <c:choose>
        <c:when test="${zm:actionSet(param, 'actionNew') or (param.action eq 'newcontact')}">
            <c:set var="contactId" value=""/>
            <fmt:message var="title" key="newContact"/>
        </c:when>
        <c:when test="${zm:actionSet(param, 'actionNewGroup')or (param.action eq 'newcontactgroup')}">
            <c:set var="contactId" value=""/>
            <c:set var="newGroup" value="true"/>
            <fmt:message var="title" key="newGroup"/>
            <!--- yeah -->
        </c:when>
        <c:when test="${zm:actionSet(param, 'actionEdit') and not empty param.actionEditId}">
            <c:set var="contactId" value="${not empty param.id ? param.id : param.actionEditId}"/>
        </c:when>
        <c:otherwise>
            <c:set var="contactId" value="${param.id}"/>            
        </c:otherwise>
    </c:choose>
    
    <zm:computeSearchContext var="context" types="contact" usecache="true"/>
    <c:set var="isCreate" value="${empty contactId}"/>

    <c:choose>
        <c:when test="${isCreate}">
            <c:set var="contact" value="${null}"/>
        </c:when>
        <c:otherwise>
            <zm:getContact id="${contactId}" var="contact"/>
            <c:set var="noDisplayAs"><fmt:message key="noDisplayAs"/></c:set>
            <c:set var="fileAsStr"><app:contactFileAs contact="${contact}" /></c:set>
            <c:set var="title" value="${empty contact.displayFileAs ? noDisplayAs : fileAsStr}" />
        </c:otherwise>
    </c:choose>
</app:handleError>

<c:set var="isGroup" value="${contact.isGroup or newGroup}"/>
<c:if test="${zm:boolean(contact.isGroup)}" >
<jsp:forward page="/h/egroup?id=${contactId}"/> 
</c:if>

<c:set var="focusField" value="${isGroup ? 'nickname' : 'lastName'}"/>

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

    <zm:currentResultUrl var="currentUrl" disp="${isCreate ? 0 : 1}" value="" action="${param.action}" context="${context}"/>
    <form action="${currentUrl}" name="contactForm" method="post" accept-charset="utf-8" onsubmit="cancelOnbeforeUnload();">
        <table width=100%>
            <tr>
                <td class='TbTop'>
                    <app:editContactToolbar create="${isCreate}" context="${context}" keys="true"/>
                </td>
            </tr>
            <tr>
                <td class='ZhAppContent'>
                        <app:editContact title="${title}" contact="${contact}" context="${context}" isgroup="${newGroup}"/>
                </td>
            </tr>
            <tr>
                <td class='TbBottom'>
                    <app:editContactToolbar create="${isCreate}" context="${context}" keys="false"/>
                </td>
            </tr>
            </table>
        <input type="hidden" name="doContactListViewAction" value="1"/>
        <input type="hidden" name="id" value="${contact.id}"/>
        <input type="hidden" name="crumb" value="${fn:escapeXml(mailbox.accountInfo.crumb)}"/>
    </form>

    <SCRIPT TYPE="text/javascript">
    <!--
    var zclick = function(id) { var e = document.getElementById(id); if (e) e.click(); }
    //-->
   </SCRIPT>
    <app:keyboard cache="ab.econtact" globals="false" passspecial="true" mailbox="${mailbox}">
        <zm:bindKey message="editContact.Save" func="function() { zclick('SOPSAVE')}"/>
    </app:keyboard>
<script type="text/javascript" >
var _fields = {} ;
grabFieldValues = function(){
    var form = document.forms["contactForm"];
    var _el = form.elements;
    for ( var _i=0;_i < _el.length; _i++){
        if((_el[_i].type == "hidden") || (_el[_i].type == "submit")){
            // do nothing
        }else if(_el[_i].type == "text"){
            _fields[_el[_i].name] = _el[_i].value;
        }else if(_el[_i].type == "radio"){
            if(_el[_i].checked){
                _fields[_el[_i].name] = _el[_i].value+"::"+_el[_i].checked;
            }
        }else if(_el[_i].type == "checkbox"){
            _fields[_el[_i].name] = _el[_i].checked;
        }else if(_el[_i].type == "select-one"){
            _fields[_el[_i].name] = _el[_i].options[_el[_i].selectedIndex].value;
        }else if(_el[_i].type == "textarea"){
            _fields[_el[_i].name] = _el[_i].value;
        }
    }
}
grabFieldValues();
var _form = document.forms["contactForm"];

checkForChanges = function(){
    var _el = _form.elements;
    var _checkFail = false;
    for ( var _i=0;_i < _el.length; _i++){
        if((_el[_i].type == "hidden") || (_el[_i].type == "submit")){
            // do nothing
        }else if(_el[_i].type == "text"){
            if(_fields[_el[_i].name] != _el[_i].value) { _checkFail = true;}
        }else if(_el[_i].type == "radio"){
            if(_el[_i].checked){
                if(_fields[_el[_i].name] != _el[_i].value+"::"+_el[_i].checked) { _checkFail = true; }
            }
        }else if(_el[_i].type == "checkbox"){
            if(_fields[_el[_i].name] != _el[_i].checked) {_checkFail = true; }

        }else if(_el[_i].type == "select-one"){
            if(_fields[_el[_i].name] != _el[_i].options[_el[_i].selectedIndex].value) {_checkFail = true;  }

        }else if(_el[_i].type == "textarea"){
            if(_fields[_el[_i].name] != _el[_i].value) {_checkFail = true;}
        }
    }
    if(_checkFail){
        return "<fmt:message key="optionsExitConfirmation"/>";
    }else{
        return;
    }
}

window.onbeforeunload = checkForChanges;

cancelOnbeforeUnload = function(){
    window.onbeforeunload = null;
}
</script>

</app:view>

