function checkSendForm(){}
checkSendForm.prototype = {
	init: function(){
		var self = this;
		this.checkMessValue = false;
		this.checkMailValue = false;
		
		this.chekingForm(self);
	},
	initCheckContainerPosition: function(){
		$('.checkSendForm').attr({style:'left:'+($(window).width()/2 - $('.checkSendForm').width()/2)+'px;top:'+($(window).height()/2 - $('.checkSendForm').height()/2+$(window).scrollTop())+'px;'});
	},
	chekingForm: function(self){
		var validEmailRegExp = /\b[-0-9a-zA-Z_.]*?\@[-0-9a-zA-Z_]*?\.[-0-9a-zA-Z_]{2,4}\b/;		
        var findHtmlTagsRegExp = /([\<])([^\>]{1,})*([\>])/i;
		
		// Проверка, был ли текст от робота
		function textFromBot(checkText)
		{
			var result = false;
			
			//alert("messFromBot __ checkText = " + checkText);
			if(findHtmlTagsRegExp.test($('#mess').val()))
			{
				result = true;
				//alert("result = " + result);
			}
			
			return result;
		}
		
		// Проверка, нужно ли показывать сообщение для робота
		function checkIsNeedShowMessageForBot()
		{
			//alert("checkIsNeedShowMessageForBot");
			//alert($('#bot-question-checkbox').css("display"));
			
			// Если текст от робота, то показываем сообщение о том, что нужно выделить галочку
			if(textFromBot($('#mess').val()))
			{
				$('#bot-question-checkbox').css("display", "table-row");
				$('#bot-question-img').css("display", "table-row");
				
			// Если сообщение не от робота, то не показываем сообщение о галочке
			}else
			{
				$('#bot-question-checkbox').css("display", "none");
				$('#bot-question-img').css("display", "none");
			}
		}
		
		$('#emailForm').submit(function(e){
			e.preventDefault();
			self.initCheckContainerPosition();
			if ($('#email').val() == ''){
				
				$('#email-check-null').fadeIn(500);
				setTimeout(function(){$('#email-check-null').fadeOut(500)},4000);
				$('#email').focus();
				
				self.checkMailValue = false;
			}
			else if (!(validEmailRegExp.test($('#email').val()))){
				
				$('#email-check-wrong').fadeIn(500);
				setTimeout(function(){$('#email-check-wrong').fadeOut(500)},4000);
				$('#email').focus();
				
				self.checkMailValue = false;
			}
			else if ($('#mess').val() == ''){
				
				$('#mess-check-null').fadeIn(500);
				setTimeout(function(){$('#mess-check-null').fadeOut(500)},4000);
				$('#mess').focus();
				
				self.checkMessValue = false;
			}
			// Проверка корректности e-mail сообщения
			//else if(findHtmlTagsRegExp.test($('#mess').val()) && !($('#bot-question').attr('checked'))){
			else if(textFromBot($('#mess').val()) && !($('#bot_question').attr('checked'))){
				
				checkIsNeedShowMessageForBot();
				
				$('#bot-question-pop-up').fadeIn(500);
				setTimeout(function(){$('#bot-question-pop-up').fadeOut(500)},4000);
				$('#mess').focus();
				
				self.checkMessValue = false;
			}
			else {
				self.checkMailValue = true;
				self.checkMessValue = true;
			}
			
			if (self.checkMessValue && self.checkMailValue) {
				
				var dataStr = $('#email').attr('name')+'='+$('#email').val()+'&'+$('#name').attr('name')+'='+$('#name').val()+'&'+$('#mess').attr('name')+'='+$('#mess').val();
				// Отправка информации о 
				dataStr = dataStr + '&' + $('#bot_question').attr('name') + '=' + $('#bot_question').attr('checked');
				
				$.ajax({
					type: this.method,
					data: dataStr,
					url: this.action,
					complete: function(){
						$('#successfull-send').fadeIn(500);
						setTimeout(function(){$('#successfull-send').fadeOut(500)},4000);
						
						$('#mess').val('');
					}, 
					error: function() {} 
				});
			}
		});
		
		
		// Слушатель изменения текста в сообщении
		$("#mess").change(function() {
			//alert("Mess change!");
			checkIsNeedShowMessageForBot();
		});
		//var checkTimer = setInterval(function(){checkIsNeedShowMessageForBot()}, 2000);
		checkIsNeedShowMessageForBot();
		$('#bot_question').attr('checked', false);
	}
	
}
$(document).ready(function(){
	new checkSendForm().init();
})
