// Handle on change events for product selectors
$(function(){
    $('#SelectorForm select').change(function(event){
        // strip the 'selector' substring from the target element's id
        var action = event.target.id.substring(6).toLowerCase();
        // store $(this) so we can use it inside each loops
        var select = $(this);
        // Store adjacent select element
        var nextSelect = select.parent().next().children('select').first();
        var category = $('#SelectorForm select').first();
        // Post back data to appropriate controller action
        $.post('/selector/' + action + '/format/json', 'param=' + select.val() + '&category=' + category.val(), function(data){
                            // clear existing options
                nextSelect.html('');
            // Get field
            if(nextSelect.length != 0){
                //nextSelect.append('<option>Select a ' + nextSelect.attr('id').substring(6) + '</option>');
                $.each(data.data, function(index, field){
                    // Append options
                    nextSelect.append('<option value="' + field.value + '">' + field.option + '</option>');
                });

                nextSelect.trigger('change');
            }
        });

    });

});
$(function(){
    $('#productchain select').change(function(event){
	// strip the 'selector' substring from the target element's id
        var action = event.target.id.substring(14).toLowerCase();
        // store $(this) so we can use it inside each loops
        var select = $(this);
        // Store adjacent select element
        var nextSelect = select.parent().next().children('select').first();
	var category = $('#productchain select').first();
        // Post back data to appropriate controller action
        $.post('/selector/' + action + '/format/json', 'param=' + select.val() + '&category=' + category.val(), function(data){
	    // clear existing options
	    nextSelect.html('');
            // Get field
            if(nextSelect.length != 0){
                //nextSelect.append('<option>Select a ' + nextSelect.attr('id').substring(6) + '</option>');
                $.each(data.data, function(index, field){
                    // Append options
                    nextSelect.append('<option value="' + field.value + '">' + field.option + '</option>');
                })
                nextSelect.trigger('change');
            }
        });

    });
    $('#product_selectCategory').trigger('change');
});
