if (typeof North52 == 'undefined') { North52 = {}; }
if (typeof North52.SourceRelationship == 'undefined') { North52.SourceRelationship = {}; }

var SourceRelationship = {}; 
SourceRelationship.Entities; 

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




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

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

  if ($("#formulaType").val() != 217890001)
  {
     $('#controlGroupFormulaSourceRelationship').hide(); 
     parent.Xrm.Page.data.entity.attributes.get("north52_sourcerelationship").setValue("");
  }
  else
  {  
     SDK.Metadata.RetrieveEntity(SDK.Metadata.EntityFilters.Relationships, entityLogicalName, null, false, function (entityMetadata) {
North52.SourceRelationship.successRetrieveAttributes(entityMetadata, entityDisplayName, initialLoad); }, North52.SourceRelationship.errorRetrieveAttributes);
   }

};

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

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

         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(North52.SourceRelationship.Sort).appendTo("#sourcerelationship");


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

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

};

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

North52.SourceRelationship.DropDownChanged= function(value, isReset) {  

  try
  {

    if (value != null) 
    {  
        var split = value.split("|");
        entityLogicalName = split[1];
        entityDisplayName = split[1];
 
		North52.TargetEntity.Load("insert", entityDisplayName, entityLogicalName, isReset);
        parent.Xrm.Page.data.entity.attributes.get("north52_sourcerelationship").setValue(value);  
     }

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

} ; 