// holidays_custom.js

$(document).ready(function () {
    // Exclude IVRQuestions
    if (customFormMode == "add" || customFormMode == "edit") {
        ExecuteOrDelayUntilScriptLoaded(function () { retrieveExcludeIVRQuestions(); }, "sp.js");

        // Validate dates
        ValidateDates();
        $("input[title*='Start datetime']").bind("keyup", function () {
            ValidateDates();
        });
        $("input[title*='Start datetime']").bind("focusout", function () {
            ValidateDates();
        });
        $("input[title*='Start datetime']").bind("mousedown", function () {
            ValidateDates();
        });
        $("input[title*='End datetime']").bind("keyup", function () {
            ValidateDates();
        });
        $("input[title*='End datetime']").bind("focusout", function () {
            ValidateDates();
        });
        $("input[title*='End datetime']").bind("mousedown", function () {
            ValidateDates();
        });
    }
});

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

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

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

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

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

function ValidateDates() {
    var pattern = localizedStrings[lcid][2].toUpperCase() + " HH:mm";
    var testValue1_0 = $("input[title*='Start datetime']").val();
    var testValue1_1 = $("select[id*='wsp_ucc_holidays_startdatetime'][id*='DateTimeFieldDateHours']").val();
    var testValue1_2 = $("select[id*='wsp_ucc_holidays_startdatetime'][id*='DateTimeFieldDateMinutes']").val();
    var testValue1 = testValue1_0 + " " + testValue1_1 + ":" + testValue1_2;
    var testValue2_0 = $("input[title*='End datetime']").val();
    var testValue2_1 = $("select[id*='wsp_ucc_holidays_enddatetime'][id*='DateTimeFieldDateHours']").val();
    var testValue2_2 = $("select[id*='wsp_ucc_holidays_enddatetime'][id*='DateTimeFieldDateMinutes']").val();
    var testValue2 = testValue2_0 + " " + testValue2_1 + ":" + testValue2_2;
    if (testValue1_0 != "" && testValue2_0 != "") {
        $("input[title*='Start datetime']").css("border", "1px solid red");
        $("input[title*='End datetime']").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");
        $("span#errorMsg").closest('tr').remove();
        var tmpStartDate = moment(testValue1, pattern).format("YYYYMMDDHHmm");
        var tmpEndDate = moment(testValue2, pattern).format("YYYYMMDDHHmm");
        if (tmpStartDate != tmpEndDate) {
            if (tmpEndDate > tmpStartDate) {
                $("input[id*='SaveItem']").prop("disabled", false);
                $("input[title*='Start datetime']").css("border", "1px solid green");
                $("input[title*='End datetime']").css("border", "1px solid green");
                $("a[id='Ribbon.ListForm.Edit.Commit.Publish-Large']").prop("class", "ms-cui-ctl-large");
                $("input[title*='End datetime']").closest('tr').after("<tr><td colspan='4' class='ms-formbody'><span id='errorMsg' style='color:green;'>OK</span></td></tr>");
            }
            else {
                var lowerValueMsg = resource_holidays_errormsg_lowervalue;
                $("input[title*='End datetime']").closest('tr').after("<tr><td colspan='4' class='ms-formbody'><span id='errorMsg' style='color:red;'>" + lowerValueMsg + "</span></td></tr>");
            }
        }
        else {
            var sameValuesMsg = resource_holidays_errormsg_samevalues;
            $("input[title*='End datetime']").closest('tr').after("<tr><td colspan='4' class='ms-formbody'><span id='errorMsg' style='color:red;'>" + sameValuesMsg + "</span></td></tr>");
        }
    }
}