OptionArray = function(data) {
    /*
    var color = new Select2('{"option":[{"id":"3","title":"blue", "model":[{"id":"7","title":"blue_1"},{"id":"13","title":"blue_2    "},{"id":"10","title":"blue_3    "},{"id":"19","title":"blue_4    "},{"id":"16","title":"blue_5    "}]}, {"id":"2","title":"green     ", "option2":[{"id":"6","title":"green_1   "},{"id":"9","title":"green_2   "},{"id":"12","title":"green_3   "},{"id":"18","title":"green_4   "},{"id":"15","title":"green_5   "}]}, {"id":"1","title":"red       ", "option2":[{"id":"5","title":"red_1     "},{"id":"8","title":"red_2     "},{"id":"11","title":"red_3     "},{"id":"17","title":"red_4     "},{"id":"14","title":"red_5     "}]}, {"id":"4","title":"white     ", "option2":[{"id":"4","title":"white_1   "},{"id":"1","title":"white_1   "},{"id":"2","title":"white_2   "},{"id":"3","title":"white_3   "}]}]}');
    */

    this.options = data;

    //OptionArray's to string function, output: 'id,title. '
    this.toString = function() {
        var tostr = '';
        var len = this.options.option.length;
        for (i = 0; i < len; i++) {
            var option = this.options.option[i];
            tostr += option.id + ", " + option.title + ". ";
        }
        return tostr;
    };

    //returns an array of option elements
    //elements are available models given a vehicle make or device brand
    this.getOption = function(id) {
        var len = this.options.option.length;
        var index = 0;
        var opts = new Array();

        if (id >= 0 && id < len) {
            //get all models for the vehicle or device
            var models = this.options.option[id].models;
            var models_count = models.length;
            for (i = 0; i < models_count; i++) {
                //create option element, add it to the array
                var opt = document.createElement('option');
                opt.value = models[i].id;
                opt.text = models[i].title;
                opts[index] = opt;
                index = index + 1;
            }
        }
        //return the array of model options
        return opts;
    };

    //updates model options list given a vehicle make or device brand
    this.updateModels = function(select_obj, model_select_id, model_value) {
        var ind = select_obj.selectedIndex;
        var i, index;
        var opt;
        index = -1;
        opt = document.createElement('option');
        opt.value = 'NULL';
        opt.text = 'Choose Model';

        //get model select element
        var model_select = document.getElementById(model_select_id);

        if (model_select != null) {
            //clear out the model select options
            for (i = model_select.length; i >= 0; i--) {
                model_select.options[i] = null;
            }
            model_select.options.length = 0;
            model_select.options[0] = opt;

            //get selected make/device model options
            var opts = this.getOption(ind - 1);
            //add each model option element to the model select list
            for (i = 0; i < opts.length; i++) {
                if (opts[i].value == model_value) {
                    index = i;
                }
                model_select.options[i + 1] = opts[i];
            }
            model_select.selectedIndex = index + 1;
        }
        return true;
    };

    //Vehicle make change clears years, then updates models
    this.onVMakeChange = function(make_select_obj, model_select_id, year_select_id, model_value) {
        var opt;
        opt = document.createElement('option');
        opt.value = 'NULL';
        opt.text = 'All Years';

        var yearlist = document.getElementById(year_select_id);
        if (yearlist != null) {
            for (i = yearlist.length; i >= 0; i--) {
                yearlist.options[i] = null;
            }
            yearlist.length = 0;
            yearlist.options[0] = opt;
        }
        this.updateModels(make_select_obj, model_select_id, model_value);
    }

    //vehicle model onchange function
    //clears out years, builds new year list
    this.onVModelChange = function(model_select_obj, make_select_id, year_select_id, year_value) {
        var modelIdx = model_select_obj.selectedIndex - 1;
        var makeIdx = document.getElementById(make_select_id).selectedIndex - 1;
        var yearIdx = 0;
        var opt;

        //create default option
        opt = document.createElement('option');
        opt.value = 'NULL';
        opt.text = 'All Years';

        //get year select element
        var year_select = document.getElementById(year_select_id);

        if (year_select != null) {
            //clear out the old year option elements
            for (i = year_select.length; i >= 0; i--) {
                year_select.options[i] = null;
            }
            //set default option
            year_select.options.length = 0;
            year_select.options[0] = opt;
            //if we have a make and model, create the year options
            if (makeIdx != -1 && modelIdx != -1) {
                //get the current model, given make and model id
                var model = this.options.option[makeIdx].models[modelIdx];
                //get the max and min year range for this made and model
                var min_year = model.min_year;
                var max_year = model.max_year;
                var index = 1;
                //build a list of year options based on the year range, incrementing by 1 full year.
                for (var i = Math.round(max_year); i >= Math.round(min_year); i--) {
                    var year_option = document.createElement('option');
                    year_option.value = i;
                    year_option.text = i;
                    year_select.options[index] = year_option;
                    if (year_value == i) { yearIdx = index; }
                    index++;
                }
                year_select.selectedIndex = yearIdx;
            }
        }
        return true;
    };

    //used to initialize dropdowns with persisted parameters
    this.SetSelectedIndex = function(select_obj, value) {
        var selectIdx;
        for (var i = 0; i < select_obj.length; i++) {
            if (select_obj.options[i].value == value) {
                selectIdx = i;
            }
        }
        select_obj.selectedIndex = selectIdx;
    };
}

