function getParam(theForm, iddoc)
{
	if (checkForm(theForm))
	{		
		$.ajax({
			method: "POST",
			url: "/back/comments/",
			data: {
				iddoc: iddoc,
				author: theForm.find('#form_author').val(),
				email: theForm.find('#form_email').val(),
				comment: theForm.find('#form_comments').val(),
				captcha: theForm.find('#txtCaptcha').val()
			},
			success: function(data){

				if(data=='1')
				{
					var formTable = $('.block-comments-form');

					theForm.after('<div class="block-comments-form__success">Ваше сообщение успешно отправлено!</div>');
					theForm.hide();
				}
				else
				{
					if(data=='12')
					{
						$('.block-comments-form__messages').text("Ваше сообщение уже было отправлено! \n Не надо отправлять его ещё раз!");
					}

					else
					{
						$('.block-comments-form__messages').text("Попробуйте ещё раз ввести число, напечатанное на картинке!");
					}
				}
				img = document.getElementById('imgCaptcha');
				img.src = '/back/comments/image/?' + Math.random();

			},
			error: function(data){

			}
		});
	 }
	 return false;
}


function checkForm(theForm)
{
	var email_filter = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.[a-zA-Z]{2,}$/;
	
	if (theForm.find('#form_author').val() == "")
	{
		$('.block-comments-form__messages').text("Введите своё имя!");
		return false;
	}
	if ((theForm.find('#form_email').val() != "") && (email_filter.test(theForm.find('#form_email').val()) != true))
	{ 
		$('.block-comments-form__messages').text("Вы ввели неверный e-mail адрес!");
		return false;
	}
	if (theForm.find('.block-comments-form__textarea').val() == "")
	{
		$('.block-comments-form__messages').text("Вы не написали комментарий!");
		return false;
	}
	if (theForm.find('.block-comments-form__input').val().length > 1500)
	{
		$('.block-comments-form__messages').text("Вы ввели сообщение, превышающее максимально допустимый размер\nПопробуйте сократить сообщение");
		return false;
	}
	return true;
}

$(document).ready(function(){
	
	$('.block-comments-form__submit').click(function(){
		var theForm = $(this).closest('form');
		getParam(theForm, DATA.docs[0]);
		return false;
	});
	
	var counterLimit = 0;
	
	$(".block-comments-form__textarea").keyup(function()
    {
		var limit = (Number)($(this).attr('maxlength'));
        var text = $(this).val();
        var length = text.length;
		var percent = length/limit*100;		
		
		if(length >= limit && $('.block-comments-form__counter span').text() == '0') {
			$('.block-comments-form__counter').stop();
			$('.block-comments-form__counter').animate({opacity: 0}, 150, function(){
				$('.block-comments-form__counter').css('opacity', '1');
			});
		} 
		
		$('.block-comments-form__counter span').html(limit - length);
		
		if(percent <= 90) {
			$('.block-comments-form__counter').hide();
			$('.block-comments-form__percent span').eq(1).css('width', '0');
			$('.block-comments-form__percent span').eq(0).animate(
			{
				"width": percent+'%',
			}, 1);
		} else {
			$('.block-comments-form__counter').show();
			$('.block-comments-form__percent span').eq(0).css('width', '90%');
			$('.block-comments-form__percent span').eq(1).animate(
			{
				"width": percent-90+'%',
			}, 1);
		}
		
        return false;
    });
	
	var b=document.cookie.split("; ");
	for(var i=0;i<b.length;i++){
		var c=b[i].split("=");
		if('PRUSER'==c[0]){
			$.ajax({
				async:true,
				dataType:"json",
				success:function (data, textStatus) {
				if(data.name){
					try{
						if($('.block-comments-form').length > 0){
							$('.block-comments .block-comments__heading').hide();
							$('.block-comments-form').prepend('<h2 class="block-comments__heading" style="display: block;">Ваш комментарий, '+data.name+'</h2>');
							
						} 
						$('.block-comments__enter-social-text, .block-comments-form__field').hide();
						$('#form_author').val(data.name);
						$('#form_email').val('social@social.ee');
						$('#comments_form_socials_div')[0].innerHTML='';
						}catch(e){};
						var socialLogin=true;
					}
				},
				type:"post",
				url:"/back/"
			});
		}
    }
    try{
        var protdomen = window.location.protocol + '//' + location.hostname;
        $('#comments_form_socials_div').html('<div class="block-comments__enter-social-text" id="comments_form_socials_div">Войдите через <a href="https://www.facebook.com/dialog/oauth?client_id=457410404372128&amp;redirect_uri='+protdomen+'/back/fb.php&amp;response_type=code" class="block-comments__enter-social-item block-comments__enter-social-item_fb" title="FaceBook">FaceBook</a><a href="https://oauth.vk.com/authorize?client_id=3924223&amp;redirect_uri=http://pravoslavie.ru/back/vk.php&amp;response_type=code" class="block-comments__enter-social-item block-comments__enter-social-item_vk" title="ВКонтакте">ВКонтакте</a><a href="https://oauth.yandex.ru/authorize?client_id=8d5f99b0402a45beb4c4f259e0308e8c&amp;redirect_uri=http://pravoslavie.ru/back/ya.php&amp;response_type=code" class="block-comments__enter-social-item block-comments__enter-social-item_ya" title="Яндекс">Яндекс</a><a href="https://connect.mail.ru/oauth/authorize?client_id=711377&amp;redirect_uri=http://pravoslavie.ru/back/mr.php&amp;response_type=code" class="block-comments__enter-social-item block-comments__enter-social-item_mr" title="Mail.Ru">Mail.Ru</a><a href="https://accounts.google.com/o/oauth2/auth?redirect_uri=http://www.pravoslavie.ru/back/gg.php&amp;client_id=821871932358.apps.googleusercontent.com&amp;scope=https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile&amp;response_type=code" class="block-comments__enter-social-item block-comments__enter-social-item_gl" title="Google">Google</a>или введите свои данные:<div class="clearfix"></div>');

		//$('#comments_form_socials_div').html('<div class="block-comments__enter-social-text" id="comments_form_socials_div">Войдите через <a href="https://www.facebook.com/dialog/oauth?client_id=457410404372128&amp;redirect_uri=http://pravoslavie.ru/back/fb.php&amp;response_type=code" class="block-comments__enter-social-item block-comments__enter-social-item_fb" title="FaceBook">FaceBook</a><a href="http://oauth.vk.com/authorize?client_id=3924223&amp;redirect_uri=http://pravoslavie.ru/back/vk.php&amp;response_type=code" class="block-comments__enter-social-item block-comments__enter-social-item_vk" title="ВКонтакте">ВКонтакте</a><a href="https://oauth.yandex.ru/authorize?client_id=8d5f99b0402a45beb4c4f259e0308e8c&amp;redirect_uri=http://pravoslavie.ru/back/ya.php&amp;response_type=code" class="block-comments__enter-social-item block-comments__enter-social-item_ya" title="Яндекс">Яндекс</a><a href="https://connect.mail.ru/oauth/authorize?client_id=711377&amp;redirect_uri=http://pravoslavie.ru/back/mr.php&amp;response_type=code" class="block-comments__enter-social-item block-comments__enter-social-item_mr" title="Mail.Ru">Mail.Ru</a><a href="https://accounts.google.com/o/oauth2/auth?redirect_uri=http://www.pravoslavie.ru/back/gg.php&amp;client_id=821871932358.apps.googleusercontent.com&amp;scope=https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile&amp;response_type=code" class="block-comments__enter-social-item block-comments__enter-social-item_gl" title="Google">Google</a>или введите свои данные:<div class="clearfix"></div>');
    }catch(e){}
    //
    Cookies.set('returl',document.location.href,{ domain: 'pravoslavie.ru' });
    Cookies.set('returl',document.location.href,{ domain: '.pravoslavie.ru' });
    Cookies.set('returl',document.location.href,{ domain: 'www.pravoslavie.ru' });
    //document.cookie='returl'+'='+document.location.href+"; path=\/ ";
	
});