function checkDigit(e) {
    var code =
            (e.charCode != undefined)
                    ? e.charCode
                    : e.keyCode;
    return (
            (code >= String('0').charCodeAt(0)
                    && code <= String('9').charCodeAt(0)) ||
            code == String('.').charCodeAt(0) ||
            code == 0 || code == 8 ||
            (code >= 35 && code <= 40)
            );
}

function checkEmpty(a) {
    if (a && (a.value == null || a.value.length == 0)) {
        a.value = 0;
    }
}

function findElement(elementType, elementId) {
    var elements = document.getElementsByTagName(elementType);
    for (var i = 0; i < elements.length; i++) {
        var fullId = elements[i].id;
        if (fullId.lastIndexOf(elementId) + elementId.length == fullId.length) {
            return elements[i];
        }
    }
    return null;
}

function brandSubmit() {
    var url = document.getElementById('brandUrl').value;
    var brandId = document.getElementById('selectBrand').value;
    window.location.href = url + '&bi=' + brandId;
}

function priceSubmit() {
    var url = document.getElementById('priceUrl').value;
    var min = findElement('input', 'inputPriceMin').value;
    if (min == null || min == "" || min < 0)
        min = 0;
    if (max == null || max == "" || max < min)
        max = min + 1;
    var max = findElement('input', 'inputPriceMax').value;
    window.location.href = url + '&price=' + min + '-' + max;
}

function numberSubmit(attrId) {
    var url = document.getElementById('numberUrl' + attrId).value;
    var min = document.getElementById('inpMin' + attrId).value;
    var max = document.getElementById('inpMax' + attrId).value;
    if (min == null || min == "" || min < 0)
        min = 0;
    if (max == null || max == "" || max < min)
        max = min + 1;
    window.location.href = url + '&ai' + attrId + '=' + min + '-' + max;
}

function sortSubmit() {
    var url = document.getElementById('sortUrl').value;
    var sort = findElement('select', 'selectModelsSort_hide').value;
    window.location.href = url + '&st=' + sort;
}

function pageSizeSubmit() {
    var url = document.getElementById('pageSizeUrl').value;
    var pageSize = findElement('select', 'selectPageSize').value;
    window.location.href = url + '&ps=' + pageSize;
}

function onlyPresentSubmit() {
    var url = document.getElementById('onlyPresentUrl').value;
    var bool = findElement('select', 'showOnlyPresent').value;
    window.location.href = url + '&ois=' + (bool == 'true' ? 't' : 'f');
}

function citySubmit() {
    var url = findElement('input', 'cityUrl').value;
    var cityId = findElement('select', 'selectCity').value;
    window.location.href = url + '&city=' + cityId;
}

