$(document).ready(function(){
       $("#ask_quest").click(function(event){
       	 $("#latest_quest").hide();
       	 $("#qa_form_title").html("Ask a Question");
         $("#quest_form").show();
		 $('#quest_title').val('');
		 $('#quest_details').val('');
		 $('#quest_cat').val('');
       });
});

$(document).ready(function(){
       $("#cancel_btn").click(function(event){
         var validator = $("#form_quest").validate();
         validator.resetForm();
         $("#qa_form_title").html("Questions Asked");
		 $('#quest_title').val('');
		 $('#quest_details').val('');
		 $('#quest_cat').val('');
         $("#quest_form").hide();
       	 $("#latest_quest").show();
       });
});

$(document).ready(function() {
   $("#form_quest").validate({
  			rules: {
    			quest_title: "required",
    			quest_cat: "required"
  			},
  			messages: {
  				quest_title:"",
  				quest_cat:""
  			}
   });
});
 
$(document).ready(function() {
   $("#postanswer").validate({
  			rules: {
    			answer_comment: "required"
  			},
  			messages: {
  				answer_comment:"Please write your answer"
  			}
  });
});

function answerCommentPreview(){
	var NewText 	= document.getElementById("answer_comment").value;
	splitText 		= NewText.replace(/\n\r?/g, '<br />');
	var DivElement 	= document.getElementById("comment_preview");
		
	if(NewText!=""){
		DivElement.innerHTML = "<p id=\"answer_comment_text\" style=\"color:red;\">Answer Preview</p><div id=\"answer_comment_tbg\"></div><div id=\"answer_comment_bg\">"+splitText+"</div><div id=\"answer_comment_bbg\"></div>";
	}else{
		DivElement.innerHTML = "";
	}
}

function bestAnswer(qid, ans_id){
		
		var ans_count = $("#ans-count").val();
		
		$.ajax({
   			type: "POST",
   			url : "/BestAnswer",
		    data: "question-id="+qid+'&answer-id='+ans_id,
	   		success: function(msg){
     			if(msg=="success"){
     				for(i=1; i<=ans_count; i++){
     					$("#choose-answer-"+i).hide();
     				}
     				$("#best-answer-"+ans_id).html('<img src="/images/green_checkmark.png" class="favorite_answer" alt="check" width="20" height="20" />');

     			}else if(msg="failure"){
     				$("#best-answer-"+ans_id).append('You are blocked by this member. You cant send a scrap.');
     				$("#best-answer-"+ans_id).fadeOut("fast", function () {
                		$(this).remove();
            		});
   				}
   			}
 		});	
}
