$(function() {  
	$('.errors').hide(); 
	$("input#submit_btn").removeAttr("disabled");
	$("a#commenttoggle").click(function () {
	  $("span#ct").hide();
      $("div#commentform").slideDown("slow");
    });    
 	$('textarea#comment').val('');
	$("input#submit_btn").click(function() {  
	
		$('.errors').hide();
		var name = $("input#name").val();
  		if (name == "") {
			$("div#name_errors").css("top","-9px");
			$("div#name_errors").html('<span>Bitte geben Sie Ihren Namen an.</span>'); 
			$("div#name_errors").show();
			$("input#name").focus();
			return false;
		}
  		
		var email = $("input#email").val();
  		if (email == "") {
			$("div#email_errors").css("top","25px");
			$("div#email_errors").html('<span>Bitte geben Sie Ihre E-Mail-Adresse an.</span>'); 
			$("div#email_errors").show();
			$("input#email").focus();
			return false;
		}
  		
		var comment = $("textarea#comment").val();
		var uid = $("input#uid").val();
  		if (comment == "") {
			if (uid == "") {
				$("div#comment_errors").css("top","60px");
				$("div#comment_errors").html('<span>Vergessen Sie Ihren Kommentar nicht!</span>'); 
			} else {
				$("div#comment_errors").css("top","-9px");
				$("div#comment_errors").html('<span>Vergiss deinen Kommentar nicht! ;-)</span>'); 
			}
			$("div#comment_errors").show();
			$("textarea#comment").focus();
			return false;
		}
		
		var type = $("input#type").val();
		var id = $("input#id").val();
		
		$("input#submit_btn").attr("disabled", "disabled"); 
		$("div#invalid_errors").html('<span><img src="/img/ajax-loader-on-yellow.gif">  Bitte etwas Geduld.</span>');
		$("div#invalid_errors").show();
		
		postObject = new Object;
		postObject.name = name;
		postObject.email = email;
		postObject.comment = comment;
		postObject.uid = uid;
		postObject.type = type;
		postObject.id = id;
		$.post('/ajax/addcomment', postObject, 
		function(data){
			if (data.status == 'submitted')
			{
				$('#commentform').html("<div id='commentmessage'></div>"); 
				$('#commentmessage').html("<h2>Vielen Dank f&uuml;r Ihren Kommentar!</h2>")
				.append("Aus rechtlichen Gr&uuml;nden m&uuml;ssen wir Ihren Kommentar vor der Ver&ouml;ffentlichung pr&uuml;fen.")
			}
			else if (data.status == 'approved')
			{
				$('#commentform').html("<div id='commentmessage'></div>"); 
				$('#commentmessage').html("<h2>Vielen Dank f&uuml;r Ihren Kommentar!</h2>")
				.append("Dein Kommentar wird in K&uuml;rze ver&ouml;ffentlicht")
			}
			else if (data.status == 'invalid')
			{	
				$("div#invalid_errors").html('<span><ol>' + data.error + '</ol></span>');
				$("div#invalid_errors").show();
				$("input#submit_btn").removeAttr("disabled");
			}
		}, "json" );
	return false;
  });  
}); 
