<div class="col-lg-9 col-md-8 col-sm-12">
	<form method="POST" name="form_letters" id="formedit" enctype="multipart/form-data">
		<div class="row" style="margin-top:15px;">
			<div class="col-lg-8 col-xs-8">
				<h1><a href="javascript: ;" onclick="history.go(-2)"><i class="fa fa-fw fa-angle-double-left" aria-hidden="true" title="Вернуться к списку"></i></a> 
				<?php if(isset($_GET['new'])) {?>Новое письмо<?php } ?>
				</h1>
			</div>
			<div class="col-lg-4 col-xs-4">
				<div class="btn-group" style="float:right;">
					<?php if(isset($_GET['new'])) {?>
					<button type="submit" name="LetterSend" class="btn btn-md btn-info" style="color:#fff;"><span class="hidden-lg hidden-md hidden-sm"><i class="fa fa-fw fa-paper-plane" aria-hidden="true"></i></span><span class="hidden-xs text">Отправить</span></button>
					<?php } ?>
				</div>
			</div>	
		</div>
		
		<div class="row" style="margin-top:15px;">
			<div class="form-group col-lg-8">
				<div class="input-group template">
					<span class="input-group-addon" title="Тема"><i class="fa fa-fw fa-font" aria-hidden="true"></i></span>
					<input type="text" class="form-control" id="inputSubject" name="inputSubject" value="" placeholder="Тема" maxlength="150">
				</div>
			</div>
			<div class="form-group col-lg-4">
				<div class="input-group">
					<span class="input-group-addon" title="Важность письма"><i class="fa fa-fw fa-exclamation-triangle" aria-hidden="true"></i></span>
					<select class="form-control" name="selectPriority">
						<option value="none">Обычное письмо</option>
						<option value="high">Важное письмо</option>
					</select>
				</div>
			</div>
			<div class="form-group col-lg-6">
				<div class="input-group">
					<span class="input-group-addon" title="Кому"><i class="fa fa-fw fa-user" aria-hidden="true"></i></span>
					<select class="form-control" name="selectUserTo" id="selectUserTo" required>
						<?
							$dom = new DOMDocument;
							$dom->load(ROOT . '/crmdata/database/users.xml');
							$xpath = new DOMXPath($dom);
							$users = $xpath->query('/users/user');
							foreach ($users as $user) {
								$UserSurName 	= $user->getElementsByTagName("usersurname")->item(0)->nodeValue;
								$UserName 		= $user->getElementsByTagName("username")->item(0)->nodeValue;
								$UserID 		= $user->getAttribute('id');
								$UserLogin 		= $user->getAttribute('login');
								if($UserID != $_SESSION["userid"]){
									echo '<option value="'.$UserID.'" login="'.$UserLogin.'">'.$UserName.' '.$UserSurName.'</option>';
								}
							}
						?>
					</select>
				</div>
			</div>
			<div class="form-group col-lg-6">
				<div class="input-group">
					<span class="input-group-addon" title="Копия"><i class="fa fa-fw fa-user-o" aria-hidden="true"></i></span>
					<select class="form-control" name="selectUserCoppy" id="selectUserCoppy">
						<option value="">--- пусто ---</option>
						<?
							$dom = new DOMDocument;
							$dom->load(ROOT . '/crmdata/database/users.xml');
							$xpath = new DOMXPath($dom);
							$users = $xpath->query('/users/user');
							foreach ($users as $user) {
								$UserSurName 	= $user->getElementsByTagName("usersurname")->item(0)->nodeValue;
								$UserName 		= $user->getElementsByTagName("username")->item(0)->nodeValue;
								$UserID 		= $user->getAttribute('id');
								$UserLogin 		= $user->getAttribute('login');
								if($UserID != $_SESSION["userid"]){
									echo '<option value="'.$UserID.'" login="'.$UserLogin.'">'.$UserName.' '.$UserSurName.'</option>';
								}
							}
						?>
					</select>
				</div>
			</div>
			<div class="form-group col-lg-12 editor-form">
				<div class="input-group" style="width: 100%!important;">
					<textarea class="form-control" name="inputComment" id="inputComment" rows="10" placeholder="Текст сообщения"></textarea>
				</div>
			</div>
			<div class="form-group col-lg-8">
				<div class="input-group">
					<span class="input-group-addon" title="Метки"><i class="fa fa-fw fa-tags" aria-hidden="true"></i></span>
					<input type="text" class="form-control" name="inputTags" value="" placeholder="Метки" maxlength="150">
				</div>
			</div>
			<div class="form-group col-lg-4">
				<input id="NewTempl" type="checkbox" value="yes" name="NewTempl">
				<label for="NewTempl" style="margin: 4px 0px;">Сохранить как шаблон</label>
			</div>
		</div>
		<h3 style="margin-top:15px;">Прикрепить к письму:</h3>
		<div class="row">
			<div class="form-group col-lg-3">
				<div class="input-group">
					<span class="input-group-addon" title="Прикрепить задачу"><i class="fa fa-fw fa-check" aria-hidden="true"></i></span>
					<select class="form-control" name="selectTask" id="selectTask">
						<option value="">--- пусто ---</option>
						<?
							$dom_t = new DOMDocument;
							$dom_t ->load($baseT);
							$xpath_t = new DOMXPath($dom_t);
							$l_tasks = $xpath_t->query('/tasks/task[@status="inwork"]');
							foreach ($l_tasks as $task) {
								$taskName 	= $task->getElementsByTagName("taskname")->item(0)->nodeValue;
								$taskID 	= $task->getAttribute('id');
								echo '<option value="'.$taskID.'">'.$taskName.'</option>';
							}
						?>
					</select>
				</div>
			</div>
			<div class="form-group col-lg-3">
				<div class="input-group">
					<span class="input-group-addon" title="Прикрепить клиента"><i class="fa fa-fw fa-address-book" aria-hidden="true"></i></span>
					<select class="form-control" name="selectClient" id="selectClient">
						<option value="">--- пусто ---</option>
						<?
							$dom_c = new DOMDocument;
							$dom_c ->load($baseC);
							$xpath_c = new DOMXPath($dom_c);
							$l_clients = $xpath_c->query('/clients/client');
							foreach ($l_clients as $client) {
								$ClientName 	= $client->getElementsByTagName("clientname")->item(0)->nodeValue;
								$ClientId 		= $client->getAttribute('id');
								echo '<option value="'.$ClientId.'">'.$ClientName.'</option>';
							}
						?>
					</select>
				</div>
			</div>
			<div class="form-group col-lg-3">
				<div class="input-group">
					<span class="input-group-addon" title="Прикрепить сделку"><i class="fa fa-fw fa-briefcase" aria-hidden="true"></i></span>
					<select class="form-control" name="selectDeal" id="selectDeal" <?php if ($GroupCheck == 'user') { echo 'disabled'; } ?> >
						<?
						$dom_d = new DOMDocument;
						$dom_d->load($baseD);
						$xpath_d = new DOMXPath($dom_d);
						$deals_a = $xpath_d->query('/deals/deal[@type="deal-a"]');
						$deals_o = $xpath_d->query('/deals/deal[@type="offer"]');
						?>
						<option value="">--- пусто ---</option>
						<optgroup label="Активные сделки">
						<?
						foreach ($deals_a as $deal) {
							$DealId 		= $deal->getAttribute('id');
							$DealNumber 	= $deal->getElementsByTagName("num")->item(0)->nodeValue;
							$DealSubject 	= $deal->getElementsByTagName("subject")->item(0)->nodeValue;
							$DealClient 	= $deal->getAttribute('client');
							$DealType 		= $deal->getAttribute('type');
							echo '<option value="'.$DealId.'">' . $nCRMdeals . ' '.$DealNumber.' - '.get_client_name($DealClient).' - "'.$DealSubject.'"</option>';
						}
						?>
						</optgroup> 
						<optgroup label="Предложения">
						<?
						foreach ($deals_o as $deal) {
							$DealId 		= $deal->getAttribute('id');
							$DealNumber 	= $deal->getElementsByTagName("num")->item(0)->nodeValue;
							$DealSubject 	= $deal->getElementsByTagName("subject")->item(0)->nodeValue;
							$DealClient 	= $deal->getAttribute('client');
							$DealType 		= $deal->getAttribute('type');
							echo '<option value="'.$DealId.'">' . $nCRMoffers . ' '.$DealNumber.' - '.get_client_name($DealClient).' - "'.$DealSubject.'"</option>';
						}
						?>
						</optgroup>
					</select>
				</div>
			</div>
			<div class="form-group col-lg-3">
				<div class="input-group">
					<span class="input-group-addon" title="Прикрепить событие"><i class="fa fa-fw fa-calendar-plus-o" aria-hidden="true"></i></span>
					<select class="form-control" name="selectEvent" id="selectEvent">
						<option value="">--- пусто ---</option>
						<?
							$dom_e = new DOMDocument;
							$dom_e ->load($baseE);
							$xpath_e = new DOMXPath($dom_e);
							$l_events = $xpath_e->query('/events/event');
							foreach ($l_events as $event) {
								$eventName 	= $event->getElementsByTagName("name")->item(0)->nodeValue;
								$eventID 	= $event->getAttribute('id');
								echo '<option value="'.$eventID.'">'.$eventName.'</option>';
							}
						?>
					</select>
				</div>
			</div>
		</div>
	</form>
</div>
<div class="col-lg-3 col-md-4 col-sm-12">
	<?php require_once(ROOT . 'templates/side-block.php'); ?>
</div>
<script type="text/javascript">
	function get_templ(id){
		$.ajax({
			type: "post",
			url: "/<? echo $folder; ?>/modules/templs/get_templ.php",
			data: {templid:id},
			beforeSend: function(){},
			success: function(html) {
                var result = JSON.parse(html);
				if(result.status == "ok"){
					$("#inputComment, .nicEdit-main").html(result.templ);
					$("#inputSubject").val(result.name);
				} else {}
			},
			error: function(xhr) {$(".page_block .title").append(' <i class="fa-exclamation-circle clr_high"></i>');},
			complete: function() {}
		});
	}
	function del_templ(id){
		$.ajax({
			type: "post",
			url: "/<? echo $folder; ?>/modules/templs/del_templ.php",
			data: {templid:id},
			beforeSend: function(){},
			success: function(html) {
                var result = JSON.parse(html);
				if(result.status == "ok"){$("#templ_" + id).fadeOut();}
			},
			error: function(xhr) {$(".page_block .title").append(' <i class="fa-exclamation-circle clr_high"></i>');},
			complete: function() {}
		});
	}
	$(document).ready(function() {
		function sorted(id){
			for(var c=document.getElementById(id),b=c.options,a=0;a<b.length;)
			if(b[a+1]&&b[a].text>b[a+1].text){c.insertBefore(b[a+1],b[a]);a=a>=1?a-1:a+1}else a++;
			b[0].selected=true; 
		};

		sorted("selectUserTo");
		sorted("selectUserCoppy");
		sorted("selectTask");
		sorted("selectClient");
		sorted("selectEvent");

		<?php if($_GET['user'] != '') { ?>
		$('#selectUserTo option[login="<?php echo $_GET['user']; ?>"]').prop('selected', true);
		<?php } ?>
	});
</script>
