
var model_ctrl = Class.create({ 

    initialize: function(id)
    {
        this.brand_lb = $(id + "_brand_id");
        this.model_lb = $(id + "_model_id");
        this.id = id;
        this.brand_id = 0;
        this.model_id = 0;
        //this.brand_lb.onchange =  this.on_brand_change.bindAsEventListener(this);
        this.brand_lb.observe('change', this.on_brand_change.bindAsEventListener(this));      
    },
    
    get_model_options: function(s_id)
    {
        if(parseInt(this.brand_id)==-1)
            return;        
        var params = {opt: 'models', id: this.id, brand_id: this.brand_id, selected_id: s_id };
        var o_id = this.id + "_model_id";
        $(o_id).disable();
        new Ajax.Updater(o_id, 'model_ctrl.php', { parameters: params, onComplete: function(t) { $(o_id).enable(); } } );      
    },
    
    get_brand_options: function(distributor_id)
    {       
        var params = {opt: 'brands', id: this.id, 'distributor_id': distributor_id };        
        var o_id = this.id + "_brand_id";
        $(o_id).disable();
        new Ajax.Updater(o_id, 'model_ctrl.php', { parameters: params, onComplete: function(t) { $(o_id).enable(); } } );       
    },    
    
    on_brand_change: function(evt)
    {
        this.model_id = 0;
        this.brand_id = $F(this.id + "_brand_id");
        this.model_lb.options.length = 0;
        this.model_lb.options[0] = new Option("Loading Models...",0);  
        this.get_model_options();
        //alert(this.brand_id);
    },    
   
    set_model_brand: function(unit_model_id,brand_id)
    {
        this.model_id = unit_model_id;
        this.brand_id = brand_id;
        this.brand_lb.value = brand_id;
        this.get_model_options(unit_model_id);        
    },
    
    reset: function()
    {
        this.brand_lb.value = -1;
        this.model_lb.options.length = 0;
        this.model_lb.options[0] = new Option("Select Brand",0);  
        this.model_lb.disabled = true;        
    }
});    
