<form method="POST" id="search_form">
	<div class="input-group input-group-md">
		<?php if(isset($_POST["search"]) AND $_POST["query"] != ''){?><span class="input-group-addon hidden-xs">Результаты по запросу:</span><?php } ?>
		<input type="text" class="form-control" name="query" placeholder="Введите поисковый запрос" value="<?php echo (isset($_POST["query"]) && !isset($_POST["reset"])) ? $_POST["query"] : null; ?>" autocomplete="off">
		<div class="input-group-btn">
			<button class="btn btn-default" type="submit" name="search">Поиск</button>
			<?php if(isset($_POST["search"]) AND $_POST["query"] != ''){?><button title="Сбросить" class="btn btn-default" type="submit" name="reset"><i class="fa fa-fw fa-times" aria-hidden="true" onclick="$('search_form').reset();"></i></button><?php } ?>
		</div>
	</div>
</form>
<div id="ajax-search-result"></div>

<script>
$(document).ready(function(){
	$('input[name="query"]').on('keyup', function(){
		var $this = $(this);
		var delay = 500;
		clearTimeout($this.data('timer'));
		$this.data('timer', setTimeout(function(){
			$this.removeData('timer');
			module = "<?php echo $aliases_arr[1];?>";
			query = $this.val();
			ajax_search(module,query);
		}, delay));
	});
});
function ajax_search(mod,q){
	$.ajax({
		type: "post",
		url: "<?php echo CRMLINK; ?>functions/ajax/func-ajax-search.php",
		data: {
			module:	mod,
			login:	"<?php echo $LoginCheck; ?>",
			query:	q
		},
		beforeSend: function(){
			$('button[name="search"]').html('<i class="fa fa-cog fa-spin fa-fw" aria-hidden="true"></i>');
		},
		success: function(response) {
			$('#ajax-search-result').html(response);
		},
		error: function(xhr) {
			$('#ajax-search-result').html('ОШИБКА');
		},
		complete: function() {
			$('button[name="search"]').html('Поиск');
		}
	});
}
</script>
