﻿var canclick = true;
$(document).ready(function() {

    $("#frmCollectingEmails").validate();
    $("a.check").click(function() {
        if (canclick) {
            canclick = false;
            $("#EmailResult").hide();
            if ($("#frmCollectingEmails").valid()) {
                CallMailService();
                $("#EmailLoading").show();

            }
            else {
                canclick = true;
            }
        }
        return false;
    });

    $("#txtEmail").focus(function() {
        if ($("#txtEmail").val() == Default_EmailValue)
        { $("#txtEmail").val(""); }
        else { $("#txtEmail").select(); }
    });


});

function CallMailService() {
    var dataStr = $("#frmCollectingEmails").serialize();
    $.ajax({
        type: "GET",
        url: EmailingServicePath,
        contentType: "application/json; charset=utf-8",
        data: dataStr + "&cm=" + cm,
        dataType: "json",
        success: SaveMailingSuccessed,
        error: SaveMailingFailed
    });
}

function SaveMailingSuccessed(data) {
    $("#EmailLoading").hide();
    var result = data[0].result;
    if (result != "success") {
        $("#txtObject").val("");
        $("#txtEmail").val("");
        $("#lbStatus").removeClass();
        $("#lbStatus").addClass("StatusSuccess");
        var err = data[0].err;
        $("#lbStatus").text(GetErrorMessage(err));
        $("#EmailResult").show();
        $("#EmailResult").fadeOut(MsgTimeout);
        canclick = true;
    }
    else {

        $("#txtObject").val("");
        $("#txtEmail").val("");
        $("#lbStatus").removeClass();
        $("#lbStatus").addClass("StatusSuccess");
        $("#lbStatus").text(EmailSuccess);
        $("#EmailResult").show();
        $("#EmailResult").fadeOut(MsgTimeout);
        canclick = true;
    }
}
function SaveMailingFailed(result) {

    $("#EmailLoading").hide();
    canclick = true;
}
  
