summaryrefslogtreecommitdiff
path: root/comment.js
blob: 4c9e63172fdde5fe4585d045f926a1f7e7fc80cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
$(document).ready(function(){
    function danger(obj) {
        obj.css("border", "2px solid red");
    }
    function safe(obj) {
        obj.css("border", "2px solid #cccccc");
    }

    var $error_msg = $("#error-msg");
    var $success_msg = $("#success-msg");
    var $comment_form_wrapper = $("#comment-form-wrapper");
    var $comment_type = $("#comment-type");
    var $comment_body= $("#comment-body");
    var $comment_notify = $("#comment-notify");
    var $comment_email = $("#comment-email");
    var $comment_email_wrapper = $("#comment-email-wrapper");
    $comment_email_wrapper.show(); // to hide change show to hide

  /*  $comment_notify.click(function() {
        if($(this).attr("checked")) {
            $comment_email_wrapper.show();
        } else {
            $comment_email_wrapper.hide();
        }
    });*/

    $("#comment-form").submit(function(e) {
        /* reset all the previous errors */
        var errors = 0;
        safe($comment_type);
        safe($comment_body);
        safe($comment_email);
        $error_msg.hide();
        
        if(!$("#example").val() && $comment_type.val() != "7") {
            $error_msg.html("Please select a category, book, chapter and an example before reporting a bug.")
            $error_msg.show();
            errors = 1;
        }
        if(!$comment_type.val()){
            danger($comment_type);
            errors = 1;
        } 
        if(!$comment_body.val()) {
            danger($comment_body);
            errors = 1;
        } 
        if($comment_notify.attr("checked") && !$comment_email.val()) {
            danger($comment_email);
            errors = 1;
        }
        if(!errors) {
            $.ajax({
                url: "comment.php",
                data: {
                    category: $("#categories").val(),
                    books: $("#books").val(),
                    chapter: $("#chapter").val(),
                    example: $("#example").val(),
                    type: $comment_type.val(),
                    comment: $comment_body.val(),
                    email: $comment_email.val(),
                },
                type: "POST",
                dataType: "html",
                success: function(data) {
                    $comment_form_wrapper.hide();
                    $success_msg.show();
                }
            });
        }
        e.preventDefault();
    });

    $("#commentBtn").click(function() {
        $error_msg.hide();
        $success_msg.hide();
        $comment_form_wrapper.show();
    });
});