var search_types = {'stock_' : 0, 'network_' : 1};

function search_populate(prefix) {
    var ml = ManufacturerList;
    var mlEl = document.getElementById(prefix + "search_manufacturer");
    var mdEl = document.getElementById(prefix + "search_model");
    for (var i = 0; i < ml.length; i++) {
        var m = ml[i];
        var entries = m[2][search_types[prefix]];
        var n = m[1] + ' (' + entries + ')';
        var option = new Option(n, m[0]);
        option.className = entries == 0 ? 'empty' : '';
        try {
            mlEl.add(option, null);
        } catch(ex) {
            mlEl.add(option);
        }
    }
}

function search_check_model(prefix) {
    var mlEl = document.getElementById(prefix + "search_manufacturer");
    var mdEl = document.getElementById(prefix + "search_model");
    if (mlEl.selectedIndex > 0 && mdEl.options.length == 1) {
        search_update_model(prefix);
    }
}

function search_update_model(prefix) {
    var ml = ManufacturerList;
    var mlEl = document.getElementById(prefix + "search_manufacturer");
    var mdEl = document.getElementById(prefix + "search_model");
    mdEl.options.length = 1;
    if (mlEl.selectedIndex > 0) {
        var mName = mlEl.options[mlEl.selectedIndex].value;
        mdEl.disabled = false;
        for (var i = 0; i < ml.length; i++) {
            var ma = ml[i];
            if (mName == ma[0]) {
                // add options from m[3]
                var models = ma[3];
                for (var j = 0; j < models.length; j++) {
                    var model = models[j];
                    var entries = model[2][search_types[prefix]];
                    var n = model[0] + ' (' + entries + ')';
                    var option = new Option(n, model[1]);
                    option.className = entries == 0 ? 'empty' : '';
                    try {
                        mdEl.add(option, null);
                    } catch (ex) {
                        mdEl.add(option);
                    }
                }
                break;
            }
        }
    }
    search_get_count(prefix);
}

function search_check_required(prefix) {
    var mlEl = document.getElementById(prefix + "search_manufacturer");
    if (mlEl.selectedIndex == 0) {
        // alert
        alert("Please choose a make");
        return false;
    }
    //    var mdEl = document.getElementById(prefix + "search_model");
    //    if (mdEl.selectedIndex == 0){
    //        // alert in some way
    //        return false;
    //    }
    return true;
}

function search_update_filter(el, map){
    var some = false;
    for (var x in map){
        some = true;
        break;
    }
    for (var i=0;i<el.options.length;i++){
        var opt = el.options[i];
        var v = map[opt.value];
        if (opt.value != "_"){
            var idx = opt.text.indexOf(' (');
            var text = opt.text;
            if (idx > -1){
                text =  text.substr(0, idx);
            }
            if (v == null){
                opt.text = text;
                opt.className = !some ? "" : "empty";
            } else {
                opt.text = text + " (" + v + ")";
                opt.className = (v == 0) ? "empty" : "";
            }
        }
    }
}

function search_get_count(prefix) {
    var mlEl = $(prefix + "search_manufacturer");
    var mdEl = $(prefix + "search_model");
    var mbEl = $(prefix + "search_body");
    var mfEl = $(prefix + "search_fuel");
    var mtEl = $(prefix + "search_type");

    var url = "/resource/search2Json?manufacturer=" + mlEl.getValue() + "&model=" + mdEl.getValue() + "&body=" + mbEl.getValue() + "&fuel=" + mfEl.getValue() + "&saleType=" + mtEl.getValue();

    new Ajax.Request(url, {
        method: 'get',
        onSuccess: function(transport) {
            var result = transport.headerJSON;

            var mbEl = $(prefix + "search_body");
            var mfEl = $(prefix + "search_fuel");

            search_update_filter(mfEl, result.fuel);
            search_update_filter(mbEl, result.body);

            try {
                var mrEl = $(prefix + "search_results");
                mrEl.innerHTML = result.count;
            } catch (e){
                // no text found - could be because IE
            }
            try {
                var id = prefix + "search_results_flash_fb";
                var mfEl = $(id);
                mfEl.changeNum(result.count, 0.75, false);
            } catch (e){
                //
            }
            try {
                var id = prefix + "search_results_flash";
                var mfEl = $(id);
                mfEl.changeNum(result.count, 0.75, false);
            } catch (e){
                //
            }
        }
    });

}