<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="spring" uri="/spring" %>
<%@ taglib prefix="nmail" uri="http://www.nir.hu/nmail/taglibs/nmail-2.0" %>

<script type="text/javascript">
    function selectMail(i) {
        var subject = document.getElementById("subject_"+i).innerHTML;
        document.message_form.subject.value = TrimString(subject);
    }
    
    function TrimString(sInString) {
        sInString = sInString.replace( /^\s+/g, "" );// strip leading
        return sInString.replace( /\s+$/g, "" );// strip trailing
    }
</script>
<div id="main">
    <jsp:include page="errormessages.jsp"/>
    <form name="message_form" action="send.htm" method="post" accept-charset="UTF-8">
        <table class="mail_header" cellspacing="10" cellpadding="3">
            <tr>
                <td>
                    <spring:message code="base.sendername"/>
                </td>
                <td class="mail_header">
                    <input type="text" name="senderName" class="mail_header" 
                    value='<c:out value="${mailsData.senderName}"/>'
                    <c:if test="${!restriction.ableToSetSender}">
                        disabled="disabled"
                    </c:if>
                    />
                </td>
            </tr>
            <tr>
                <td>
                    <spring:message code="base.senderaddress"/>
                </td>
                <td class="mail_header">
                    <input type="text" name="senderAddress" class="mail_header" 
                    value="${mailsData.senderAddress}" 
                    <c:if test="${!restriction.ableToSetSender}">
                        disabled="disabled"
                    </c:if>
                    />
                </td>
            </tr>
            <tr>
                <td>
                    <spring:message code="base.subject"/>:
                </td>
                <td class="mail_header">
                    <input type="text" name="subject" class="mail_header" value='<c:out value="${mailsData.subject}"/>' />
                </td>
            </tr>
            <tr>
                <td>
                    <spring:message code="base.testaddresses"/><sup>*</sup><br />
                    <span class="note"><spring:message code="base.maxtestaddress"/></span>
                </td>
                <td class="mail_header">
                    <input type="text" name="testAddresses" class="mail_header" value="${mailsData.testAddresses}" />
                </td>
            </tr>
        </table>

        <div class="label"><spring:message code="base.lastemails"/></div>

        <table id="mails" cellpadding="5" border="1">
            <tr id="mails-title">
                <td></td>
                <td><spring:message code="base.sender"/></td>
                <td class="mails-subject"><spring:message code="base.subject"/></td>
                <td><spring:message code="base.date"/></td>
            </tr>
            
            <c:set var="index" value="<%= 0 %>"/>
            <c:forEach items="${mailsData.messages}" var="message">
                <tr>
                    <td>
                        <input 
                            type="radio" 
                            name="messageIndex" 
                            value="${index}"
                            onclick="selectMail(${index})"
                            <c:if test="${mailsData.messageIndex == index}">checked="checked"</c:if>
                            />
                    </td>
                    <td>
                        <nmail:printFrom from="${message.from}"/>
                    </td>
                    <td class="mails-subject">
                        <div class="mails-subject-value" id="subject_${index}">
                            <c:out value="${message.subject}" />
                        </div>
                    </td>
                    <td>
                        <fmt:formatDate value="${message.sentDate}" type="both" dateStyle="short" timeStyle="short"/>
                    </td>
                </tr>
                <c:set var="index" value="${index + 1}"/>
            </c:forEach>
        </table>

        <div id="send_buttons">
            <input type="submit" name="test" value="Teszt üzenet küldés"/>
            &nbsp;
            <input type="submit" name="send" value="Levélküldés"
            <c:if test="${!restriction.canSendEmail}">
                        disabled="disabled"
            </c:if>
            />
        </div>
    </form>
</div>
