function sqnSpinnerMinus(thisEl) {
    var thisSpinnerInput = thisEl.parentElement.parentElement.getElementsByTagName('input')[0];
    if (thisSpinnerInput.disabled) {
        return;
    }
    var timeSpinner = thisSpinnerInput.dataset.value;
    var min = thisSpinnerInput.getAttribute('min');
    var max = thisSpinnerInput.getAttribute('max');
    var step = thisSpinnerInput.getAttribute('step');
    min = (min === undefined) ? 0 : parseFloat(min);
    max = (max === undefined) ? 10000 : parseFloat(max);
    step = (step === undefined) ? 1 : parseFloat(step);
    var currentNumber = thisSpinnerInput.value;
    if (timeSpinner !== undefined) {
        currentNumber = timeSpinner;
    }
    if (currentNumber == '' || isNaN(currentNumber) || parseFloat(currentNumber) <= min) {
        currentNumber = min;
    } else {
        currentNumber = Math.round(parseFloat(currentNumber) * 1000000 - step * 1000000) / 1000000;
    }
    if (currentNumber < min) {
        currentNumber = min;
    }
    if (timeSpinner !== undefined) {
        thisSpinnerInput.dataset.value = currentNumber;
        thisSpinnerInput.value = sqnGetTime(currentNumber);
    } else {
        thisSpinnerInput.value = currentNumber;
    }
    thisSpinnerInput.focus();
    sqnFireChangeEvent(thisSpinnerInput);
}

function sqnSpinnerPlus(thisEl) {
    var thisSpinnerInput = thisEl.parentElement.parentElement.getElementsByTagName('input')[0];
    if (thisSpinnerInput.disabled) {
        return;
    }
    var timeSpinner = thisSpinnerInput.dataset.value;
    var min = thisSpinnerInput.getAttribute('min');
    var max = thisSpinnerInput.getAttribute('max');
    var step = thisSpinnerInput.getAttribute('step');
    min = (min === undefined) ? 0 : parseFloat(min);
    max = (max === undefined) ? 10000 : parseFloat(max);
    step = (step === undefined) ? 1 : parseFloat(step);
    var currentNumber = thisSpinnerInput.value;

    if (timeSpinner !== undefined) {
        currentNumber = timeSpinner;
    }
    if (currentNumber == '' || isNaN(currentNumber) || parseFloat(currentNumber) >= max) {
        currentNumber = max;
    } else {
        currentNumber = Math.round(parseFloat(currentNumber) * 1000000 + step * 1000000) / 1000000;
    }
    if (currentNumber > max) {
        currentNumber = max;
    }
    if (timeSpinner !== undefined) {
        thisSpinnerInput.dataset.value = currentNumber;
        thisSpinnerInput.value = sqnGetTime(currentNumber);
    } else {
        thisSpinnerInput.value = currentNumber;
    }
    thisSpinnerInput.focus();
    sqnFireChangeEvent(thisSpinnerInput);
}

function sqnSpinnerBlur(thisEl) {
    var min = thisEl.getAttribute('min');
    var max = thisEl.getAttribute('max');
    min = (min === undefined) ? 0 : parseFloat(min);
    max = (max === undefined) ? 100000 : parseFloat(max);
    var newValue = thisEl.value;
    if (isNaN(newValue) || newValue < min || newValue > max) {
        thisEl.value = thisEl.dataset.lastvalue;
        sqnFireChangeEvent(thisEl);
    }
}

function sqnSpinnerFocus(thisEl) {
    thisEl.dataset.lastvalue = thisEl.value;
}

function sqnSelectBlur(thisEl) {
    thisEl.parentElement.classList.remove('focused');
}

function sqnSelectFocus(thisEl) {
    thisEl.parentElement.classList.add('focused');
}

function sqnSelectChange(thisEl) {
    var selectedText = thisEl.options[thisEl.selectedIndex].text;
    thisEl.parentElement.getElementsByTagName('span')[0].innerHTML = selectedText;
}

function sqnFireChangeEvent(element) {
    if ("createEvent" in document) {
        var evt = document.createEvent("HTMLEvents");
        evt.initEvent("change", false, true);
        element.dispatchEvent(evt);
    } else
        element.fireEvent("onchange");
}

function sqnGetTime(milis) {
    try {
        var date = new Date(null);
        date.setSeconds(milis);
        return date.toISOString().substr(11, 5);
    } catch (err) {
        return "00:00";
    }
}