<script type="text/javascript">
<!--
<?php $myBlock = $this->getLayout()->getBlock('user.roles.grid'); ?>
<?php if( is_object($myBlock) && $myBlock->getJsObjectName()): ?>
    var radioBoxes = $H({});
    var warning = false;
    var userRoles = $H(<?php echo $myBlock->_getSelectedRoles(true) ?>);
    if (userRoles.size() > 0) warning = true;
    $('user_user_roles').value = userRoles.toQueryString();

    function registerUserRole(grid, element, checked){
        if(checked){
            userRoles.each(function(o){userRoles.remove(o[0]);});
            userRoles[element.value] = 0;
        } else {
            userRoles.remove(element.value);
        }
        $('user_user_roles').value = userRoles.toQueryString();
        grid.reloadParams = {'user_roles[]':userRoles.keys()};
    }

    function roleRowClick(grid, event){
        var trElement = Event.findElement(event, 'tr');
        var isInput   = Event.element(event).tagName == 'INPUT';
        if(trElement){
            var checkbox = Element.getElementsBySelector(trElement, 'input');
            if(checkbox[0] && !checkbox[0].checked){
                var checked = isInput ? checkbox[0].checked : !checkbox[0].checked;
                if (checked && warning && radioBoxes.size() > 0) {
                    if ( !confirm("<?php echo $this->__('Warning!\r\nThis action will remove this user from already assigned role\r\nAre you sure?') ?>") ) {
                        checkbox[0].checked = false;
                        for(i in radioBoxes) {
                            if( radioBoxes[i].status == 1) {
                                radioBoxes[i].object.checked = true;
                            }
                        }
                        return false;
                    }
                    warning = false;
                }
                <?php echo $myBlock->getJsObjectName() ?>.setCheckboxChecked(checkbox[0], checked);
            }
        }
    }

    function rolesRowInit(grid, row){
        var checkbox = $(row).getElementsByClassName('radio')[0];
        if (checkbox) {
            radioBoxes[checkbox.value] = {'status' : ((checkbox.checked) ? 1 : 0), 'object' : checkbox};
        }
    }

<?php echo $myBlock->getJsObjectName() ?>.rowClickCallback = roleRowClick;
<?php echo $myBlock->getJsObjectName() ?>.initRowCallback = rolesRowInit;
<?php echo $myBlock->getJsObjectName() ?>.checkboxCheckCallback = registerUserRole;
<?php echo $myBlock->getJsObjectName() ?>.rows.each(function(row){rolesRowInit(<?php echo $myBlock->getJsObjectName() ?>, row)});
<?php endif; ?>
//-->
</script>
