﻿jQuery(document).ready(function() {
    var exclude = true;

    primeDDs();

    exclude = false;

    jQuery("#dnn_ctr463_LaserSearch_ddlManu").change(function() {		// Add an event listener to the Saddle type.
        if (!exclude) {
            exclude = true;

            removeOptions('dnn_ctr463_LaserSearch_ddlModel');   // Remove all options

            if (jQuery(this).val() != "") {
                buildOptions(jQuery(this).val());
            }
            else {
                jQuery('#dnn_ctr463_LaserSearch_ddlModel').append('<option value="0"> </option>');
            }

            exclude = false;
        }
    });
});

// Function that appends options to a named selection
function primeDDs() {
    var manu;

    if (jQuery('#dnn_ctr463_LaserSearch_manu').val() != undefined && jQuery('#dnn_ctr463_LaserSearch_manu').val() != "") {
        manu = jQuery('#dnn_ctr463_LaserSearch_manu').val();
    }
    else {
        manu = "1";
    }

    if (manu != "") {
        buildOptions(manu);
    }
}

// Function that appends options to a named selection
function buildOptions(manu) {
    jQuery.ajax({
        type: "GET",
        dataType: "xml",
        cache: false,
        url: "/DesktopModules/LaserSearch/Model.aspx",
        data: "manu=" + manu,
        success: function(data) {
            removeOptions('dnn_ctr463_LaserSearch_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_ctr463_LaserSearch_ddlModel').append('<option value="' + id + '">' + name + '</option>');
            });

            // If there is a value set, we need to select the item
            if (jQuery('#dnn_ctr463_LaserSearch_model').val() != undefined) {
                jQuery('#dnn_ctr463_LaserSearch_ddlModel').val(jQuery('#dnn_ctr463_LaserSearch_model').val());
            }
        }
    });
}

// Function to remove all children within a named selection
function removeOptions(whichList) {
    $('#' + whichList).empty();
}
