$(document).ready(function() 
{
	$("#reactiesform").validate();
});

// Afvangen als het formulier verzonden wordt
$.validator.setDefaults({
	submitHandler: function() {
		$.post(
			"templates/pages/reacties/ajax/checkcaptcha.php",
			{ captcha: $('#controle').val() },
			function(data){
				if( data.captcha == "true" )
				{
					submitReactie();
				}
				else
				{
					$("#captcha_error").css( "display", "block" );
				}
			},
			"json"
		);
	}
});

function ongewenst( p_iReactieId, p_sTabel )
{
	$.post(
		"templates/pages/reacties/ajax/ongewenst.php",
		{ reactieId: p_iReactieId, tabel: p_sTabel },
		function(data){
			reactie = $('#reactie_' + p_iReactieId );
			
			if( data.affected_rows == 0 )
			{
				reactie.html( "Uw melding is al geregistreerd" );
			}
			else
			{
				reactie.html( "Bedankt voor uw melding" );
			}
			
			reactie.slideDown("normal");
		},
		"json"
	);
}

function submitReactie()
{
	$.post(
		"templates/pages/reacties/ajax/save_reactie.php",
		{
			naam: $('#reactieformnaam').val(),
			reactie: $('#reactieformopmerking').val(),
			item_id: $('#item_id').val(),
			tabel: $('#tabel').val(),
			foreignkey: $('#foreignkey').val()
		},
		function(data){
			$('#reacties').html( data.reactie_template );
			$('#reactie_formulier').html( "<p class=\"bedankt\">Bedankt voor uw reactie.</p>" );
		},
		"json"
	);
}