function progress()
{
	var button = document.getElementById('addCommentButton');
	button.innerHTML = '<img src="/i/progressSetup.gif" class="progressSetup" />';	
}


function add_comment(postId, user)
{	
	var comment = $F('tresc').replace(/^\s+|\s+$/g, '');
			
	if(comment != '')
	{
	
		
		if (!user)
		{
			
			var name = $F('name').replace(/^\s+|\s+$/g, '');
			var email = $F('email').replace(/^\s+|\s+$/g, '');
			
			if (name == '')
			{
				showAlert('Nowy komentarz', 'Musisz wpisać swoje imię!');
				return false;
			}
			
			if (email == '')
			{
				showAlert('Nowy komentarz', 'Musisz wpisać swój adres e-mail!');
				return false;
			}
		}
		
		if (user)
		{
			new Ajax.Request(siteurl + 'blog/ajaxAddComment',
		  	{
				asynchronous: true,
		   		method: 'post',
		   		postBody: 
		   			"id=" + postId + 
		   			"&comment=" + encodeURIComponent(comment),
		   		onComplete: function(obj) { 
		   												
		   			add_comment_complete(obj.responseText); 
		   			$('addCommentButton').innerHTML = '<input class="button" type="button" name="add-comment" onclick="return add_comment(' + postId + ', true);" value="Dodaj"  />';
		   		},
		   		onLoading: progress() 
		   	});	 
		} else
		{
			
			var name = $F('name').replace(/^\s+|\s+$/g, '');
			var email = $F('email').replace(/^\s+|\s+$/g, '');		
			var captcha = $F('captcha');
			
			new Ajax.Request(siteurl + 'blog/ajaxAddComment',
		  	{
				asynchronous: true,
		   		method: 'post',
		   		postBody: 
		   			"id=" + postId + 
		   			"&comment=" + encodeURIComponent(comment) +
		   			"&email=" + encodeURIComponent(email) +
		   			"&name=" + encodeURIComponent(name) +
		   			"&captcha=" + encodeURIComponent(captcha),
		   		onComplete: function(obj) { 
		   			
		   			add_comment_complete(obj.responseText); 

					if ($('captcha'))
					{
						$('captcha').value = '';		
						$('captcha_image').src = '/utils/captchaImage.php';					
					}	
		   			
		   			$('addCommentButton').innerHTML = '<input class="button" type="button" name="add-comment" onclick="return add_comment(' + postId + ', false);" value="Dodaj"  />';
		   		},
		   		onLoading: progress() 	
		   	});	 
		}
   	}
   	else
   	{
		showAlert('Nowy komentarz', 'Musisz wpisać treść komentarza!');
	}
	return false;
}

function add_comment_complete(output)
{
	
	if(output.indexOf('ERROR') != -1)
	{		
		showAlert('Nowy komentarz', 'Wystąpił nieoczekiwany błąd. Przepraszamy i prosimy spróbować później.');
		return;
	} else if(output.indexOf('captchaError') != -1)
	{		
		showAlert('Nowy komentarz', 'Kod niepoprawny.');
		return;
	} else if(output.indexOf('mailError') != -1)
	{
		showAlert('Nowy komentarz', 'To nie jest poprawny adres e-mail.');
		return;
	}
	else
	{				
		var r = output.split('#');
		var author = r[0];		
		var send_date = r[1];		
		var guid = r[2];
		
    	attach_comment($F('tresc').stripTags(), author, send_date, guid, r[3]);
    
		$('tresc').value = '';	
		
	}
}

function attach_comment(comment_text, author, send_date, guid, cc)
{
	comment_text = comment_text.replace(/^\s+|\s+$/g, '');	
	
	var com_div = document.createElement('div');
	com_div.className = 'comment';
	
	
	var authorInfo = document.createElement('div');
	
	if (guid != '[brak]')
	{
		authorInfo.innerHTML = '<a href="' + siteurl + 'profil/' + guid + '">' + author + '</a>, ' + send_date;
	} else
	{
		authorInfo.innerHTML = author + ', ' + send_date;
	}
					
	var content = document.createElement('p');
	
	
	
	content.innerHTML = comment_text;

	
	$('com_count').innerHTML = '(' + cc + ')';

	if (cc == 1)
	{
		$('coms_count').style.display = 'block';
	}
	
	
	com_div.appendChild(authorInfo);
	com_div.appendChild(content);
	com_div.style.display = 'none';
	
	write_root = $('com');		
	write_root.appendChild(com_div);
	
	
	new Effect.Appear(com_div, {duration: 0.5});	
}

