﻿var problemString = "There was a problem signing you up.   Please try again later.";


function signup() {
    var serializedForm = $("#in_email_enter").serialize();
    var targetUrl = "/credit-cards/RegisterEmailAddress";
    $.ajax({
        type: "POST",
        url: targetUrl,
        data: serializedForm,
        success: function(response) {
            displayEmailSubmit(response);
        }
,
        error: function(response) {
            displayEmailFail(response);
        }

    });

}

function displayEmailSubmit(result) {
    var obj = eval("(" + result + ")");
    $("#response_overlay").show();

    var valid = obj.valid;

    if (valid == true) {
        if (obj.exists == true) {
            $('.newsletterSignup').text('This email has already been submitted.');
        }
        else {
            if (obj.success == true) {
                $('.newsletterSignup').text('Your address has been added to our list.');
            }
            else {
                $('.newsletterSignup').text(problemString);
            }
        }
    }
    else {
        $('.newsletterSignup').text('The email address typed in is invalid.');
    }
}

function displayEmailFail(result) {
    $("#response_overlay").show();
    $('#newsletterSignup').text(problemString);
}


function InitialiseEmailBox() {
    $("#response_overlay").hide();
    $("#in_email_enter").addClass("focused_email_signup");

    $("#in_email_enter").focus(function() {
        $(this).removeClass("focused_email_signup")
        if (this.value == this.defaultValue) {
            this.value = '';
        }
        if (this.value != this.defaultValue) {
            this.select();
        }
    });

    $("#in_email_enter").blur(function() {

        if ($.trim(this.value) == '') {
            $("#in_email_enter").addClass("focused_email_signup");
            this.value = (this.defaultValue ? this.defaultValue : '');
        }
    });
}