﻿<html>
<head>
    <title></title>
    <style>
        .hideCtrl {
            display: none;
        }

        .tabText {
            color: #262626;
            font-family: 'Segoe UI';
            font-size: 21px;
            font-weight: normal;
        }

        .labelText {
            font-family: 'Segoe UI';
            font-size: 12px;
            color: #444444;
        }

        .tinyText {
            font-family: 'Segoe UI';
            font-size: 10px;
            color: #444444;
        }

        .toggle {
            font-family: 'Segoe UI';
            font-size: 10px;
            color: #444444;
        }

        .controlText {
            font-family: 'Segoe UI';
            font-size: 12px;
            color: black;
        }

            .controlText label {
                display: block;
                padding: 2px 2px 2px 2px;
                border-bottom: #E6E6E6 1px dashed;
            }

                .controlText label:hover {
                    background-color: #B1D6F0;
                }

        .tooltip {
            display: none;
            position: absolute;
            border: 1px solid #333;
            background-color: #ffff99;
            border-radius: 1px;
            padding: 5px;
            color: black;
            font-size: 12px;
            font-family: 'Segoe UI';
        }
    </style>

    <script src="ClientGlobalContext.js.aspx"></script>
    <script src="work365_addon_attachmentrelocator_jquery3.1.0.min.js" type="text/javascript"></script>
    <script src="work365_addon_attachmentrelocator_json2.js" type="text/javascript"></script>
    <script src="work365_addon_attachmentrelocator_configuration.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">

        var showDiv;
        var currUserLcid = Xrm.Page.context.getUserLcid();
        var parentIdDict = new Object();
        var customizableEntityList;
        var isOrgOwned = false;

        $(document).ready(function () {

            Work365_AddOn_AttachmentRelocator_PopulateEntityList(currUserLcid);

            //Load existing values in case of an existing record
            if (window.parent.Xrm.Page.ui.getFormType() != 1)
                Work365_AddOn_AttachmentRelocator_LoadExistingValues();

        });


        Work365_AddOn_AttachmentRelocator_PopulateEntityList = function( lcd )
        {
            try {

                var ctrlEntityList = document.getElementById("entityList");
                ctrlEntityList.innerHTML = "";

                var entityColl = [];
                customizableEntityList = Work365_AddOn_AttachmentRelocator_GetCustomizableEntityList();

                for (var i = 0; i < customizableEntityList.length; i++) {
                    if (customizableEntityList[i].DisplayName.UserLocalizedLabel) {
                        entityColl.push({
                            'SchemaName': customizableEntityList[i].LogicalName,
                            'DisplayName': customizableEntityList[i].DisplayName.UserLocalizedLabel.Label
                        });
                    }
                    else {
                        var labelColl = customizableEntityList[i].DisplayName.LocalizedLabels;
                        for (var key in labelColl) {
                            if (labelColl.hasOwnProperty(key)) {
                                if (labelColl[key].LanguageCode == lcd) {
                                    entityColl.push({
                                        'SchemaName': customizableEntityList[i].LogicalName,
                                        'DisplayName': labelColl[key].Label
                                    });
                                }
                            }
                        }
                    }
                }

                entityColl.sort(function (a, b) {
                    var textA = a.DisplayName;
                    var textB = b.DisplayName;
                    return (textA < textB) ? -1 : (textA > textB) ? 1 : 0;
                });

                //Below code not needed as we are cshoeing all entities irrespective of emailEnabled entities
                //var emailEntities = window.parent.Xrm.Page.data.entity.attributes.get("work365_emailenabledentities").getValue().replace(/\s/g, "").split(",");
                //for (var i = 0; i < entityColl.length; i++) {
                //    if ($.inArray(entityColl[i].SchemaName, emailEntities) > -1)
                //        Work365_AddOn_AttachmentRelocator_CreateChkboxControl(ctrlEntityList, "validEntities", entityColl[i].DisplayName, entityColl[i].SchemaName, false);
                //}


                for ( var i = 0; i < entityColl.length; i++ ) {
                        Work365_AddOn_AttachmentRelocator_CreateChkboxControl( ctrlEntityList, "validEntities", entityColl[i].DisplayName, entityColl[i].SchemaName, false );
                }

            }
            catch ( ex ) {
                alert( ex.message )
            }
        }

        Work365_AddOn_AttachmentRelocator_LoadExistingValues = function ()
        {
            var entityNames = window.parent.Xrm.Page.data.entity.attributes.get("work365_entitylist").getValue();
            var counter = 0;
            if (entityNames != null) {
                var entityLst = entityNames.replace(/\s/g, "").split(',');
                $.each($("input[name='validEntities']"), function () {
                    if ($.inArray($(this).val(), entityLst) >= 0) {
                        $(this).prop("checked", true);
                        $(this).parent().css('background-color', '#B1D6F0');
                        counter++;
                    }
                    else {
                        $(this).parent().css('background-color', 'white');
                    }
                });

                $("#lblEntityStatus").html("Total Selected : " + counter);
            }

        }

        Work365_AddOn_AttachmentRelocator_SetLabelStatus = function (ctrl) {
            var counter = 0;
            var statusCtrl;
            $.each($("input[name='" + ctrl.name + "']"), function () {
                if ($(this).prop("checked") == true) {
                    $(this).parent().css('background-color', '#B1D6F0');
                    counter++;
                }
                else {
                    $(this).parent().css('background-color', 'white');
                }
            });

            $("#lblEntityStatus").html("Total Selected : " + counter);
        }

        Work365_AddOn_AttachmentRelocator_CreateChkboxControl = function (parentElm, chkboxGrpName, chkboxDisplayName, chkboxValue, isChecked) {
            var chkd = "";
            var style = "";
            if (isChecked == true) {
                chkd = "checked";
                style = "style=background-color:#B1D6F0;";
            }

            try {
                $(parentElm).append("<label id='lbl" + chkboxValue + "' " + style + "><input type='checkbox'  name='" + chkboxGrpName + "' value='" + chkboxValue + "' " + chkd + " onclick='Work365_AddOn_AttachmentRelocator_SetLabelStatus(this)' /> " + chkboxDisplayName + "</label>");
            }
            catch (ex) {
                throw ex;
            }

        }


        Work365_AddOn_AttachmentRelocator_GetCustomizableEntityList = function ()
        {
            var coll;
            try {
                var httpReq = Work365_AddOn_AttachmentRelocator_CreateHTTPRequest("GET", "EntityDefinitions?$select=LogicalName,DisplayName,IsCustomEntity", false);
                httpReq.send( null );

                if ( httpReq.status === 200 ) {
                    coll = JSON.parse( httpReq.responseText ).value;
                }
                return coll;
            }
            catch ( ex ) {
                throw ex;
            }
        }

        Work365_AddOn_AttachmentRelocator_ToggleSelection = function (ctrl, sectionId, statusId) {
            var ticked = true;
            var counter = 0;
            var section = $('#' + sectionId);
            var status = $('#' + statusId);

            if ($(ctrl).hasClass("toggle"))
                ticked = false;

            try {
                $.each($("input[name='" + sectionId + "']"), function () {
                    $(this).prop("checked", ticked);
                    if (ticked) {
                        $(this).parent().css('background-color', '#B1D6F0');
                        counter++;
                    }
                    else {
                        $(this).parent().css('background-color', 'white');
                    }
                });

                if (ticked)
                    $(ctrl).addClass("toggle");
                else
                    $(ctrl).removeClass("toggle");

                status.html("Total Selected : " + counter);
            }
            catch (ex) {
                throw ex;
            }
        }

        Work365_AddOn_AttachmentRelocator_ProcessSave = function () {

            //clear any existing form notifications
            window.parent.Xrm.Page.ui.clearFormNotification("1");

            var validEnts = [];
            $.each($("input[name='validEntities']:checked"), function () {
                validEnts.push($(this).val());
            });
            var validEntities = validEnts.join(",");//no space needed

            if (validEnts.length == 0) {
                window.parent.Xrm.Page.ui.setFormNotification("You must provide a value for Valid Entities", "ERROR", "1");
                return false;
            }

            //set values in crm form
            window.parent.Xrm.Page.data.entity.attributes.get("work365_entitylist").setValue(validEntities);

            return true;
        }


    </script>
</head>
<body style="margin-left:0px;margin-top:0px;margin-bottom:0px;margin-right:0px;padding-top:0px;padding-bottom:0px;padding-left:0px;padding-right:0px;">

    <div>
        <table width="100%" cellpadding="2px" cellspacing="10px">
            <!--thead>
                <tr style="height:1px">
                    <td width="12%"></td>
                    <td width="37%"></td>
                    <td width="2%"></td>
                    <td width="12%"></td>
                    <td width="37%"></td>
                </tr>
            </thead>-->
            <tr>
                <td width="12%" class="labelText customToolTip" style="text-align: left; vertical-align: top" title="Select the list of entities that need to be enabled for Azure Sync">Select Entities</td>
                <td width="37%" style="text-align: left;; vertical-align: top">
                    <span class="tinyText" style="text-align: left" id="lblEntityStatus"></span><span class="tinyText" style="text-align: right; cursor: hand; text-decoration: underline; float: right; width: 50%; min-width: 50%;" onclick="Work365_AddOn_AttachmentRelocator_ToggleSelection(this, 'validEntities', 'lblEntityStatus')">Select/Unselect All</span>
                    <div id="entityList" style="width: 100%; height: 165px; overflow-y: scroll; border: #d6d6d6 1px solid; " class="controlText"> --Please select an Entity-- </div>
                </td>
                <td width="2%">&nbsp;</td>
                <td width="12%">&nbsp;</td>
                <td width="37%">&nbsp;</td>
            </tr>
        </table>
    </div>


</body>
</html>
