﻿var tallestDiv = 0;

function standardiseArticleHeight(selector) {
    selector.each(setTallestDiv);
    selector.each(function(index) { $(this).height(tallestDiv + 5); })
}

function setTallestDiv(index) {
    if ($(this).height() > tallestDiv) {
        tallestDiv = $(this).height();
    }
}
// Do things that will sometimes call sizeChangeCallback()
function sizeChangeCallback() {
    FB.Canvas.setSize();
}

$(document).ready(function () {
    $("#newsletterSignup").submit(function () {
        valid = true;
        emailadd = $("#news-emailAddress").val();
        name = $("#news-name").val();
        //validate email
        if(!checkEmail(emailadd)){
            $("#feedbackMsg").html("Email is incorrect.")
                        .addClass("newsError");
            valid = false;
        }
        //validate name
        if(name == "" || name == " "){
            $("#feedbackMsg").html("Name is required.")
                        .addClass("newsError");
            valid = false;
        }

        if(valid){
            $.ajax({
                url: "/Newsletters/AddSubscriber",
                type: "POST",
                data: $(this).serialize(),
                success: function(data){
                    //set in CSS errorMsg to display:none
                    if(data.indexOf("Success") != -1){
                        $("#feedbackMsg").removeClass();
                        $("#feedbackMsg").html("<h3>Thank you for subscribing.</h3>"+
                                    "<p>Please check your email and click the confirm link. (Check your spam box if it does not appear in your inbox.)</p>")
                                    .addClass("newsSuccess")
                                    .show(1000);
                    }else{
                        $("#feedbackMsg").removeClass();
                        $("#feedbackMsg").html("<h3>Failed to add you.</h3><p>"+data+"</p>")
                                    .addClass("newsError")
                                    .show(1000);
                    }
                },
            });
        }else{
            $("#feedbackMsg").show(1000);
        }
        return false;
    });
});

function checkEmail(email) {
    var pattern = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    return pattern.test(email);
}
