//
// round comments
//
function load_comments(round_id) {
	regenerate_captcha();
	$.ajax({
		type: "POST",
		url: "index.php",
		data: "action=rlc&id=" + round_id,
		success: function(html){
			$('#box_comments').html(html);
		}
	 });		
}

function write_comment() {
	$('#box_write_comments').slideDown();
	$('#lnk_write_comment').hide();
	document.getElementById('comments').focus();
}

function delete_comment(id) {
	if ( confirm('Tem certeza?') ) {
		$.ajax({
			type: "POST",
			url: "index.php",
			data: "action=rcd&id=" + id,
			success: function(r){
				$('#box_comments').html('Atualizando comentários');

				load_comments(round_id);

				$('#box_comments_message').html('<b>'+r+'</b>');
				$('#box_comments_message').show();
				setTimeout("$('#box_comments_message').html('')",5000);
				
				$('#box_write_comments').hide();
				$('#lnk_write_comment').show();
				document.getElementById('form_comment').reset();
			}
		 });
	}
}


function send_comments(fname) {
	form = document.getElementById(fname);

	action = form.action.value;
	id = form.id.value;

	anonymous = '';
	if ( typeof(form.anonymous) != 'undefined' ) {
		if ( form.anonymous.checked ) {
			anonymous = form.anonymous.value;
		}
	}
	
	comments = form.comments.value;
	
	// verify captcha
	captcha = form.captcha.value;
	captcha_ok = false;
	$.ajax({
		type: "POST",
		async: false,
		url: "index.php",
		data: "action=vcapt&captcha=" + captcha,
		success: function(r){
			captcha_ok = ( r == 1);
		}
	 });		


	if ( ! captcha_ok ) {
		regenerate_captcha();
		alert('Por favor digite o código corretamente.');
		form.captcha.value = "";
		form.captcha.focus();
		return false;
	}

	data = "action=" + action;
	data+= "&id=" + id;
	data+= "&anonymous=" + anonymous;
	data+= "&comments=" + comments;
	data+= "&captcha=" + captcha;
	
	$.ajax({
		type: "POST",
		url: "index.php",
		data: data,
		success: function(r){
			$('#box_comments').html('Atualizando comentários');

			load_comments(round_id);

			$('#box_comments_message').html('<b>'+r+'</b>');
			$('#box_comments_message').show();
			setTimeout("$('#box_comments_message').html('')",5000);
			
			$('#box_write_comments').hide();
			$('#lnk_write_comment').show();
			document.getElementById('form_comment').reset();
		}
	 });		
}


function regenerate_captcha() {
	n = parseInt( Math.random() * 1000);
	document.getElementById('icaptcha').src = "./func/captcha.php?" + n;
}


