<?php
    $before = create_function(
        '$_product',
        '<?php if(!$_product->isComposite() && $_product->isSaleable()): ?>'
            . '<?php if (!$_product->getRequiredOptions()): ?>'
               . '<input type="checkbox" class="checkbox related-checkbox" id="related-checkbox<?php echo
               $_product->getId() ?>" name="related_products[]" value="<?php echo $_product->getId() ?>" />'
            . '<?php endif ?>'
        . '<?php endif ?>'
    );
?>

<?php
    echo Mage::helper('designer')->createSlider('product_slider')
        ->setItems($this->getItems())->setSliderId('related_slider')
        ->setBefore($before)->toHtml()
?>

<script type="text/javascript">
//<![CDATA[
$$('.related-checkbox').each(function(elem){
    Event.observe(elem, 'click', addRelatedToProduct)
});

var relatedProductsCheckFlag = false;
function selectAllRelated(txt) {
    if (relatedProductsCheckFlag == false) {
        $$('.related-checkbox').each(function(elem){
            elem.checked = true;
        });
        relatedProductsCheckFlag = true;
        txt.innerHTML="<?php echo $this->__('unselect all') ?>";
    } else {
        $$('.related-checkbox').each(function(elem){
            elem.checked = false;
        });
        relatedProductsCheckFlag = false;
        txt.innerHTML="<?php echo $this->__('select all') ?>";
    }
    addRelatedToProduct();
}

function addRelatedToProduct(){
    var checkboxes = $$('.related-checkbox');
    var values = [];
    for(var i=0;i<checkboxes.length;i++){
        if(checkboxes[i].checked) values.push(checkboxes[i].value);
    }
    if($('related-products-field')){
        $('related-products-field').value = values.join(',');
    }
}
//]]>
</script>