$("#btnchangePwd").click(function () {
    var Password = $("#txtcurrent_password").val();
    var NewPassword = $("#txtnew_password").val();
    var ConfirmPassword = $("#txtconfirm_password").val();
    if ($.trim(Password) == "") {
        $('#Ptxtcurrent_password').show();
        $('#Ptxtcurrent_password').fadeOut(8000);
        $('#Ptxtcurrent_password').html('Enter Current Password');
        $("#txtcurrent_password").val('');
        $("#txtcurrent_password").focus();
        return false;
    }
    if ($.trim(NewPassword) == "") {
        $('#Ptxtnew_password').show();
        $('#Ptxtnew_password').fadeOut(8000);
        $('#Ptxtnew_password').html('Enter New Password');
        $("#txtnew_password").val('');
        $("#txtnew_password").focus();
        return false;
    }
    if ($.trim(ConfirmPassword) == "") {
        $('#Ptxtconfirm_password').show();
        $('#Ptxtconfirm_password').fadeOut(8000);
        $('#Ptxtconfirm_password').html('Enter Confirm Password');
        $("#txtconfirm_password").val('');
        $("#txtconfirm_password").focus();
        return false;
    }
    if ($.trim(NewPassword).length < 8) {
        $('#Ptxtnew_password').show();
        $('#Ptxtnew_password').fadeOut(8000);
        $('#Ptxtnew_password').html('New Password Length must be in Minimum of 8 Characters');
        $("#txtnew_password").val('');
        $("#txtnew_password").focus();
        return false;
    }
    if ($.trim(ConfirmPassword).length < 8) {
        $('#Ptxtconfirm_password').show();
        $('#Ptxtconfirm_password').fadeOut(8000);
        $('#Ptxtconfirm_password').html('Confirm Password Length must be in Minimum of 8 Characters');
        $("#txtconfirm_password").val('');
        $("#txtconfirm_password").focus();
        return false;
    }
    if ($.trim(Password) == $.trim(NewPassword))
    {
        $('#Ptxtconfirm_password').show();
        $('#Ptxtconfirm_password').fadeOut(8000);
        $('#Ptxtconfirm_password').html('Current Password and New Password should not be same');
        $("#txtnew_password").val('');
        $("#txtconfirm_password").val('');
        $("#txtnew_password").focus();
        return false;
    }
    if (NewPassword.length > 0 && ConfirmPassword.length > 0 && NewPassword != ConfirmPassword) {
        $('#Ptxtconfirm_password').show();
        $('#Ptxtconfirm_password').fadeOut(8000);
        $('#Ptxtconfirm_password').html('New Password and Confirm Password does not match');
        $("#txtnew_password").val('');
        $("#txtconfirm_password").val('');
        $("#txtnew_password").focus();
        return false;
    }
    var UId = $('#myHiddenuser').val();
        var PasswordUpdationData = {
            doctor_user_name: UId,
            doctor_user_password: $.trim($('#txtcurrent_password').val()),
            doctor_user_new_password: $.trim($('#txtnew_password').val())
        }
        if (Password.length > 0 && NewPassword.length > 0 && ConfirmPassword.length > 0) {
            $.ajax({
                async: true,
                type: 'POST',
                url: 'ChangePassword/PasswordUpdation',
                contentType: 'application/json; charset=UTF-8',
                data: JSON.stringify(PasswordUpdationData),
                success: function (data) {
                    debugger;
                    var data2 = JSON.parse(data);
                    if (jQuery.isEmptyObject((data2.data))) {
                        $('#PbtnchangePwd').show();
                        $('#PbtnchangePwd').fadeOut(8000);
                        $('#PbtnchangePwd').html('Sorry, The Current password you entered do not match.Please try again');
                    }
                    else {

                        if (data2.msg == "Success") {
                            var data1 = data2.data;
                            if (data1[0].StringResult == 'success') {
                                //var data1 = JSON.parse(data);
                                //console.log(data1);
                                $('#PbtnchangePwd').html('Password Updated sucessfully..');
                                $('#logout').click();
                               // window.location = "/Dashboard";
                            }
                            else {
                                $('#PbtnchangePwd').show();
                                $('#PbtnchangePwd').fadeOut(8000);
                                $('#PbtnchangePwd').html('Sorry, The Current password you entered do not match.Please try again');
                            }
                        }
                        else {
                            console.log(data);
                        }
                    }
                }
            });
        }      
});



function TestOnTextChange() {

    var NewPassword = $("#txtnew_password").val();
    var ConfirmPassword = $("#txtconfirm_password").val();

    if ($.trim(NewPassword).length < 8) {
        $('#Ptxtnew_password').html('New Password Length must be in 8 Characters');
        
        $("#txtnew_password").focus();
        return false;
    }

    if ($.trim(ConfirmPassword).length < 8) {
        $('#Ptxtconfirm_password').html('Confirm Password Length must be in Minimum of 8 Characters');
        
        $("#txtconfirm_password").focus();
        return false;
    }
}

$('#cmpin').click(function () {
    $('#loaderimg').show();
    var mpindata = {
        doctor_id: $('#HiddenDoctorID').val(),
        indicator_id : "SOTP"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: 'ChangePassword/mpin_update',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(mpindata),
        success: function (data) {
            debugger;
            $('#loaderimg').hide();
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data)) || data2.data.length == "0") {
            }
            else
            {
                var data1 = JSON.parse(data);
                if (data1.data.length > 0)
                {
                    msg = "An OTP has been sent to your mobile number " + data1.data[0].doctor_phone;
                   
                }
                $('#changeMpin').modal('toggle');
            }
        }
    });
});


$('#verifyotp').click(function () {
    var mpindata = {
        doctor_id: $('#HiddenDoctorID').val(),
        otp: $('#delotp').val(),
        indicator_id: "COTP"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: 'ChangePassword/mpin_update',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(mpindata),
        success: function (data) {
            $('#delotp').val('');
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data)) || data2.data.length == "0") {
                toastr.warning("Invalid OTP");
            }
            else 
            {
                var data1 = JSON.parse(data);
                console.log(data1);
                if (data1.data[0].msg=="success")
                {
                    $('#changeMpin').modal('toggle');
                    $('#mPinopen').show();
                }
                else
                {
                    toastr.warning("Invalid OTP");
                }
             }
        }
    });
});

$('#btnchangempin').click(function () {
    if ($.trim($('#txtmpin_password').val()).length == "") {
        $('#Ptxtmpin_password').show();
        $('#Ptxtmpin_password').fadeOut(8000);
        $('#Ptxtmpin_password').html('Enter MPIN');
        $('#txtmpin_password').focus();
        $('#txtmpin_password').val('');
        return false;
    }
    if ($.trim($('#txtmpinconfirm_password').val()).length == "") {
        $('#Ptxtmpinconfirm_password').show();
        $('#Ptxtmpinconfirm_password').fadeOut(8000);
        $('#Ptxtmpinconfirm_password').html('Reenter MPIN');
        $('#txtmpinconfirm_password').focus();
        $('#txtmpinconfirm_password').val('');
        return false;
    }
    if(($('#txtmpin_password,#txtmpinconfirm_password').val()).length > 2)
    {
        if(($.trim($('#txtmpin_password').val())) == ($.trim($('#txtmpinconfirm_password').val())))
        {
            var mpindata = {
                doctor_id: $('#HiddenDoctorID').val(),
                otp: $('#txtmpin_password').val(),
                indicator_id: "UMPIN"
            }
            $.ajax({
                async: true,
                type: 'POST',
                url: 'ChangePassword/mpin_update',
                contentType: 'application/json; charset=UTF-8',
                data: JSON.stringify(mpindata),
                success: function (data) {
                    location.reload();
                }
            });
        }
        else
        {
            toastr.warning("Re-entered MPIN is Wrong");
        }
    }
});