
function replace(expr,a,b) {
	var i = 0;
	while (i != -1) {
		i=expr.indexOf(a,i);
		if (i>=0) {
			expr = expr.substring(0,i)+b+expr.substring(i+a.length);
			i += b.length;
		}
	}
	return expr;
}

function postString(){
	var post = $('comment_post').getProperty('value');
	var name = $('comment_name').getProperty('value');
	var email = $('comment_email').getProperty('value');
	var url = $('comment_url').getProperty('value');
	var content = $('comment_content').getProperty('value');
	if (name != '' && content != ''){
		return 'post='+post+'&name='+replace(name,'&','%26')+'&email='+replace(email,'&','%26')+'&url='+replace(url,'&','%26')+'&content='+replace(content,'&','%26');
	}else{
		return false;
	}
}

window.addEvent('domready', function(){
	
	$('confirm_buttons').setStyle('visibility', 'hidden');
	$('confirm_buttons').setStyle('height', '0');
	
	$('comment_post_button').addEvent('click', function(){
		var queryString = postString();
		if (queryString){
			$('comment_form_container').setStyle('visibility', 'hidden');
			$('confirm_buttons').setStyle('visibility', 'visible');
			$('confirm_buttons').setStyle('height', '30px');
			var request = new Ajax('ajax_blog_preview.php', { method : 'post', update : 'comment_preview', data : queryString }).request();
		}else{
			$('comment_preview').setHTML('<p style="background-color:#ca2121;color:#fff;padding:3px;">The fields \'name\' and \'content\' must be completed to send a comment</p>');
		}
	});
	
	$('comment_confirm_back').addEvent('click', function(){
		$('confirm_buttons').setStyle('visibility', 'hidden');
		$('confirm_buttons').setStyle('height', '0');
		$('comment_form_container').setStyle('visibility', 'visible');
		$('comment_preview').setText('');
	});
	
	$('comment_confirm_post').addEvent('click', function(){
		var queryString = postString();
		var request = new Ajax('ajax_blog_confirm.php', { method : 'post', data : queryString }).request();
		request.onSuccess = function(){
			window.location.reload(true);
		};
	});
	
});