<%@ 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"/>
    <app:skin mailbox="${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:setBundle basename="/messages/I18nMsg" var="i18n"/>

    <zm:computeSearchContext var="context" usecache="true"/>
    <c:set var="useTo" value="${context.folder.isPlacedCalls}"/>
</app:handleError>

<html>
<app:head mailbox="${mailbox}"/>

<body style='background:white;'>
    <div class='ZhCallListPrintView'>
        <table><tr>
            <td class='ZhZimbraTitle'><fmt:message key="zimbraTitle"/></td>
            <td nowrap width='1%'><b>${fn:escapeXml(mailbox.name)}</b></td>
        </tr></table>
        <hr/>

        <h1>${zm:getVoiceFolderName(pageContext, context.folder)}</h1>
        <table class='ZhCallListPrintTable' cellpadding=2 cellspacing=0 width="80%">
            <tr class='Header'>
                <th width="30%"><fmt:message key="${useTo ? 'to' : 'from'}"/>
                <th width="30%"><fmt:message key="duration"/></th>
                <th width="20%"><fmt:message key="received"/></th>
            </tr>

            <c:forEach items="${context.searchResult.hits}" var="hit" varStatus="status">
            <tr>
                <td nowrap>${zm:getDisplayCaller(pageContext, useTo ? hit.callHit.recipient : hit.callHit.caller)}</td>
                <td nowrap>${fn:escapeXml(zm:displayDuration(pageContext, hit.callHit.duration))}</td>
                <td nowrap>
                    <div id="vm_${status.count}_date">${fn:escapeXml(zm:displayVoiceDate(pageContext, hit.callHit.date))}</div>
                </td>
            </tr>
            </c:forEach>
        </table>
        <c:if test="${context.searchResult.size == 0}">
            <div class='NoResults'><fmt:message key="noResultsFound"/></div>
        </c:if>
    </div>
    <script type="text/javascript">
        <!--

		<c:if test="${!empty param.clientTime}">

			function pad(number, length) {
				var str=""+number;
				var pad=[];
				for (var i=0, cnt=length-str.length; i<cnt; i++) {
					pad[i]="0";
				}
				return pad.join("")+str;
			}
			function insert(str, value, replacement) {
				var _str=""+str;
				if (value&&_str.indexOf(value)==-1) return _str;
				var out="";
				var escaped=false;
				for (var i=0;i<_str.length;i++) {
					var c=_str.substr(i,1);
					if (c=="'") {
						escaped=!escaped;
					}
					else if (!escaped) {
						var tok=_str.substr(i,value.length);
						if (tok==value) {
							out+=replacement;
							i+=value.length-1;
							continue;
						}
					}
					out+=c;
				}
				return out;
			}

			var dates = {};
			<c:forEach items="${context.searchResult.hits}" var="hit" varStatus="status">
			<fmt:formatDate var="date" pattern="yyyyMMdd'T'HHmmss'.'SSS'Z'" value="${hit.callHit.date}" timeZone="UTC"/>
			dates['vm_${status.count}_date'] = "${date}";
            </c:forEach>

			var resources = {
				ZM_formatVoiceDateToday: "<fmt:message key='ZM_formatVoiceDateToday'/>",
				ZM_formatVoiceDateYesterday: "<fmt:message key='ZM_formatVoiceDateYesterday'/>",
				ZM_formatVoiceDate: "<fmt:message key='ZM_formatVoiceDate'/>"
			};

			var days = ["<fmt:message key='weekdaySunMedium' bundle='${i18n}'/>","<fmt:message key='weekdayMonMedium' bundle='${i18n}'/>","<fmt:message key='weekdayTueMedium' bundle='${i18n}'/>","<fmt:message key='weekdayWedMedium' bundle='${i18n}'/>","<fmt:message key='weekdayThuMedium' bundle='${i18n}'/>","<fmt:message key='weekdayFriMedium' bundle='${i18n}'/>","<fmt:message key='weekdaySatMedium' bundle='${i18n}'/>"];

			var ONE_DAY = 24 * 3600000;
			var nowDate = new Date();
			var nowTime = nowDate.getTime();
			var nowDay = nowDate.getDate();
			var nowYesterday = new Date(nowTime - ONE_DAY).getDate();
			for (var id in dates) {
				if (dates.hasOwnProperty(id)) {
					var el = document.getElementById(id);
					if (el) {
						var msgDate = new Date();
						var str = dates[id];
						msgDate.setUTCFullYear(parseInt(str.substr(0,4),10));
						msgDate.setUTCMonth(parseInt(str.substr(4,2),10)-1);
						msgDate.setUTCDate(parseInt(str.substr(6,2),10));
						msgDate.setUTCHours(parseInt(str.substr(9,2),10));
						msgDate.setUTCMinutes(parseInt(str.substr(11,2),10));
						msgDate.setUTCSeconds(parseInt(str.substr(13,2),10));
						
						var msgTime = msgDate.getTime();
						var msgDay = msgDate.getDate();
						var resource;

						if (nowTime - msgTime < ONE_DAY && nowDay == msgDay) {
							resource = "ZM_formatVoiceDateToday";
						} else if ((nowTime - msgTime) < (2 * ONE_DAY) && nowYesterday == msgDay) {
							resource = "ZM_formatVoiceDateYesterday";
						} else {
							resource = "ZM_formatVoiceDate";
						}
						var am = msgDate.getHours() < 12;
						var ampmhours = msgDate.getHours() % 12;
						if (ampmhours == 0) ampmhours = 12;

						var out = resources[resource];
							out = insert(out, "EEE", "'"+days[msgDate.getDay()]+"'");
							out = insert(out, "M", msgDate.getMonth()+1);
							out = insert(out, "d", msgDate.getDate());
							out = insert(out, "yy", (""+msgDate.getFullYear()).substr(2,2));
							out = insert(out, "h", ampmhours);
							out = insert(out, "HH", pad(ampmhours,2));
							out = insert(out, "mm", pad(msgDate.getMinutes(),2));
							out = insert(out, "ss", pad(msgDate.getSeconds(),2));
							out = insert(out, "a", am ? "AM" : "PM");
							out = out.replace(/'/g, '');

						el.innerHTML = out;
					}
				}
			}

		</c:if>

        setTimeout(window.print, 2000);
        // -->
    </script>
</body>
</html>
