

function PopulateSelectableObjectProperties(Url, Token, Pnl, Loading) {

    var HF_Sync = $('#' + Pnl).find($("[id$=HF_SyncMembers]")).attr('id');
    var LBX_Selectable = $('#' + Pnl).find($("[id$=LBX_SelectableMembers]")).attr('id');
    var LBX_Selected = $('#' + Pnl).find($("[id$=LBX_SelectedMembers]")).attr('id');
    var HF_Selected = $('#' + Pnl).find($("[id$=HF_SelectedMembers]")).attr('id');
    var HF_PreSelectedMembers = $('#' + Pnl).find($("[id$=HF_PreSelectedMembers]")).attr('id');

    //alert(HF_Sync.val());
    $('#' + HF_Sync).val('0');
    $('#' + Pnl).hide();
    $('#' + Loading).show();

    var param;

    param = "{ActionParameters:'" + Token + "'}";

    $.ajax(

        {
            type: "Post",
            url: Url,
            data: param,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {

                var array = msg.d;

                $.each(array, function (i, e) {

                    $('#' + LBX_Selectable + ' option').filter(function () {
                        return $(this).val().toLowerCase().indexOf(e.split("|")[0].toLowerCase()) > -1;
                    }).prop('selected', true).attr("selected", "selected");

                });

                $('#' + LBX_Selectable + ' option:selected').remove().appendTo('#' + LBX_Selected);
                $('#' + LBX_Selected + ' option').prop('selected', false).removeAttr("selected");

                SortListBoxItemsByText(LBX_Selectable);
                SortListBoxItemsByText(LBX_Selected);

                //CopySelectedListBoxItemsValueToHiddenField(HF_Selected, LBX_Selectable);
                CopyAllListBoxItemsValueToHiddenField(HF_Selected, LBX_Selected);

                $('#' + Pnl).show();
                $('#' + Loading).hide();
                $('#' + HF_Sync).val('1');
                var copyData = $('#' + HF_Selected).val();
                $('#' + HF_PreSelectedMembers).val(copyData);
                //alert($('#' + HF_PreSelectedMembers).val());
            }
        });

}

function PopulateAutomaticRepliesProperties(Url, Token, Pnl, Loading, TxBx_InternalMessage, TxBx_ExternalMessage, ShortDatePattern, DateSeparator) {

    var HF_Sync = $('#' + Pnl).find($("[id$=HF_AutomaticReplies]")).attr('id');

    //alert(HF_Sync.val());
    $('#' + HF_Sync).val('0');
    $('#' + Pnl).hide();
    $('#' + Loading).show();

    var param;

    param = "{ActionParameters:'" + Token + "'}";

    $.ajax(

        {
            type: "Post",
            url: Url,
            data: param,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (AutomaticReplies) {

                //alert(AutomaticReplies.d);

                try {
                    var Content = "";

                    if (AutomaticReplies.d.AutoReplyState != 0) //0=Disabled
                    {
                        $('#' + Pnl).find($("[id$=CB_Enabled]")).prop('checked', true);
                        $('#' + Pnl).find($("[id$=TR_OOO]")).attr("style", "display:none");
                        Content = AutomaticReplies.d.InternalMessage;
                        //$('#' + Pnl).find($("[id$=TxBx_InternalMessage]")).val(Content);
                        oboutGetEditorByClientId(TxBx_InternalMessage).setContent(Content);

                        if (AutomaticReplies.d.ExternalAudience == 0) //0=Constants.ExternalAudienceType.None
                        {
                            $('#' + Pnl).find($("[id$=CB_SendOOOExternal]")).prop('checked', false);
                            $('#' + Pnl).find($("[id$=TR_OOO]")).attr("style", "display:none");
                            //$('#' + Pnl).find($("[id$=TxBx_ExternalMessage]")).val("");
                            Content = "";
                            oboutGetEditorByClientId(TxBx_ExternalMessage).setContent(Content);
                        }
                        else {
                            $('#' + Pnl).find($("[id$=CB_SendOOOExternal]")).prop('checked', true);
                            $('#' + Pnl).find($("[id$=TR_OOO]")).attr("style", "display:''");
                            var RBL_ExternalOOOOption = $('#' + Pnl).find($("[id$=DDL_StartTime]")).attr('id');
                            $('#' + RBL_ExternalOOOOption + ' input[value="True"]').attr('checked', false);
                            $('#' + RBL_ExternalOOOOption + ' input[value="False"]').attr('checked', false);
                            Content = AutomaticReplies.d.ExternalMessage;
                            //$('#' + Pnl).find($("[id$=TxBx_ExternalMessage]")).val(Content);
                            oboutGetEditorByClientId(TxBx_ExternalMessage).setContent(Content);

                            if (AutomaticReplies.d.ExternalAudience == 1) //1=Constants.ExternalAudienceType.All
                            {
                                $('#' + RBL_ExternalOOOOption + ' input[value="True"]').attr('checked', true);
                                $('#' + RBL_ExternalOOOOption + ' input[value="False"]').attr('checked', false);
                            }
                            else {
                                $('#' + RBL_ExternalOOOOption + ' input[value="True"]').attr('checked', false);
                                $('#' + RBL_ExternalOOOOption + ' input[value="False"]').attr('checked', true);
                            }
                        }

                        if (AutomaticReplies.d.AutoReplyState != 1) //1=Constants.AutoReplyStateType.Enabled
                        {
                            //ToString("MM,dd,yyyy,HH:mm")                            
                            var sTime = AutomaticReplies.d.StartTime.split(',');
                            var eTime = AutomaticReplies.d.EndTime.split(',');
                            var sDate = setDateTimeUICulture(AutomaticReplies.d.StartTime, ',', ShortDatePattern, DateSeparator);
                            var eDate = setDateTimeUICulture(AutomaticReplies.d.EndTime, ',', ShortDatePattern, DateSeparator);
                                                        
                            $('#' + Pnl).find($("[id$=TxBx_StartDate]")).val(sDate);
                            $('#' + Pnl).find($("[id$=HF_StartDate]")).val(sDate);
                            $('#' + Pnl).find($("[id$=TxBx_EndDate]")).val(eDate);
                            $('#' + Pnl).find($("[id$=HF_EndDate]")).val(eDate);

                            //alert(new Date(parseInt(AutomaticReplies.d.StartTime.replace("/Date(", "").replace(")/", ""), 10)));
                            //var STTime = new Date(parseInt(AutomaticReplies.d.StartTime.replace("/Date(", "").replace(")/", ""), 10));
                            //var EndTime = new Date(parseInt(AutomaticReplies.d.EndTime.replace("/Date(", "").replace(")/", ""), 10));

                            //$('#' + Pnl).find($("[id$=TxBx_StartDate]")).val(STTime.toLocaleDateString());
                            //$('#' + Pnl).find($("[id$=TxBx_EndDate]")).val(EndTime.toLocaleDateString());

                            var DDL_StartTimeID = $('#' + Pnl).find($("[id$=DDL_StartTime]")).attr('id');
                            var DDL_EndTimeID = $('#' + Pnl).find($("[id$=DDL_EndTime]")).attr('id');

                            //alert(STTime.toLocaleTimeString());
                            var STHours = sTime[3].split(':')[0];
                            var STMinutes = sTime[3].split(':')[1];

                            if (STHours.length < 2)
                                STHours = "0" + STHours;
                            if (STMinutes.length < 2)
                                STMinutes = "0" + STMinutes;

                            setTimeout(function () { $("#" + DDL_StartTimeID).val(STHours + ":" + STMinutes + ":00"); $("#" + DDL_StartTimeID).trigger('change'); }, 500);

                            var eTHours = eTime[3].split(":")[0];
                            var eTMinutes = eTime[3].split(":")[1];

                            if (eTHours.length < 2)
                                eTHours = "0" + eTHours;
                            if (eTMinutes.length < 2)
                                eTMinutes = "0" + eTMinutes;

                            setTimeout(function () { $("#" + DDL_EndTimeID).val(eTHours + ":" + eTMinutes + ":00"); $("#" + DDL_EndTimeID).trigger('change'); }, 500);

                            $('#' + Pnl).find($("[id$=CB_Schedule]")).prop('checked', true);
                            $('#' + Pnl).find($("[id$=TR_Schedule]")).attr("style", "display:''");
                        }
                        else {
                            $('#' + Pnl).find($("[id$=CB_Schedule]")).prop('checked', false);
                            $('#' + Pnl).find($("[id$=TR_Schedule]")).attr("style", "display:none");
                        }
                    }
                    else {
                        $('#' + Pnl).find($("[id$=CB_Enabled]")).prop('checked', false);
                        $('#' + Pnl).find($("[id$=TR_OOO]")).attr("style", "display:none");
                    }

                }
                catch (e) { alert("error" + e.message); }

                $('#' + Pnl).show();
                $('#' + Loading).hide();
                $('#' + HF_Sync).val('1');
            },
            failure: function OnWSRequestFailed(error) {
                alert("Stack Trace: " + error.get_stackTrace() + "/r/n" +
                    "Error: " + error.get_message() + "/r/n" +
                    "Status Code: " + error.get_statusCode() + "/r/n" +
                    "Exception Type: " + error.get_exceptionType() + "/r/n" +
                    "Timed Out: " + error.get_timedOut());
            }
        });

}

function reloadPanel(Pnl, HFDoPostBack, BtnDoPostBack, IsAutomaticReply) {
    //alert("IN");

    if (IsAutomaticReply == '0') {
        if ($('#' + HFDoPostBack).val() == "1") {
            $('#' + HFDoPostBack).val("0");
            $('#' + BtnDoPostBack).trigger('click');
        }
    }
    else {
        if ($('#' + HFDoPostBack).val() == "1" && $('#' + Pnl).find($("[id$=CB_Enabled]")).prop('checked') == true) {
            $('#' + HFDoPostBack).val("0");
            $('#' + BtnDoPostBack).trigger('click');
        }
    }
}

function setDateTimeUICulture(DateTimeString, DateTimeSplitter, ShortDatePattern, DateSeparator) {
    var sTime = DateTimeString.split(DateTimeSplitter);

    var sDateToDisplay = "";
    var i = 0;

    for (i = 0; i < ShortDatePattern.split(DateSeparator).length; i++) {
        if (ShortDatePattern.split(DateSeparator)[i].includes("M") == true || ShortDatePattern.split(DateSeparator)[i].includes("m") == true) {
            sDateToDisplay += sTime[0] + DateSeparator;
        }
        else if (ShortDatePattern.split(DateSeparator)[i].includes("D") == true || ShortDatePattern.split(DateSeparator)[i].includes("d") == true) {
            sDateToDisplay += sTime[1] + DateSeparator;
        }
        else if (ShortDatePattern.split(DateSeparator)[i].includes("Y") == true || ShortDatePattern.split(DateSeparator)[i].includes("y") == true) {
            sDateToDisplay += sTime[2] + DateSeparator;
        }
    }
    sDateToDisplay = sDateToDisplay.substr(0, sDateToDisplay.length - 1);
    return sDateToDisplay;
}

function PopulateObjectPropertiesAndReload(Url, Token, Pnl, Loading, HF_Sync, HF_Existing) {

    $('#' + HF_Sync).val('0');
    $('#' + Pnl).hide();
    $('#' + Loading).show();
    $('#' + HF_Existing).val('');

    var param;

    param = "{ActionParameters:'" + Token + "'}";

    $.ajax(

        {
            type: "Post",
            url: Url,
            data: param,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {

                //alert(msg.d);
                $('#' + HF_Existing).val(msg.d);

                $('#' + Pnl).show();
                $('#' + Loading).hide();
                $('#' + HF_Sync).val('1');
            },
            failure: function OnWSRequestFailed(error) {
                alert("Stack Trace: " + error.get_stackTrace() + "/r/n" +
                    "Error: " + error.get_message() + "/r/n" +
                    "Status Code: " + error.get_statusCode() + "/r/n" +
                    "Exception Type: " + error.get_exceptionType() + "/r/n" +
                    "Timed Out: " + error.get_timedOut());
            }
        });

}
