// outbounddialercalls_custom.js
var pattern;

$(document).ready(function () {
    if (customFormMode == "listview") {
        // Change new item link
        ChangeOutboundDialerCallsNewItemLink();
    }
    else if (customFormMode == "add" || customFormMode == "edit") {
        pattern = new RegExp("^([0-1][0-9]|[2][0-3]):([0-5][0-9])$");

        // Validate timefields
        ValidateTimeFields();
        $("input[title='Time start']").bind("keyup", function () {
            ValidateTimeFields();
        });
        $("input[title='Time start']").bind("focusout", function () {
            ValidateTimeFields();
        });
        $("input[title='Time start']").bind("mousedown", function () {
            ValidateTimeFields();
        });
        $("input[title='Time end']").bind("keyup", function () {
            ValidateTimeFields();
        });
        $("input[title='Time end']").bind("focusout", function () {
            ValidateTimeFields();
        });
        $("input[title='Time end']").bind("mousedown", function () {
            ValidateTimeFields();
        });
    }
});

function ChangeOutboundDialerCallsNewItemLink() {
    // Determine if OutboundDialerCalls list
    var requestUrl = document.URL;
    var pos0 = requestUrl.toLowerCase().indexOf("/lists/outbounddialercalls/");
    if (pos0 != -1) {
        // Determine view name
        var pos1 = requestUrl.lastIndexOf("/");
        var pos2 = requestUrl.lastIndexOf(".");
        if (pos1 != -1 && pos2 != -1) {
            var viewName = (requestUrl.substring(pos1 + 1, pos2));

            // Get target link string
            var target = $("td.ms-list-addnew a:first").attr("onclick");
            if (target == null) {
                target = $("td.ms-addnew a:first").attr("onclick");
            }

            if (target != null && target != "") {
                var pos3 = target.indexOf('"');
                if (pos3 != -1) {
                    var pos4 = target.indexOf('"', pos3 + 1);
                    if (pos4 != -1) {
                        var oldLink = target.substring(pos3 + 1, pos4);

                        // Determine content type id
                        var contentTypeId = "0x0100cad41f0665004f3a8ae37ae69ba532c2";
                        var tmpViewName = viewName.toLowerCase();
                        if (tmpViewName == "autonomousdialerview") {
                            contentTypeId = "0x0100ef8655606cae4e4e986b0c792a21a3c9";
                        }

                        // Determine new link
                        var newLink = requestUrl;
                        var pos5 = newLink.indexOf("?");
                        if (pos5 != -1) {
                            newLink = newLink.substring(0, pos5);
                        }
                        newLink = newLink.replace(viewName, "NewForm");
                        newLink += "?Source=" + encodeURIComponent(requestUrl) + "&RootFolder=&ContentTypeId=" + contentTypeId;

                        // Change new link
                        target = target.replace(oldLink, newLink);
                        $("td.ms-list-addnew a:first").attr("onclick", target);
                        $("td.ms-list-addnew a:first").attr("href", newLink);
                        $("td.ms-addnew a:first").attr("onclick", target);
                        $("td.ms-addnew a:first").attr("href", newLink);
                    }
                }
            }
        }
    }
}

function ValidateTimeFields() {
    var timeStartValid = true;
    var timeEndValid = true;
    var timeStartValue = $("input[title='Time start']").val();
    var timeEndValue = $("input[title='Time end']").val();
    $("input[title='Time start']").css("border", "");
    $("input[title='Time end']").css("border", "");
    $("span#errorMsg1").remove();
    $("span#errorMsg2").remove();
    $("a[id='Ribbon.ListForm.Edit.Commit.Publish-Large']").prop("class", "ms-cui-ctl-large ms-cui-disabled");
    $("input[id*='SaveItem']").prop("disabled", true);

    if (timeStartValue != "") {
        $("input[title='Time start']").after("<span id='errorMsg1' style='color:red;'>Time format must be: HH:mm</span>");
        $("input[title='Time start']").css("border", "1px solid red");
        if (pattern.test(timeStartValue) == true) {
            $("input[title='Time start']").css("border", "1px solid green");
            $("span#errorMsg1").remove();
        }
        else {
            timeStartValid = false;
        }
    }

    if (timeEndValue != "") {
        $("input[title='Time end']").after("<span id='errorMsg2' style='color:red;'>Time format must be: HH:mm</span>");
        $("input[title='Time end']").css("border", "1px solid red");
        if (pattern.test(timeEndValue) == true) {
            $("input[title='Time end']").css("border", "1px solid green");
            $("span#errorMsg2").remove();
        }
        else {
            timeEndValid = false;
        }
    }

    if (timeStartValid == true && timeEndValid == true) {
        $("a[id='Ribbon.ListForm.Edit.Commit.Publish-Large']").prop("class", "ms-cui-ctl-large");
        $("input[id*='SaveItem']").prop("disabled", false);
    }
}