$(document).ready(function() {
    var loaderImage = new Image();
    loaderImage.src = Marketecture.base_url + 'images/ajaxload.gif';

    var loaderImage2 = new Image();
    loaderImage2.src = Marketecture.base_url + 'images/ajax-loader2.gif';

    /** B2B Bitesize Newsletter **/
    $('#newsletter').validate({
        rules: {
            Email: {
                email: true,
                required: true
            },
            FirstName: {
                required: true
            },
            Surname: {
                required: true
            }
        },
        invalidHandler: function() {
            
        },
        submitHandler: function(form) {
            var formData = {
                SubmitEnquiry: 'Submit',
                FirstName: $(form).find('input[name=FirstName]').val(),
                Surname: $(form).find('input[name=Surname]').val(),
                Email: $(form).find('input[name=Email]').val(),
				is_ajax: 'true'
            };

            var loadImage = $(loaderImage2).css({
                position: 'relative',
                top: '30px',
                left: '100px'
            });

            var newElm = $('<div></div>').css({
                width: 220,
                height: 158
            }).append(loadImage);

            $(form).replaceWith(newElm);

            $.ajax({
                url: Marketecture.base_url + "includes/sendmail_newsletter.php",
                type: 'POST',
                data: formData,
                success: function(msg) {
                    var responseText = $('<p></p>').css({
                        background: '#313131',
                        padding: '7px',
                        fontSize: '16px',
                        color: '#fff'
                    }).text('Thank you for signing up for B2B bitesize.');

                    loadImage.replaceWith(responseText);
                }
            });
        }
    });

    /** contact form ajax**/
    $('#contactform').validate({
        rules: {
            'ContactEmail': {
                email: true,
                required: true
            },
            'Message': {
                required: true,
                minlength: 3
            },
            'FullName': {
                required: true,
                minlength: 3
            }
        },
        messages: {
            'Message': "Please enter a message.",
            'ContactEmail': {
               required: "We need your email address to contact you.",
               email: "Your email address must be in the format of name@domain.com."
            },
            'FullName': 'Please enter your name.'

        },
        success: function(label) {
            //label.addClass('valid').text('Ok!');
            label.remove();

        },
        errorPlacement: function(error, element) {
            $('#contactform fieldset').after(error)
        },
        submitHandler: function(form) {
            var formData = {
                SubmitEnquiry: 'Submit',
                FullName: $(form).find('input[name=FullName]').val(),
                Company: $(form).find('input[name=Company]').val(),
                Telephone: $(form).find('input[name=Telephone]').val(),
                ContactEmail: $(form).find('input[name=ContactEmail]').val(),
                Message: $(form).find('textarea[name=Message]').val(),
				is_ajax: 'true'
            };

            var loadImage = $(loaderImage).css({
                position: 'relative',
                top: '30px',
                left: '180px'
            });

            var newElm = $('<div></div>').css({
                width: 430,
                height: 158
            }).append(loadImage);

            $(form).replaceWith(newElm);

            $.ajax({
                url: Marketecture.base_url + "includes/sendmail.php",
                type: 'POST',
                data: formData,
                success: function(msg) {
                    var responseText = $('<p></p>').css({
                        background: '#313131',
                        padding: '7px',
                        fontSize: '16px',
                        color: '#fff'
                    }).text('Thank you for getting in touch. We will get back to you shortly.');

                    loadImage.replaceWith(responseText);
                }
            });
        }
    });

    /** make feature boxes, e.g. on about us page those on  fully clickable **/

    $('#featureboxnav div.featurebox').each(function() {
        var box = this;
        var boxLocation = $(box).find('h3 a').eq(0).attr('href');
        $(box).css('cursor', 'pointer').click(function() {
            window.location = boxLocation;
        });
    });
});