// Init:
const ql = ["ql0","ql1","ql2","ql3","ql4","ql5","ql6","ql7","ql8","ql9"]
checkStorageSupport();

async function checkStorageSupport(){
    if (window.localStorage){
        localStorage.setItem("support-check","8441");
        if (localStorage.getItem("support-check")=="8441"){
            console.log("Local Storage supported.")
            localStorage.removeItem("support-check")
        } else if (localStorage.getItem("support-check" != "8441")){
            console.log("Error fetching test value.")
        }
    } else {
        console.log("Local Storage is not supported")
        window.alert("Settings cannot be saved on your device, try updating your browser.")
    }
}

async function getQl(){
    var qlOpt = JSON.parse(localStorage.getItem("qlOpt"))
    if (qlOpt){
        var i = 0
        while (i < 10) {
            if (qlOpt[i] != 'undefined') {
                console.log(`Setting box ql${i} to ${qlOpt[i]}`)
                document.getElementById(`ql${i}`).value = qlOpt[i]
                i +=1
            }
        }
    }
}

async function setQl(){
    var qlSet = []
    for (i in ql) {
        console.log(`Try to get value of 'ql${i}'`)
        var opt = document.getElementById(`ql${i}`).value
        console.log(`Found value: ${opt}`)
        if (opt != ""){
            qlSet.push(opt)
        }
        qlSet.sort()
    }
    console.log(qlSet)
    localStorage.setItem("qlOpt", JSON.stringify(qlSet))
}

async function clearQl(){
    localStorage.removeItem("qlOpt")
    getQl()
}