{include file="$gentemplates/$header_tpl"}
	<div style="width:100%">
		<h2>{$lang.section.email}</h2>
		<!-- begin top menu table -->
		{strip}
		<!-- main tabs -->	
		<div class="main_tabs">
			<div class="tab_block first">
				<a href="{$form.inbox_link}" onclick="">{$lang.mailbox.inbox}&nbsp;({$form.inbox_new}/{$form.inbox_all})</a>
			</div>
			<div class="tab_block">
				<a href="{$form.outbox_link}" onclick="">{$lang.mailbox.outbox}&nbsp;({$form.outbox_all})</a>
			</div>
			<div class="tab_block active">
				<a href="{$form.write_link}" onclick="">{$lang.mailbox.compose}</a>
			</div>
		</div>
		<!-- end main tabs -->		
		{/strip}
		
		<!-- begin main cell -->
		{strip}
		<form action="{$form.action}" method="post" name="mailbox_write" id="mailbox_write" enctype="multipart/form-data">
			{$form.hidden}
			<input type="hidden" name="act" id="act" value="">
			<input type="hidden" name="id_attach" id="id_attach" value="">
			<div class="mailbox_write">
				<div class="content">					
					<dl class="text">
						<dt>{$lang.mailbox.to}&nbsp;<font class="error_msg">*</font></dt>
						<dd>
							<span>
								<input type="hidden" name="mail_to_arr" id="mail_to_arr" value="{$data.to_user_id}">
								<input type="text" maxlength="100" name="to" value="{$data.to}" id="mail_to" onkeyup="SearchVariants(this.value, event);">&nbsp;
							<a class="arr-left1-icon" href="#" onclick="launchWindow('{$form.hotlist_link}','',500,500); return false;">&nbsp;</a>
							<a href="#" onclick="launchWindow('{$form.hotlist_link}','',500,500); return false;">
								&nbsp;{$lang.mailbox.add_from_hotlist}
							</a>
						</span>						
							<div id="variants">
							</div>
						</dd>
					</dl>
					<dl>
						<dt>{$lang.mailbox.subject}&nbsp;<font class="error_msg">*</font></dt>
						<dd>
                            <input type="text" maxlength="250" name="subject" value="{$data.subject}" onkeyup="BadWords('mail_subject');" id="mail_subject" >
                            <div id="bad_div_mail_subject" class="bad_div_left"></div>
                        </dd>
					</dl>
					<dl>
						<dt>{$lang.mailbox.attach_file}</dt>
						<dd>
							<input type="file" name="attach" id="attach">&nbsp;
							<input type="button" value="{$lang.button.upload}" onclick="document.getElementById('act').value='upload_attach'; document.getElementById('mailbox_write').submit();">
						</dd>
					</dl>
					
					{if isset($data.attaches)}
					<dl>									
						<div class="attaches">
							<div class="text">{$lang.mailbox.attaches}</div>
							{foreach item=item from=$data.attaches}
								<div>
									<span class="name_attaches">
										<a href="{$item.path}" target="_blank">{$item.name}</a>
									</span>
									<span class="del">
										<a href="#" onclick="document.getElementById('act').value='delete_attach'; document.getElementById('id_attach').value='{$item.id}'; document.getElementById('mailbox_write').submit();">
											{$lang.button.delete}
										</a>
									</span>
								</div>
							{/foreach}
							</div>
						</div>
					</dl>
					{/if}
				</div>
			<!-- begin message form -->
			<div class="mailbox_send message_form">
				{$redactor}
				<textarea id="redactor" name="body" class="redactor_wide">{*$data.body*}</textarea>
				<div id="bad_div_redactor" class="bad_div_left"></div>

				<div class="send_btn">
					<input type="button" class="index_btn" onclick="if (CheckMail()) document.mailbox_write.submit(); else alert('{$lang.err.invalid_feilds|escape:javascript}');" value="{$lang.button.send}">
				</div>
			</div>
			<!-- end message form -->
		</form>
		{/strip}
				
		{if $data.reply}
		<div class="message_form">
			<div class="div_comment">
				<div class="float_left"><b>{$lang.mailbox.received_email}:</b>&nbsp;{$data.subject_msg_last}</div>
				<div class="float_right">{$data.date_msg_last}</div>
			</div>			
			<div>{$data.text_msg_last}</div>	
		</div>		
		{/if}
	</div>  	
		{literal}
		<script language="JavaScript" type="text/javascript">
			function SearchVariants(par, event){
				$('#variants').html('');
				var flag = 0;
				if (par == '') {$('#mail_to_arr').val('');return;}
				arr_user = par.split(';');
				var text = arr_user[arr_user.length-1];
				if (event.which == 8) {DeleteId();}
				if (event.which == 59 && arr_user.length>1) {text = arr_user[arr_user.length-2];flag=1;}
				if (event.which == 59 && arr_user.length==1) {text = arr_user[arr_user.length-1];flag=1;}
				if (arr_user.length == 1 && arr_user[arr_user.length-1] == '') {$('#mail_to_arr').val('');}
				if (arr_user.length==1){$('#mail_to_arr').val('');}
				$.post("mailbox.php?ajax=1",{async:false, sel:'search_variants',text:text, flag:flag}, function(data){
					$('#variants').html('');
					if (!data) {
						$('#variants').hide();
						if (flag == 1) DeleteName(0);
					} else {
						$('#variants').show();
						resp = getJQData(data);
						if (flag == 1){
							Select(resp[0]['id'],1)
						} else {
							for (i=0;i<resp.length;i++) {
								$('#variants').append("<div style='padding:5px;cursor:pointer;' onclick='Select("+resp[i]['id']+",0);'><div id='to_"+resp[i]['id']+"' class='float_left' style='width:190px;overflow:hidden;padding-top:25px;'>"+resp[i]['name']+"<\/div><div class='imgcanvas_container'><img class='imgcanvas' src='"+resp[i]['icon_path']+"'><\/div><\/div>");
							}
						}
					}
				});
			}
			function Select(par, flag){
				var to_user = $('#mail_to').val();
				var arr_user = to_user.split(';');
				var str_val = '';
				if ($('#mail_to_arr').val() != '') {
					$('#mail_to_arr').val($('#mail_to_arr').val()+','+par);
				} else {
					$('#mail_to_arr').val(par);
				}
				if (flag == 1){$('#variants').hide();DeleteId();return ;}
				for (i=0;i<arr_user.length-1;i++) {
					str_val = str_val+arr_user[i]+';';
				}
				str_val = str_val + $('#to_'+par).text()+';';
				$('#mail_to').val(str_val);
				$('#variants').hide();
			}
			function DeleteId(){
				var to_user = $('#mail_to').val();
				var arr_user = to_user.split(';');
				var str_val = '';
				
				var to_user_id = $('#mail_to_arr').val();
				var arr_user_id = to_user_id.split(',');
				var str_val_to_id = '';
				
				if (arr_user_id.length != arr_user.length || arr_user[arr_user.length-1] == '') {
					for (i=0;i<arr_user.length-1;i++) {
						if (str_val_to_id == '') str_val_to_id = arr_user_id[i];
						else str_val_to_id = str_val_to_id+','+arr_user_id[i];
					}
					$('#mail_to_arr').val(str_val_to_id);
				}
			}
			function DeleteName(par){
				var to_user = $('#mail_to').val();
				var arr_user = to_user.split(';');
				var str_val = '';
				if (par == 1){
					for (i=0;i<arr_user.length-1;i++) {
						str_val = str_val+arr_user[i]+';';
					}
				} else {
					for (i=0;i<arr_user.length-2;i++) {
						str_val = str_val+arr_user[i]+';';
					}
				}
				$('#mail_to').val(str_val);
				$('#variants').hide();
				DeleteId();
			}
			function CheckName(){
				par = $('#mail_to').val();
				arr_user = par.split(';');
				var text = arr_user[arr_user.length-1];
				if (text == '') return;
				$.post("mailbox.php?ajax=1",{async:false, sel:'search_variants',text:text, flag:1}, function(data){
					$('#variants').html('');
					if (!data) {
						DeleteName(1);
					} else {
						resp = getJQData(data);
						if ($('#mail_to_arr').val() != '')
							$('#mail_to_arr').val($('#mail_to_arr').val()+','+resp[0]['id']);
						else 
							$('#mail_to_arr').val(resp[0]['id']);
					}
				});
				$('#variants').hide();
			}

			function CheckMail() {
				CheckName();
				var result = true;
				if (document.getElementById('mail_to') == undefined || document.getElementById('mail_to').value == ''){
				   result = false;
				}
				if (document.getElementById('mail_subject') == undefined || document.getElementById('mail_subject').value == ''){
				   result = false;
				}
				return result;
			}
			{/literal}{if $write_limit}{literal}
				launchWindow(SITE_ROOT+"/backend.php",'sel=mailbox_write_limit',400,110,1);
			{/literal}{/if}{literal}
		</script>
		{/literal}
		{* {if $banner.right}<div style="float: left;">{$banner.right}</div>{/if}	*}
							  	
{include file="$gentemplates/$footer_tpl"}