<html>
<head>
     <meta charset="utf-8">
</head>

<body style="word-wrap: break-word;">
     <script src="ClientGlobalContext.js.aspx"></script>

     <script language="JavaScript" type="text/javascript">

          (function openReviewApp() {
               debugger;
               var oDataPath = Xrm.Page.context.getClientUrl() + "/xrmservices/2011/organizationdata.svc";

               var filter = "/dqg_settingsSet?" +
                    "$select=dqg_value&$filter=dqg_category eq 'Deduplication' and dqg_description eq 'Review App Url'";

               var retrieveRecordsReq = new XMLHttpRequest();
               retrieveRecordsReq.open("GET", oDataPath + filter, true);
               retrieveRecordsReq.setRequestHeader("Accept", "application/json");
               retrieveRecordsReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");

               retrieveRecordsReq.onreadystatechange = function () {
                    if (this.readyState == 4) {
                         debugger;
                         if (this.status == 200) {
                              var retrievedRecords = JSON.parse(retrieveRecordsReq.responseText).d;
                              if (retrievedRecords.results.length > 0) {
                                   reviewAppUrl = retrievedRecords.results[0].dqg_value;
                                   makeFrame(reviewAppUrl);
                              }
                              else {
                                   reviewAppUrl = "https://perfectandmerge.com/DQGPAM3/DQGPAM/sessions";
                                   makeFrame(reviewAppUrl);
                              }
                         }
                    }
               };
               retrieveRecordsReq.send();
          })();

          function makeFrame(reviewAppUrl) {
               debugger;
               var currentUserId = Xrm.Page.context.getUserId();
               var clienturl = "";

               try {
                    if (Xrm.Page.context.getClientUrl() != null) {
                         clienturl = Xrm.Page.context.getClientUrl();
                    }
               }
               catch (err) {
               }

               //if (clienturl == "") {
                    //  clienturl = Xrm.Page.context.getServerUrl();
               //}

               clienturl = clienturl + "/xrmservices/2011/organization.svc";
               var orgUnName = Xrm.Page.context.getOrgUniqueName();

               reviewAppUrl = reviewAppUrl + "?orgurl=" + clienturl + "&currentUserId=" +
                    currentUserId + "&OrgName=" + orgUnName;

               iframe = document.createElement("IFRAME");
               iframe.setAttribute("src", reviewAppUrl);
               iframe.setAttribute("scrolling", "no");
               iframe.style.width = "100%";
               iframe.style.height = "95%";
               iframe.style.border = "0px";

               if (document.body != null) {
                    var ribbon = window.parent.document.getElementById('crmRibbonManager');
                    ribbon.style.display = 'none';
                    document.body.appendChild(iframe);
                    window.refreshRibbon();
               }
          }

     </script>
</body>

</html>