$(function () {
    $('input[name="selectedentity"]').click(changeSmptSettings);
    $('form').validate({ ignore: '.ignore' });
    $('#password2').rules('add', { equalTo: '#password' });
});

$(document).ready(function () {
    $('input[name="selectedentity"]:checked').first().trigger('click');
});

function changeSmptSettings() {
    $("#authentificationMode").val("0");
    $('#fieldsMsGraph').hide();
    $('#server').addClass('required');
    $('#verifyfield').show();
    if ($(this).val() == "3") {
        $('#fields').hide();
        $('#fieldsMsGraph').show();
        $('.newpwd').hide();
        $('#password').add('#password2').attr('disabled', true);
        $('#verifywrapper').hide();
        $("#notusesmpt").val("false");
        $("#authentificationMode").val("1");
        $('#server').removeClass('required');
        $("#uselocalhost").val("False");
    }
    else
    if ($(this).val() == "2") {
        $('#fields').hide();
        $('#pwdplaceholder').show();
        $('.newpwd').hide();
        $('#password').add('#password2').attr('disabled', false);
        $('#verifywrapper').hide();
        $("#notusesmpt").val("True");
        $("#uselocalhost").val("False");
        $('#verifyfield').hide();
    }
    else if ($(this).val() == "0") {
        $('#fields').show();
        $('#server').val('localhost').attr('readonly', true);
        $('#port').hide().val('');
        $("label[for='port']").hide();
        $('#ssl').hide().removeAttr('checked');
        $("label[for='authorizationmode']").hide();
        $('#account').hide().val('');
        $("label[for='account']").hide();
        $('#pwdplaceholder').hide();
        $('#smtpdetails').hide();
        $("label[for='password']").hide();
        $('#password').attr('disabled', false).val('');
        $('#password2').attr('disabled', false).val('');
        $(".newpwd").hide();
        $("#notusesmpt").val("False");
        $("#uselocalhost").val("True");
    } else {
        $('#fields').show();
        $('#server').attr('readonly', false);
        $('#port').show();
        $("label[for='port']").show();
        $("label[for='authorizationmode']").show();
        $("label[for='ssl']").show();
        $('#account').show();
        $("label[for='account']").show();
        $('#pwdplaceholder').show();
        $("label[for='password']").show();
        $('#smtpdetails').show();
        $(".newpwd").hide();
        $('#password').attr('disabled', false);
        $('#password2').attr('disabled', false);
        $("#notusesmpt").val("False");
        $("#uselocalhost").val("False");
    }
}


function verifyEmailServer() {
    if ($("#email").val() == "" || $("#email").hasClass("error")) {
        alert(SmtpResources.WrongEmail);
        return;
    }
    $('.send').attr("disabled", true);
    $.getJSON(base + 'smtp.ivp/VerifyEmailServer', {
        server: $('#server').val(), port: $('#port').val(), authorizationmode: $('#authorizationmode').val(), account: $('#account').val(), password: $('#password').val(), emailfrom: $('#emailfrom').val(), emailto: $("#email").val()
    , authentificationMode: $("#authentificationMode").val(), tenantId: $("#tenantId").val(), clientId: $("#clientId").val(), clientSecret: $("#clientSecret").val()
    }, function (result) {
        if (result.hasOwnProperty('IsOK') && !result.IsOK) {
            $("#verifyemail").hide();
            $('.send').attr("disabled", false);
            var verify = SmtpResources.VerifyFailed + "<br>" + result.Description;
            $('#errorholder').empty().css("background-color", "#ffd8d8").css("border-color", "#fca6a6").html("<div id='mailerror'>" + verify + "</div>").show();
            $('#errorholder').css("height", $('#mailerror').height() + 20);
            return;
        }
        $("#verifyemail").hide();
        $('.send').attr("disabled", false);
        var verify = SmtpResources.VerifySuccess + " ";
        verify += SmtpResources.TestMessageSend + " " + result;
        $('#errorholder').empty().css("background-color", "#e3ffd8").css("border-color", "#96e577").html("<div id='mailerror'>" + verify + "</div>").show();
        $('#errorholder').css("height", $('#mailerror').height() + 20);
    });
    return false;
}