/*
   Script name  : PHP and Ajax Comment
   File Name 	: script.js
   Developed By : Amit Patil (India)
   Email Id 	: amitpatil321@gmail.com
   Date Created : 21 June 2009
   Last Updated : 24 Aug 2009
         This program is freeware.There is no any fucking copyright and bla bla bla.
   You can use it for your personal use.You can also make any changes to this script.
   But before using this script i would appericiate your mail.That will encourage me a lot.
   Any suggestions are always welcome.
         Have a fun with PHP.   
*/
$(document).ready(function(){
  $(".comment_table").find('textarea, input:text').blur(function () {
	if ($(this).val() != ''){
		$(this).removeClass("required");
	}
  });	
  $("#submit_test").click(function(){
	  var anyBlank = 0;
	  $(".comment_table").find('textarea, input:text').each(function () {
		if ($(this).val() == ''){
			$(this).addClass("required");
			anyBlank = 1;
		}
	  });	
	  if(anyBlank == "0")
	  {
		  var name    = $("#name").val();
		  var email   = $("#email").val();
		  var comment = $("#comment").val();
		  comment = comment.replace(/\n\r?/g, '<br />');
		  $("#loading").css("visibility","visible");
			$.ajax({
			   type: "POST",
			   url: "ajax_server.php",
			   data: "name="+name+"&email="+email+"&comment="+comment,
			   success: function(date_added){
				  if(date_added != 0)
				   {
					   structure = '<div class="comment_holder"><div id="photo"><img src="images/user.gif" width="60" height="70"><br>'+name+'</div><div id="comment_text"><div id="date_posted">'+date_added+'</div>'+comment+'</div></div>';				  	
					   $(".no_comments").fadeOut("slow");
					   $("#ajax_response").prepend(structure);
					   $(".comment_table").find('textarea, input:text').each(function () {
						   $(this).val("");
					   });
				   }
				  else
					  alert("Unexpected error...!");
  					  $("#loading").css("visibility","hidden");
			   }
			 });
	  }
  });
  $("#ajax_response").mouseover(function(){
	 $(this).find(".comment_holder").mouseover(function(){
		$(this).addClass("highlight");
	 });
  });
  $("#ajax_response").mouseout(function(){
	 $(this).find(".comment_holder").mouseout(function(){
		$(this).removeClass("highlight");
	 });
  });
});
