<%--
/*
 * ***** BEGIN LICENSE BLOCK *****
 * Zimbra Collaboration Suite Web Client
 * Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 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='request' />

    <c:set var="selected" value="${empty param.section ? param.selected : param.section}"/>
    <c:set var="selected" value="${empty selected ? 'general' : fn:toLowerCase(zm:cook(selected))}"/>

    <c:choose>
        <%-- cases not covered here are assumed to have the same name in both clients (bug 60599) --%>
        <c:when test="${selected eq 'contacts'}">
            <c:set var="selected" value="addressbook"/>
        </c:when>
        <c:when test="${selected eq 'filters'}">
            <c:set var="selected" value="filter"/>
        </c:when>
    </c:choose>

    <c:set var="voiceStatus" value="false"/>
	<c:if test="${selected eq 'voice' and mailbox.features.voice}">
		<zm:checkVoiceStatus var="voiceStatus"/>
	</c:if>
   <c:set var="prevApp" value="${zm:cook(param.prev)}" />
    <c:choose>
        <c:when test="${zm:actionSet(param, 'actionSave')}">
            <zm:checkCrumb crumb="${param.crumb}"/>
            <c:choose>
                <c:when test="${selected eq 'voice'}">
                    <app:optVoiceAction accountindex="0" />
                </c:when>
                <c:otherwise>
                    <app:optAction selected="${selected}"/>
                </c:otherwise>
            </c:choose>
        </c:when>
        <c:when test="${zm:actionSet(param, 'actionCancel')}">
            <zm:checkCrumb crumb="${param.crumb}"/>
            <c:choose>
                <c:when test="${prevApp eq 'contacts'}" >
                    <c:redirect url="/h/search">
                        <c:param name="st" value="contact"/>
                    </c:redirect>
                </c:when>
                <c:when test="${prevApp eq 'tasks'}" >
                    <c:redirect url="/h/search">
                        <c:param name="st" value="task"/>
                    </c:redirect>
                </c:when>
                <c:when test="${prevApp eq 'options'}" >
                    <c:redirect url="/h/options" />
                </c:when>
                 <c:when test="${prevApp eq 'voice'}" >
                    <c:redirect url="/h/search?st=voicemail" />
                </c:when>
                <c:when test="${empty prevApp or prevApp eq 'mail' or prevApp eq 'compose'}">
                    <c:redirect url="/h/search" />
                </c:when>
                <c:when test="${not empty prevApp and prevApp ne 'mail'}">
                    <c:redirect url="/h/search">
                        <c:param name="st" value="${prevApp}"/>
                    </c:redirect>
                </c:when>
            </c:choose>
        </c:when>
        <c:when test="${selected eq 'signatures'}">
            <c:forEach var="p" items="${paramValues}">
                <c:if test="${fn:startsWith(p.key,'actionDeleteSig:')}">
                    <zm:checkCrumb crumb="${param.crumb}"/>
                    <c:set var="sigId" value="${fn:substringAfter(p.key,'actionDeleteSig:')}"/>
                    <zm:deleteSiganture id="${sigId}"/>
                    <app:status><fmt:message key="optionsSignatureDeleted"/></app:status>
                </c:if>
            </c:forEach>
        </c:when>
        <c:when test="${zm:actionSet(param, 'actionFilterCancel')}">
            <app:status>
                <fmt:message key="${zm:actionSet(param, 'actionNewFilter') ? 'filterCancelCreate' : 'filterCancelModify'}"/>
            </app:status>
        </c:when>
        <c:when test="${zm:actionSet(param, 'actionDeleteFilter') and not empty param.ruleName}">
            <zm:checkCrumb crumb="${param.crumb}"/>
            <zm:deleteFilterRule name="${zm:cook(param.ruleName)}"/>
        </c:when>
        <c:when test="${zm:actionSet(param, 'actionMoveFilterUp') and not empty param.ruleName}">
            <zm:checkCrumb crumb="${param.crumb}"/>
            <zm:moveFilterRule direction="up" name="${zm:cook(param.ruleName)}"/>
        </c:when>
        <c:when test="${zm:actionSet(param, 'actionMoveFilterDown') and not empty param.ruleName}">
            <zm:checkCrumb crumb="${param.crumb}"/>
            <zm:moveFilterRule direction="down" name="${zm:cook(param.ruleName)}"/>
        </c:when>
        <c:when test="${zm:actionSet(param, 'actionFilterSave') and not empty param.frompost}">
            <c:set var="filterSave" value="fail" scope="request"/>
            <app:constructRule var="postRule"/>
            <c:choose>
                <c:when test="${empty postRule.name}">
                    <app:status>
                        <fmt:message key="actionRuleNoName"/>
                    </app:status>
                </c:when>
                <c:when test="${empty postRule.conditions}">
                    <app:status>
                        <fmt:message key="actionRuleNoConditions"/>
                    </app:status>
                </c:when>
                <c:when test="${empty postRule.actions}">
                    <app:status>
                        <fmt:message key="actionRuleNoActions"/>
                    </app:status>
                </c:when>
                <c:when test="${zm:actionSet(param, 'actionNewFilter')}">
                    <zm:checkCrumb crumb="${param.crumb}"/>
                    <zm:createFilterRule rule="${postRule}"/>
                    <c:set var="filterSave" value="success" scope="request"/>
                </c:when>
                <c:when test="${zm:actionSet(param, 'actionEditFilter')}">
                    <zm:checkCrumb crumb="${param.crumb}"/>
                    <zm:modifyFilterRule originalname="${zm:cook(param.origname)}" rule="${postRule}"/>
                    <c:set var="filterSave" value="success" scope="request"/>
                </c:when>
            </c:choose>
        </c:when>
    </c:choose>
</app:handleError>

<c:choose>
    <%------------------- GENERAL ------------------%>
    <c:when test="${selected eq 'general'}">
        <c:set var="subKey" value="general"/>
    </c:when>
    <%------------------- MAIL ------------------%>
    <c:when test="${selected eq 'mail'}">
        <c:set var="subKey" value="mail"/>
    </c:when>
    <%------------------- COMPOSING ------------------%>
    <c:when test="${selected eq 'composing'}">
        <c:set var="subKey" value="optionsComposing"/>
    </c:when>
    <%------------------- SIGNATURES ------------------%>
    <c:when test="${selected eq 'signatures'}">
        <c:set var="subKey" value="optionsSignatures"/>
    </c:when>
    <%------------------- MAIL FILTERS ------------------%>
    <c:when test="${selected eq 'filters' and mailbox.features.filters}">
        <c:set var="subKey" value="mailFilters"/>
    </c:when>
    <%------------------- Voice  ------------------%>
    <c:when test="${selected eq 'voice' and mailbox.features.voice}">
        <c:set var="subKey" value="voice"/>
    </c:when>
    <%------------------- ADDRESS BOOK ------------------%>
    <c:when test="${selected eq 'addressbook' and mailbox.features.contacts}">
        <c:set var="subKey" value="addressBook"/>
    </c:when>
    <%------------------- ADDRESS BOOK ------------------%>
    <c:when test="${selected eq 'calendar' and mailbox.features.calendar}">
        <c:set var="subKey" value="calendar"/>
    </c:when>
    <%------------------- Accounts ------------------%>
    <c:when test="${selected eq 'accounts'}">
        <c:set var="subKey" value="optionsAccounts"/>
    </c:when>
    <%------------------- SHORTCUTS ------------------%>
    <c:when test="${selected eq 'shortcuts'}">
        <c:set var="subKey" value="shortcuts"/>
    </c:when>
    <c:otherwise>
        <c:set var="subKey" value=""/>
    </c:otherwise>
</c:choose>

<c:set var="title">
    <fmt:message key="options"/>
    <c:if test="${not empty subKey}">:
        <fmt:message key="${subKey}"/>
    </c:if>
</c:set>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<app:view mailbox="${mailbox}" title="${title}" context="${null}" selected='options' folders="true" searches="true" tags="true" keys="true" editmode="true">

    <form action="" method="post" name="optionsForm" onsubmit="cancelOnbeforeUnload();" accept-charset="utf-8">
        <table width="100%">
            <!--tr>
                <td class='TbTop'>
                    <table class='Tb'>
                        <tr>
                            <td></td>
                        </tr>
                    </table>
                </td>
            </tr-->
            <tr>
                <td class='ZhAppContent'>
                    <table width="100%">
                        <tr>
                            <td class='ZhSubTabs'>
                                <app:optTabs mailbox="${mailbox}" selected="${selected}" prev="${prevApp}"/>
                            </td>
                        </tr>
                        <c:if test="${mailbox.attrs.zimbraStandardClientCustomPrefTabsEnabled[0] eq 'FALSE' or empty param.status}">
                            <%--Hide the Save and Cancel buttons for the custom tabs--%>
                            <tr>
                                <td class='TbBottom'>
                                    <app:optSubToolbar selected="${selected}"/>
                                </td>
                            </tr>
                        </c:if> 
                        <tr>
                            <td>
                                <c:choose>
                                    <%------------------- GENERAL ------------------%>
                                    <c:when test="${selected eq 'general'}">
                                        <app:optGeneral mailbox="${mailbox}"/>
                                    </c:when>
                                    <%------------------- MAIL ------------------%>
                                    <c:when test="${selected eq 'mail'}">
                                        <app:optMail mailbox="${mailbox}"/>
                                    </c:when>
                                    <%------------------- COMPOSING ------------------%>
                                    <c:when test="${selected eq 'composing'}">
                                        <app:optComposing mailbox="${mailbox}"/>
                                    </c:when>
                                    <%------------------- SIGNATURES ------------------%>
                                    <c:when test="${selected eq 'signatures'}">
                                        <app:optSignatures mailbox="${mailbox}"/>
                                    </c:when>
                                    <%------------------- MAIL FILTER ------------------%>
                                    <c:when test="${selected eq 'filter' and mailbox.features.filters}">
                                        <app:optMailFilter mailbox="${mailbox}"/>
                                    </c:when>
                                    <%------------------- Voice ------------------%>
                                    <c:when test="${(selected eq 'voice') and mailbox.features.voice}">
                                        <c:if test="${voiceStatus ne 'false'}">
                                                                <app:optVoice mailbox="${mailbox}"/>
                                        </c:if>
                                        <c:if test="${(voiceStatus eq 'false') and (lastErrorCode eq 'voice.SECONDARY_NOT_ALLOWED')}">
                                            <fmt:message key="voice.SECONDARY_NOT_ALLOWED_PREFS"/>
                                        </c:if>
                                    </c:when>
                                    <%------------------- ADDRESS BOOK ------------------%>
                                    <c:when test="${selected eq 'addressbook'}">
                                        <app:optAddressBook mailbox="${mailbox}"/>
                                    </c:when>
                                    <%------------------- CALENDARW ------------------%>
                                    <c:when test="${selected eq 'calendar'}">
                                        <app:optCalendar mailbox="${mailbox}"/>
                                    </c:when>
                                    <%------------------- ACCOUNTS ------------------%>
                                    <c:when test="${selected eq 'accounts'}">
                                        <app:optAccounts mailbox="${mailbox}"/>
                                    </c:when>
                                    <%------------------- SHORTCUTS ------------------%>
                                    <c:when test="${selected eq 'shortcuts'}">
                                        <app:optShortcuts mailbox="${mailbox}"/>
                                    </c:when>
                                    <%-------------------CUSTOM TABS------------------%>
                                    <c:when test="${mailbox.attrs.zimbraStandardClientCustomPrefTabsEnabled[0] eq 'TRUE'}">
                                        <c:set var="customTabs" value="${mailbox.attrs.zimbraStandardClientCustomPrefTab}"/>
                                        <c:set var="tab" value="${fn:split(customTabs[param.status],',')}"/>
                                        
                                        <%--Load the external site associated with the custom tab in an iframe--%>
                                        <table class="ZOptionsSectionMain" width="100%">
                                            <tbody>
                                                <tr>
                                                    <td colspan="3"> &nbsp; </td>
                                                </tr>
                                                <tr>
                                                    <td class="TbBt" style="font-size:14px;" align="center"><iframe width="100%" height="500px" src="${tab[1]}"></iframe></td>
                                                </tr>
                                                <tr>
                                                    <td colspan="3"> &nbsp; </td>
                                                </tr>
                                            </tbody>
                                        </table>                                            
                                    </c:when>
                                </c:choose>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
        <input type="hidden" name="doAction" value="1"/>
        <input type="hidden" name="crumb" value="${fn:escapeXml(mailbox.accountInfo.crumb)}"/>
    </form>
    <app:keyboard cache="options" globals="true" tags="true" folders="true" calendars="true" contacts="true" mailbox="${mailbox}"/>

</app:view>
<script type="text/javascript">
var _fields = {};

var trim = function(strVal){
    return strVal.replace(/^\s*/, "").replace(/\s*$/, "");
}

var _fields = {} ;
grabFieldValues = function(){
    var form = document.forms["optionsForm"];
    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;
        }
    }

	<c:if test="${param.selected eq 'signatures'}">
	   setTimeout("grabSignatures()",1000);
	</c:if>
}

grabSignatures = function () {
	for(var j = 0 ;j < sigcount ; j++) {
		if(myEdit[j] != null) {
		myEdit[j].save();
		var fname = "signatureValue"+j;
		var sigValue = trim(document.getElementById(fname).value);
		_fields[fname] = sigValue;
		}
	}
}

grabFieldValues();
var _form = document.forms["optionsForm"];

checkForChanges = function(){
    var _el = _form.elements;
    var _checkFail = false;
    for ( var _i=0;_i < _el.length; _i++){
        if(_el[_i].name != null && _el[_i].name != ""){
            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; }
            }
        }
    }

    <c:if test="${param.selected eq 'signatures'}">
        for(var j = 0 ;j < sigcount ; j++) {
            if(myEdit[j] != null) {
                myEdit[j].save();
                var fname = "signatureValue"+j;
                var sigValue = trim(document.getElementById(fname).value);
                if(trim(_fields[fname]) != sigValue) {
                    _checkFail = true;
                }
            }
        }
        if(document.getElementById("newSignatureValue")) {
            myEditor.save();
            var newSig = trim(document.getElementById("newSignatureValue").value);
            if(trim(_fields["newSignatureValue"]) != newSig) {
                _checkFail = true;
            }
        }
    </c:if>

    if(_checkFail){
        return "<fmt:message key="optionsExitConfirmation"/>";
    }else{
        return;
    }
}

window.onbeforeunload = checkForChanges;

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