﻿////////////////////////////////////////////////////////////////////////////////////
// FORM CLEARING FUNCTIONALITY

/**
* Clears the form data.  Takes the following actions on the form's input fields:
*  - input text fields will have their 'value' property set to the empty string
*  - select elements will have their 'selectedIndex' property set to 0
*  - checkbox and radio inputs will have their 'checked' property set to false
*  - inputs of type submit, button, reset, and hidden will *not* be affected
*  - button elements will *not* be affected
*/

Search =
{
}

$.fn.clearForm = function() {
    return this.each(function() {
        $('input,select,textarea', this).clearFields();
    });
};

/**
* Clears the selected form elements.
*/
$.fn.clearFields = $.fn.clearInputs = function() {
    return this.each(function() {
        var t = this.type, tag = this.tagName.toLowerCase();
        if (t == 'text' || t == 'password' || tag == 'textarea')
            this.value = '';
        else if (t == 'checkbox' || t == 'radio')
            this.checked = false;
        else if (tag == 'select')
            this.selectedIndex = 0;
    });
};

// call this to clear search form
/*
$(function() {
    $('a.clearSearch').click(function() {
        $(this).closest('form').clearForm();
        return false;
    });
});
*/

////////////////////////////////////////////////////////////////////////////////////
// MAKE / MODEL INTERACTION

// generic event to update dropdown based on passed in url
(function($) {
    $.fn.selectChain = function(options) {
        var defaults = { key: "Id", value: "Name" };
        var settings = $.extend({}, defaults, options);

        if (!(settings.target instanceof $)) settings.target = $(settings.target);

        return this.each(function() {
            var $$ = $(this);

            $$.change(function() {
                var data = null;
                if (typeof settings.data == 'string') {
                    data = settings.data + '&' + this.name + '=' + $$.val();
                } else if (typeof settings.data == 'object') {
                    data = settings.data;
                    data[this.name] = $$.val();
                }

                settings.target.empty();

                // did we get a default top option passed in
                if (settings.top) {
                    var o = document.createElement("OPTION");
                    o.text = settings.top;
                    settings.target.get(0).options[0] = o;
                }

                $.ajax({
                    url: settings.url(),
                    data: data,
                    type: (settings.type || 'get'),
                    dataType: 'json',
                    success: function(j) {

                        for (var i = 0; i < j.length; i++) {
                            // required to get around IE bug (http://support.microsoft.com/?scid=kb%3Ben-us%3B276228)
                            var o = document.createElement("OPTION");
                            o.id = typeof j[i] == 'object' ? j[i][settings.key] : j[i];
                            o.text = typeof j[i] == 'object' ? j[i][settings.value] : j[i];
                            settings.target.get(0).options[i + 1] = o;
                        }

                        // hand control back to browser for a moment
                        setTimeout(function() {
                            settings.target
                                .find('option:first')
                                .attr('selected', 'selected')
                                .parent('select')
                                .trigger('change');
                        }, 0);
                    },
                    error: function(xhr, desc, er) {
                        // add whatever debug you want here.
                        alert("An error occurred");
                    }
                });
            });
        });
    };
})(jQuery);

// sets up the connection between make and models drop downs
$(function() {
    var makes = $(Search.Config.makeId);

    var topItem = (Search.Config.modelId == '#pa_model') ? '- Please Choose -' : 'All Models';

    makes.selectChain({
        target: $(Search.Config.modelId),
        url: getModelsUrl,
        top: topItem,
        type: 'post',
        data: "ajax=true"
    });
});

// returns url to get models based on current make selection
function getModelsUrl() {
    var make = $(Search.Config.makeId);
    var makeId = make[0].options[make[0].selectedIndex].id;
    if (makeId != null)
        makeId = makeId.replace(' ', '_');
    return modelUrl + makeId;
}

Search.Config =
{
    makeId: "#s_make",
    modelId: "#s_model"
}


////////////////////////////////////////////////////////////////////////////////////
