/**
 * @requires jquery-1.3.2
 */
function CarVersionSearchForm(id, brandId, modelId, filterlist_id)
{
    this.id = id;
    this.brandId = brandId;
    this.modelId = modelId;
    this.brandnames = [];
    this.brands = [];
    this.models = [];
    this.filterlist_id = filterlist_id;

    this.getObject = function()
    {
        return $('#' + this.id);
    }

    this.init = function()
    {
        // get all the brand-ids with their model-array
        var url = '/?module=MHS&action=GetBrandModels&filterlist_id=' + this.filterlist_id + '&output=json';
        $.getJSON(url, Delegate.create(this, this.initBrandModels));

        var searchform = this.getObject();
        if (searchform.size()) {

            var self = this;

            $('#' + this.modelId + ' option').each(function()
                {
                    value = this.value;
                    if (!value) value = 0;
                    self.models[value] = this.innerHTML;
                }
            );

            $('#' + this.brandId + '').bind('change', function(e) { self.onChange(e); });

        }

        // get all the brand-ids
        var url = '/?module=MHS&action=GetBrands&filterlist_id=' + this.filterlist_id + '&output=json';
        $.getJSON(url, Delegate.create(this, this.initBrands));
    }

    this.onChange = function(e)
    {
        // change model values
        if (($(e.target).attr('name') == this.brandId) || ($(e.target).attr('name') == 'fld'+this.brandId) || ($(e.target).attr('id') == this.brandId)) {

            var model = $('#' + this.modelId + '');

            if (this.brands[0] != undefined) {
                model.empty();

                var carBrandId = $('#' + this.brandId + '').val();
                if (!carBrandId) carBrandId = 0;
                if (carBrandId in this.brands) {

                    // always add first value
                    model.append('<option value="">' + this.models[0] + '</option>');

                    for (var i = 0; i < this.brands[carBrandId].length; i++) {
                        model.append('<option value="' + this.brands[carBrandId][i] + '">' + this.brands[carBrandId][i] + '</option>');
                    }
                } else {

                    // always add first value
                    model.append('<option value="">' + this.models[0] + '</option>');
                }
            }
        }
    }

    this.initBrands = function(result)
    {
        this.brandnames = result;

        var brand = $('select#' + this.brandId + '');
        if (brand[0] != undefined) {
            var firstOption = brand[0].options[0].text;

            brand.empty();

            // always add first value
            brand.append('<option value="">' + firstOption + '</option>');

            if (this.brandnames != null) {
                for (var i = 0; i < this.brandnames.length; i++) {
                    brand.append('<option value="' + this.brandnames[i] + '">' + this.brandnames[i] + '</option>');
                }
            }

            $('#' + this.brandId + '').trigger('change');
        }
    }

    this.initBrandModels = function(result)
    {
        if (result.brands !== undefined) {
            this.brands = result.brands;
        }
    }
}

