// supervisors_custom.js
var pattern1;
var pattern2;
var pattern3;
var validateCheck1 = false;
var validateCheck2 = false;
var validateCheck3 = false;

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

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

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

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

function ValidateSupervisorSip() {
    validateCheck1 = false;
    var testValue = $("input[title*='Supervisor']").val();
    if (testValue != null && testValue != "") {
        $("input[title*='Supervisor']").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 (pattern1.test(testValue) == true) {
            validateCheck1 = true;
            $("input[title*='Supervisor']").css("border", "1px solid green");
            if (validateCheck1 && validateCheck2 && validateCheck3)
            {
                $("input[id*='SaveItem']").prop("disabled", false);
                $("a[id='Ribbon.ListForm.Edit.Commit.Publish-Large']").prop("class", "ms-cui-ctl-large");
            }
        }
    }
    else {
        validateCheck1 = true;
        $("input[title*='Supervisor']").css("border", "1px solid #2a8dd4");
        if (validateCheck1 && validateCheck2 && validateCheck3) {
            $("input[id*='SaveItem']").prop("disabled", false);
            $("a[id='Ribbon.ListForm.Edit.Commit.Publish-Large']").prop("class", "ms-cui-ctl-large");
        }
    }
}

function ValidatePhoneUri() {
    validateCheck2 = false;
    var testValue = $("input[title*='PhoneUri']").val();
    if (testValue != null && testValue != "") {
        var matchIndex = testValue.indexOf(";");
        if (matchIndex != -1) {
            testValue = testValue.substring(0, matchIndex);
        }

        $("input[title*='PhoneUri']").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 (pattern2.test(testValue) == true) {
            validateCheck2 = true;
            $("input[title*='PhoneUri']").css("border", "1px solid green");
            if (validateCheck1 && validateCheck2 && validateCheck3) {
                $("input[id*='SaveItem']").prop("disabled", false);
                $("a[id='Ribbon.ListForm.Edit.Commit.Publish-Large']").prop("class", "ms-cui-ctl-large");
            }
        }
    }
    else
    {
        validateCheck2 = true;
        $("input[title*='PhoneUri']").css("border", "");
        if (validateCheck1 && validateCheck2 && validateCheck3) {
            $("input[id*='SaveItem']").prop("disabled", false);
            $("a[id='Ribbon.ListForm.Edit.Commit.Publish-Large']").prop("class", "ms-cui-ctl-large");
        }
    }
}

function ValidateUpn() {
    validateCheck3 = false;
    var testValue = $("input[title*='UPN']").val();
    if (testValue != null && testValue != "") {
        var matchIndex = testValue.indexOf(";");
        if (matchIndex != -1) {
            testValue = testValue.substring(0, matchIndex);
        }

        $("input[title*='UPN']").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 (pattern3.test(testValue) == true) {
            validateCheck3 = true;
            $("input[title*='UPN']").css("border", "1px solid green");
            if (validateCheck1 && validateCheck2 && validateCheck3) {
                $("input[id*='SaveItem']").prop("disabled", false);
                $("a[id='Ribbon.ListForm.Edit.Commit.Publish-Large']").prop("class", "ms-cui-ctl-large");
            }
        }
    }
    else {
        validateCheck3 = true;
        $("input[title*='UPN']").css("border", "");
        if (validateCheck1 && validateCheck2 && validateCheck3) {
            $("input[id*='SaveItem']").prop("disabled", false);
            $("a[id='Ribbon.ListForm.Edit.Commit.Publish-Large']").prop("class", "ms-cui-ctl-large");
        }
    }
}