<!--// $Id$ //-->
<form method="post" action="?m=helpdesk" name="helpdesk" enctype="multipart/form-data">
{box title="New Request"}
<table id="newrequest" width="100%" cellpadding="3" class="light">
	{if $queues|size == 1}
	<input type="hidden" name="helpdesk[queue]" value="{$queues[0].id}">
	{else}
	<tr class="light">
		<td width="1%">
			{img src="queue.gif" alt=""}
		</td>
		<td width="1%">
			<strong>{trans("Queue:")}</strong>
		</td>
		<td width="98%">
			<SELECT name="helpdesk[queue]" {userpaneltip text="Select queue" trigger="queue"}>
				{foreach $queues as $queue}
				<OPTION value="{$queue.id}"{if $helpdesk.queue == $queue.id} selected{/if}>{$queue.name}</OPTION>
				{/foreach}
			</SELECT>
		</td>
	</tr>
	{/if}
	<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}"
					maxlength="{ConfigHelper::getConfig('rt.subject_max_length', 50)}"
					{userpaneltip text="Enter request 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 request contents" trigger="body"} rows="5" cols="40">{$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}
</form>
<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();
		});

		$('#newrequest .submit-button').click(function() {
			if ($(this).is("[disabled]")) {
				return false;
			}
			$(this).attr('disabled', 'disabled');
			if (check_file_sizes()) {
				document.helpdesk.submit();
			} else {
				$(this).removeAttr('disabled');
			}
		});
	});

</script>

<style>

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

</style>
