﻿$(document).ready(function() {
    $("[id$='ddlMake']").change(function() { AddLoadingImg(this); Make_OnChange(); });
    $("[id$='ddlSeries']").change(function() { AddLoadingImg(this); Series_OnChange(); });
    $("[id$='ddlModel']").change(function() { AddLoadingImg(this); Model_OnChange(); });
    $("[id$='ddlComponent']").change(function() { AddLoadingImg(this); Component_OnChange(); });
    $("[id$='ddlSubComponent']").change(function() { AddLoadingImg(this); SubComponent_OnChange(); });
    SetInitialVisibility();
    SetSteps();
});

var whse = (function() { return $("#whse").val(); });
var make = (function() { return $("[id$='ddlMake']").attr("value"); });
var series = (function() { return $("[id$='ddlSeries']").attr("value"); });
var model = (function() { var m = $("[id$='ddlModel']").attr("value"); if(m == null || m == "") m = $("#_SingleModel").attr("value"); return m; });
var component = (function() { return $("[id$='ddlComponent']").attr("value"); });
var subcomponent = (function() { return $("[id$='ddlSubComponent']").attr("value"); });

function PopulateDropdown(opts, dd, firstOption) {
    $(dd).empty().append("<option value=\"\">" + firstOption + "</option>").parent().css('display','block');
    $.each(opts, function() {
        $(dd).append("<option value=\"" + this.Value + "\">" + this.Text + "</option>")
        });
        
    SetVisibility(dd);
}

function AddLoadingImg(obj) {
    $("#imgLoading").clone().attr("id","imgLoadingClone").insertAfter(obj);
}

function SetVisibility(dd) {
    $(dd).parent().nextAll(".ddlRow").find("select").empty().parent().css("display","none");
}

function SetInitialVisibility() {
    $(".ddlRow").find("select").each(function(i) { if($(".ddlRow").find("select")[i].options.length > 1) { $(this).parent().css("display","block"); }});
}

function SetSteps() {
    $(".ddlRow:visible span").each(function(i) { $(this).html("STEP " + (i + 1) + ": Select "); });
}

function DetectSingleModel(_model) {
    if (_model != "") {
        $("#_SingleModel").attr("value", _model);
        $("[id$='ddlModel']").empty().append("<option value=\"\">Model...</option>").parent().css('display', 'block');
        $("[id$='ddlModel']").append("<option value=\"" + _model + "\">" + _model + "</option>")
        $("[id$='ddlModel']").attr("value", _model);

        //hide the model drop down if the single model is the same as the series.
        if (_model == series())
            $("[id$='ddlModel']").parent().css("display", "none"); //hide the model drop down only
        
        //$("[id$='ddlModel']").parent().css("display", "none"); //hide the model drop down only
        SetSteps(); //renumber steps
    }
}

function GetDropdownData(fn, d) {
    $.ajax({
        url: "/Ajax/DropDownService.asmx/" + fn,
        cache: false, type: "POST", data: d,
        contentType: "application/json; charset=utf-8", dataType: "json",
        success: DropDownChange_OnSuccess
        });
}

function SubComponent_OnChange() {
    GetDropdownData("GetResultsUrl", '{whse:"' + whse() + '",make:"' + make() + '",series:"' + series() + '",model:"' + model() + '",component:"' + component() + '",subcomponent:"' + subcomponent() + '"}');
}

function Series_OnChange() {
    GetDropdownData("GetComponentsOrModels", '{whse:"' + whse() + '",make:"' + make() + '",series:"' + series() + '",model:""}');
}

function Model_OnChange() {
    GetDropdownData("GetComponentsOrModels", '{whse:"' + whse() + '",make:"' + make() + '",series:"' + series() + '",model:"' + model() + '"}');
}

function Make_OnChange() {
    GetDropdownData("GetSeries", '{whse:"' + whse() + '",make:"' + make() + '"}');
}

function GetMakes() {
    GetDropdownData("GetMakes", '{whse:"' + whse() + '"}');
}

function Component_OnChange() {
    GetDropdownData("GetSubComponents",'{whse:"' + whse() + '",make:"' + make() + '",series:"' + series() + '",model:"' + model() + '",component:"' + component() + '"}');
}

function DropDownChange_OnSuccess(r) { 
    $("#imgLoadingClone").remove();
    if(r != null && r.d != null) {
        if(r.d.Url != null) {
            window.location = r.d.Url;
        }
        else {
            PopulateDropdown(r.d.Options, $(r.d.DropDownID), r.d.FirstElement);
            SetSteps();
            DetectSingleModel(r.d.SingleModel);
        }
    }
}
