$(document).ready(function () {
  
  
  $('#txtEmail').autoclear();
  
    $("#frmSubscribeMail").validate({
        rules: {
            txtEmail: {
                required: true,
                email: true
            }
        },
        errorPlacement: function (error, element) {
            if ($(element).is("[name='txtEmail']")) {
                $('#SubScribeError').html(' ');
                error.appendTo($('#SubScribeError'));
            }
        },
        messages: {
            txtEmail: {
                required: "Please enter a valid email address",
                email: "Please enter a valid email address"
            }
        },
        submitHandler: function () {
            SubscribeGraphicMail();
        }
    });
}); 

function SubscribeGraphicMail()
{
    document.getElementById('imgBtn').disabled = true;
    document.getElementById('imgAjaxPreload').style.display = 'inline';
    document.getElementById('SubScribeError').innerText = '';
    $.ajax({
        type: 'POST',
        url: "/base/crumpledmailsignuprest/SubscribeGraphicMail.aspx",
        data: {

            subscribeEmail: document.getElementById('txtEmail').value,
            currentNodeID: document.getElementById('hidCurrentNodeID').value,
            hidjavascript: 1
        },
        success:
                    function (data) {
                        document.getElementById('imgBtn').disabled = false;
                        document.getElementById('imgAjaxPreload').style.display = 'none';
                        var ReturnData = data;
                        if (ReturnData == "0") {
                            $('#SubScribeError').html('<label class="error" for="txtEmail" generated="true">Unexpected error, sorry, try again.</label>');                          
                        }
                        if (ReturnData == "1") {                            
                            $('#SubScribeError').html('<label class="error" for="txtEmail" generated="true">Thanks, that worked a treat.</label>');
                        }
                        if (ReturnData == "2") {                          
                            $('#SubScribeError').html('<label class="error" for="txtEmail" generated="true">Sorry, email already in use, try another?</label>');
                        }
                        if (ReturnData == "3") {                           
                            $('#SubScribeError').html('<label class="error" for="txtEmail" generated="true">Please enter a valid email address</label>');                                                   
                        }
                    }
    });
    return false;
}

