﻿//***************************************************************************
//dAloor created on 18May2017
//Helper script file for all azure setting configuration
//***************************************************************************


//Create the HTTP Request object
Work365_AddOn_AttachmentRelocator_CreateHTTPRequest = function (action, query, async, impersonatingUserId) {
    try {

        var httpReq = new XMLHttpRequest();
        httpReq.open(action, Work365_AddOn_AttachmentRelocator_GetClientUrl() + "/api/data/v8.0/" + query, async);
        httpReq.setRequestHeader("Accept", "application/json");
        httpReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        httpReq.setRequestHeader("OData-MaxVersion", "4.0");
        httpReq.setRequestHeader("OData-Version", "4.0");

        if (impersonatingUserId) {
            httpReq.setRequestHeader("MSCRMCallerID", impersonatingUserId);
        }
    }
    catch (ex) {
        throw ex;
    }
    return httpReq;
}

Work365_AddOn_AttachmentRelocator_GetClientUrl = function () {
    return Xrm.Page.context.getClientUrl();
}

Work365_AddOn_AttachmentRelocator_ConfigSave = function (context) {
    try {

        var eventArgs = context.getEventArgs();

        //Disable auto-save 
        if (eventArgs.getSaveMode() == 70)
            eventArgs.preventDefault();

        //call the webresource save function to capture field values
        var storageManagerSetting = Xrm.Page.ui.controls.get("WebResource_StorageManagerConfiguration");
        if (storageManagerSetting)
            var settingDoc = storageManagerSetting.getObject().contentWindow;
        if (settingDoc) {
            var retVal = settingDoc.Work365_AddOn_AttachmentRelocator_ProcessSave();

            if (retVal == false)
                eventArgs.preventDefault();
        }
    }
    catch (ex) {
        alert("ERROR : Work365 Attachment Relocator [" + ex.message + "]");
    }
}




