var val_grpl_sonst = 0;

function init(mode) {
    optBuildClass();
    
    if (val_rvcateg > 0) { 
        optRvProd(); 
        optGroundplan(); 
        optModel();
    } 
    
    if (is_profi == 1) {
        optProd(); 
        optType(); 
        optFuel();
    }
}

jQuery(document).ready(function() { 
    jQuery("td.rv_cat input[type=radio]").bind("change", function() {
        val_rvcateg = this.value;
        val_rvprod  = 0;
        val_grpl    = 0;
        val_model   = 0;
        optRvProd();
        optGroundplan();
        optModel();
        
        val_buildc = 0;
        optBuildClass();
        
        if (is_profi == 1) {
            val_prod = 0;
            val_type = 0;
            val_fuel = 0;
            optProd();
            optType();
            optFuel();
        }
    });
});

jQuery(document).ready(function() { 
    jQuery("td.rvprod_id select").bind("change", function() {
        val_rvprod = this.value;
        val_grpl   = 0;
        val_model  = 0;
        optGroundplan();
        optModel();
    });
});

jQuery(document).ready(function() { 
    jQuery("td.plan select").bind("change", function() {
        val_grpl  = this.value;
        val_model = 0;
        optModel(1);
    });
});

jQuery(document).ready(function() { 
    jQuery("td.prod_id select").bind("change", function() {
        val_prod = this.value;
        val_type = 0;
        optType();
    });
});

function optBuildClass() {
    delOpt('b_class');
    var index = 0;
    var h_sub = new String(val_rvcateg);
    for (var i in BUILDC) {
        var hilf_build = BUILDC[i][1];
        if (hilf_build.indexOf(h_sub)>0) {
            document.womo.b_class.options[document.womo.b_class.length] = new Option(BUILDC[i][0], i, false, true);
            if (i==val_buildc) { index = document.womo.b_class.length - 1; }
        }
    }
    document.womo.b_class.options[index].selected = true;
}

function optRvProd() {  
    delOpt('rvprod_id');
    var index = 0;
    if (val_rvcateg > 0) {
        eval('var ARR = RVPROD_'+val_rvcateg+';');
        for (var i in ARR) {
            document.womo.rvprod_id.options[document.womo.rvprod_id.length] = new Option(ARR[i][0], i, false, true);
            if (i==val_rvprod) { index = document.womo.rvprod_id.length - 1; }
        }
        document.womo.rvprod_id.options[index].selected = true;
    } else {  
        document.womo.rvprod_id.options[0] = new Option("---", "", false, true);
    }

    if (is_profi==1) {
        if (val_rvcateg!=2) {
            document.womo.prod_id.disabled    = false;
            document.womo.type_id.disabled    = false;
            document.womo.fuel.disabled       = false;
            document.womo.km_min.disabled     = false;
            document.womo.km_max.disabled     = false;
            document.womo.kw_min.disabled     = false;
            document.womo.kw_max.disabled     = false;
            document.womo.asu.disabled        = false;
            document.womo.type_addon.disabled = false;
        } else {
            document.womo.prod_id.disabled    = true;
            document.womo.type_id.disabled    = true;
            document.womo.fuel.disabled       = true;
            document.womo.km_min.value        = 0;
            document.womo.km_min.disabled     = true;
            document.womo.km_max.value        = 0;
            document.womo.km_max.disabled     = true;
            document.womo.kw_min.value        = 0;
            document.womo.kw_min.disabled     = true;
            document.womo.kw_max.value        = 0;
            document.womo.kw_max.disabled     = true;
            document.womo.asu.value           = 0;
            document.womo.asu.disabled        = true;
            document.womo.type_addon.value    = "";
            document.womo.type_addon.disabled = true;
        }
    }
}

function optGroundplan() {
    delOpt("plan");
    var index = 0;
    document.womo.plan.options[0] = new Option("Alle Grundrisse", "", false, true);
    if (val_rvprod > 0) {
        eval('var ARR = RVPROD_'+val_rvcateg+'['+val_rvprod+'];');
        for (var i=1;i<ARR.length;i++) {
            document.womo.plan.options[document.womo.plan.length] = new Option(ARR[i][1], ARR[i][0], false, true);
            if (val_grpl > 0) {
                if (ARR[i][0]==val_grpl) { index = document.womo.plan.length - 1; }
            } else {
                if (ARR[i][1]=="Sonstige") { 
                    val_grpl = ARR[i][0];
                    val_grpl_sonst = val_grpl;
                }
            }
        }
        document.womo.plan.options[index].selected = true;
    }
}

function optProd() {
    delOpt('prod_id');
    var index = 0;
    document.womo.prod_id.options[0] = new Option("Alle Marken", "", false, true);
    if (val_rvcateg!=2) {
        for (var i in PROD) {
            document.womo.prod_id.options[document.womo.prod_id.length] = new Option(PROD[i][0], i, false, true);
            if (i==val_prod) { index = document.womo.prod_id.length - 1; }
        }
        document.womo.prod_id.options[index].selected = true;
    }
}

function optType() {
    delOpt('type_id');
    var index = 0;
    document.womo.type_id.options[0] = new Option("Alle Modelle", "", false, true);
    if (val_prod > 0) {
        eval('var ARR = PROD;');
        for (var i=1;i<ARR[val_prod].length;i++) {
            document.womo.type_id.options[document.womo.type_id.length] = new Option(ARR[val_prod][i][1], ARR[val_prod][i][0], false, true);
            if (ARR[val_prod][i][0]==val_type) { index = document.womo.type_id.length - 1; }
        }
        document.womo.type_id.options[index].selected = true;
    }
}

function delOpt(name) {
    var len = eval('document.womo.'+name+'.length;');
    for (i=0;i<len;i++){
        eval('document.womo.'+name+'.options[document.womo.'+name+'.length-1] = null;');
    }
}

function showOpt(arr, name, selected) {
    var index = 0;
    for(var i in arr) {
        eval('document.womo.'+name+'.options[document.womo.'+name+'.length] = new Option(arr[i], i, false, true);');
        if(i==selected) { index = eval('document.womo.'+name+'.length - 1;'); }
    }
    eval('document.womo.'+name+'.options['+index+'].selected = true;');
}

function optModel(flag) {
    if (flag==1) {
        if (!val_grpl) {
            val_grpl = val_grpl_sonst;
        }
    }

    var name = "model";
    var ind_grpl = 0;
    if (val_rvprod > 0) {
        eval('var ARR = RVPROD_'+val_rvcateg+'['+val_rvprod+'];');
        for (var j=1;j<ARR.length;j++) {
            if (ARR[j][0]==val_grpl) { ind_grpl = j; }
        }
    }

    delOpt(name);
    var index = 0;
    eval('document.womo.'+name+'.options[0] = new Option("Alle Modelle", "", false, true);');
    if(ind_grpl > 0) {
        eval('var ARR = RVPROD_'+val_rvcateg+'['+val_rvprod+'];');
        var str = new String(ARR[ind_grpl][2]);
        var toks = str.split(";");
        for (var i in MODEL) {
            for (var j=0;j<toks.length;j++) {
                if (toks[j]==i) {
                    eval('document.womo.'+name+'.options[document.womo.'+name+'.length] = new Option(MODEL[i], i, false, true);');
                    if (i==val_model) { index = eval('document.womo.'+name+'.length - 1;'); }
                }
            }
        }
        eval('document.womo.'+name+'.options['+index+'].selected = true;');
    }
}

function optFuel() {
    var name = "fuel";
    delOpt(name);
    var index = 0;
    if (val_rvcateg!=2) {
        for (var i in FUEL) {
            eval('document.womo.'+name+'.options[document.womo.'+name+'.length] = new Option(FUEL[i], i, false, true);');
            if (i==val_fuel) { index = eval('document.womo.'+name+'.length - 1;'); }
        }
        eval('document.womo.'+name+'.options['+index+'].selected = true;');
    } else {
        eval('document.womo.'+name+'.options[0] = new Option("beliebig", "", false, true);');
    }
}