
	var mail_window = null;
	var scroll_loadin_interval = null;
	var dbody = null;
	
	window.addEvent('domready', function() {
		dbody = document.body;
	});

	
	
// --------------------------------------------------------------------------------	
	/**
	 * Показывает картинку загрузки в правом верхнем углу экрана
	 * @return
	 */
	function showLoadingPic(){
		if ($('body')){
			if ($('#loadingElement')){
				$('#loadingElement').remove();
				clearInterval(scroll_loadin_interval);
			}
			var loadingElement = jQuery('<div id = "loadingElement" style = "position: absolute;"></div>');
			$('body').add('#loadingElement');
			$(loadingElement).css({'top': $('body').scrollTop(), 'right': 0});
			$(loadingElement).html('<img src="/rs/images/loading.gif" />');
			scroll_loadin_interval = setInterval("scrollLoadingElement()", 100);
		}
	}
	
	function scrollLoadingElement(){
		$('#loadingElement').css({'top': $('body').scrollTop()});
	}
	
	function hideLoadingPic(){
		if ($('#loadingElement')){
			$('#loadingElement').remove();
		}
		clearInterval(scroll_loadin_interval);
	}
	
// --------------------------------------------------------------------------------	

	function onSendMessageClick(mail){
		
		var request = new Request({
			method: 'get',
			url:'/mailto',
			onRequest:function(){
				showLoadingPic();
			},
			onSuccess:function(responseText, responseXML){
				onSendMessageClickRequest(responseText);
				hideLoadingPic();
			}
		});
		request.send('mail=' + mail);
	}
	
	function onSendMessageClickRequest(responseText){

		mail_window = openModalWindowInfo('Отправить сообщение консультанту', responseText, 400, $('body'));
		
	}
	
	function onSendMessageSubmit(form){
		var name = $(form).attr('name').value;
		var mail = $(form).attr('mail').value;
		var message = $(form).attr('message').value;
		var reg_mail = /(\S+)@(\S+)\.(\S+)/;
		if (name == ''){
			alert('Введите Ваше имя!');
			$(form).attr('name').focus();
		}
		else if (mail == ''){
			alert('Введите Ваш E-Mail!');
			$(form).attr('mail').focus();
		}
		else if ( ! reg_mail.test(mail)){
			alert('Не правильно введен E-mail!');
			$(form).attr('mail').focus();
		}
		else if (message == ''){
			alert('Введите текст сообщения!');
			$(form).attr('message').focus();
		}
		else
		{
		    showLoadingPic();
			$.ajax({
			    'url': '/mailto/sendmail', 
				'type': 'post',
				'data': {
				    name: name,
					mail: mail,
					message: message
				},
				success: function (text){
					onSendMessageSubmitRequest(text);
					hideLoadingPic();
				}
			});
		}
		
	}
	
	function onSendMessageSubmitRequest(responseText){
		if (responseText == '1'){
		    $('form input, form textarea').each(function () {$(this).val('')});
			alert('Ваше сообщение отправлено.');
		}
		else{
			alert('Сообщение не отправлено!');
		}
	}
	
	function isValidEmail (email)
	{
		return (/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i).test(email);
	}
	
	$(function() {
		$('#search').click(function() {
			$('form').submit();
			return false;
		});
	});
	
