$(document).on('keyup change','form input',function () {
    var input = $(this);
    if (input.parent().find('.validation-error,.custom-error').length) {
        input.parent().find('.validation-error,.custom-error').html('');
    } else {
        var inputName = input.attr('name');
        $("#"+inputName+"_error").html('');
    }
    input.removeClass('invalid-input').addClass('border-clr');
});

$(document).on("click",".wantToUpdateEmailBtn,.backBtn",function () {
    $("#updateEmailForm").toggle();
    $("#resendLinkForm").toggle();
});

function postreq(event, fid) {
    event.preventDefault();

    let form = $('#' + fid);
    let $sbutton = form.find('button');
    let lstext = $sbutton.text();
    $sbutton.attr('disabled', true).html(renderIconTag('link')+"Sending Verification Link...");

    let formData = $('#' + fid)[0];
    // Create an FormData object
    let fd = new FormData(formData);
    $.ajax({
        url: form.attr('action'),
        type: "POST",
        data: fd,

        processData: false,
        contentType: false,
        success: function (data, textStatus, jqXHR) {
            if (data.status == true) {
                let count = data.data;
                let si = setInterval(() => {
                    if (count == 0) {
                        $sbutton.attr('disabled', false).html(renderIconTag('link')+lstext);
                        clearInterval(si);
                    } else {
                        $sbutton.html(renderIconTag('link')+`Request new  link ${count} seconds`);
                    }
                    count--;
                }, 1000);
                //data - response from server

                if (data.email) {
                    $('#loggedin_user_title').text(data.email);
                }
                swalMsg(data.msg || 'Getting some error','success');
            } else {
                $sbutton.attr('disabled', false).text(lstext);

                swalMsg(data.msg || 'Getting some error','error');
            }
            $('#preloader').hide();
            $('.textdanger').remove();
        },
        error: function (jqXHR, textStatus, errorThrown) {

            if (globalErrorHandler(jqXHR.responseJSON))
                return false
            $sbutton.attr('disabled', false).html(renderIconTag('link')+lstext);
            $('.textdanger').remove();
            showValidationErrors(jqXHR.responseJSON.errors)
        }
    });
}
