﻿$(document).ready(function () {
    $.validator.required = "Feltet er påkrævet.";
    $.validator.email = "Angiv gyldig email-addresse.";
    $("#btnSubscribe").click(function (event) {
        $("#Form1").validate().element("#txtSName");
        $("#Form1").validate().element("#txtSEmail");
        if ($("#txtSName").valid() && $("#txtSEmail").valid()) {
            $("#subscribtionError").hide();
            $("#newslettersubmit").toggle(750);
            $.ajax({
                type: "POST",
                url: "/umbraco/webservices/ShiftNewsletter.asmx/SubscribeUser",
                data: '{"name":"' + $("#txtSName").val() + '","email":"' + $("#txtSEmail").val() + '","documentId":"' + $("[id$='_hfSubscribePageId']").val() + '"}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    $("#txtName").attr("value", '');
                    $("#txtEmail").attr("value", '');
                    $("#subscribed").show(500);
                },
                error: function (xhr, status, error) {
                    // Boil the ASP.NET AJAX error down to JSON.
                    var err = eval("(" + xhr.responseText + ")");
                    // Display the specific error raised by the server
                    $("span[id$='lblSError']").text(err.Message);
                    $("#newslettersubmit").toggle(500, function () {
                        $("#subscribtionError").show(500);
                    });
                }
            });
        }
    });
});
