//$('#btnPrevious1').on('click', function () {
//    var mybtn = $("#btnPrevious1");
//    previousClicked(mybtn);
//});
//$('#btnPrevious2').on('click', function () {
//    var mybtn = $("#btnPrevious2");
//    previousClicked(mybtn);
//});
//$('#btnPrevious3').on('click', function () {
//    var mybtn = $("#btnPrevious3");
//    previousClicked(mybtn);
//});
$('#btnStep0Next').on('click', function () {
    //debugger;

    let isShowValidate = ('@Model.IsValidateFlag.ToString().ToLower()' === 'true');
    let isShowError = ('@Model.IsShowError.ToString().ToLower()' === 'true');
    //if (isShowValidate) {
    //    HideShowButtons_MSTOTPAuthentication(false, false, true, false, isShowError);
    //}
    //else {
    //    HideShowButtons_MSTOTPAuthentication(true, false, false, false, isShowError);
    //}
    var parent_fieldset = $(this).parents('fieldset');
    var next_step = true;
    // navigation steps / progress steps
    var current_active_step = $(this).parents('.f1').find('.f1-step.active');
    var progress_line = $(this).parents('.f1').find('.f1-progress-line');

    parent_fieldset.fadeOut(400, function () {
        //			// change icons
        current_active_step.removeClass('active').addClass('activated').next().addClass('active');
        //			// progress bar
        bar_progress(progress_line, 'right');
        //			// show next step
        $(this).next().fadeIn();
        // 		// scroll window to beginning of the form
        scroll_to_class($('.f1'), 45);

    });
});

$('#btnStep1Next').on('click', function () {
    //debugger;

     //let isShowValidate = ('@Model.IsValidateFlag.ToString().ToLower()' === 'true');
     //let isShowError = ('@Model.IsShowError.ToString().ToLower()' === 'true');
     //if (isShowValidate) {
     //    HideShowButtons_MSTOTPAuthentication(false, false, true, false, isShowError);
     //}
     //else {
     //    HideShowButtons_MSTOTPAuthentication(true, false, false, false, isShowError);
     //}
    var parent_fieldset = $(this).parents('fieldset');
    var next_step = true;
    // navigation steps / progress steps
    var current_active_step = $(this).parents('.f1').find('.f1-step.active');
    var progress_line = $(this).parents('.f1').find('.f1-progress-line');

    parent_fieldset.fadeOut(400, function () {
        //			// change icons
        current_active_step.removeClass('active').addClass('activated').next().addClass('active');
        //			// progress bar
        bar_progress(progress_line, 'right');
        //			// show next step
        $(this).next().fadeIn();
        // 		// scroll window to beginning of the form
        scroll_to_class($('.f1'), 20);

    });
});
$('#btnStep2Next').on('click', function () {
    //debugger;

    
    var parent_fieldset = $(this).parents('fieldset');
    var next_step = true;
    // navigation steps / progress steps
    var current_active_step = $(this).parents('.f1').find('.f1-step.active');
    var progress_line = $(this).parents('.f1').find('.f1-progress-line');

    parent_fieldset.fadeOut(400, function () {
        //			// change icons
        current_active_step.removeClass('active').addClass('activated').next().addClass('active');
        //			// progress bar
        bar_progress(progress_line, 'right');
        //			// show next step
        $(this).next().fadeIn();
        // 		// scroll window to beginning of the form
        scroll_to_class($('.f1'), 20);

    });
    //debugger;
    
    

    setTimeout(function () {

        // Something you want delayed.
        //$("#SecureCode").val('test');
        //$("#SecureCode").val('');
        $("#SecureCode").focus();
        //$("#SecureCode").select();
        //$("#SecureCode").removeClass("active");
        //$("#SecureCode").addClass("active"); // instead of this do the below 
        //$(this).addClass("active");
        //alert('test');
    }, 700);
});
function IsMFAMicrosoftEnrollmentValid() {
    let secureCode = $("#SecureCode");
    if (secureCode.val() === '') {
        secureCode.addClass('is-invalid');
        return false;
    }
    else {
        secureCode.removeClass('is-invalid');
    }

    return true;
}
$("#btnValidate").click(function () {
    //debugger;
    //let securecode = $("#SecureCode");
    //if (securecode.val() === '') {
    //    securecode.addClass('is-invalid');
    //    $("#divTxtSecureCode").addClass('is-invalid');
    //    return ;
    //}
    //else {
    //    securecode.removeClass('is-invalid');
    //    $("#divTxtSecureCode").removeClass('is-invalid');
    //}
    $("#mfaMicrosoftTOTPEnrollment").valid();
    $("#divAlertSuccessMsg").hide();
    $("#divAlertErrorMsg").hide();
    $('#errorMsg').text('');

    if (!IsMFAMicrosoftEnrollmentValid()) {
        return false;
    }
    //var mybtn = $("#btnValidate");
    //GoNext(mybtn); 
    //return;
    var formdata = $("#mfaMicrosoftTOTPEnrollment").serialize();
    
    $.ajax({
        type: "POST",
        url: "MFAEnrollment/ValidateMicrosoftTOTPAuthentication",
        cache: false,
        async: false,
        dataType: 'json',
        contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
        data: formdata,
        success: function (data) {
            //debugger;
            var body = "";
            var title = "";
            
            if (data.result == true) {
                //debugger;
                //var msg = $('#validationSuccessMsg');
                //body = '<img src="../img/vgidentityserver/Correct.png" /> ' + data.message;

                //$("#mydialog .modal-body").html(body);
                //$("#mydialog").modal('show');
                $("#divAlertErrorMsg").hide();
                var mybtn = $("#btnValidate");
                GoNext(mybtn);                          
            }            
            else {
                //debugger;
                //var msg = $('#validationFailedMsg');
                //var statustring = data.connectionStorageStatusString;
                //body = '<img src="../img/vgidentityserver/InCorrect.png" /> ' + data.message;
                $("#SecureCode").val('');
                $("#SecureCode").focus();
                $("#divAlertSuccessMsg").hide();
                $("#divAlertErrorMsg").show();
                $('#errorMsg').text(data.message); 

                //$("#mydialog .modal-body").html(body);
                //$("#mydialog").modal('show');
                //$("#dialog_simple").html(title);

                //$("#dialog_simple").html(body);
                //$('#dialog_simple').dialog('open');
                return false;
            }
            
        },
        failed: function (err) {
            //waitingDialog.hide();
            //debugger;
            var body = "Failed: " + err;
            $("#mydialog .modal-body").html(body);
            $("#mydialog").modal('show');
            // $("#dialog_simple").html(title);
            //$("#dialog_simple").html(body);
            //$('#dialog_simple').dialog('open');
            return false;
        }
    });
});

function ShowMessage(message, isError) {
    var body = message;
    if (isError) {
        body = '<span style="color:red"> <b>Warning!  </b></span> ' + message;
    }
    else {
        body = '<span style="color:green"> <i class="fal fa-check-square" style="color:green"></i></span> ' + message;
    }
    $("#mydialog .modal-body").html(body);
    $("#mydialog").modal('show');
}




function scroll_to_class(element_class, removed_height) {
    var scroll_to = $(element_class).offset().top - removed_height;
    if ($(window).scrollTop() != scroll_to) {
        $('html, body').stop().animate({ scrollTop: scroll_to }, 0);
    }
}

function bar_progress(progress_line_object, direction) {
    var number_of_steps = progress_line_object.data('number-of-steps');
    var now_value = progress_line_object.data('now-value');
    var new_value = 0;
    if (direction == 'right') {
        new_value = now_value + (100 / number_of_steps);
    }
    else if (direction == 'left') {
        new_value = now_value - (100 / number_of_steps);
    }
    progress_line_object.attr('style', 'width: ' + new_value + '%;').data('now-value', new_value);
}

function getNextStep(step, fieldSet, stepToCheck) {
    let nextStep = step;
    if (fieldSet[0].id.indexOf(stepToCheck) > -1) {
        //if (IsSQLServerSelected()) {
        //    nextStep = $("#step4_sqlrepository");
        //}
        //else{
        //    nextStep = $("#step4_oraclerepository");
        //}
    }

    return nextStep;
}

// previous step
$('.f1 .btn-previous').on('click', function () {


    var btn = $(this);

    // navigation steps / progress steps
    var current_active_step = $(this).parents('.f1').find('.f1-step.active');
    var progress_line = $(this).parents('.f1').find('.f1-progress-line');

    $(this).parents('fieldset').fadeOut(400, function () {
        // change icons
        current_active_step.removeClass('active').prev().removeClass('activated').addClass('active');
        // progress bar
        bar_progress(progress_line, 'left');
        // show previous step
        $(this).prev().fadeIn();
        // scroll window to beginning of the form
        scroll_to_class($('.f1'), 20);
    });
});

function prevBtnClicked(thisObj) {
    //    // navigation steps / progress steps
    var current_active_step = thisObj.parents('.f1').find('.f1-step.active');
    var progress_line = thisObj.parents('.f1').find('.f1-progress-line');

    thisObj.parents('fieldset').fadeOut(400, function () {
            // change icons
            current_active_step.removeClass('active').prev().removeClass('activated').addClass('active');
            // progress bar
            bar_progress(progress_line, 'left');
            // show previous step
        thisObj.prev().fadeIn();
            // scroll window to beginning of the form
            scroll_to_class($('.f1'), 20);
        });
}

function GoNext(thisObj) {
    var parent_fieldset = thisObj.parents('fieldset');
    var next_step = true;
    // navigation steps / progress steps
    var current_active_step = thisObj.parents('.f1').find('.f1-step.active');
    var progress_line = thisObj.parents('.f1').find('.f1-progress-line');

    // fields validation
    //parent_fieldset.find('input[type="text"], input[type="password"], textarea').each(function () {
    //    if ($(this).val() == "") {
    //        $(this).addClass('input-error');
    //        next_step = false;
    //    }
    //    else {
    //        $(this).removeClass('input-error');
    //    }
    //});
    // fields validation

    if (next_step) {
        parent_fieldset.fadeOut(400, function () {
            // change icons
            current_active_step.removeClass('active').addClass('activated').next().addClass('active');
            // progress bar
            bar_progress(progress_line, 'right');
            // show next step
            //let nextStep = $(this).next();

            let nextStep = getNextStep($(this).next(), parent_fieldset, 'step2');

            nextStep.fadeIn();
            // scroll window to beginning of the form
            scroll_to_class($('.f1'), 20);
        });
    }
}

function NextClick(obj) {


    var parent_fieldset = obj.parents('fieldset');
    var next_step = true;
    // navigation steps / progress steps
    var current_active_step = obj.parents('.f1').find('.f1-step.active');
    var progress_line = obj.parents('.f1').find('.f1-progress-line');

    if (next_step) {
        parent_fieldset.fadeOut(400, function () {
            // change icons
            current_active_step.removeClass('active').addClass('activated').next().addClass('active');
            // progress bar
            bar_progress(progress_line, 'right');

            let nextStep = getNextStep(obj.next(), parent_fieldset, 'step2');
            // show next step
            nextStep.fadeIn();
            // scroll window to beginning of the form
            scroll_to_class($('.f1'), 20);
        });
    }

}


jQuery(document).ready(function () {
    /*
        Fullscreen background
    */
    /*$.backstretch("../assets/img/backgrounds/1.jpg");*/

    $('#top-navbar-1').on('shown.bs.collapse', function () {
        $.backstretch("resize");
    });
    $('#top-navbar-1').on('hidden.bs.collapse', function () {
        $.backstretch("resize");
    });

    /*
        Form
    */
    $('.f1 fieldset:first').fadeIn('slow');


    //$('.f1 input[type="text"], .f1 input[type="password"], .f1 textarea').on('focus', function () {
    //    $(this).removeClass('input-error');
    //});



    // next step
    //$('.f1 .btn-next').on('click', function () {

    //	var parent_fieldset = $(this).parents('fieldset');
    //	var next_step = true;
    //	// navigation steps / progress steps
    //	var current_active_step = $(this).parents('.f1').find('.f1-step.active');
    //	var progress_line = $(this).parents('.f1').find('.f1-progress-line');

    //	// fields validation
    //    parent_fieldset.find('input[type="text"], input[type="password"], textarea').each(function () {
    //        //if (parent_fieldset.attr("Id") == "step2_connectRepository")
    //        //{
    //        //    if ($("#cboConnType").find("option:selected").text() == "SQLServer") {

    //        //    }
    //        //    else
    //        //    {
    //        //    }
    //        //}
    //		if( $(this).val() == "" ) {
    //			//$(this).addClass('input-error');
    //			//next_step = false;
    //		}
    //		else {
    //			/*$(this).removeClass('input-error');*/
    //		}
    //	});
    //	// fields validation

    //	if( next_step ) {
    //		parent_fieldset.fadeOut(400, function() {
    //			// change icons
    //			current_active_step.removeClass('active').addClass('activated').next().addClass('active');
    //			// progress bar
    //			bar_progress(progress_line, 'right');
    //			// show next step
    // 		$(this).next().fadeIn();
    // 		// scroll window to beginning of the form
    //			scroll_to_class( $('.f1'), 20 );
    // 	});
    //	}

    //});
    //});

    // previous step
    $("#btnPrevious").on('click', function () {
        //debugger;
        // navigation steps / progress steps
        var current_active_step = $(this).parents('.f1').find('.f1-step.active');
        var progress_line = $(this).parents('.f1').find('.f1-progress-line');

        $(this).parents('fieldset').fadeOut(400, function () {
            // change icons
            current_active_step.removeClass('active').prev().removeClass('activated').addClass('active');
            // progress bar
            bar_progress(progress_line, 'left');

            let current_fieldset = $(this);
            let prevStep = $(this).prev();
            if (current_fieldset[0].id.indexOf('step2') > -1) {
                prevStep = $("#step1_selectrepositorytype");
            }

            // show previous step
            $(this).prev().fadeIn();
            // scroll window to beginning of the form
            scroll_to_class($('.f1'), 20);
        });
    });

    // submit
    //$('.f1').on('submit', function(e) {

    //	// fields validation
    //	$(this).find('input[type="text"], input[type="password"], textarea').each(function() {
    //		if( $(this).val() == "" ) {
    //			e.preventDefault();
    //			$(this).addClass('input-error');
    //		}
    //		else {
    //			$(this).removeClass('input-error');
    //		}
    //	});
    //	// fields validation

    //});




});