function email_validate(email) 
{
  var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
  return regex.test(email);
}
var author;
var email;
var content;
var ret;
$(document).ready(function()
{
	SyntaxHighlighter.all();
	function checkForm() {
		ret = '<div class="error_box"><ul>';
		author  = $('#fAuthor').attr('value');
		content = $('#fContent').val();
		if(author == '' || content == '')
		{
			if (author == '')
				ret = ret+'<li>Nie podałeś swojego nicku!</li>';
			if (content == '')
				ret = ret+'<li>Nie podałeś treści komentarza!</li>';
			ret = ret+'</ul></div>';
			return false;
		}
		return true;
	}

	$('#form_comments').submit(function()
	{
		if(!checkForm())
		{
			$('#comment_before').html(ret);
			return false;
		} else {
			$('#comment_add').hide('slow');
			$('#comment_new').hide();
			ajax();
			return false;
		}
	}
	);
	function ajax() 
	{
	$.ajax({
			type: 'POST',
			url:  'includes/ajax_class_comment_add.php',
			data: "author="+author+"&content="+content+"&id="+self.location.href,
			//jezeli wszystko jest ok
			success: function(msg) {
				$('#comment_before').html('');
				$('#comment_after').append(msg);
				var ret2 = '<strong>'+author+'</strong> napisał(a):<br/>';
				ret2 = ret2+'<span style="font-size: x-small;">Dnia: Dzisiaj, przed sekundą</span>';
				ret2 = ret2+'<p style="margin:1px;">'+content+'</p>';
				ret2 = ret2+'<hr />';
				$('#comment_new').html(ret2);
				$('#comment_new').show('slow');
				var amount = parseInt($('#comment_amount').html());
				$('#comment_amount').html(amount+1);
			},
			// wystąpiły błędy
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				$('#comment_after').append('Błąd podczas komunikacji z serwerem. Spróbuj ponownie później.<br />');
			}
		});
	}
	
	function checkFormContact() {
		ret = '<div class="error_box"><ul>';
		author  = $('#fAuthor').attr('value');
		email   = $('#fEmail').attr('value');
		content = $('#fContent').val();

		if(author == '' || content == '' || email == '' || !email_validate(email))
		{
			if (author == '')
				ret = ret+'<li>Nie podałeś swojego nicku!</li>';
			if (content == '')
				ret = ret+'<li>Nie podałeś treści komentarza!</li>';
			if (email == '')
				ret = ret+'<li>Nie podałeś swojego adresu email</li>';
			else if (!email_validate(email))
				ret = ret+'<li>Nie podałeś prawidłowego adresu email</li>';
			ret = ret+'</ul></div>';
			return false;
		}
		return true;
	}
	
	$('#form_contact').submit(function()
	{
		$('#contact').hide('slow');
		$('#comment_new').hide('slow');
		if(!checkFormContact())
		{
			$('#contact_before').html(ret);
			return false;
		} else {
			$('#contact_before').html('');
			$('#contact_div').hide('slow');
			contact_ajax();
			return false;
		}
	}
	);
	function contact_ajax() 
	{
	$.ajax({
			type: 'POST',
			url:  'includes/ajax_class_mail_send.php',
			data: "author="+author+"&content="+content+"&email="+email,
			//jezeli wszystko jest ok
			success: function(msg) {
				$('#contact_after').append(msg);
			},
			// wystąpiły błędy
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				$('#contact_after').append('Błąd podczas komunikacji z serwerem. Spróbuj ponownie później.<br />');
			}
		});
	}
}
);
