$(document).ready(function() {

    //if submit button is clicked
    $('#submit-contact').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 subject = $('input[name=subject]');
        var message = $('textarea[name=message]');
        var security_code = $('input[name=security_code]');

        //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 (message.val()=='') {
                message.addClass('highlight');
                ok = false;
        } else message.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() + '&subject='
        + subject.val() + '&message='  + encodeURIComponent(message.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/submitContact.php",

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

            //pass the data
            data: data,

            //Do not cache the page
            cache: false,

            //success
            success: function (msg) {
                //if submitContact.php returned 1/true (success)
                if (msg==1) {
                    //hide the form
                    $('.contact-form').fadeOut('slow');

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

                //if submitContact.php returned 0/false (failed)
                } else {
                    $('.fail').text(msg);
                    $('.fail').fadeIn('slow');
                }
            }
        });

        //cancel the submit button default behaviours
        return false;
    });
});