﻿
/* Show/Hide default text if no input */
$(document).ready(function() {
    $('input[type="text"], textarea').focus(function() {
        if (this.value == this.defaultValue) {
            this.value = '';
        }
        if (this.value != this.defaultValue) {
            this.select();
        }
    });

    $('input[type="text"], textarea').blur(function() {
        //$.trim(this.value == '')
        if (this.value == '') {
            this.value = (this.defaultValue ? this.defaultValue : '');
        }
    });
});

/* Disable jQuery Ajax Caching  */
$(document).ready(function() {
    $.ajaxSetup({
        cache:false
    });
});

/* Validation */
$(document).ready(function() {

    jQuery.validator.addMethod("ukPhone", function(value, element) {
        return this.optional(element) || /^(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$/.test(value);
    }, "Please specify a valid uk phone number");
    
});

/* Ajax Submit N.C. */
$(document).ready(function() {

    $.fn.ajaxSubmit = function(e) {
        // Change a form's submission type to ajax
        this.submit(function(e) {

            var form = $(this);
            var formObj = $(this).serialize();

            var $submitButton = $(this, "input[type='submit'], button[type='submit']");

            $.ajax({
                url: this.action,
                type: 'POST',
                data: formObj,
                dataType: 'json',
                cache: false,
                beforeSend: function() {
                    $("body").addClass("curWait");
                    //$submitButton.attr("disabled", "true");
                },
                success: function(result) {

                    var tmp = $('<div id="comms-form-results"><p><span></span>' + result.Message + '</p></div>');

                    if (result.Success) {
                        // Success
                        // Find all the input elements under those.
                        $(form).find(':input')
                               .each(function(i) {
                                   $(this).val($(this)[0].defaultValue);
                               });
                    }
                    else {
                        // Error
                        $(tmp).addClass('error-msg');
                    }

                    var offset = $(form).parent().offset();
                    $(tmp).css({
                        'top': offset.top - 50 + "px",
                        'left': offset.left + "px"
                    });

                    $(tmp).appendTo($(form).parent())
                    $(tmp).fadeIn("normal");
                    $(tmp).fadeOut(9000, function() {
                        tmp.remove();
                    });
                },
                complete: function(xmlhttpRequest) {
                    //$submitButton.attr("disabled", "false");
                    $("body").removeClass("curWait");
                }
            });
            return false;
        });
        return this;
    }
});

$(document).ready(function() {
    $(".popup-info").hover(
        function() {
            $(this).siblings("a").css({ 'color': '#F74843' });
        },
        function() {
            $(this).siblings("a").css({ 'color': '#655F5F' });
        }
    );
});

/* IE Fix On Key Down Submit button 
$(function() {
    $('input').keydown(function(e) {
        if (e.keyCode == 13) {
            $(this).parents('form').submit();
            return false;
        }
    });
});*/

/* Comms Request Demo Form Submission */
$(document).ready(function() {
    $('#comms-contact-form').ajaxSubmit();
});

/* Contact Us Form Submission */
$(document).ready(function() {
    $('#contact-form').submit(function(e) {
        /*
        */
        var form = $(this);
        var formObj = $(this).serialize();

        var $submitButton = $("input[type='submit'], button[type='submit']", this);

        $.ajax({
            url: this.action,
            type: 'POST',
            data: formObj,
            dataType: 'json',
            cache: false,
            beforeSend: function() {
                $("body").addClass("curWait");
                // Disable Form Controls
                $('input[type=submit]', this).attr('disabled', 'disabled');
                $('input[type=text]', this).attr('readonly', 'readonly');
                $('textarea', this).attr('readonly', 'readonly');
            },
            success: function(result) {

                var msg = $('<div id="contact-form-results"><p><span></span>' + result.Message + '</p></div>');

                if (result.Success) {
                    // Success
                    $(form).find(':input')
                               .each(function(i) {
                                   $(this).val($(this)[0].defaultValue);
                               });
                }
                else {
                    $(msg).addClass('error-msg');
                }

                var offset = $(form).find(':submit').offset();
                var formOffset = $(form).offset();

                // outerWidth(true)                    
                $(msg).css({'top': offset.top - 50 + "px",
                            'left': (formOffset.left + 5) + "px"
                });
                
                $(msg).appendTo($(form).parent())
                $(msg).fadeIn("normal");
                $(msg).fadeOut(9000, function() {
                    msg.remove();
                });
            },
            complete: function(xmlhttpRequest) {
                /* Enable Controls */
                $('input[type=submit]', this).removeAttr('disabled');
                $('input[type=text]', this).removeAttr('readonly');
                $('textarea', this).removeAttr('readonly');
                $("body").removeClass("curWait");
            }
        });
        return false;
    });
});

/* Popup's */
$(document).ready(function() {

    $(".more-info").fancybox({
        'centerOnScroll': true,
        'hideOnContentClick': true,
        'autoScale': false,
        'zoomOpacity': true,
        'transitionIn': 'none',
        'transitionOut': 'none',
        'overlayShow': true,
        'overlayOpacity': 0.3,
        'zoomSpeedIn': 300,
        'zoomSpeedOut': 300,
        'padding': 0,
        'cyclic': true,
        'scrolling': 'no',
        'autoDimensions': false,
        'width': 800,
        'height': 550
    });

    $(".popup-info").fancybox({
        'centerOnScroll': true,
        'hideOnContentClick': true,
        'autoScale': false,
        'zoomOpacity': true,
        'transitionIn': 'none',
        'transitionOut': 'none',
        'overlayShow': true,
        'overlayOpacity': 0.3,
        'zoomSpeedIn': 300,
        'zoomSpeedOut': 300,
        'padding': 0,
        'cyclic': true,
        'autoDimensions': false,
        'width': 800,
        'height': 550,
        'titleShow': false,
        'scrolling': 'no',
        'onComplete': function() {
        }
    });
});

