$(document).ready(function () {
    $('#nltrSubmit').click(nltrPost);
    if ($.browser.mozilla) {
        $('#nltrEmail').keypress(checkForEnter);
    } else {
        $('#nltrEmail').keydown(checkForEnter);
    }
});

function nltrPost ()
{
    if (isValidEmail($('#nltrEmail').val())) {
        $('#nltrSubmit').attr("disabled", true);
        $.post('/home/newsletter-signup', { email: $('#nltrEmail').val() }, function () {
            $('#newsletter').children('input').fadeOut('normal', function () {
                $('#nltrEmail').val('');
                $('#newsletter').children('.nltr').html('Thank You');
                $('#newsletter').children('p').html('You have been added to the Consensus Point email list.');
            });
        });
    }
}

function checkForEnter (event) {
    if (event.keyCode == 13) {
        event.preventDefault();
        if (isValidEmail($('#nltrEmail').val())) {
            nltrPost();
        }
    } 
}

function isValidEmail (email) {
    return /^[a-z0-9\._-]+@([a-z0-9_-]+\.)+[a-z]{2,6}$/i.test(email);
}
