﻿var IsDesignMode = false;
var animating = false;
var popupmanager = null;
var newsCategory = null;
var newsSort = null;

if (!Array.prototype.indexOf)
{
    Array.prototype.indexOf = function (o)
    {
        var _this = this;
        try
        {
            for (var _i = 0; _i < _this.length; _i++)
            {
                if (_this[_i] == o)
                    return _i;
            }
            return -1;
        }
        finally
        {
            _this = null;
        }
    };
}

$(document).ready(function ()
{
    /* Filter Links
    ******************************************************************************/
    $("#pSearchCaseStudies").show();
    $("#pSearchClients").show();
    $("#pSearchAwards").show();

    //$(".NoJS").remove();
    $(".Grid .Item a").show();

    /* Carousel
    ******************************************************************************/
    $(".Carousel").indigoCarousel();

    /* Header
    ******************************************************************************/
    $(".ExpandableContent").brassHeader();

    // Speech bubble
    $(".ContactUs .PhoneNumber a").hoverIntent(function ()
    {
        var 
            isInternetExplorer = new RegExp("MSIE", "g").test(window.navigator.userAgent),
            version = 0;

        if (isInternetExplorer)
        {
            var 
                iMSIE = window.navigator.userAgent.indexOf("MSIE ") + 5,
                iSemiColon = window.navigator.userAgent.indexOf(";", iMSIE) - 1;

            if (iMSIE > -1 && iSemiColon > -1)
            {
                try
                {
                    version = Math.floor(parseFloat(window.navigator.userAgent.substring(iMSIE, iSemiColon)));
                }
                catch (e) { }
            }
        }

        $(".ContactUs .Speech").css({
            "top": $(".ContactUs .PhoneNumber a").position().top - $(".ContactUs .Speech").outerHeight(),
            "left": 50
        });

        if (!(isInternetExplorer && version == 8))
        {
            $(".ContactUs .Speech").fadeIn(250);
        }
        else
        {
            $(".ContactUs .Speech").css({
                "display": "block"
            });
        }
    },
    function ()
    {
        var 
            isInternetExplorer = new RegExp("MSIE", "g").test(window.navigator.userAgent),
            version = 0;

        if (isInternetExplorer)
        {
            var 
                iMSIE = window.navigator.userAgent.indexOf("MSIE ") + 5,
                iSemiColon = window.navigator.userAgent.indexOf(";", iMSIE) - 1;

            if (iMSIE > -1 && iSemiColon > -1)
            {
                try
                {
                    version = Math.floor(parseFloat(window.navigator.userAgent.substring(iMSIE, iSemiColon)));
                }
                catch (e) { }
            }
        }

        if (!(isInternetExplorer && version == 8))
        {
            $(".ContactUs .Speech").fadeOut(250);
        }
        else
        {
            $(".ContactUs .Speech").css({
                "display": "none"
            });
        }
    });

    /* Home
    ******************************************************************************/
    $(".Home .CaseStudy .Overlay")
        .removeClass("Overlay")
        .addClass("Text")
        .append('<div class="Corner"></div>')
        .children("span").css({ "position": "relative",
            "display": "block",
            "opacity": "0",
            "z-index": "75"
        });

    // Case study hover behaviour
    $(".Home .CaseStudy .Text").hoverIntent(function ()
    {
        $(this).addClass("Item-Hover");
        $(".Corner", this).animate({ borderWidth: 185 }, 250).siblings("span").delay(250).animate({ opacity: 1 }, 150);
    },
    function ()
    {
        $(this).removeClass("Item-Hover");
        $(this).children("span").animate({ opacity: 0 }, 100).siblings(".Corner").delay(100).animate({ borderWidth: 20 }, 250);
    });

    /* Radio Buttons
    ******************************************************************************/
    BeautifyRadioButtons();

    /* Pop ups
    ******************************************************************************/
    popupmanager = new popUpManager();
    var qs = new queryStringManager();
    var designmode = qs.get("designmode", "");
    IsDesignMode = designmode;
    if (designmode != "on")
        popupmanager.initialise();

    /* Case Study
    ******************************************************************************/
    if (IsDesignMode != null && !IsDesignMode)
    {
        $(".CaseStudy .Print").show();

        $(".CaseStudy .HiddenContent").each(function ()
        {
            $(this).attr("id", $(this).outerHeight());
        });
        $(".CaseStudy .HiddenContent").css({ "height": 0 });
        $(".CaseStudy .Expand").fadeIn(100);

        $(".CaseStudy .Expand a").toggle(function ()
        {
            if (!animating)
            {
                animating = true;
                var _this = this;
                var hidden = $(this).parent().parent().siblings(".HiddenContent");
                $(hidden).stop().animate({ height: $(hidden).attr("id") }, 500, function ()
                {
                    $(_this).text("Hide").siblings("span").text("-");
                    animating = false;
                });
            }
        },
        function ()
        {
            if (!animating)
            {
                animating = true;
                var _this = this;
                $(this).parent().parent().siblings(".HiddenContent").stop().animate({ height: 0 }, 500, function ()
                {
                    $(_this).text("Expand to find out more").siblings("span").text("+");
                    animating = false;
                });
            }
        });
    }
    else
        $(".HiddenContent").css({ "display": "block" });


    /* Awards
    /******************************************************************************/
    $(".Awards").indigoSlidingGrid({
        filterFunctionName: "AwardPages_Filter"
    });

    /* News
    /******************************************************************************/
    $(".News select").hide();
    if ($.browser.msie && ($.browser.version == 6 || $.browser.version == 7))
        $(".News .BrassDropDownList").css({ "display": "inline", "zoom": "1" });
    else
        $(".News .BrassDropDownList").css({ "display": "inline-block" });

    $(".News .ViewCateogry").brassDropDownList({ selectedValue: newsCategory });
    $(".News .OrderBy").brassDropDownList({
        followLinks: false,
        selectedValue: newsSort,
        click: function (item)
        {
            // Make webservice call
            var service = new indigo.communication.webService(CONTENT_URL + "NewsService.asmx");
            service.getDataSet("NewsItems_SetOrdering", "Ordering=" + $(item).attr("href").replace("../", "").replace("../", "").replace("#", ""), true, function (ds)
            {
                window.location = window.location;
            });
        }
    });

    /* Our Approach
    /******************************************************************************/
    if (IsDesignMode != null && !IsDesignMode && $(".OurApproach").length > 0)
    {
        // Init tabs
        $(".Tabs .Tab-Holder").removeClass().addClass("Tab-ViewPort");
        $(".Tabs .Content-Item").children("div").width(570);
        $(".Tabs .Links").removeClass().addClass("TabStrip");

        if ($.browser.msie && $.browser.version == 6)
        {
            $(".TabStrip .Background").each(function ()
            {
                $(this).css({
                    "width": $(this).parent().outerWidth()
                });
            });
        }

        $(".TabStrip a").click(function (evt)
        {
            evt.preventDefault();
            if (!$(this).parent().hasClass("Selected"))
            {
                var _this = this;
                $(".TabStrip a").parent().removeClass("Selected");
                $(".TabStrip .Background").stop().animate({ height: 0 }, 150);
                $(".Tab-Content").stop().animate({
                    left: -$(".Tab-Content").children("[name=" + $(this).attr("href").replace("#", "") + "]").position().left
                }, 750, function ()
                {
                    $(".Background", $(_this).parent()).stop().animate({
                        height: $(_this).outerHeight() + 10
                    }, 250, function ()
                    {
                        $(_this).parent().addClass("Selected");

                        if ($(".TabStrip .Tab").first().hasClass("Selected"))
                            $(".Tabs .Previous").fadeOut(250);
                        else
                            $(".Tabs .Previous").fadeIn(250);

                        if ($(".TabStrip .Tab").last().hasClass("Selected"))
                            $(".Tabs .Next").fadeOut(250);
                        else
                            $(".Tabs .Next").fadeIn(250);
                    });
                });
            }
        });
        $(".TabStrip a").first().click();

        // Next & previous
        $(".Tabs .Previous, .Tabs .Next").hover(function ()
        {
            $("div", this).fadeIn(250);
        },
        function ()
        {
            $("div", this).fadeOut(250);
        });

        $(".Tabs .Previous div").click(function ()
        {
            if ($(".TabStrip .Selected").prev().length > 0)
                $("a", $(".TabStrip .Selected").prev()).click();
        });

        $(".Tabs .Next div").click(function ()
        {
            if ($(".TabStrip .Selected").next().length > 0)
                $("a", $(".TabStrip .Selected").next()).click();
        });
    }
    else
    {
        $(".Tabs").addClass("Tabs-CMS");
        $(".Tabs .Links").removeClass().addClass("TabStrip");
    }

    /* Our Clients
    /******************************************************************************/
    $(".OurClients").indigoSlidingGrid({
        filterFunctionName: "ClientPages_Filter"
    });

    /* Our Work
    /******************************************************************************/
    $(".OurWork").indigoSlidingGrid({
        name: "our-work",
        cornerSize: 21,
        cornerAnimateSize: 41,
        itemHeight: 175,
        itemsCanExpand: false,
        filterFunctionName: "CaseStudyPages_Filter"
    });

    /* Vacancies
    ******************************************************************************/
    if (IsDesignMode != null && !IsDesignMode)
    {
        $(".Vacancies .Vacancy .HiddenContent").each(function ()
        {
            $(this).attr("id", $(this).outerHeight());
        });
        $(".Vacancies .Vacancy .HiddenContent").css({ "height": 0 });
        $(".Vacancies .Vacancy .Expand").fadeIn(100);

        $(".Vacancies .Vacancy .Expand a").toggle(function ()
        {
            if (!animating)
            {
                animating = true;
                var _this = this;
                var hidden = $(this).parent().parent().siblings(".HiddenContent");
                $(hidden).stop().animate({ height: $(hidden).attr("id") }, 500, function ()
                {
                    $(_this).text("Hide").siblings("span").text("-");
                    animating = false;
                });
            }
        },
        function ()
        {
            if (!animating)
            {
                animating = true;
                var _this = this;
                $(this).parent().parent().siblings(".HiddenContent").stop().animate({ height: 0 }, 500, function ()
                {
                    $(_this).text("Tell me more").siblings("span").text("+");
                    animating = false;
                });
            }
        });
    }
    else
        $(".HiddenContent").css({ "display": "block" });

    /* Search
    ******************************************************************************/
    $(".Header .Search .TextBox").focus(function ()
    {
        $(this).parent().addClass("Search-Highlight");
    });

    $(".Header .Search .TextBox").blur(function ()
    {
        $(this).parent().removeClass("Search-Highlight");
    });

    $(".Search select").hide();
    if ($.browser.msie && ($.browser.version == 6 || $.browser.version == 7))
        $(".Search .BrassDropDownList").css({ "display": "inline", "zoom": "1" });
    else
        $(".Search .BrassDropDownList").css({ "display": "inline-block" });

    $(".Search .OrderBy").brassDropDownList({
        followLinks: false,
        selectedValue: "#LatestFirst",
        click: function (item)
        {
            // Make webservice call
            var service = new indigo.communication.webService(CONTENT_URL + "NewsService.asmx");
            service.getDataSet("SearchItems_SetOrdering", "Ordering=" + $(item).attr("href").replace("../", "").replace("../", "").replace("#", ""), true, function (ds)
            {
                window.location = window.location;
            });
        }
    });

    /* Services
    ******************************************************************************/
    if (IsDesignMode != null && !IsDesignMode)
    {
        $(".ServiceOverview .HiddenContent").each(function ()
        {
            $(this).attr("id", $(this).outerHeight());
        });
        $(".ServiceOverview .HiddenContent").css({ "height": 0 });
        $(".ServiceOverview .Expand").fadeIn(100);

        $(".ServiceOverview .Expand a").toggle(function ()
        {
            if (!animating)
            {
                animating = true;
                var _this = this;
                var hidden = $(this).parent().parent().siblings(".HiddenContent");
                $(hidden).stop().animate({ height: $(hidden).attr("id") }, 500, function ()
                {
                    if ($(this).siblings(".Expand").hasClass("Left"))
                        $(this).siblings(".Expand").animate({ width: 720 }, 500);

                    $(_this).text("Hide").siblings("span").text("-");
                    animating = false;
                });
            }
        },
        function ()
        {
            if (!animating)
            {
                animating = true;
                var _this = this;
                var duration = 0;
                if ($(this).parent().parent().hasClass("Left"))
                    duration = 250;

                $(this).parent().parent().animate({ width: 160 }, duration, function ()
                {
                    $(this).siblings(".HiddenContent").stop().animate({ height: 0 }, 500, function ()
                    {
                        $(_this).text("Tell me more").siblings("span").text("+");
                        animating = false;
                    });
                });
            }
        });
    }
    else
        $(".HiddenContent").css({ "display": "block" });

    $(".ServiceOverview .CaseStudy .Overlay")
        .removeClass("Overlay")
        .addClass("Text")
        .append('<div class="Corner"></div>')
        .children("span").css({ "position": "relative",
            "display": "block",
            "opacity": "0",
            "z-index": "75"
        });

    // Case study hover behaviour
    $(".ServiceOverview .CaseStudy .Text").hoverIntent(function ()
    {
        $(this).addClass("Item-Hover");
        $(".Corner", this).animate({ borderWidth: 185 }, 250).siblings("span").delay(250).animate({ opacity: 1 }, 150);
    },
    function ()
    {
        $(this).removeClass("Item-Hover");
        $(this).children("span").animate({ opacity: 0 }, 100).siblings(".Corner").delay(100).animate({ borderWidth: 20 }, 250);
    });

    /* Footer
    ******************************************************************************/
    //    var _footerHeight = $(".ExploreBrass").height();
    //    $(".Footer .Explore").toggle(function (evt)
    //    {
    //        $(this).html("<span>-</span> Hide");

    //        var 
    //            _exploreBrass = $(".ExploreBrass"),
    //            _htmlBody = $("html,body");

    //        try
    //        {
    //            _htmlBody.stop().animate({ scrollTop: _htmlBody.scrollTop() + _footerHeight }, { duration: 1000 });
    //            _exploreBrass.stop().animate({ height: "toggle" }, { duration: 1000 });
    //        }
    //        finally
    //        {
    //            _exploreBrass = null;
    //            _htmlBody = null;
    //        }

    //    }, function (evt)
    //    {
    //        $(this).html("<span>+</span> Explore Brass");
    //        $(".ExploreBrass").stop().animate({ height: "toggle" }, 1000);
    //    });

    /*****************************************************************************************************************************/
    /* Forms
    /*****************************************************************************************************************************/
    // Highlight - focus
    $(".Form .TextBox, .CVUpload .TextBox").live("focus", function ()
    {
        if ($(this).hasClass("TextBox-Small"))
            $(this).parent().append('<div id="TextBox-Highlight-' + $(this).attr("id") + '" class="TextBox-Small-Highlight"></div>');
        else
            $(this).parent().append('<div id="TextBox-Highlight-' + $(this).attr("id") + '" class="TextBox-Highlight"></div>');

        $("#TextBox-Highlight-" + $(this).attr("id")).css({
            "top": $(this).position().top - 7,
            "left": $(this).position().left - 7
        }).fadeIn(250);
    });

    // Highlight - blur
    $(".Form .TextBox, .CVUpload .TextBox").live("blur", function ()
    {
        $("#TextBox-Highlight-" + $(this).attr("id")).fadeOut(250, function ()
        {
            $(this).remove();
        });
    });

    // Javascript & non-js buttons
    $(".Form input.Submit").css({
        "display": "none"
    });

    if ($.browser.msie && ($.browser.version == 6 || $.browser.version == 7))
        $(".Form a.Submit").css({ "display": "inline", "zoom": "1" });
    else
        $(".Form a.Submit").css({ "display": "inline-block" });

    /* C.V. Upload
    /******************************************************************************/
    $(".CVUpload .Close").click(function (evt)
    {
        evt.preventDefault();
        popupmanager.close();
    });

    if (IsDesignMode)
        $(".CVUpload .ThankYou").css({ "display": "block", "height": $(".CVUpload .Form").height() });

    $(".CVUpload .Submit").click(function (evt)
    {
        evt.preventDefault();
        var _parent = $(this).parent().parent().parent();
        var validator = new validation();
        var isValid = true;

        if ($(".RoleType", _parent).length > 0)
        {
            // Role type
            if ($(".RadioButton-Freelance input", _parent).attr("checked") == false
                && $(".RadioButton-WorkPlacement input", _parent).attr("checked") == false
                && $(".RadioButton-Speculative input", _parent).attr("checked") == false)
            {
                isValid = (isValid && false);
                $(".RoleType p").addClass("Label-Error").html('What kind of work are you looking for? <span="Highlight">*</span> Please indicate what kind of work you\'re looking for.');
            }
            else
                $(".RoleType p").removeClass("Label-Error").html('What kind of work are you looking for? <span="Highlight">*</span>');
        }

        // First name - required field
        if (!validator.required($(".TextBox-FirstName", _parent).val()))
        {
            $(".TextBox-FirstName", _parent).prev("label")
                .addClass("Label-Error")
                .html('First Name <span class="Highlight">*</span> Please enter your first name.');
            isValid = (isValid && false);
        }
        else
            $(".TextBox-FirstName", _parent).prev("label")
                .removeClass("Label-Error")
                .html('First Name <span class="Highlight">*</span>');

        // Surname - required field
        if (!validator.required($(".TextBox-Surname", _parent).val()))
        {
            $(".TextBox-Surname", _parent).prev("label")
                .addClass("Label-Error")
                .html('Surname <span class="Highlight">*</span> Please enter your surname.');
            isValid = (isValid && false);
        }
        else
            $(".TextBox-Surname", _parent).prev("label")
                .removeClass("Label-Error")
                .html('Surname <span class="Highlight">*</span>');

        // Email - required field & must be valid email
        if (!validator.required($(".TextBox-Email", _parent).val()))
        {
            $(".TextBox-Email", _parent).prev("label")
                .addClass("Label-Error")
                .html('Email <span class="Highlight">*</span> Please enter your email.');
            isValid = (isValid && false);
        }
        else if (!validator.email($(".TextBox-Email", _parent).val()))
        {
            $(".TextBox-Email", _parent).prev("label")
                .addClass("Label-Error")
                .html('Email <span class="Highlight">*</span> Whoops! Please enter a valid email.');
            isValid = (isValid && false);
        }
        else
            $(".TextBox-Email", _parent).prev("label")
                .removeClass("Label-Error")
                .html('Email <span class="Highlight">*</span>');

        // Confirm email - required field
        if (!validator.match($(".TextBox-Email", _parent).val(), $(".TextBox-ConfirmEmail", _parent).val()))
        {
            $(".TextBox-ConfirmEmail").prev("label")
                .addClass("Label-Error")
                .html('Confirm email <span class="Highlight">*</span> Please confirm you email address.');
            isValid = (isValid && false);
        }
        else
            $(".TextBox-ConfirmEmail").prev("label")
                .removeClass("Label-Error")
                .html('Confirm email <span class="Highlight">*</span>');

        // Telephone - required field
        if (!validator.required($(".TextBox-Telephone", _parent).val()))
        {
            $(".TextBox-Telephone", _parent).prev("label")
                .addClass("Label-Error")
                .html('Telephone <span class="Highlight">*</span> Please enter your telephone.');
            isValid = (isValid && false);
        }
        else
            $(".TextBox-Telephone", _parent).prev("label")
                .removeClass("Label-Error")
                .html('Telephone <span class="Highlight">*</span>');

        // CV Upload - required field

        if (isValid)
        {
            $(".Sending", _parent).css({ "height": $(".Form", _parent).height() });
            $(".Sending h3", _parent).css({ "line-height": ($(".Form", _parent).height() - 70) + "px" });

            $(".Form", _parent).fadeOut(150, function ()
            {
                $(".Sending", _parent).fadeIn(150, function ()
                {
                    // Make webservice call
                    var service = new indigo.communication.webService(WEBSITE_ROOT_URL + "plugins/contact/dataservice.asmx");
                    service.getDataSet("FormSubmissions_AddUpdate", "ID=0&TypeLKP=" + $(_parent).children("input[type=hidden]").val(), true, function (ds)
                    {
                        var id = ds.tables.items[0].rows.items[0].getValue("fsu_ID");
                        service.getScalar("FormSubmissionData_AddUpdate", "ID=0&FormSubmissionID=" + id + "&Name=FirstName&Value=" + $(".TextBox-FirstName", _parent).val(), true);
                        service.getScalar("FormSubmissionData_AddUpdate", "ID=0&FormSubmissionID=" + id + "&Name=Surname&Value=" + $(".TextBox-Surname", _parent).val(), true);
                        service.getScalar("FormSubmissionData_AddUpdate", "ID=0&FormSubmissionID=" + id + "&Name=Email&Value=" + $(".TextBox-Email", _parent).val(), true);
                        service.getScalar("FormSubmissionData_AddUpdate", "ID=0&FormSubmissionID=" + id + "&Name=Telephone&Value=" + $(".TextBox-Telephone", _parent).val(), true);

                        var 
                            roleType = "",
                            role = "";

                        if ($(".RoleType", _parent).length > 0)
                        {
                            if ($(".RadioButton-Freelance input", _parent).attr("checked"))
                            {
                                service.getScalar("FormSubmissionData_AddUpdate", "ID=0&FormSubmissionID=" + id + "&Name=Freelance&Value=true", true);
                                roleType = "Freelance";
                            }
                            if ($(".RadioButton-WorkPlacement input", _parent).attr("checked"))
                            {
                                service.getScalar("FormSubmissionData_AddUpdate", "ID=0&FormSubmissionID=" + id + "&Name=WorkPlacement&Value=true", true);
                                roleType = "WorkPlacement";
                            }
                            if ($(".RadioButton-Speculative input", _parent).attr("checked"))
                            {
                                service.getScalar("FormSubmissionData_AddUpdate", "ID=0&FormSubmissionID=" + id + "&Name=Speculative&Value=true", true);
                                roleType = "Speculative";
                            }
                        }
                        else
                        {
                            service.getScalar("FormSubmissionData_AddUpdate", "ID=0&FormSubmissionID=" + id + "&Name=Vacancy&Value=" + $(".Form", _parent).children("p").children(".Highlight").text(), true);
                            role = $(".Form", _parent).children("p").children(".Highlight").text();
                        }

                        $(".Sending", _parent).fadeOut(150, function ()
                        {
                            $(".ThankYou", _parent).fadeIn(250);
                        });

                        service.getDataSet("CVUpload_SendEmails", "&RoleType=" + roleType +
                                                                    "&Role=" + role +
                                                                    "&FirstName=" + $(".TextBox-FirstName", _parent).val() +
                                                                    "&Surname=" + $(".TextBox-Surname", _parent).val() +
                                                                    "&Telephone=" + $(".TextBox-Telephone", _parent).val() +
                                                                    "&Email=" + $(".TextBox-Email", _parent).val());
                    },
                    function ()
                    {
                        // Oh no!
                    });
                });
            });
        }
    });

    /* Gallery Sign Up
    /******************************************************************************/
    $(".GallerySignUp .Close").click(function (evt)
    {
        evt.preventDefault();
        popupmanager.close();
    });

    if (IsDesignMode)
        $(".GallerySignUp .ThankYou").css({ "display": "block", "height": $(".CVUpload .Form").height() });

    $(".GallerySignUp .Submit").click(function (evt)
    {
        evt.preventDefault();
        var _parent = $(this).parent().parent().parent();
        var validator = new validation();
        var isValid = true;

        // First name - required field
        if (!validator.required($(".TextBox-FirstName", _parent).val()))
        {
            $(".TextBox-FirstName").prev("label")
                .addClass("Label-Error")
                .html('First name <span class="Highlight">*</span> Please enter your first name.');
            isValid = (isValid && false);
        }
        else
            $(".TextBox-FirstName").prev("label")
                .removeClass("Label-Error")
                .html('First name <span class="Highlight">*</span>');

        // Surname - required field
        if (!validator.required($(".TextBox-Surname", _parent).val()))
        {
            $(".TextBox-Surname").prev("label")
                .addClass("Label-Error")
                .html('Surname <span class="Highlight">*</span> Please enter your surname.');
            isValid = (isValid && false);
        }
        else
            $(".TextBox-Surname").prev("label")
                .removeClass("Label-Error")
                .html('Surname <span class="Highlight">*</span>');

        // Email - required field & must be valid email
        if (!validator.required($(".TextBox-Email", _parent).val()))
        {
            $(".TextBox-Email").prev("label")
                .addClass("Label-Error")
                .html('Email <span class="Highlight">*</span> Please enter your email.');
            isValid = (isValid && false);
        }
        else if (!validator.email($(".TextBox-Email", _parent).val()))
        {
            $(".TextBox-Email").prev("label")
                .addClass("Label-Error")
                .html('Email <span class="Highlight">*</span> Whoops! Please enter a valid email.');
            isValid = (isValid && false);
        }
        else
            $(".TextBox-Email").prev("label")
                .removeClass("Label-Error")
                .html('Email <span class="Highlight">*</span>');

        // Confirm email - required field
        if (!validator.match($(".TextBox-Email", _parent).val(), $(".TextBox-ConfirmEmail", _parent).val()))
        {
            $(".TextBox-ConfirmEmail").prev("label")
                .addClass("Label-Error")
                .html('Confirm email <span class="Highlight">*</span> Please confirm you email address.');
            isValid = (isValid && false);
        }
        else
            $(".TextBox-ConfirmEmail").prev("label")
                .removeClass("Label-Error")
                .html('Confirm email <span class="Highlight">*</span>');

        // Address - required field
        if (!validator.required($(".TextBox-AddressOne", _parent).val()))
        {
            $(".TextBox-AddressOne").prev("label")
                .addClass("Label-Error")
                .html('Address <span class="Highlight">*</span> Please enter your address.');
            isValid = (isValid && false);
        }
        else
            $(".TextBox-AddressOne").prev("label")
                .removeClass("Label-Error")
                .html('Address <span class="Highlight">*</span>');

        // City/Town - required field
        if (!validator.required($(".TextBox-CityOrTown", _parent).val()))
        {
            $(".TextBox-CityOrTown").prev("label")
                .addClass("Label-Error")
                .html('City/Town <span class="Highlight">*</span> Please enter your city or town.');
            isValid = (isValid && false);
        }
        else
            $(".TextBox-CityOrTown").prev("label")
                .removeClass("Label-Error")
                .html('City/Town <span class="Highlight">*</span>');

        // Post code - required field
        if (!validator.required($(".TextBox-Postcode", _parent).val()))
        {
            $(".TextBox-Postcode").prev("label")
                .addClass("Label-Error").removeClass("Label-Small")
                .html('Post code <span class="Highlight">*</span> Please enter your post code.');
            isValid = (isValid && false);
        }
        else
            $(".TextBox-Postcode").prev("label")
                .removeClass("Label-Error").addClass("Label-Small")
                .html('Post code <span class="Highlight">*</span>');

        if (isValid)
        {
            $(".Sending", _parent).css({ "height": $(".Form", _parent).height() });
            $(".Sending h3", _parent).css({ "line-height": ($(".Form", _parent).height() - 70) + "px" });

            $(".Form", _parent).fadeOut(150, function ()
            {
                $(".Sending", _parent).fadeIn(150, function ()
                {
                    // Make webservice call
                    var service = new indigo.communication.webService(WEBSITE_ROOT_URL + "plugins/contact/dataservice.asmx");
                    service.getDataSet("FormSubmissions_AddUpdate", "ID=0&TypeLKP=" + $(_parent).children("input[type=hidden]").val(), true, function (ds)
                    {
                        var id = ds.tables.items[0].rows.items[0].getValue("fsu_ID");
                        service.getScalar("FormSubmissionData_AddUpdate", "ID=0&FormSubmissionID=" + id + "&Name=FirstName&Value=" + $(".TextBox-FirstName", _parent).val(), true);
                        service.getScalar("FormSubmissionData_AddUpdate", "ID=0&FormSubmissionID=" + id + "&Name=Surname&Value=" + $(".TextBox-Surname", _parent).val(), true);
                        service.getScalar("FormSubmissionData_AddUpdate", "ID=0&FormSubmissionID=" + id + "&Name=Company&Value=" + $(".TextBox-Company", _parent).val(), true);
                        service.getScalar("FormSubmissionData_AddUpdate", "ID=0&FormSubmissionID=" + id + "&Name=Email&Value=" + $(".TextBox-Email", _parent).val(), true);
                        service.getScalar("FormSubmissionData_AddUpdate", "ID=0&FormSubmissionID=" + id + "&Name=Address1&Value=" + $(".TextBox-AddressOne", _parent).val(), true);
                        service.getScalar("FormSubmissionData_AddUpdate", "ID=0&FormSubmissionID=" + id + "&Name=Address2&Value=" + $(".TextBox-AddressTwo", _parent).val(), true);
                        service.getScalar("FormSubmissionData_AddUpdate", "ID=0&FormSubmissionID=" + id + "&Name=CityOrTown&Value=" + $(".TextBox-CityOrTown", _parent).val(), true);
                        service.getScalar("FormSubmissionData_AddUpdate", "ID=0&FormSubmissionID=" + id + "&Name=PostCode&Value=" + $(".TextBox-Postcode", _parent).val(), true);
                        service.getScalar("FormSubmissionData_AddUpdate", "ID=0&FormSubmissionID=" + id + "&Name=Newsletter&Value=" + $(".CheckBox-NewsLetter input", _parent).is(":checked"), true);
                        $(".Sending", _parent).fadeOut(150, function ()
                        {
                            $(".ThankYou", _parent).fadeIn(250);
                        });

                        service.getDataSet("GallerySignUp_SendEmails", "FirstName=" + $(".TextBox-FirstName", _parent).val() +
                                                                       "&Surname=" + $(".TextBox-Surname", _parent).val() +
                                                                       "&Company=" + $(".TextBox-Company", _parent).val() +
                                                                       "&AddressOne=" + $(".TextBox-AddressOne", _parent).val() +
                                                                       "&AddressTwo=" + $(".TextBox-AddressTwo", _parent).val() +
                                                                       "&CityOrTown=" + $(".TextBox-CityOrTown", _parent).val() +
                                                                       "&Postcode=" + $(".TextBox-Postcode", _parent).val() +
                                                                       "&Email=" + $(".TextBox-Email", _parent).val() +
                                                                       "&MarketingOptIn=" + $(".CheckBox-NewsLetter input", _parent).is(":checked"));
                    },
                    function ()
                    {
                        // Oh no!
                    });
                });
            });
        }
    });

    /* Mailing List
    /******************************************************************************/
    $(".MailingList .Close").click(function (evt)
    {
        evt.preventDefault();
        popupmanager.close();
    });

    if (IsDesignMode)
        $(".MailingList .ThankYou").css({ "display": "block", "height": $(".CVUpload .Form").height() });

    $(".MailingList .Submit").click(function (evt)
    {
        evt.preventDefault();
        var _parent = $(this).parent().parent().parent();
        var validator = new validation();
        var isValid = true;

        // Name - required field
        if (!validator.required($(".TextBox-FirstName", _parent).val()))
        {
            $(".TextBox-FirstName").prev("label")
                .addClass("Label-Error")
                .html('First name <span class="Highlight">*</span> Please enter your first name.');
            isValid = (isValid && false);
        }
        else
            $(".TextBox-FirstName").prev("label")
                .removeClass("Label-Error")
                .html('First name <span class="Highlight">*</span>')

        // Surname - required field
        if (!validator.required($(".TextBox-Surname", _parent).val()))
        {
            $(".TextBox-Surname").prev("label")
                .addClass("Label-Error")
                .html('Surname <span class="Highlight">*</span> Please enter your surname.');
            isValid = (isValid && false);
        }
        else
            $(".TextBox-Surname").prev("label")
                .removeClass("Label-Error")
                .html('Surname <span class="Highlight">*</span>')

        // Email - required field & must be valid email
        if (!validator.required($(".TextBox-Email", _parent).val()))
        {
            $(".TextBox-Email").prev("label")
                .addClass("Label-Error")
                .html('Email <span class="Highlight">*</span> Please enter your email.');
            isValid = (isValid && false);
        }
        else if (!validator.email($(".TextBox-Email", _parent).val()))
        {
            $(".TextBox-Email").prev("label")
                .addClass("Label-Error")
                .html('Email <span class="Highlight">*</span> Whoops! Please enter a valid email.');
            isValid = (isValid && false);
        }
        else
            $(".TextBox-Email").prev("label")
                .removeClass("Label-Error")
                .html('Email <span class="Highlight">*</span>');

        // Confirm email - required field
        if (!validator.match($(".TextBox-Email", _parent).val(), $(".TextBox-ConfirmEmail", _parent).val()))
        {
            $(".TextBox-ConfirmEmail").prev("label")
                .addClass("Label-Error")
                .html('Confirm email <span class="Highlight">*</span> Please confirm your email address.');
            isValid = (isValid && false);
        }
        else
            $(".TextBox-ConfirmEmail").prev("label")
                .removeClass("Label-Error")
                .html('Confirm email <span class="Highlight">*</span>');

        if (isValid)
        {
            $(".Sending", _parent).css({ "height": $(".Form", _parent).height() });
            $(".Sending h3", _parent).css({ "line-height": ($(".Form", _parent).height() - 70) + "px" });

            $(".Form", _parent).fadeOut(150, function ()
            {
                $(".Sending", _parent).fadeIn(150, function ()
                {
                    // Make webservice call
                    var service = new indigo.communication.webService(WEBSITE_ROOT_URL + "plugins/contact/dataservice.asmx");
                    service.getDataSet("FormSubmissions_AddUpdate", "ID=0&TypeLKP=" + $(_parent).children("input[type=hidden]").val(), true, function (ds)
                    {
                        var id = ds.tables.items[0].rows.items[0].getValue("fsu_ID");
                        service.getScalar("FormSubmissionData_AddUpdate", "ID=0&FormSubmissionID=" + id + "&Name=FirstName&Value=" + $(".TextBox-FirstName", _parent).val(), true);
                        service.getScalar("FormSubmissionData_AddUpdate", "ID=0&FormSubmissionID=" + id + "&Name=Surname&Value=" + $(".TextBox-Surname", _parent).val(), true);
                        service.getScalar("FormSubmissionData_AddUpdate", "ID=0&FormSubmissionID=" + id + "&Name=Company&Value=" + $(".TextBox-Company", _parent).val(), true);
                        service.getScalar("FormSubmissionData_AddUpdate", "ID=0&FormSubmissionID=" + id + "&Name=Email&Value=" + $(".TextBox-Email", _parent).val(), true);

                        $(".Sending", _parent).fadeOut(150, function ()
                        {
                            $(".ThankYou", _parent).fadeIn(250);
                        });

                        service.getDataSet("MailingList_SendEmails", "FirstName=" + $(".TextBox-FirstName", _parent).val() +
                                                                       "&Surname=" + $(".TextBox-Surname", _parent).val() +
                                                                       "&Company=" + $(".TextBox-Company", _parent).val() +
                                                                       "&Email=" + $(".TextBox-Email", _parent).val());
                    },
                    function ()
                    {
                        // Oh no!
                    });
                });
            });
        }
    });

    /* Unsubscribe
    /******************************************************************************/
    if (IsDesignMode)
        $(".Unsubscribe .ThankYou").css({ "display": "block", "height": $(".CVUpload .Form").height() });

    $(".Unsubscribe .Submit").click(function (evt)
    {
        evt.preventDefault();
        var _parent = $(this).parent().parent();
        var validator = new validation();
        var isValid = true;

        // Email - required field & must be valid email
        if (!validator.required($(".TextBox-Email", _parent).val()))
        {
            $(".TextBox-Email", _parent).prev().prev("label")
                .addClass("Label-Error")
                .html('Please enter your email address: Please enter your email.');
            isValid = (isValid && false);
        }
        else if (!validator.email($(".TextBox-Email", _parent).val()))
        {
            $(".TextBox-Email", _parent).prev().prev("label")
                .addClass("Label-Error")
                .html('Please enter your email address: Whoops! Please enter a valid email.');
            isValid = (isValid && false);
        }
        else
            $(".TextBox-Email", _parent).prev().prev("label")
                .removeClass("Label-Error")
                .html('Please enter your email address: ');

        // Confirm email - required field
        if (!validator.match($(".TextBox-Email", _parent).val(), $(".TextBox-ConfirmEmail", _parent).val()))
        {
            $(".TextBox-ConfirmEmail", _parent).prev().prev("label")
                .addClass("Label-Error")
                .html('Confirm your email: Please confirm your email address.');
            isValid = (isValid && false);
        }
        else
            $(".TextBox-ConfirmEmail", _parent).prev().prev("label")
                .removeClass("Label-Error")
                .html('Confirm your email:');

        // Unsubscribe items
        if ($(".CheckBox-GalleryNewsLetter input", _parent).attr("checked") == false && $(".CheckBox-NewsLetter input", _parent).attr("checked") == false)
        {
            isValid = (isValid && false);
            if ($(".Label-CheckBoxError", _parent).length == 0)
                $('<label class="Label Label-CheckBoxError Label-Error">Please select at least one item to unsubscribe from.</label>').insertAfter(".TextBox-ConfirmEmail", _parent);
        }
        else
            $(".Label-CheckBoxError", _parent).remove();

        if (isValid)
        {
            $(".Sending", _parent).css({ "height": $(".Form", _parent).height() });
            $(".Sending h3", _parent).css({ "line-height": ($(".Form", _parent).height() - 70) + "px" });

            $(".Form", _parent).fadeOut(150, function ()
            {
                $(".Sending", _parent).fadeIn(150, function ()
                {
                    // Make webservice call
                    var service = new indigo.communication.webService(WEBSITE_ROOT_URL + "plugins/contact/dataservice.asmx");
                    service.getDataSet("FormSubmissions_AddUpdate", "ID=0&TypeLKP=" + $(_parent).children("input[type=hidden]").val(), true, function (ds)
                    {
                        var id = ds.tables.items[0].rows.items[0].getValue("fsu_ID");
                        service.getScalar("FormSubmissionData_AddUpdate", "ID=0&FormSubmissionID=" + id + "&Name=Email&Value=" + $(".TextBox-Email", _parent).val(), true);

                        if ($(".CheckBox-GalleryNewsLetter input", _parent).attr("checked") == true)
                            service.getScalar("FormSubmissionData_AddUpdate", "ID=0&FormSubmissionID=" + id + "&Name=GalleryNewsLetter&Value=true", true);

                        if ($(".CheckBox-NewsLetter input", _parent).attr("checked") == true)
                            service.getScalar("FormSubmissionData_AddUpdate", "ID=0&FormSubmissionID=" + id + "&Name=NewsLetter&Value=true", true);

                        $(".Sending", _parent).fadeOut(150, function ()
                        {
                            $(".ThankYou", _parent).fadeIn(250);
                        });

                        service.getDataSet("Unsubscribe_SendEmails", "Email=" + $(".TextBox-Email", _parent).val());
                    },
                    function ()
                    {
                        // Oh no!
                    });
                });
            });
        }
    });
});


function validation()
{

}
validation.prototype.required = function (input)
{
    return !(input == "");
}
validation.prototype.email = function (address)
{
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    return reg.test(address);
}
validation.prototype.match = function (inputOne, inputTwo)
{
    return (inputOne == inputTwo);
}


function BeautifyRadioButtons()
{
    $(".RadioButton").each(function ()
    {
        if (!$(this).hasClass("RadioButton-Loaded"))
        {
            $(this).children("input").hide();
            var button = $('<div class="Icon"><div class="Overlay"></div></div>').prependTo(this);
            $(button).hover(function ()
            {
                $(".Overlay", this).fadeIn(250);
            }, function ()
            {
                $(".Overlay", this).fadeOut(100);
            });

            $(button).add($(button).siblings("label")).click(function ()
            {
                $(this).siblings("input").attr("checked", true);
                if ($.browser.msie)
                {
                    this.blur();
                    this.focus();
                }
                $("input[name='" + $(this).siblings("input").attr("name") + "']").each(function ()
                {
                    if ($(this).is(':checked'))
                        $(this).siblings(".Icon").addClass("RadioButton-Selected");
                    else
                        $(this).siblings(".Icon").removeClass("RadioButton-Selected");
                });
            });

            $(button).mousedown(function ()
            {
                $(".Overlay", this).addClass("Overlay-Pressed");
            });

            $(button).mouseup(function ()
            {
                $(".Overlay", this).removeClass("Overlay-Pressed");
            });

            $(this).addClass("RadioButton-Loaded");
        }
    });
}