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

    /*//ambiguousRules
    var ambiguousRulesIds = $("#AmbiguousRulesIds").val();
    if (ambiguousRulesIds != undefined) {
        var ids = ambiguousRulesIds.split("|");
        $(ids).each(function() {
            var tr = $("#id" + this).closest("tr");
            if(tr.length != 0)
                tr.addClass("yellow");
        });

        var ytr = $("tr.yellow");
        if (ytr.length != 0) {
            $("#ambiguousRulesWarning").show();
        }
    }*/

    
});

function delWorkflowRule() {
    var idsDefaultRule = $("#IdsDefault").val();
    var isDelete = true;
    if (idsDefaultRule != undefined) {
        jQuery.each(selectedIds, function (i, val) {
            if (idsDefaultRule.indexOf(val) != -1) {
                alert(RuleResources.NotDeleteDefaultRule);
                isDelete = false;
            }
        });

    }

    if (isDelete)
        del();
}


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: '500px', init: true, single: true, pixelwidth: 500 });
    });
    return false;
}

function checkRule() {
    var url = base + tableController() + 'CheckRule';
    var obj = $('#workflowResult');
    if (!obj.length) {
        obj = $('#checkruleholder #checkrule').append('<div id="workflowResult"></div>').find('#workflowResult');
    }
    
    obj.load(url, { ServicePath: $("input[id*=ServicePath]:checked").val(), taskTypeId: $("input[id*=taskTypeId]:checked").val(), assetTypeId: $("input[id*=AssetTypeId]:checked").val(), categoryId: $("input[id*=CategoryId]:checked").val() }, function () {
        $('#workflowResult ul.data li.result').append('<span class="checkruledescription">' + WorkflowResources.WorkflowRuleSatisfiesConditions + '</span>');
        $('#workflowResult 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*=taskTypeId]:checked").attr("checked", false);
    $("input[id*=AssetTypeId]:checked").attr("checked", false);
    $("input[id*=CategoryId]").attr("checked", false);
    $('select').trigger('change');
    $('#workflowResult').html(null);
}

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

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

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

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

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

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