// ucccreator.js
var sipDomains = [];
var pattern1;
var pattern2;
var validateCheck1 = false;
var validateCheck2 = false;

$(document).ready(function () {
    pattern1 = new RegExp(/^[^`~!@#$%^&*()=+]+$/);
    pattern2 = new RegExp(/^(tel:)\+(?:[0-9] ?){6,14}[0-9]$/);

    if (customFormMode == "add" || customFormMode == "edit") {
        ExecuteOrDelayUntilScriptLoaded(function () { retrieveUccCreatorValues(); }, "sp.js");

        // Validate UCC Name
        ValidateUccName();
        $("input[title*='UCC Name']").bind("keyup", function () {
            ValidateUccName();
        });
        $("input[title*='UCC Name']").bind("focusout", function () {
            ValidateUccName();
        });
        $("input[title*='UCC Name']").bind("mousedown", function () {
            ValidateUccName();
        });

        // Validate Line URI
        ValidateLineUri();
        $("input[title*='Line URI']").bind("keyup", function () {
            ValidateLineUri();
        });
        $("input[title*='Line URI']").bind("focusout", function () {
            ValidateLineUri();
        });
        $("input[title*='Line URI']").bind("mousedown", function () {
            ValidateLineUri();
        });
    }
});

function retrieveUccCreatorValues() {
    try {
        var clientContext = SP.ClientContext.get_current();
        var oList = clientContext.get_web().get_lists().getByTitle('Anywhere365 UCC Creator');

        var camlQuery = new SP.CamlQuery();
        this.collUccCreatorListItems = oList.getItems(camlQuery);

        clientContext.load(collUccCreatorListItems);
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQueryUccCreatorValuesSucceeded), Function.createDelegate(this, this.onQueryFailed));
    }
    catch (exp) {
        console.log("retrieveUccCreatorValues error: " + exp);
    }
}

function onQueryUccCreatorValuesSucceeded(sender, args) {
    var listItemEnumerator = collUccCreatorListItems.getEnumerator();
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        var oListItemSipDomain = oListItem.get_item('A365_SipDomain');

        var tmpIndex = 0;
        if (oListItemSipDomain != null && oListItemSipDomain != "") {
            tmpIndex = $.inArray(oListItemSipDomain, sipDomains);
            if (tmpIndex == -1) {
                sipDomains.push(oListItemSipDomain);
            }
        }

    }

    sipDomains.sort();

    $("input[title*='Sip Domain']").autocomplete({
        source: sipDomains
    });
}

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

function ValidateUccName() {
    validateCheck1 = false;
    var testValue = $("input[title*='UCC Name']").val();
    if (testValue != null && testValue != "") {
        $("input[id*='SaveItem']").prop("disabled", true);
        $("a[id='Ribbon.ListForm.Edit.Commit.Publish-Large']").prop("class", "ms-cui-ctl-large ms-cui-disabled");
        $("input[title*='UCC Name']").css("border", "1px solid red");
        if (pattern1 != null) {
            if (pattern1.test(testValue) == true) {
                validateCheck1 = true;
                if (validateCheck1 && validateCheck2) {
                    $("input[id*='SaveItem']").prop("disabled", false);
                    $("a[id='Ribbon.ListForm.Edit.Commit.Publish-Large']").prop("class", "ms-cui-ctl-large");
                }
                
                $("input[title*='UCC Name']").css("border", "1px solid green");
            }
        }
    }
    else {
        validateCheck1 = true;
        if (validateCheck1 && validateCheck2) {
            $("input[id*='SaveItem']").prop("disabled", false);
            $("a[id='Ribbon.ListForm.Edit.Commit.Publish-Large']").prop("class", "ms-cui-ctl-large");
        }

        $("input[title*='UCC Name']").css("border", "1px solid #2a8dd4");
    }
}

function ValidateLineUri() {
    validateCheck2 = false;
    var testValue = $("input[title*='Line URI']").val();
    if (testValue != null && testValue != "") {
        $("input[id*='SaveItem']").prop("disabled", true);
        $("a[id='Ribbon.ListForm.Edit.Commit.Publish-Large']").prop("class", "ms-cui-ctl-large ms-cui-disabled");
        $("input[title*='Line URI']").css("border", "1px solid red");
        if (pattern2 != null) {
            if (pattern2.test(testValue) == true) {
                validateCheck2 = true;
                if (validateCheck1 && validateCheck2) {
                    $("input[id*='SaveItem']").prop("disabled", false);
                    $("a[id='Ribbon.ListForm.Edit.Commit.Publish-Large']").prop("class", "ms-cui-ctl-large");
                }

                $("input[title*='Line URI']").css("border", "1px solid green");
            }
        }
    }
    else {
        validateCheck2 = true;
        if (validateCheck1 && validateCheck2) {
            $("input[id*='SaveItem']").prop("disabled", false);
            $("a[id='Ribbon.ListForm.Edit.Commit.Publish-Large']").prop("class", "ms-cui-ctl-large");
        }
        
        $("input[title*='Line URI']").css("border", "1px solid #2a8dd4");
    }
}