﻿function populateMakes(dropDownId) {
    dropDown = document.getElementById(dropDownId);
    dropDown.options.length = 0;

    dropDown.options[0] = new Option('Bitte wählen', '0');

    $.each(arrMakes, function (index, value) {
        try {
            var id = value.split(',')[0];
            var name = value.split(',')[1];

            dropDown.options[dropDown.options.length] = new Option(name, id);
        }
        catch (Error) {
        }
    });
    return true;
}

function choosedMake(ddlMakes, modelDropDownId) {
    choosedMakeWithOptions(ddlMakes, modelDropDownId, false, false);
}

/*
ignoreModelLines: Do not show models which are model line nodes, e.g. "3er (alle)",
used for editing vehicle data
isEdit: Keep Make and Model disabled, used for editing vehicle data
*/
function choosedMakeWithOptions(ddlMakes, modelDropDownId, ignoreModelLines, isEdit) {
    ddlModels = document.getElementById(modelDropDownId);
    ddlModels.options.length = 0;
    ddlModels.options[0] = new Option('Bitte wählen', '0');
    if (ddlMakes.selectedIndex == 0) {
        ddlModels.disabled = true;
    }
    else {
        ddlModels.disabled = isEdit;
        ddlMakes.disabled = isEdit;
        var make = ddlMakes.selectedIndex;
        fillModels(make, ddlModels, ignoreModelLines);
    }
}

function fillModels(make, ddlModels, ignoreModelLines) {
    var models = arrModels[make].split(';');
    $.each(models, function (index, value) {
        var id = value.split(',')[0];
        var name = value.split(',')[1];
        if (ignoreModelLines) {
            if (id.indexOf('-') == -1) {
                ddlModels.options[ddlModels.options.length] = new Option(trimModel(name), id);
            }
        }
        else {
            ddlModels.options[ddlModels.options.length] = new Option(name, id);
        }
    });
}

// fills the modelDropDown for caravans
function populateCaravanModels(modelDropDownId) {
    // need this workaround for getting the actual index
    // of caravans, because it´s index may vary if new makes
    // are listed
    var caravanMakeId = '15672';
    var make = 0;

    for (var i = 1; i < arrMakes.length + 1; i++) {
        var m = arrMakes[i].split(',');
        if (m[0] == caravanMakeId) {
            make = i;
            break;
        }
    }

    ddlModels = document.getElementById(modelDropDownId);
    fillModels(make, ddlModels, false);
    return true;
}

function getMakeIndexByMakeName(makeName) {
    var position = -1;
    for (i = 0; i < arrMakes.length; i++) {

    }
}

function trimModel(model) {
    var result = model.replace(/^\s+/, '');
    return result;
}
