$(function() {
	$("#flash").hide();

	$("form#knjiga-gostiju").submit(function() {

		var ime = $("#ime").val();
		var email = $("#email").val();
		var komentar = $("#komentar").val();
		var dataString = 'ime='+ ime + '&email=' + email + '&komentar=' + komentar;	
		
		var protectwebformcode = $("#protectwebformcode").val();
		var dataString2 = 'protectwebformcode='+ protectwebformcode;
		
		
		function unesi_podatke()
		{			
			//$("#flash").show();
			$("#flash").fadeIn(400).html('<img src="img/load.gif" align="absmiddle">');
			$.ajax({
					type: "POST",
				  url: "./include/commentajax.php",
				   data: dataString,
				  cache: false,
				  success: function(html){				 
					  $("#komentari").prepend(html);
					  document.getElementById('email').value='';
					  document.getElementById('ime').value='';
					  document.getElementById('komentar').value='';
					  $("#ime").focus();					 
					  $("#flash").hide();				
				  }
			 });
		}
		
		if(ime=='' || email=='' || komentar=='' || protectwebformcode=='')
		{
			alert('Morate ispuniti sva polja!');
		}
		else
		{
			$.ajax({
				type : 'POST',
				url : './include/nospam.php',
				data: dataString2,
				success : function(html){
					if(html.length > 0)
					{
						alert('Unijeli ste krivi kod. Pokusajte ponovo!');
					}
					else 
					{
						unesi_podatke();
					}								
					document.getElementById('protectwebformcode').value='';
				}			
			});	
		}
		
		return false;
		
	});
});
