$(function(){
	$("#add_comment").click(function(){
		
		
        $("#add_comment_form").validate({	
       rules:{
        				       	
       	subject:{
       		required:true
       	},
       	
       
       	
       	comment: {
			required: true
				
			
			}
       	
	   },
       messages:{
		   subject: {
				required: "<div class='error_msg'> من فضلك أدخل موضوع التعليق  </div>"
			},
			
			comment: {
				required: "<div class='error_msg'> من فضلك أدخل التعليق  </fdivont>"
			   }
			
	   }
	
        });//validate end
        
        if($("#add_comment_form").valid())
        		{
        	
        	 var subject=$("#subject").val();
       	     var comment=$("#comment").val();
       	     var book_id=$("#book_id").val();
       	     var user_id=$("#user_id").val();
       	     
       	    subject=addslashes(subject);
       	    comment=addslashes(comment);
       	     
       	    //alert(subject+"<br>"+comment+"<br>"+book_id+"<br>"+user_id+"<br>");
       	    
       	    saveComment(subject,comment,book_id,user_id);
        	  
        		}
        
        
	}); // add comment action end
	
	$(".report").click(function(){
		var com_id=$(this).attr("id");
		 $.ajax({
				
			      url:"ajax/book_comments.php?action=report&com_id="+com_id,
			      type: "POST",
			      dataType: "html",
			      success: function(res)
			      {
			     alert("تم الابلاغ عن التعليق");		      			      
			      }
		     
			   });

		
	});
	
	
	
	
}); // ready function end
        

	function addslashes(str) {
	str=str.replace(/\\/g,'\\\\');
	str=str.replace(/\'/g,'\\\'');
	str=str.replace(/\"/g,'\\"');
	str=str.replace(/\0/g,'\\0');
	return str;
	}


  function saveComment(subject,comment,book_id,user_id)
  {
  	                 
	       $.ajax({
	  				
	  			      url:"ajax/book_comments.php?subject="+subject+"&comment="+comment+"&user_id="+user_id+"&book_id="+book_id,
	  			      type: "POST",
	  			      dataType: "html",
	  			      success: function(response)
	  			      {
	    	   		  //alert("تم اضافة التعليق");
	  					
	  			     $("#all_comments").prepend(response);			      
	  			      }
	  		     
	  			   });
  
  }
  function close_dialog(dialog)
  {
	  $("#"+dialog).dialog('close');  
  }