$(document).ready( 
  function() { 
    $(".browse_heading > em").click(function() {
        var m = 'image_' + $(this).parent().parent().attr('id');
        var new_obj = $(this).parent();
        if ($('../../p.browse_content_middle',new_obj).is(':hidden')) {
          document.getElementById(m).src = THEME_PATH + "/images/grey_arrow.gif";
          $('../../p.browse_content_middle', new_obj).show('slow');
        }
        else {
          document.getElementById(m).src = THEME_PATH + "/images/grey_arrow_right.gif";
          $('../../p.browse_content_middle', new_obj).hide('slow');
        }
      }
    
    );    
  }  
);

function showHideCommentBlock(obj, action){
   if(action == 'show'){
      obj.style.height='60px';
      $('#reply_info').show();
   }else if (action=='hide'){
      if(obj.value.replace(/ /g,'') == ''){
        obj.style.height='30px';
        $('#reply_info').hide();
      }     
   }   
 }
 //Generate Preview while user is typing the comment..
 function ShowPreview(){
   userComment =  $("#comment_body").val();
   curDate = $('#curdate').val();
   PostedByInfo = "";
   if(userComment.replace(/ /g,'').length){
     var name = $('#name').val(); 
     userComment = userComment.replace(/\n/g,"<br>");
     var designation = $('#designation').val(); 
     var company = $('#company').val(); 
     $("#reply_preview").show(300);    
     document.getElementById('preview_reply_body').innerHTML = userComment;
     if(name.length && designation.length && company.length) {
       PostedByInfo = "Posted By " +  name + ", " +designation+ " at " +company+ " | " + curDate;
     }else if(name.length && designation.length) {
       PostedByInfo = "Posted By " +  name + ", " +designation+ " | " + curDate;
     }else if(name.length && company.length) {
       PostedByInfo = "Posted By " +  name + ", " +company+ " | " + curDate;
     }else if(name.length){
       PostedByInfo = "Posted By " +  name + " | " + curDate;
     }
     document.getElementById('preview_posted_by').innerHTML = PostedByInfo;
   }else{
      $("#reply_preview").hide(300);    
   }
 }
 //Post the comment made by user if validated
 function SubmitComment(){

try{
   var comment =  $("#comment_body").val();
   var name = $('#name').val(); 
   var email = $('#email').val(); 
   var designation = $('#designation').val(); 
   var company = $('#company').val();
   var parent_id = $('#parent_id').val();
   //Running through all the validations
   if(comment.replace(/ /g,'') == '' ){
     alert("Please enter your comment")
     return false;
   }else if(name.replace(/ /g,'') == '' ){
     alert("Please enter your full name");
     $('#name')[0].focus();
     return false;
   }else if(email.replace(/ /g,'') == '' ){
     alert("Please enter email id");
     $('#email')[0].focus();
     return false;
   }else if(email.indexOf('@') == -1 || email.indexOf('.') == -1 ){
     alert("Please enter valid email id");
     $('#email')[0].focus();
     return false;
   }/*else if(designation.replace(/ /g,'') == '' ){
     alert("Please enter your designation");
     $('#designation')[0].focus();
     return false;
   }else if(company.replace(/ /g,'') == '' ){
     alert("Please enter your company name");
     $('#company')[0].focus();
     return false;
   }  */
   
   //if paased through all the valdations then
   var dofollow = '';
   if(document.getElementById('follow').checked){dofollow = 'follow';}else{dofollow = '';}
   url = $("#ajax_action_url").val();

   $.post(url,{'body':comment, 'name':name, 'email':email, 'designation' : designation, 'company':company, 'parent_id':parent_id, 'follow':dofollow},function(data){
	var num_reply_obj = $('span.reply_count');
	var pre_count = parseFloat(num_reply_obj[0].innerHTML);
	$('span.reply_count').html(pre_count+1);
	var PreReplies = $('#replies_list_block').html();
	$('#replies_list_block').html(data+PreReplies);
	clearAndHide(); 
   });
  }catch(e){
    alert(e.description);
  }
 }

 	function cancelReply(){
		var comment =  $("#comment_body").val();
		var res = true;
		if(comment.replace(/ /g,'').length){
		  res = confirm("Whatever you have typed will be removed. Are you sure you do not want to submit your response?");
		}
		if(res){
			clearAndHide();
		}
	}
	//Clean the text fields and hide it
	function clearAndHide(){
		$("#comment_body").val('');
		$('#name').val('');
		$('#email').val('');
		$('#designation').val('');
		$('#company').val('');
		$("#reply_preview").hide(300,function(){
			$('#preview_posted_by').html('');
			$('#preview_reply_body').html('');
		});
		obj = $("#comment_body");
		showHideCommentBlock(obj[0], 'hide');
	}
