if(!window.ScnUtil) {
    window.ScnUtil = {};
}

ScnUtil.radioButton = function(radio) {
    if(radio && radio.name) {
        var id = radio.name.substring(radio.name.lastIndexOf(':'));
        var el = radio.form.elements;

        for (var i = 0; i < el.length; i++) {
            if (el[i].name.substring(el[i].name.lastIndexOf(':')) == id) {
                el[i].checked = false;
            }
        }

        radio.checked = true;
    }
}

ScnUtil.selectRadio = function(form, radioComponentId, value) {
    if(form) {
        var el = form.elements;

        for (var i = 0; i < el.length; i++) {
            var lastIndex = el[i].name.lastIndexOf(':');

            if(lastIndex >= 0) {
                if (el[i].name.substring(lastIndex + 1) == radioComponentId) {
                    if(el[i].value == value) {
                        el[i].checked = true;
                    } else {
                        el[i].checked = false;
                    }
                }
            }
        }
    }
}

ScnUtil.SelectAllTitle = 'Remove Selections';

ScnUtil.onSelectOneClick = function(selectOneCheckbox, selectAllCheckboxComponentId) {
    if(selectOneCheckbox) {
        var el = selectOneCheckbox.form.elements;
        var selectOneCheckboxComponentId = selectOneCheckbox.name.substring(selectOneCheckbox.name.lastIndexOf(':') + 1);
        var hasUncheckedCheckbox = false;
        var selectAllCheckbox = null;

        for (var i = 0; i < el.length; i++) {
            var lastIndex = el[i].name.lastIndexOf(':');

            if(lastIndex >= 0) {
                var componentId = el[i].name.substring(el[i].name.lastIndexOf(':') + 1);

                if (componentId == selectOneCheckboxComponentId) {
                    if(!el[i].checked) {
                        hasUncheckedCheckbox = true;
                    }
                }

                if (componentId == selectAllCheckboxComponentId) {
                    selectAllCheckbox = el[i];
                }
            }
        }

        if(hasUncheckedCheckbox) {
            selectAllCheckbox.checked = false;
            selectAllCheckbox.title = '';
        }
    }
}


ScnUtil.onSelectOnlyOneClick = function(selectOneCheckbox, selectOneCheckboxComponentId) {

    if(selectOneCheckbox)
    {
        var checked = selectOneCheckbox.checked;
        var el = selectOneCheckbox.form.elements;

        for (var i = 0; i < el.length; i++)
        {
            if( el[i].name.substring(el[i].name.lastIndexOf(':') + 1) == selectOneCheckboxComponentId )
            {
                el[i].checked = false;
            }
        }

        selectOneCheckbox.checked = checked;
    }
}

ScnUtil.onSelectAllClick = function(selectOneCheckboxComponentId, selectAllCheckbox) {
    if(selectAllCheckbox) {
        var el = selectAllCheckbox.form.elements;

        for (var i = 0; i < el.length; i++) {
            if (el[i].name.substring(el[i].name.lastIndexOf(':') + 1) == selectOneCheckboxComponentId) {
                el[i].checked = selectAllCheckbox.checked;
            }
        }

        if(selectAllCheckbox.checked) {
            selectAllCheckbox.title = ScnUtil.SelectAllTitle;
        } else {
            selectAllCheckbox.title = '';
        }
    }
}

ScnUtil.scrollToTop = function() {
    if(window.scrollTo) {
        window.scrollTo(0, 0);
    }
}

ScnUtil.refreshRowClasses = function(tableId, oddRowClass, evenRowClass) {
    var table = $(tableId);

    if(table) {
        var rows = table.rows;

        if(rows) {
            var count = 0;
            for(var i = 0; i < rows.length; i++) {
                if(rows[i].style.display != 'none')    {
                    if(count % 2 == 0) {
                        rows[i].className = evenRowClass;
                    } else {
                        rows[i].className = oddRowClass;
                    }

                    count++;
                }
            }

            var tfoot = table.getElementsByTagName('tfoot');
            if(tfoot) {
                if(tfoot.length > 0) {
                    rows = tfoot[0].rows;
                    for(var i = 0; i < rows.length; i++) {
                        if(rows[i].style.display != 'none')    {
                            if(count % 2 == 0) {
                                rows[i].className = oddRowClass;
                            } else {
                                rows[i].className = evenRowClass;
                            }

                            count++;
                        }
                    }
                }
            }
        }
    }
}
