// voicemailagents_custom.js
var pattern;
var pattern1;
var pattern2;

$(document).ready(function () {
    pattern = null;
    pattern1 = new RegExp(/^(sip:)[A-Za-z0-9\-_\.!~*'()]+@([\w-]+\.)+[\w-]{2,}$/);
    pattern2 = new RegExp(/^(tel:)\+(?:[0-9] ?){6,14}[0-9]$/);

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

function ValidateUri() {
    var testValue = $("input[title*='Uri']").val();
    if (testValue != null && testValue != "") {
        var matchIndex = testValue.indexOf(";");
        if (matchIndex != -1) {
            testValue = testValue.substring(0, matchIndex);
        }
        $("input[title*='Uri']").css("border", "1px solid red");
        $("input[id*='SaveItem']").prop("disabled", true);
        $("a[id='Ribbon.ListForm.Edit.Commit.Publish-Large']").prop("class", "ms-cui-ctl-large ms-cui-disabled");
        if (testValue.indexOf("sip:") != -1) {
            pattern = pattern1;
        }
        else if (testValue.indexOf("tel:") != -1) {
            pattern = pattern2;
        }

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