<!--// $Id$ //-->
{include file="header.html"}
{$allow_reopen_tickets_newer_than = intval(ConfigHelper::getConfig('userpanel.allow_reopen_tickets_newer_than', 0))}
{$allow_message_add_to_closed_tickets = intval(ConfigHelper::getConfig('userpanel.allow_message_add_to_closed_tickets', 1))}

<table style="width: 100%;" cellpadding="10">
    <tr>
        <td>
<form method="post" action="?m=helpdesk&id={$ticket.id}" name="helpdesk" enctype="multipart/form-data">
{box title="Message"}
<input type="hidden" name="helpdesk[inreplyto]" value="{$helpdesk.inreplyto}" />
<input type="hidden" name="helpdesk[references]" value ="{$helpdesk.references|escape|default:""}" />
<table id="newmessage" width="100%" cellpadding="3" class="light">
	<tr class="light">
		<td width="1%">
			{img src="helpdesk.gif" alt=""}
		</td>
		<td width="1%">
			<strong>{trans("Subject:")}</strong>
		</td>
		<td width="98%">
			<input style="width:99%;" type="text" name="helpdesk[subject]" value="{$helpdesk.subject}" {userpaneltip text="Enter message subject" trigger="subject"} />
		</td>
	</tr>
	<tr class="light">
		<td width="1%">
			{img src="help.gif" alt=""}
		</td>
		<td width="1%">
			<strong>{trans("Body:")}</strong>
		</td>
		<td width="98%">
			<textarea style="width:99%;" name="helpdesk[body]" rows="5" {userpaneltip text="Enter message content" trigger="body"}>{$helpdesk.body}</textarea>
		</td>
	</tr>
	<tr class="light">
		<td>
			{img src="attach.gif" alt=""}
		</td>
		<td>
			<strong>{trans("Attachments:")}</strong>
		</td>
		<td>
			<input type="file" name="files[]" id="attachments" size="55" {userpaneltip text="Enter path to file or click 'Browse' button" trigger="files"} multiple />
			<div id="file-status" style="display: none;"></div>
			<br>
			<input type="hidden" id="post_max_size" value="{$post_max_size.bytes}">
			{t a=$post_max_size.text}Maximum total file size: $a{/t}
			<br>
			<input type="hidden" id="upload_max_filesize" value="{$upload_max_filesize.bytes}">
			{t a=$upload_max_filesize.text}Maximum single file size: $a{/t}
		</td>
	</tr>
	<tr class="light">
		<td width="100%" colspan="3" align="right">
			<a class="submit-button" accesskey="S">{trans("<!helpdesk>Submit")} {img src="save.gif" alt="<!helpdesk>Submit"}</a>
		</td>
	</tr>
</table>
{/box}
<br />
{box title=$title}
<table class="width: 100%;" cellpadding="3">
	{foreach from=$ticket.messages item=message}
        <tr class="darkest">
		<td width="1%" align="right">
			<strong>{$message.createtime|date_format:"Y-m-d<br/>H:i"}</strong>
		</td>
		<td width="1%" align="right">
			<strong>{trans("From:")}<br/>{trans("Subject:")}</strong>
		</td>
		<td width="96%" class="nobr">
			    {if $message.customerid}
				    {$message.customername}
			    {elseif $message.userid}
				    {$message.username}
			    {elseif $message.mailfrom}
				    {$message.mailfrom|escape}
			    {else}
				    {$ticket.requestor|escape}
				    {if $ticket.requestoremail}
					    &lt;{$ticket.requestoremail|escape}&gt;
				    {/if}
			    {/if}
			    <br />
			    {$message.subject|escape}
		</td>
		<td width="1%" nowrap>
			{if $message.attachments}
			{foreach $message.attachments as $attachment}
			{if !$attachment@first}<br>{/if}<a href="?m=helpdesk&f=attachment&msgid={$message.id}&file={$attachment.filename}">{$attachment.filename} {img src="attach.gif" alt=""}</a>
			{/foreach}
			{/if}
		</td>
		<td width="1%">
			{if ($ticket.state != $smarty.const.RT_RESOLVED || $allow_message_add_to_closed_tickets)
				&& (!$allow_reopen_tickets_newer_than || $smarty.now - $allow_reopen_tickets_newer_than <= $ticket.lastmod)}
					<a href="?m=helpdesk&op=message&id={$ticket.id}&msgid={$message.id}">{trans("Reply")}</a>
			{else}
				&nbsp;
			{/if}
		</td>
	</tr>
	<tr>
		<td colspan="5">
			{$message.body|escape|replace:"\n":"<br/>"}
		</td>
	</tr>
	{/foreach}
</table>
{/box}
</form>
        </td>
    </tr>
</table>

<script>

	$(function() {
		function check_file_sizes() {
			var attachments = $('#attachments').get(0);
			$('#file-status').hide().text();
			if (attachments.files.length) {
				var error = '';
				var post_max_size = parseInt($('#post_max_size').val());
				var upload_max_filesize = parseInt($('#upload_max_filesize').val());
				var total_size = 0;
				$.each(attachments.files, function (index, file) {
					if (upload_max_filesize && file.size > upload_max_filesize) {
						error = '{trans("One from files is too large!")}';
					}
					total_size += file.size;
				});
				if (post_max_size && total_size > post_max_size) {
					error = '{trans("Total file size is too large!")}';
				}
				if (error.length) {
					$('#file-status').text(error).show();
					return false;
				}
			}
			return true;
		}

		$('#attachments').change(function() {
			check_file_sizes();
		});

		$('#newmessage .submit-button').click(function() {
			if (check_file_sizes()) {
				document.helpdesk.submit();
			}
		});
	});

</script>

<style>

	#file-status {
		margin-top: 1em;
		color: red;
	}

</style>

{include file="footer.html"}
