function wait(ms) {
    var start = new Date().getTime();
    var end = start;
    while (end < start + ms) {
        end = new Date().getTime();
    }
}






function ValidateEmailForm() {
    //var form = $("#mfaEmailEnrollment");

    //var result = $("#mfaEmailEnrollment").valid();
    //if (!result) {
    //    event.preventDefault();
    //    event.stopPropagation();
    //}
    //form.addClass('was-validated');
    let username = $("#UserName");
    if (username.val() === '') {
        username.addClass('is-invalid');
        return false;
    }
    else {
        username.removeClass('is-invalid');
    }
    let email = $("#Email");
    if (email.val() === '') {
        email.addClass('is-invalid');
        return false;
    }
    else {
        if (ValidateEmail(email.val())) {
            email.removeClass('is-invalid');
        }
        else {
            email.addClass('is-invalid');
            return false;
        }
    }

    return true;
}


function ValidateMobileForm() {
    //var form = $("#mfaMobileEnrollment");

    //var result = $("#mfaMobileEnrollment").valid();
    //if (!result) {
    //    event.preventDefault();
    //    event.stopPropagation();
    //}
    //form.addClass('was-validated');
    let username = $("#UserName");
    if (username.val() === '') {
        username.addClass('is-invalid');
        return false;
    }
    else {
        username.removeClass('is-invalid');
    }
    let mobile = $("#MobileNumber");
    if (mobile.val() === '') {
        mobile.addClass('is-invalid');
        return false;
    }
    else {
        mobile.removeClass('is-invalid');
        return true;
        if (ValidateMobile(mobile.val())) {

            mobile.removeClass('is-invalid');
        }
        else {
            mobile.addClass('is-invalid');
            return false;
        }
    }

    return true;
}

function ValidateMicrosoftTOTPForm() {
   
    let username = $("#UserName");
    if (username.val() === '') {
        username.addClass('is-invalid');
        return false;
    }
    else {
        username.removeClass('is-invalid');
    }    

    return true;
}
function ValidateEmail(input) {

    var validRegex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;

    if (input.match(validRegex)) {

        //alert("Valid email address!");

        //document.form1.text1.focus();

        return true;

    } else {

        //alert("Invalid email address!");

        //document.form1.text1.focus();

        return false;

    }

}
function ValidateMobile(input) {

    var validRegex = /^\+?([0-9]{2})\)?[-. ]?([0-9]{4})[-. ]?([0-9]{4})$/;
    return true;
    if (input.match(validRegex)) {

        //alert("Valid email address!");

        //document.form1.text1.focus();

        return true;

    } else {

        //alert("Invalid email address!");

        //document.form1.text1.focus();

        return false;

    }

}



function SendEmail(isResend) {
    //debugger;

    if (!isResend) {
        var result = ValidateEmailForm();
        if (!result) {
            event.preventDefault();
            event.stopPropagation();
            var form = $("#mfaEmailEnrollment");
            form.addClass('was-validated');
            return;
        }
    }

    var formdata = $("#mfaEmailEnrollment").serialize();
    //waitingDialog_temp.show();
    HideShowButtons_Email(false, true, false, false,false);
    //wait(200);
    $.ajax({
        type: "POST",
        url: "MFAEnrollment/SendEmailVerification",
        async: true,
        cache: false,
        dataType: 'json',
        contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
        data: formdata,
        success: function (data) {
            //progressBarSendingEmail.hide();
            //$("divProgressBarSendingEmail").hide();
            //waitingDialog_temp.hide();
            //debugger;
            var body = "";
            var title = "";
            if (data.result == true) {
                //debugger;
                body = '<img src="../img/vgidentityserver/Correct.png" /> ' + data.message;

                //if (isResend) {
                //    title = "Resend Verification email"
                //}
                //else {
                //    title = "Send Verification email";
                //}

                //$("#mydialog .modal-body").html(body);
                //$("#mydialog .modal-title").html(title);
                $("#divAlertSuccessMsg").show();
                $("#divAlertErrorMsg").hide();
                $("#successMsg").text(data.message);
                //$("#mydialog").modal('show');
                HideShowButtons_Email(false, false, true, true,false);
                //HideShowButtons_Email(true);
                return false;
            }
            else {
                //debugger;
                body = '<img src="../img/vgidentityserver/InCorrect.png" /> ' + data.message;
                /*$("#mydialog .modal-body").html(body);*/
                $("#divAlertSuccessMsg").hide();
                $("#divAlertErrorMsg").show();
                $("#errorMsg").text(data.message);
                //$("#mydialog").modal('show');
                HideShowButtons_Email(true, false, false, false,true);
                return false;
            }
        },
        failed: function (err) {
            $("errorMsg").text(data.message);
            HideShowButtons_Email(true, false, false, false,true);
            //waitingDialog_temp.hide();
            //$("divProgressBarSendingEmail").hide();
            //debugger;
            var body = "It seems there is some issue, We could not send you an email with verification code, Please check your email-address and try again. " + err;
            $("#mydialog .modal-body").html(body);
            $("#mydialog").modal('show');
            return false;
        }
    });
}

function SendSMS(isResend) {
    //debugger;

    if (!isResend) {
        var result = ValidateMobileForm();
        if (!result) {
            event.preventDefault();
            event.stopPropagation();
            var form = $("#mfaMobileEnrollment");
            form.addClass('was-validated');
            return;
        }
    }

    var formdata = $("#mfaMobileEnrollment").serialize();
    HideShowButtons_Mobile(false, true, false, false,false);
    //waitingDialog_temp.show();
    //wait(200);
    $.ajax({
        type: "POST",
        url: "MFAEnrollment/SendSMSVerification",
        async: true,
        cache: false,
        dataType: 'json',
        contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
        data: formdata,
        success: function (data) {
            //waitingDialog_temp.hide();
            var body = "";
            var title = "";
            if (data.result == true) {
                //body = '<img src="../img/vgidentityserver/Correct.png" /> ' + data.message;

                //if (isResend) {
                //    title = "Resend Verification SMS"
                //}
                //else {
                //    title = "Send Verification SMS";
                //}

                //$("#mydialog .modal-body").html(body);
                //$("#mydialog .modal-title").html(title);
                $("#divAlertSuccessMsg").show();
                $("#divAlertErrorMsg").hide();
                $("#successMsg").text(data.message);
                //$("#mydialog").modal('show');
                HideShowButtons_Mobile(false, false, true, true,false);
                return false;
            }
            else {
                //body = '<img src="../img/vgidentityserver/InCorrect.png" /> ' + data.message;
                //$("#mydialog .modal-body").html(body);
                //$("#divAlertSuccessMsg").hide();
                //$("#divAlertErrorMsg").show();
                $("#errorMsg").text(data.message);
                //$("#mydialog").modal('show');
                HideShowButtons_Mobile(true, false, false, false,true);
                return false;
            }
        },
        failed: function (err) {
            $("#errorMsg").text(data.message);
            HideShowButtons_Mobile(true, false, false, false,true);
            //waitingDialog_temp.hide();
            //$("divProgressBarSendingEmail").hide();
            //debugger;
            var body = "It seems there is some issue, We could not send you an SMS with verification code, Please check your mobile number and try again. " + err;
            $("#mydialog .modal-body").html(body);
            $("#mydialog").modal('show');
            return false;
        }
    });
}

function HideShowButtons_Email(isDefault, isSendingEmail, isShowValidate, isSuccessfulSend, isShowError) {
    if (isDefault === true) {
        $("#divNoteSendEmail").show();
        $("#btnSendEmail").show();
        $("#btnCancel").show();

        $("#divAlertSuccessMsg").hide();
        $("#divAlertErrorMsg").hide();
        $("#divNoteTryAgain").hide();

        $('#divProgressBarSendingEmail').hide();
        $("#divSecureCode").hide();

        $("#btnResend").hide();
        $("#btnValidate").hide();
        $("#Email").prop("readonly", false);
    }
    else if (isSendingEmail === true) {
        $("#divProgressBarSendingEmail").show();

        $("#divNoteTryAgain").hide();
        $("#divSecureCode").hide();
        $("#divNoteSendEmail").hide();
        $("#divAlertSuccessMsg").hide();
        $("#divAlertErrorMsg").hide();

        $("#btnCancel").hide();
        $("#btnResend").hide();
        $("#btnSendEmail").hide();
        $("#btnValidate").hide();
        $("#Email").prop("readonly", true);
    }
    else if (isShowValidate === true) {
        $("#btnResend").show();
        $("#btnCancel").show();
        $("#btnValidate").show();

        $("#divNoteTryAgain").show();
        $("#divSecureCode").show();

        $("#divNoteSendEmail").hide();
        $("#divProgressBarSendingEmail").hide();
       
        if (isSuccessfulSend) {
            $("#divAlertSuccessMsg").show();
            $("#divAlertErrorMsg").hide();
        }
        else {
            $("#divAlertSuccessMsg").hide();
            $("#divAlertErrorMsg").show();
        }      
        $("#btnSendEmail").hide();       
        $("#Email").prop("readonly", true);
    }
    else { }
    if (isShowError) {
        $("#divAlertSuccessMsg").hide();
        $("#divAlertErrorMsg").show();
    }
    else {

    }

}
//function HideShowButtons_MSTOTPAuthentication(isDefault, isBarcodeGenerating, isShowValidate, isSuccessfulSend, isShowError) {
//    if (isDefault === true) {
//        $("#divNoteScanBarcode").show();
//        //$("#btnSendEmail").show();
//        $("#btnCancel").show();
//        $("#divSecureCode").show();
//        $("#btnValidate").show();

//        $("#divAlertSuccessMsg").hide();
//        $("#divAlertErrorMsg").hide();
//        $("#divNoteTryAgain").hide();

//        $('#divProgressBarScanningBarcode').hide();
        

//        $("#btnRegenerate").hide();
        
//        /*$("#Email").prop("readonly", false);*/
//    }
//    else if (isBarcodeGenerating === true) {
//        $("#divProgressBarScanningBarcode").show();

//        $("#divNoteTryAgain").hide();
//        $("#divSecureCode").hide();
//        $("#divNoteScanBarcode").hide();
//        $("#divAlertSuccessMsg").hide();
//        $("#divAlertErrorMsg").hide();

//        $("#btnCancel").hide();
//        $("#btnRegenerate").hide();
//        /*$("#btnSendEmail").hide();*/
//        $("#btnValidate").hide();
//        /*$("#Email").prop("readonly", true);*/
//    }
//    else if (isShowValidate === true) {
//        $("#btnRegenerate").show();
//        $("#btnCancel").show();
//        $("#btnValidate").show();

//        $("#divNoteTryAgain").show();
//        $("#divSecureCode").show();

//        $("#divNoteScanBarcode").hide();
//        $("#divProgressBarScanningBarcode").hide();

//        if (isSuccessfulSend) {
//            $("#divAlertSuccessMsg").show();
//            $("#divAlertErrorMsg").hide();
//        }
//        else {
//            $("#divAlertSuccessMsg").hide();
//            $("#divAlertErrorMsg").show();
//        }
//        /*$("#btnSendEmail").hide();*/
//        /*$("#Email").prop("readonly", true);*/
//    }
//    else { }
//    if (isShowError) {
//        $("#divAlertSuccessMsg").hide();
//        $("#divAlertErrorMsg").show();
//    }
//    else {

//    }

//}
function HideShowButtons_Mobile(isDefault, isSendingEmail, isShowValidate, isSuccessfulSend, isShowError) {
    if (isDefault === true) {
        $("#divNoteSendSMS").show();
        $("#btnSendSMS").show();
        $("#btnCancel").show();

        $("#divAlertSuccessMsg").hide();
        $("#divAlertErrorMsg").hide();
        $("#divNoteTryAgain").hide();

        $('#divProgressBarSendingSMS').hide();
        $("#divSecureCode").hide();


        $("#btnResend").hide();
        $("#btnValidate").hide();

        $("#MobileNumber").prop("readonly", false);
    }
    else if (isSendingEmail === true) {
        $("#divProgressBarSendingSMS").show();

        $("#divNoteTryAgain").hide();
        $("#divSecureCode").hide();
        $("#divNoteSendSMS").hide();
        $("#divAlertSuccessMsg").hide();
        $("#divAlertErrorMsg").hide();

        $("#btnCancel").hide();
        $("#btnResend").hide();
        $("#btnSendSMS").hide();
        $("#btnValidate").hide();
        $("#MobileNumber").prop("readonly", true);
    }
    else if (isShowValidate === true) {
        $("#divNoteTryAgain").show();
        $("#divSecureCode").show();
        $("#btnCancel").show();
        $("#btnResend").show();
        $("#btnValidate").show();

        $("#divNoteSendSMS").hide();
        $("#divProgressBarSendingSMS").hide();
        
        if (isSuccessfulSend) {
            $("#divAlertSuccessMsg").show();
            $("#divAlertErrorMsg").hide();
        }
        else {
            $("#divAlertSuccessMsg").hide();
            $("#divAlertErrorMsg").show();
        }
        
        $("#btnSendSMS").hide();        
        $("#MobileNumber").prop("readonly", true);
    }
    else { }

    if (isShowError) {
        $("#divAlertSuccessMsg").hide();
        $("#divAlertErrorMsg").show();
    }
    else {

    }
}

