var selected = [];
var dots = document.querySelectorAll('#pattern-dots .pin-dot');

function selectPosition(btn) {
    if (selected.length >= 4) return;
    selected.push(btn.getAttribute('data-token'));
    btn.style.opacity = '0.4';
    btn.disabled = true;
    updatePatternDots();
}

function clearPattern() {
    selected = [];
    var btns = document.querySelectorAll('#pattern-grid .key-btn:not(.clr):not(.ent)');
    for (var i = 0; i < btns.length; i++) { btns[i].style.opacity = '1'; btns[i].disabled = false; }
    updatePatternDots();
}

function submitPattern() {
    if (selected.length !== 4) return;
    document.getElementById('pattern-tokens').value = selected.join(',');
    document.getElementById('pattern-form').submit();
}

function updatePatternDots() {
    for (var i = 0; i < dots.length; i++) {
        dots[i].classList.toggle('filled', i < selected.length);
    }
}