var dialogWindow;

$(document).ready(function () {
    $("input[type=radio]").css("margin-right", "5px");
    $('#listTree input[type=checkbox]').live("change",  recheckServices);

    $('input[id$=All]').bind("change", function (e) {
        var div = $(this).closest("div");
        if (div.length)
        {
            var id = div.attr("id");
            if (id != undefined && id.toLowerCase() == 'listuserall') {
                $("#listUserId option").removeAttr("selected");
                $("ul[class='users']").html("");
            }
        }
    });

    $('input[id$=All]').bind("click", function (e) {
        var inputId = $(this).attr("name") + "Select";
        var block = $("#" + inputId).parent();
        var select = block.find("select");
        
        if (select.length) {
            var id = $(select).attr("id");
            var objs = $('input[id^=' + id + ']');
            var s = $(select).find("option");

            objs.removeAttr("checked");
            s.removeAttr("selected");

            var text = MultipleSelectResources.Choose;
            block.find("a:first").attr('title', text).html(text);
        }
        
    });
    
    $("#content").show();
    $("#weight").focus();    
    
    if ($.multipleSelect) $.multipleSelect.initByClassName('.multipleselect', { maxwidth: '300px', init: true });
});

var ServiceLevelRule = {
    selectService: function selectService(url, msName) {
        if (url.indexOf('?') > -1) {
            url += "&";
        } else {
            url += "?";
        }

        url += ('msName=' + msName);
        url += '&cbmode=ServiceLevelRule';

        dlg = openDialog(url, 'selectservices', { width: 900, height: 650 });
        dlg.callback = ServiceLevelRule.servicesCallback;
        return false;
    },
    servicesCallback: function servicesCallback(result) {
        var msName = result['msName'];
        addSelectedValues(result, msName);
    },

    selectTaskType: function selectTaskType(url, msName) {
        if (url.indexOf('?') > -1) {
            url += "&";
        } else {
            url += "?";
        }

        url += ('msName=' + msName);
        url += '&cbmode=ServiceLevelRule';

        dlg = openDialog(url, 'selecttasktypes', { width: 900, height: 650 });
        dlg.callback = ServiceLevelRule.taskTypesCallback;
        return false;
    },
    taskTypesCallback: function taskTypesCallback(result) {
        var msName = result['msName'];
        addSelectedValues(result, msName);
    },

    selectAssetType: function selectAssetType(url, msName) {
        if (url.indexOf('?') > -1) {
            url += "&";
        } else {
            url += "?";
        }

        url += ('msName=' + msName);
        url += '&cbmode=ServiceLevelRule';

        dlg = openDialog(url, 'selectassettypes', { width: 900, height: 650 });
        dlg.callback = ServiceLevelRule.assetTypesCallback;
        return false;
    },
    assetTypesCallback: function assetTypesCallback(result) {
        var msName = result['msName'];
        addSelectedValues(result, msName);
    },

    selectCategory: function selectCategory(url, msName) {
        if (url.indexOf('?') > -1) {
            url += "&";
        } else {
            url += "?";
        }

        url += ('msName=' + msName);
        url += '&cbmode=ServiceLevelRule';

        dlg = openDialog(url, 'selectcategories', { width: 990, height: 660 });
        dlg.callback = ServiceLevelRule.categoriesCallback;
        return false;
    },
    categoriesCallback: function categoriesCallback(result) {
        var msName = result["msName"];
        addSelectedPaths(result, msName);
    },

    selectCompany: function selectCompany(url, msName) {
        if (url.indexOf('?') > -1) {
            url += "&";
        } else {
            url += "?";
        }

        url += ('msName=' + msName);
        url += '&cbmode=ServiceLevelRule';

        dlg = openDialog(url, 'selectcompanies', { width: 990, height: 660 });
        dlg.callback = ServiceLevelRule.companiesCallback;
        return false;
    },
    companiesCallback: function companiesCallback(result) {
        var msName = result["msName"];
        addSelectedValues(result, msName);
    },

    selectUser: function selectUser(url, msName) {
        if (url.indexOf('?') > -1) {
            url += "&";
        } else {
            url += "?";
        }

        url += ('msName=' + msName);
        url += '&cbmode=ServiceLevelRule';

        dlg = openDialog(url, 'selectusers', { width: 990, height: 660 });
        dlg.callback = ServiceLevelRule.usersCallback;
        return false;
    },
    usersCallback: function usersCallback(result) {
        var msName = result["msName"];
        addSelectedValues(result, msName);
    },
}