// form validation
jQuery(document).ready(function($){
    $("#guestbookForm").validate({
        submitHandler: function(form) {
            // form is valid so submit
            ajaxGuest(form);
            return false;
        }
    });
});

function ajaxGuest(theForm) {
    var $ = jQuery;

    $('#loader').fadeIn();

    var formData = $(theForm).serialize(), note = $('#Note');

    $.ajax({
        type: "POST",
        url: "guestbook-send.php",
        data: formData,
        success: function(response) {
            if ( note.height() ) {
                note.fadeIn('fast', function() {
                    $(this).hide();
                });
            } else {
                note.hide();
            }

            $('#LoadingGraphic').fadeOut('fast', function() {
                if (response.indexOf("success") != -1) {
                    if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) <= 8) {
                        $(theForm).css('display','none');
                    } else {
                        $(theForm).animate({
                            'opacity': 0
                        },'fast');
                    }
                }

                // Show success or error message.
                result = '';
                c = '';
                if (response === 'success') {
                    result = 'Your message has been sent. Thank you!';	// The thank you message shown after sending
                    c = 'success';
                } else {
                    result = response;
                    c = 'error';
                }

                note.removeClass('success').removeClass('error').text('');
                var i = setInterval(function() {
                    if ( !note.is(':visible') ) {
                        note.html(result).addClass(c).slideDown('fast');
                        clearInterval(i);
                    }
                }, 40);
            }); // end loading image fadeOut
        }
    });

    return false;
}
