(function($) {
    $.fn.inputDefualts = function(options) {
        // default
        var defaults = {
            cl: 'inactive', // for inactive
            text: 'part number or model' 
        }, 	opts = $.extend(defaults, options);

        this.addClass(opts['cl']); 
        this.val(opts['text']);	

        this.focus(function() {
            if($(this).val() == opts['text']) $(this).val(''); // nulled
            $(this).removeClass(opts['cl']); 
        });

        this.blur(function() {
            if($(this).val() == '') {
                $(this).val(opts['text']); 
                $(this).addClass(opts['cl']); 
            }
        });
    };

})(jQuery);
    
$(document).ready(function() {
    // search form
    $('#what').inputDefualts({
        cl: 'inactive',
        text: 'part number or model'
    });

    $("#update_indicator").ajaxStart(function(){
        $(this).show();
        $("#selectmodel").attr('disabled', true);
    });
    $("#update_indicator").ajaxStop(function(){
        $(this).hide();
        $("#selectmodel").removeAttr('disabled');
    });

    // 3-way filter
    $.ajaxSetup({
        url:"filter",
        type: "POST",
        timeout: "25000",
        cache: false
    });

    // on change brand
    $("select#selectbrand").change( function() {
        var curbrand = $('select#selectbrand option:selected').val();
        $.ajax({
            data: "brand="+curbrand,
            success: function(data){
                // replace model select
                var dataobject = eval('(' + data + ')');
                $('select#[name=model] option').remove();

                $.each(dataobject, function(i,item){
                    $('select#[name=model]').append('<option value="'+item.id+'" >'+item.name+'</option>');
                });

            },
            error: function(xhr, status){
                alert("Error "+status);
            }
        });
    });
});
