var dlg = null;

$(document).ready(function () {
    $('#helplink').unbind('click').click(function () { openDialog($('#helplink').attr('href')); return false; });

    var url = window.location.href;
    
    if (url.length) {

        if (url.indexOf("checkrule") >= 0) {
            showCheckRule();
            $('#checkruleholder').css("left", "60px");
            $('#checkruleholder').css("margin-left", "0");
        }
    }    
});


function showCheckRule() {

    if ($('#checkruleholder #checkrule').length > 0) { $('#checkruleholder').show(); return false; }
    var url = base + tableController() + 'GetCheckRule';
    $('body').append('<div id="checkruleholder" style="display: none;" class="nopopupwrapper"></div>').find('#checkruleholder').load(url, { timestamp: new Date().toString() }, function () {
        $('#checkruleholder').show();
        if ($.multipleSelect) $.multipleSelect.initByClassName('.multipleselect', { maxwidth: '300px', init: true, single: true });
    });
    return false;
}

function checkRule() {
    var url = base + tableController() + 'CheckRule';
    var obj = $('#servicelevelResult');
    if (!obj.length) {
        obj = $('#checkruleholder #checkrule').append('<div id="servicelevelResult"></div>').find('#servicelevelResult');
    }

    obj.load(url, { ServicePath: $("input[id*=ServicePath]:checked").val(), userId: $("#userId").val(), CompanyId: $("input[id*=CompanyId]:checked").val(), taskTypeId: $("input[id*=taskTypeId]:checked").val(), assetTypeId: $("input[id*=AssetTypeId]:checked").val(), categoryId: $("input[id*=CategoryId]:checked").val() }, function () {
        $('#servicelevelResult ul.data li.result').append('<span class="checkruledescription">' + WorkflowResources.WorkflowRuleSatisfiesConditions + '</span>');
        $('#servicelevelResult ul.data li:first span.checkruledescription').text(WorkflowResources.WorkflowRuleUse);
    });
    return false;
    
}

function clearRule() {
    $('select').val(null);
    $("input[id*=ServicePath]:checked").attr("checked", false);    
    $("input[id*=CompanyId]:checked").attr("checked", false);
    $("input[id*=taskTypeId]:checked").attr("checked", false);
    $("input[id*=AssetTypeId]:checked").attr("checked", false);
    $("input[id*=CategoryId]:checked").attr("checked", false);
    $('select').trigger('change');
    $('#servicelevelResult').html(null);
}

function selectUsers() {
    var url = 'user.ivp/select?reset=true&tb_mode=forallservices&tb_serviceid=0';
    dlg = openDialog(url, 'all', { width: 900, height: 650 });
    dlg.callback = usersCallback;
    return false;
}

function usersCallback(result) {
    var uids = result["selectedIds"];
    usersSelected(uids);
};

function usersSelected(uids) {
    var id = uids[0];
    $('#userId').val(id);

    $.getJSON(base + 'user.ivp/GetCompanyIdByUserId', { userid: id }, function (companyId) {
        $('#companyId').val(companyId);
    });
}

var ServiceLevelRule = {
    selectCategory: function selectCategory(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.categoriesCallback;
        return false;
    },
    categoriesCallback: function categoriesCallback(result) {
        var msName = result["msName"];
        addSelectedSingleValue(result, msName);
    },

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

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

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

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

        url += ('msName=' + msName);
        url += '&cbmode=ServiceLevelRuleList';
        url += '&selectmode=SINGLE';

        dlg = openDialog(url, 'selectcompanies', { width: 900, height: 650 });
        dlg.callback = ServiceLevelRuleList.servicesCallback;
        return false;
    },
    servicesCallback: function servicesCallback(result) {
        var msName = result['msName'];
        addSelectedSinglePath(result, msName);
    },
    selectTaskType: function selectTaskType(url, msName) {
        if (url.indexOf('?') > -1) {
            url += "&";
        } else {
            url += "?";
        }

        url += ('msName=' + msName);
        url += '&cbmode=ServiceLevelRuleList';
        url += '&selectmode=SINGLE';

        dlg = openDialog(url, 'selectcompanies', { width: 900, height: 650 });
        dlg.callback = ServiceLevelRuleList.taskTypesCallback;
        return false;
    },
    taskTypesCallback: function taskTypesCallback(result) {
        var msName = result['msName'];
        addSelectedSingleValue(result, msName);
    },
    selectAssetType: function selectAssetType(url, msName) {
        if (url.indexOf('?') > -1) {
            url += "&";
        } else {
            url += "?";
        }

        url += ('msName=' + msName);
        url += '&cbmode=ServiceLevelRuleList';
        url += '&selectmode=SINGLE';

        dlg = openDialog(url, 'selectassettypes', { width: 900, height: 650 });
        dlg.callback = ServiceLevelRuleList.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=ServiceLevelRuleList';
        url += '&selectmode=SINGLE';

        dlg = openDialog(url, 'selectcompanies', { width: 990, height: 660 });
        dlg.callback = ServiceLevelRuleList.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=ServiceLevelRuleList';
        url += '&selectmode=SINGLE';

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