﻿/*
/ --------------------------------------
/	Manston Airport
/   Frame Digital
/   Custom homepage javascript
/ --------------------------------------
/
/	-	Load events for newsletter and large buttons
*/


/* --------------------------------------
/    Big old onload event
/ -------------------------------------- */

$(document).ready(function () {

    // --------------------------------------
    // Newsletter signup functionality
    $("#firstname").focus(function () {
        if ($(this).val() == "Firstname")
            $(this).val("")
    });

    $("#firstname").blur(function () {
        if ($(this).val() == "")
            $(this).val("Firstname")
    });

    $("#lastname").focus(function () {
        if ($(this).val() == "Lastname")
            $(this).val("")
    });

    $("#lastname").blur(function () {
        if ($(this).val() == "")
            $(this).val("Lastname")
    });

    $("#email").focus(function () {
        if ($(this).val() == "Email")
            $(this).val("")
    });

    $("#email").blur(function () {
        if ($(this).val() == "")
            $(this).val("Email")
    });

    $("#save").click(function () {


        var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; 

        if (!emailPattern.test($("#email").val())) {
            $(".complete-message").html("<p class='alert'>Please enter a valid email address.</p>");
            return;
        }

        if ($("#firstname").val() == "Firstname") {
            $(".complete-message").html("<p class='alert'>Please enter your firstname.</p>");
            return;
        }

        if ($("#lastname").val() == "Lastname") {
            $(".complete-message").html("<p class='alert'>Please enter your lastname.</p>");
            return;
        }

        var url = "/common/handlers/signup.ashx";

        $.post(url, { email: encodeURIComponent($("#email").val()),
            firstname: encodeURIComponent($("#firstname").val()),
            lastname: encodeURIComponent($("#lastname").val())
        },
                function (data) {
                    if (data.Complete) {
                        $(".complete-message").html(data.Message);
                        $(".newsletter-form").css("display", "none");
                    }
                    else {
                        $(".complete-message").html(data.Message);
                    }

                }, 'json');
    });


    // --------------------------------------
    // big button click handler
    var cms = document.getElementById("cms-controls");
    if (cms == null) {
        $(".purple-departures").attr("url", "/flight-and-passenger-info/flight-information/departures.html");
        $(".purple-arrivals").attr("url", "/flight-and-passenger-info/flight-information/arrivals.html");
        $(".purple-security").attr("url", "/contact-us.html");

        $(".purple-departures").attr("title", "View latest departure information");
        $(".purple-arrivals").attr("title", "View latest flight arrival information");
        $(".purple-security").attr("title", "Contact us");

        $("div.clickable").css("cursor", "pointer");


        $("div.clickable").click(

        function () {

            window.location = $(this).attr("url");

            return false;

        });
    }

});

