var selectedFieldsNames = new Array();
var selectedMainFieldsNames = new Array();


$(function () {
    // $.multipleSelect.initByClassName('.multipleselect');
    $('#tasktypeids').multipleSelect({ customClick: true, functionClickCheck: 'addNewType', functionClickUnCheck: 'deleteType' });
    $('#mainfields').multipleSelect({ customClick: true, functionClickCheck: 'addNewMainField', functionClickUnCheck: 'deleteType' });
});

function addNewType(check, value, name) {
    //    $('#selTypeFields').append('<div id="tasktype_' + value + '" typeid="' + value + '"></div>');
    var div = ('<div class="tasktypediv" id="tasktype_' + value + '" typeid="' + value + '"></div>');
    $(div).appendTo('#selTypeFields');
    $('#tasktype_' + value).load(base + 'workflow.ivp/GetTaskTypeFields', { typeid: value }, function () {
        $('#tasktypefieldids_' + value).multipleSelect({ customClick: true, functionClickCheck: 'addRequiredField', functionClickUnCheck: 'deleteRequiredField' });
    });
    return;
}

function addNewMainField(check, value, name) {
    selectedMainFieldsNames["id" + value] = name;
    return;
}

function addRequiredField(check, value, name) {
    selectedFieldsNames["id" + value] = name;
    return;
}

function deleteRequiredField(check, value, name) {
    return;
}

function deleteType(check, value, name) {
    var typeId = '#tasktype_' + value;
    $(typeId).remove();
    return;
}

function addSelectedFields(id) {
    var fieldids = new Array();
    var mfieldids = new Array();
    $('select.typefieldselect').each(function () {
        var selectedIds = $(this).val();
        if (selectedIds) {
            if (fieldids.length == 0) {
                fieldids = selectedIds;
            }
            else {
                $.each(selectedIds, function (i, elem) {
                    fieldids.push(elem);
                });
            }
        }
    });
    $('select#mainfields').each(function () {
        var selectedIds = $(this).val();
        if (selectedIds) {
            if (mfieldids.length == 0) {
                mfieldids = selectedIds;
            }
            else {
                $.each(selectedIds, function (i, elem) {
                    mfieldids.push(elem);
                });
            }
        }
    });
    var fids = fieldids.join(',');
    var mfids = mfieldids.join(',');

    var result = new Array();
    result["selectedMainFields"] = mfieldids;
    result["selectedFields"] = fieldids;
    result["selectedMainFieldsNames"] = selectedMainFieldsNames;
    result["selectedFieldsNames"] = selectedFieldsNames;

    if (self.callback == null) {
        if (getURLParameter('cbmode') != null && getURLParameter('cbmode') != '') {
            eval('opener.' + getURLParameter('cbmode') + '.workflowsCallback(result)');
        } else {
            self.callback = opener.workflowsCallback;
            self.callback(result);
        }
    } else {
        self.callback(result);
    }

    self.close();
    return false;
}

function deleteTaskType(elem) {
    var typeid = $(elem).attr('typeid');
    var elementCheck = '.checklist input#tasktypeids_' + typeid;
    $(elementCheck).removeAttr('checked');
    $(elementCheck).trigger('change');
    $('.typetable' + typeid).remove();
    //return false;
}