
var mainSelector = "";

function clearList(obj) {
    while (IsGreaterThan(obj.options.length, 1))
        obj.options[obj.options.length - 1] = null;
}

function fillList(list, data, cols, vi, ni, filter) {
    clearList(list);
    var len = data.length / cols;
    for (var x = 0; IsLessThan(x, len); x++) {
        if (filter != null) {
            if (eval(filter))
                list.options.add(new Option(data[x * cols + ni], data[x * cols + vi]));
        }
        else
            list.options.add(new Option(data[x * cols + ni], data[x * cols + vi]));
    }
}

function cid_change(obj) {
    var fields = document.forms["inventorySearch"].elements;
    var i = obj.selectedIndex;
    var val = obj.options[i].value;
    var tidObj = fields["tid"];
    var modObj = fields["mod"];


    if (IsGreaterThan(i, 0)) {
        if (tidObj.selectedIndex == 0 || mainSelector == "cid") {
            mainSelector = "cid";
            obj.style.backgroundColor = "yellow";
            fillList(tidObj, arrayBodyStylesFabricants, 3, 1, 2, "data[x*cols] == " + val);
            fillList(modObj, arrayModels, 3, 2, 2, "data[x*cols] == " + val);
        }
        else if (IsGreaterThan(tidObj.selectedIndex, 0)) {
            fillList(modObj, arrayModels, 3, 2, 2, "And(data[x*cols] == " + val + ",data[x*cols+1]==" + tidObj.options[tidObj.selectedIndex].value + ")");
        }
    }
    else {
        if (mainSelector == "cid") {
            mainSelector = "";
            obj.style.backgroundColor = "";
            fillList(tidObj, arrayFabricants, 2, 0, 1);
            fillList(modObj, arrayModels, 3, 2, 2);
        }
        else if (tidObj.selectedIndex == 0) {
            fillList(modObj, arrayModels, 3, 2, 2);
        }
        else if (IsGreaterThan(tidObj.selectedIndex, 0)) {
            fillList(modObj, arrayModels, 3, 2, 2, "data[x*cols+1]==" + tidObj.options[tidObj.selectedIndex].value);
        }
    }
}

function tid_change(obj) {
    var fields = document.forms["inventorySearch"].elements;
    var i = obj.selectedIndex;
    var val = obj.options[i].value;
    var cidObj = fields["cid"];
    var modObj = fields["mod"];

    if (IsGreaterThan(i, 0)) {
        if (cidObj.selectedIndex == 0 || mainSelector == "tid") {
            mainSelector = "tid";
            obj.style.backgroundColor = "yellow";
            fillList(cidObj, arrayFabricantsBodyStyles, 3, 1, 2, "data[x*cols] == " + val);
            fillList(modObj, arrayModels, 3, 2, 2, "data[x*cols+1] == " + val);
        }
        else if (IsGreaterThan(cidObj.selectedIndex, 0)) {
            fillList(modObj, arrayModels, 3, 2, 2, "And(data[x*cols+1] == " + val + ",data[x*cols]==" + cidObj.options[cidObj.selectedIndex].value + ")");
        }
    }
    else {
        if (mainSelector == "tid") {
            mainSelector = "";
            obj.style.backgroundColor = "";
            fillList(cidObj, arrayBodyStyles, 2, 0, 1);
            fillList(modObj, arrayModels, 3, 2, 2);
        }
        else if (cidObj.selectedIndex == 0) {
            fillList(modObj, arrayModels, 3, 2, 2);
        }
        else if (IsGreaterThan(cidObj.selectedIndex, 0)) {
            fillList(modObj, arrayModels, 3, 2, 2, "data[x*cols]==" + cidObj.options[cidObj.selectedIndex].value);
        }
    }
}			
