$(document).ready(function() {

    //if submit button is clicked
    $('#submit-comment').click(function () {

        //hide the error messages if needed
        $('.fail').hide();
        $('.success').hide();
        
        //Get the data from all the fields
        var name = $('input[name=name]');
        var email = $('input[name=email]');
        var website = $('input[name=website]');
        var comment = $('textarea[name=comment]');
        var security_code = $('input[name=security_code]');

        var entry_id = $('input[name=entry_id]');
        var entry_title = $('input[name=entry_title]');

        //Simple validation to make sure user entered something
        //If error found, add hightlight class to the text field
        var ok = true;
        if (name.val()=='') {
                name.addClass('highlight');
                ok = false;
        } else name.removeClass('highlight');

        if (email.val()=='') {
                email.addClass('highlight');
                ok = false;
        } else email.removeClass('highlight');

        if (comment.val()=='') {
                comment.addClass('highlight');
                ok = false;
        } else comment.removeClass('highlight');

        if (security_code.val()=='') {
                security_code.addClass('highlight');
                ok = false;
        } else security_code.removeClass('highlight');

        if(!ok) return false;

        //organize the data properly
        var data = 'name=' + name.val() + '&email=' + email.val() + '&website='
        + website.val() + '&comment='  + encodeURIComponent(comment.val()) +
        '&entry_id=' + entry_id.val() + '&entry_title=' + entry_title.val() +
        '&security_code=' + security_code.val();

        //disabled all the text fields
//        $('.text').attr('disabled','true');

        //show the loading sign
//        $('.loading').show();

        //start the ajax
        $.ajax({
            //this is the php file that processes the data and send mail
            url: "/pages/submitComment.php",

            //POST method is used
            type: "POST",

            //pass the data
            data: data,

            //Do not cache the page
            cache: false,

            //success
            success: function (msg) {
                //if submitComment.php returned 1/true (success)
                //first character will be 1/0 telling whether success/fail
                if (msg.substr(0, 1)==1) {
                    //hide the form
                    $('.comment-form').fadeOut('slow');

                    //show the success message
                    $('.success').fadeIn('slow');

                    $('#comments-list').append(msg.substring(1)).fadeIn('slow');

                //if submitComment.php returned 0/false (failed)
                } else if (msg.substr(0, 1)==0) {
                    $('.fail').text(msg.substring(1));
                    $('.fail').fadeIn('slow');
                } else {
                    $('.fail').fadeIn('slow');
                }
            }
        });
        
        //cancel the submit button default behaviours
        return false;
    });
});