function openLinkModal() {
    $('#addLinkModal').modal('show');
}

function refreshPage() {
    window.location.reload();
}

function addUrl() {
    $("#link-btn").prop("disabled", true);

    url = $('#link-form').val();

    if (url.trim().length === 0) {
        showError('URL is required');
        return false;
    }

    $.ajax({
        url: '/add',
        method: 'POST',
        data: { "url": url },
        success: function(data) {
            $('#link-form').val('');
            $("#link-btn").prop("disabled", false);
            if (data !== 'Error')
                window.location.reload();
            else
                showError('URL cannot be empty');
        }
    });
}

function showError(error) {
    $('#error-alert').text(error);
    $('#error-alert').show();
}

$('#addLinkModal').on('shown.bs.modal', function () {
  $('#link-form').trigger('focus');
});