$.validator.setDefaults({
	debug: true,
	submitHandler: 
		function() { 
			
		}
});



$(document).ready(function(){
	$("#post_box").hide();
	$("#thanks").hide();
	$("#post_box2").hide();
	$("#thanks2").hide();
	
	$("p#post a").click(function(){
		// show posting box
		
		if ($(this).text() == "POST A COMMENT") {
			$("#post_box").slideDown("slow");
			$(this).text("NEVER MIND");
		}
		else if ($(this).text() == "NEVER MIND") {
			$("#post_box").slideUp("slow");
			$(this).text("POST A COMMENT");
		}
		return false;
	});
	
	$("p#post2 a").click(function(){
		// show posting box
		
		if ($(this).text() == "POST A COMMENT") {
			$("#post_box2").slideDown("slow");
			$(this).text("NEVER MIND");
		}
		else if ($(this).text() == "NEVER MIND") {
			$("#post_box2").slideUp("slow");
			$(this).text("POST A COMMENT");
		}
		return false;
	});
	
	$("input, textarea").focus(function () {
        if ($(this).val() == $(this).attr("id").toUpperCase()) {
        	$(this).val("");
        }
    });
    
    $("input, textarea").blur(function () {
    	if ($(this).val() == "") {
			//$(this).val($(this).attr("id").toUpperCase());
		}
    });
    
    function clearDefaultText (element) {
		$(element).focus(function(){
			$(this).next("span").css('display','inline').fadeOut(1000);
		});
	}
		
	// validate signup form on keyup and submit
	$("#commentForm").validate({
		errorElement: "label",
		rules: {
			name: {
				required: true
			},
			email: {
				required: true,
				email: true
			},
			comment: {
				required: true
			}
		},
		messages: {
			name: "required",
			email: "valid email required",
			comment: "required"
		},
		submitHandler: function(form) {
   			//$(form).ajaxSubmit();
   			//alert("thanks");
   			var name = $("#commentForm .name").val();
			var email = $("#commentForm .email").val();
			var comment = $("#commentForm .comment").val();
			var essay_id = $("#essay_id").html();
			var dataString = 'name='+name + '&email='+email + '&comment='+comment  + '&essay_id='+essay_id;
			//alert(dataString);
			
			$.ajax({
				type: "POST",
				url: "essays-process.php",
				data: dataString,
				success: function(server_msg) {
					//alert(server_msg);
					$("p #post").text("POST A COMMENT");
					$("#post_box").slideUp("slow");
					$("#thanks").fadeIn("slow")
								.animate({opacity: 1.0}, 3000)
								.slideUp("slow");
				}
			});
			return false;
   		}
	});
	
	$("#commentForm2").validate({
		errorElement: "label",
		rules: {
			name2: {
				required: true
			},
			email: {
				required: true,
				email: true
			},
			comment: {
				required: true
			}
		},
		messages: {
			name: "required",
			email: "valid email required",
			comment: "required"
		},
		submitHandler: function(form) {
   			//$(form).ajaxSubmit();
   			//alert("thanks 2");
   			var name = $("#commentForm2 .name").val();
			var email = $("#commentForm2 .email").val();
			var comment = $("#commentForm2 .comment").val();
			var essay_id = $("#essay_id").html();
			var dataString = 'name='+name + '&email='+email + '&comment='+comment  + '&essay_id='+essay_id;
			//alert(dataString);
			
			$.ajax({
				type: "POST",
				url: "essays-process.php",
				data: dataString,
				success: function(server_msg) {
					//alert(server_msg);
					$("p #post2").text("POST A COMMENT");
					$("#post_box2").slideUp("slow");
					$("#thanks2").fadeIn("slow")
								.animate({opacity: 1.0}, 3000)
								.slideUp("slow");
				}
			});
			return false;
   		}
	});

});
