$(document).ready(function() {
    // Comment on this page.
    $("div#comment h1").after("<span class=\"hide\">( <a href=\"comment-on-this-page\" id=\"hide-comment\">Hide</a> )</span>");
    $("a#show-comment").click(function(event) {
        $("#comment").slideToggle("fast");
        //event.preventDefault();
    });
    $("a#hide-comment").click(function(event) {
        $("#comment").slideUp("fast");
        event.preventDefault();
    });
    
    $("#comment").hide();

    $("form#CommentForm").submit(
        function(event) {
            event.preventDefault();
            // validate form ...
            var cotp_form = {
                name: $(this).find("#cotp_name").val().replace(/^\s*/, "").replace(/\s*$/, ""),
                western_id: $(this).find("#cotp_western_id").val().replace(/^\s*/, "").replace(/\s*$/, ""),
                email: $(this).find("#cotp_email").val().replace(/^\s*/, "").replace(/\s*$/, ""),
                comments: $(this).find("#cotp_comments").val().replace(/^\s*/, "").replace(/\s*$/, ""),
                regarding: $(this).find("#cotp_regarding").val().replace(/^\s*/, "").replace(/\s*$/, ""),
                comment_send: true
            };

            if (cotp_form.email == "" || cotp_form.comments == "") {
                alert("Your email and a comment is required to comment on this page.");
            }
            else {
                function cotp_process() {
                    $("div.cotp-process:first").removeClass("cotp-process").fadeOut("slow", function() {
                        $(this).remove();
                    });
                }

                $("form#CommentForm").find(":input").attr("disabled", "disabled");
                $.ajax({
                    type: "POST",
                    url: "/include/web-feedback.php",
                    data: cotp_form,
                    success: function(response_data, textStatus) {
                        $("form#CommentForm").find(":input").removeAttr("disabled");
                        $("#CommentForm").before(response_data);
                        setTimeout(cotp_process, 5000);
                    },
                    error: function(response_data, textStatus) {
                        $("form#CommentForm").find(":input").removeAttr("disabled");
                        $("#CommentForm").before(response_data);
                        setTimeout(cotp_process, 5000);
                    }
                });

            }

        }
    );
});
