// skillsperagent.js

$(document).ready(function () {
    // Exclude Forward Skills
    if ($("select[title*='Skill']").length) {
        ExecuteOrDelayUntilScriptLoaded(function () { retrieveExcludeSkills(); }, "sp.js");
    }
});

// Skill Exclude Values Query
function retrieveExcludeSkills() {
    var clientContext = SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle('Skills');

    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml("<View><Query><Where><Or><Eq><FieldRef Name='ContentType' /><Value Type='Text'>ForwardSkill</Value></Eq><Eq><FieldRef Name='ContentType' /><Value Type='Text'>ChatForwardSkill</Value></Eq></Or></Where></Query><OrderBy><FieldRef Name='Title' /></OrderBy></View>");
    this.collListItem = oList.getItems(camlQuery);

    clientContext.load(collListItem);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQueryRetrieveExcludeSkillsSucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQueryRetrieveExcludeSkillsSucceeded(sender, args) {
    var listItemEnumerator = collListItem.getEnumerator();
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        var oListItemTitle = oListItem.get_item('Title');
        $("select[title*='Skill'] option").each(function () {
            if ($(this).text() == oListItemTitle) {
                $(this).remove();
            }
        });
    }
}

function onQueryFailed(sender, args) {
    console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}