﻿jQuery(document).ready(function() {
    var exclude = true;

    primeDDs();

    exclude = false;

    jQuery("#dnn_ctr662_PreOwnedSearchHome_ddlMake").change(function() {		// Add an event listener to the Saddle type.
        if (!exclude) {
            exclude = true;

            removeOptions('dnn_ctr662_PreOwnedSearchHome_ddlModel');   // Remove all options

            if (jQuery(this).val() != "") {
                buildOptions(jQuery(this).val(), jQuery("#dnn_ctr662_PreOwnedSearchHome_ddlYear").val(), jQuery("#dnn_ctr662_PreOwnedSearchHome_ddlYear").val(), "");
            }
            else {
                jQuery('#dnn_ctr662_PreOwnedSearchHome_ddlModel').append('<option value="">Any</option>');

                buildOptions(jQuery(this).val(), "", "", "");
            }

            exclude = false;
        }
    });

    jQuery("#dnn_ctr662_PreOwnedSearchHome_ddlYear").change(function() {		// Add an event listener to the Saddle type.
        if (!exclude) {
            exclude = true;

            removeOptions('dnn_ctr662_PreOwnedSearchHome_ddlYearTo');   // Remove all options

            if (jQuery(this).val() != "") {
                var d = new Date();
                for (var i = parseInt(jQuery(this).val()); i <= d.getFullYear(); i++) {
                    jQuery('#dnn_ctr662_PreOwnedSearchHome_ddlYearTo').append('<option value="' + i + '">' + i + '</option>');
                }

                buildMakes(jQuery(this).val(), jQuery(this).val(), "");
                buildOptions("", jQuery(this).val(), jQuery(this).val(), "");
            }
            else {
                jQuery('#dnn_ctr662_PreOwnedSearchHome_ddlYearTo').append('<option value="">Any</option>');

                buildMakes(jQuery(this).val(), jQuery(this).val(), "");
                buildOptions("", jQuery(this).val(), jQuery(this).val(), "");
            }

            exclude = false;
        }
    });

    jQuery("#dnn_ctr662_PreOwnedSearchHome_ddlYearTo").change(function() {		// Add an event listener to the Saddle type.
        if (!exclude) {
            exclude = true;

            removeOptions('dnn_ctr662_PreOwnedSearchHome_ddlMake');   // Remove all options

            if (jQuery(this).val() != "") {
                buildMakes(jQuery("#dnn_ctr662_PreOwnedSearchHome_ddlYear").val(), jQuery(this).val(), "");
                buildOptions("", jQuery("#dnn_ctr662_PreOwnedSearchHome_ddlYear").val(), jQuery(this).val(), "");
            }
            else {
                jQuery('#dnn_ctr662_PreOwnedSearchHome_ddlMake').append('<option value="">Any</option>');
            }

            exclude = false;
        }
    });
});

// Function that appends options to a named selection
function primeDDs() {
    buildMakes(jQuery('#dnn_ctr662_PreOwnedSearchHome_ddlYear').val(), jQuery('#dnn_ctr662_PreOwnedSearchHome_ddlYear').val(), "load");

    buildOptions("", jQuery('#dnn_ctr662_PreOwnedSearchHome_ddlYear').val(), jQuery('#dnn_ctr662_PreOwnedSearchHome_ddlYear').val(), "load");
}

// Function that appends options to a named selection
function buildOptions(make, fromDate, toDate, mode) {
    jQuery.ajax({
        type: "GET",
        dataType: "xml",
        cache: false,
        url: "/DesktopModules/PreOwnedSearchHome/Model.aspx",
        data: "m=" + make + "&f=" + fromDate + "&t=" + toDate,
        success: function(data) {
            removeOptions('dnn_ctr662_PreOwnedSearchHome_ddlModel');   // Remove all options

            jQuery('model', data).each(function() {
                var id = jQuery(this).find('id').text();
                var name = jQuery(this).find('name').text();

                jQuery('#dnn_ctr662_PreOwnedSearchHome_ddlModel').append('<option value="' + id + '">' + name + '</option>');
            });
        }
    });
}

// Function that appends options to a named selection
function buildMakes(from, to, mode) {
    jQuery.ajax({
        type: "GET",
        dataType: "xml",
        cache: false,
        url: "/DesktopModules/PreOwnedSearchHome/Make.aspx",
        data: "f=" + from + "&t=" + to,
        success: function(data) {
            removeOptions('dnn_ctr662_PreOwnedSearchHome_ddlMake');   // Remove all options

            jQuery('make', data).each(function() {
                var id = jQuery(this).find('id').text();
                var name = jQuery(this).find('name').text();

                jQuery('#dnn_ctr662_PreOwnedSearchHome_ddlMake').append('<option value="' + id + '">' + name + '</option>');
            });
        }
    });
}

// Function to remove all children within a named selection
function removeOptions(whichList) {
    $('#' + whichList).empty();
}
