<html><head><title>North52 Formula Manager Source Attribute</title>

<style type="text/css">

html, body{
margin: 0px;
padding: 0px;
background-color: transparent;
}
</style>

<script src="ClientGlobalContext.js.aspx"></script>

<script type="text/javascript" src="north52_/javascript/jquery"></script>

<script type="text/javascript" src="north52_/javascript/JSON2"></script>

<script type="text/javascript" src="north52_/Javascript/sdk.metadata.js"></script>

<script type="text/javascript" src="north52_/javascript/north52.htmlwebresource.helper"></script>






<script type="text/javascript">

  $(document).ready(function () { SourceRelationship.Init();
     });

var SourceRelationship = {}; 
SourceRelationship.Entities;

SourceRelationship.Disable= function() {  
   $("#sourcerelationship").attr("disabled", true);
}

SourceRelationship.Init= function() {  
  
  $("#sourcerelationship").hide();


    var data = N52.HTMLWebResourceHelper.GetParameterByName('data');


    if (data!= "")
     {
      var vals = N52.HTMLWebResourceHelper.GetDataParameterArray(data);
     var Name=  vals[0][1];
     var Value=  vals[1][1];
     var isInitialLoad =  vals[2][1];

     SourceRelationship.Load (Name, Value, isInitialLoad );
   }


}


SourceRelationship.Sort = function(a, b) {  
   
  return (a.innerHTML > b.innerHTML ) ? 1 : -1;
};

SourceRelationship.Load = function(entityDisplayName, entityLogicalName, initialLoad) {  

 
 //Retrieve attributes
     SDK.Metadata.RetrieveEntity(SDK.Metadata.EntityFilters.Relationships, entityLogicalName, null, false, function (entityMetadata) {
SourceRelationship.successRetrieveAttributes(entityMetadata, entityDisplayName, initialLoad); }, SourceRelationship.errorRetrieveAttributes);

};

SourceRelationship.successRetrieveAttributes = function(entityMetadata, entityDisplayName, initialLoad) {  

 try
 {
   
   $("select[id$=sourcerelationship] > option").remove();
   if (entityMetadata.ManyToOneRelationships!= null)
   {

        //entityMetadata.ManyToOneRelationships.sort(compare);

         for (var i = 0; i < entityMetadata.ManyToOneRelationships.length; i++)
         {
             var relationship = entityMetadata.ManyToOneRelationships[i];

              var txt = relationship.ReferencedEntity;
              var val = relationship.ReferencingAttribute;
   
              text = val + " (" + txt + ")";
              value = val+"|"+txt;

              N52.HTMLWebResourceHelper.InsertOptionSetItem("sourcerelationship", text,value);
          }  
     }

     isReset = true;
     $("#sourcerelationship option").sort(SourceRelationship.Sort).appendTo("#sourcerelationship");

      var formulaType = parent.Xrm.Page.data.entity.attributes.get("north52_formulatype").getValue();
      if ( formulaType  != 217890001 ) 
      { 
         parent.Xrm.Page.ui.controls.get("WebResource_html_sourcerelationship").setVisible(false);
         return;
      }

      currentRelationship = parent.Xrm.Page.getAttribute("north52_sourcerelationship").getValue();
      if (currentRelationship !== null && currentRelationship !=="") {  
            if (initialLoad == true) {isReset =  false;}

            SourceRelationship.DropDownChanged(currentRelationship, isReset );
            $("#sourcerelationship").val(currentRelationship );
      }
      else {
        $('#sourcerelationship option:first-child').attr("selected", "selected");        
        currentRelationship = $("#sourcerelationship option:selected").val();
        SourceRelationship.DropDownChanged(currentRelationship);
       }
  
     $('#loading').hide(); 
     $('#sourcerelationship').show(); 
   
   }
   catch(ex)
   {
     alert('North52 Formula Manager Exception: ' + ex.message);
   }

};

SourceRelationship.errorRetrieveAttributes = function(error) {  
    alert("North52 Formula Manager : " + error.message);
  }

SourceRelationship.DropDownChanged= function(value, isReset) {  

  try
  {

    if (value != null) 
    {  
        var split = value.split("|");
        entityLogicalName = split[1];
        entityDisplayName = split[1];
 
        var url = parent.Xrm.Page.ui.controls.get('WebResource_html_targetentity').getSrc();
        url = url.indexOf('?') > 0 ? url.substring(0, url.indexOf('?')) : url;

        command = encodeURIComponent("type=insert&entityDisplayName ="+entityDisplayName + "&entityLogicalName=" + entityLogicalName+ "&isReset=" + isReset + "&random=" + Math.random());
        url = url + "?data=" +command;

        parent.Xrm.Page.ui.controls.get('WebResource_html_targetentity').setSrc(url);

        parent.Xrm.Page.data.entity.attributes.get("north52_sourcerelationship").setValue(value);  
     }

   }
   catch(ex)
   {
     alert('North52 Formula Manager Exception: ' + ex.message);
   }

} ; 



</script>

<style type="text\css">
#loading { 
  width: 100%; 
  height: 100%; 
  top: 0px; 
  left: 0px; 
  position: fixed; 
  display: block; 
  opacity: 0.7; 
  background-color: #fff; 
  z-index: 99; 
  text-align: center; 
} 
 
#loading-image { 
  position: absolute; 
  top: 100px; 
  left: 240px; 
  z-index: 100; 
}

</style>

<meta charset="utf-8">
</head><body>&nbsp;</body></html>