$(document).ready(function() {

var brw = '';

	
    //if submit button is clicked
    $('.contattisubmit').click(function() {

        //Get the data from all the fields
        var nome = $('input[name=nome]');
        var email = $('input[name=email]');
        var comment = $('textarea[name=comment]');

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

        if (email.val() == '') {
            email.addClass('hightlight');
            return false;
        } else email.removeClass('hightlight');

        if (comment.val() == '') {
            comment.addClass('hightlight');
            return false;
        } else comment.removeClass('hightlight');

        //organize the data properly
        var data = 'nome=' + nome.val() + '&email=' + email.val() + '&comment=' + encodeURIComponent(comment.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: "<?php bloginfo('template_directory'); ?>/contatti_form.php",

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

            //pass the data			
            data: data,

            //Do not cache the page
            cache: false,

            //success
            success: function(html) {
                //if process.php returned 1/true (send mail success)
                if (html == 1) {
                    //hide the form
                    $('#contatti_form').fadeOut('slow');
                    $('.loading').hide();

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

                    //if process.php returned 0/false (send mail failed)
                } else alert('Sorry, unexpected error. Please try again later.');
            }
        });

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


    // Checks the browser and adds classes to the body to reflect it.
        var userAgent = navigator.userAgent.toLowerCase();
        $.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());

        // Is this a version of IE?
        if ($.browser.msie) {
            // Add the version number
            brw = $.browser.version.substring(0, 1);
        }

        // Is this a version of Chrome?
        if ($.browser.chrome) {

            $('body').addClass('browserChrome');

            //Add the version number
            userAgent = userAgent.substring(userAgent.indexOf('chrome/') + 7);
            userAgent = userAgent.substring(0, 1);
            $('body').addClass('browserChrome' + userAgent);

            // If it is chrome then jQuery thinks it's safari so we have to tell it it isn't
            $.browser.safari = false;
        }

        // Is this a version of Safari?
        if ($.browser.safari) {
            // Add the version number
            userAgent = userAgent.substring(userAgent.indexOf('version/') + 8);
            userAgent = userAgent.substring(0, 1);
            $('body').addClass('browserSafari' + userAgent);
        }

        // Is this a version of Mozilla?
        if ($.browser.mozilla) {

            //Is it Firefox?
            if (navigator.userAgent.toLowerCase().indexOf('firefox') != -1) {
                $('body').addClass('browserFirefox');

                // Add the version number
                userAgent = userAgent.substring(userAgent.indexOf('firefox/') + 8);
                userAgent = userAgent.substring(0, 1);
                $('body').addClass('browserFirefox' + userAgent);
            }
            // If not then it must be another Mozilla
            else {
                $('body').addClass('browserMozilla');
            }
        }

        // Is this a version of Opera?
        if ($.browser.opera) {
            $('body').addClass('browserOpera');
        }
    

});

