﻿$(document).ready(function() {
    initDealCheck();
});

function initDealCheck() {
    dropdown_makeId = $("#makeId");
    dropdown_modelId = $("#modelId");
    dropdown_editionId = $("#editionId");

    //Add a change event to the important dropdowns
    $(dropdown_makeId).change(function() { updateModel(this.value); });
    $(dropdown_modelId).change(function() { updateEdition(this.value); });

    //Trigger the change event for anyone who's used their back button
    $(dropdown_makeId).change();
}

function updateModel(parentValue) {
    //Trigger the loading message
    $(dropdown_modelId).loadingDropdown();
    //Request the data
    $.getJSON(dealCheckModelJSONURL, { makeId: parentValue }, function(json) { endUpdateModel(json); });
}

function endUpdateModel(json) {
    var modelList = buildModelList(json);

    //Remove the loading message
    $(dropdown_modelId).removeOptionDropdown(0);
    //Add the any item tag
    $(dropdown_modelId).addOptionDropdown("and choose a model", "null");
    //Fill up the dropdown
    $(dropdown_modelId).populateDropdown(modelList);
    //Re-enable the dropdown
    $(dropdown_modelId).removeAttr("disabled");
}

function updateEdition(parentValue) {
    //Trigger the loading message
    $(dropdown_editionId).loadingDropdown();
    //Request the data
    $.getJSON(dealCheckEditionJSONURL, { modelId: parentValue }, function(json) { endUpdateEdition(json); });
}

function endUpdateEdition(json) {
    var modelList = buildModelVersionList(json);

    //Remove the loading message
    $(dropdown_editionId).removeOptionDropdown(0);
    //Add the any item tag
    $(dropdown_editionId).addOptionDropdown("and choose a version", "null");
    //Fill up the dropdown
    $(dropdown_editionId).populateDropdown(modelList);
    //Re-enable the dropdown
    $(dropdown_editionId).removeAttr("disabled");
}


function buildModelList(json) {
    //Because the JSON contains 2 keys per item, we need to amalgamate them into an ID/Name list
    var modelList = new Array();
    for (var i = 0; i < json.length; i++) {
        var item = new Object();
        item.value = json[i].id;
        var modelName = (json[i].modelName == null) ? "Unnamed" : json[i].modelName;
        item.text = modelName;
        modelList.push(item);
    }
    return (modelList);
}

function buildModelVersionList(json) {
    //Because the JSON contains 2 keys per item, we need to amalgamate them into an ID/Name list
    var modelList = new Array();
    for (var i = 0; i < json.length; i++) {
        var item = new Object();
        item.value = json[i].id;
        var modelName = (json[i].bodyRangeName == null) ? "Unnamed" : json[i].bodyRangeName;
        modelName += (json[i].editionName == null) ? " Unnamed" : " " + json[i].editionName;
        modelName += (json[i].doors == null) ? "" : " " + json[i].doors + " dr";
        item.text = modelName;
        modelList.push(item);
    }
    return (modelList);
}
