//      Attribute constructor
    function Attribute(name)
    {
        this.name = name;
        this.options = new Array();
        this.options.push('---');

        this.render = function()
        {
            var comboBoxRef = document.getElementById(name.replace(/'/gi, "_"));
            this.options.sort();
            for (var i = 0; i < this.options.length; i++)
            {
                var option = document.createElement("OPTION");
                option.value = this.options[i];
                option.innerHTML = this.options[i];
                comboBoxRef.appendChild(option);
            }
        }

        this.add = function(value)
        {
            for (var i = 0; i < this.options.length; i++) 
               if (this.options[i] == value) return;
            this.options.push(value);
        }

        this.clear = function()
        {
            this.options = new Array();
             var comboBoxRef = document.getElementById(name.replace(/'/gi, "_"));
            comboBoxRef.innerHTML = "";
            this.options.push("---");
        }

        this.getSelected = function()
        {
            var comboBoxRef = document.getElementById(name.replace(/'/gi, "_"));
            return comboBoxRef.value;
        }

        this.setSelected = function(value)
        {
            var comboBoxRef = document.getElementById(name.replace(/'/gi, "_"));
            comboBoxRef.value = value;
        }

    }
//     Tire constructor
    function Tire(valuesList)
    {
        this.attrValues = new Array();
        for (var i = 0; i < valuesList.length; i++)
            this.attrValues.push(valuesList[i]);
    }
//     Filter constructor
    function Filter(valuesList)
    {
        this.template = valuesList;
        this.check = function(tire)
        {
            if (tire.attrValues.length != this.template.length) return false;
            for (var i = 0; i < this.template.length-1; i++)
                if (this.template[i] != "---" && this.template[i] != tire.attrValues[i]) return false;
            return true;
        }

        this.add = function(tire, id, tyreFinder)
        {
            if (tire.attrValues.length != this.template.length) return false;
            var index = -1;
            for (var i = 0; i < this.template.length-1; i++)
                if (this.template[i] != "---" && this.template[i] != tire.attrValues[i]) {
                    if (index != -1)
                        return;
                    index = i;
                }
            if (index == -1) {
                return;
            }
            if (tyreFinder.attrs[index].name.replace(/'/gi, "_") == id) {
                return;
            }
            tyreFinder.attrs[index].add(tire.attrValues[index]);
        }
    }

//     TireSelector constructor
   function TireSelector(attrs, tires)
   {

        this.attrs = attrs;
        this.tires = tires;

        this.createFilter = function()
        {
            var valuesList = new Array();
            for (var i = 0; i < this.attrs.length-1; i++)
                valuesList.push(this.attrs[i].getSelected());
			valuesList.push("");
            return new Filter(valuesList);
        }

        this.onChange = function(comboBoxRef, reset)
        {
//    var timeStart = new Date();
            var id = (comboBoxRef.value=="---")?("-1"):(comboBoxRef.id);
            var filter = this.createFilter();
           var tr = new Array();
            tr.push("<tr>");
            for (var i = 0; i < this.attrs.length; i++)
            {
                tr.push("<td class='header'>"+this.attrs[i].name+"</td>");
                if (this.attrs[i].name.replace(/'/gi, "_") != id) if (i != this.attrs.length - 1) this.attrs[i].clear();
            }
            tr.push("</tr>");
            var blnReset = false;
            if (id == "-1")
            {
                blnReset = true;
                for (var i = 0; i < this.attrs.length - 1; i++)
                    if (filter.template[i] != "---")
                    {
                        blnReset = false;
                        break;
                    }
            }
            if (reset) {
               blnReset = true;
            }

            for (var i = 0; i < this.tires.length; i++)
            {
               filter.add(this.tires[i], id, this);
               if (blnReset || filter.check(this.tires[i]) == true)
               {
                    var tire = this.tires[i];
                    this.add(tire, id);
                    if (!reset) {
                        var td = new Array();
                        for (var j = 0; j < tire.attrValues.length; j++)
                            td.push("<td>"+tire.attrValues[j]+"</td>")
                        tr.push("<tr>"+td.join("\n")+"</tr>");
                    }
               }
            }

            for (var i = 0; i < this.attrs.length - 1; i++)
            {
                if (this.attrs[i].name.replace(/'/gi, "_") == id) continue;
                this.attrs[i].render(document.getElementById(this.attrs[i].name.replace(/'/gi, "_")));
                this.attrs[i].setSelected(filter.template[i]);
            }

//            var statObj = document.getElementById("statisticStr");
//            if (statObj) statObj.parentNode.removeChild(statObj);
            
            var tbTires = document.getElementById("resultTB"); 
            if (tbTires) tbTires.parentNode.removeChild(tbTires);
            var tableAttrs = document.getElementById("tableAttrs");
            insertAdjacentHTMLCross(tableAttrs, "afterBegin", "<table cellspacing='1' id='resultTB' >"+tr.join("\n")+"</table>");
//   var timeFinish = new Date();
//            var timeStr = "</br><div id='statisticStr'>rendering time: "+new Date(timeFinish.getTime()-timeStart.getTime()).getTime()/1000+"</div>";
//            insertAdjacentHTMLCross(tableAttrs, "afterEnd", timeStr);
        }

        this.add = function(tire, id)
        {
	        for (var i = 0; i < this.attrs.length - 1; i++)
            {
                if (this.attrs[i].name.replace(/'/gi, "_") == id) continue;
                this.attrs[i].add(tire.attrValues[i]);
            }
        }
   }