<html><head><title>North52 Formula Manager Metadata Property</title>
<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.formula.loadalpha"></script>

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




</script>
<link rel="stylesheet" type="text/css" href="/_common/styles/fonts.css.aspx?lcid=1033"><link rel="stylesheet" type="text/css" href="/_common/styles/global.css.aspx?lcid=1033"><link rel="stylesheet" type="text/css" href="/_common/styles/select.css.aspx?lcid=1033">
<script type="text/javascript">


var MetadataProperty = {}; 
MetadataProperty.parentReferenceProperty = "";
MetadataProperty.CacheKey = "";

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

MetadataProperty.Init= function() {  
  
   $("#metadataproperty").hide();
  

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

    if (data!= "")
     {
       var vals = N52.HTMLWebResourceHelper.GetDataParameterArray(data);
       var type =  vals[0][1];
           

     
       if (type == 'load')
       {
            var entity =  vals[1][1];
            var parentReferenceProperty =  vals[2][1];
           var addAllPropertiesQuery=  vals[3][1];
           MetadataProperty.parentReferenceProperty = parentReferenceProperty ;

           MetadataProperty.Load(entity , parentReferenceProperty , addAllPropertiesQuery);
       }
       else if (type == 'query')
       {
              var entity =  vals[1][1];
             var parentReferenceProperty =  vals[2][1];
             var addAllPropertiesQuery=  vals[3][1];
             MetadataProperty.parentReferenceProperty = parentReferenceProperty ;

             MetadataProperty.LoadFromQuery(addAllPropertiesQuery); 
       }
       else if (type == 'clear')
          MetadataProperty.Clear(); 
     }

      


   }

MetadataProperty.Clear= function() {  
   $("select[id$=metadataproperty] > option").remove();
   N52.HTMLWebResourceHelper.InsertOptionSetItem("metadataproperty", "","");

   $('#loading').hide(); 
   $('#metadataproperty').show(); 

}

MetadataProperty.Sort = function(a, b) {  
   
  if (a.innerHTML == 'All Properties') {
       return -1;
  }
  else if (b.innerHTML == 'All Properties') {
     return 1;
  }

  return (a.innerHTML > b.innerHTML ) ? 1 : -1;
}

MetadataProperty.Load = function(entity, parentReferenceProperty, addAllProperties) {  

   MetadataProperty.parentReferenceProperty = parentReferenceProperty;
   MetadataProperty.CacheKey = 'SingleEntity' + parent.Xrm.Page.context.getOrgUniqueName() + entity;


         SDK.Metadata.RetrieveEntity(SDK.Metadata.EntityFilters.Attributes, entity, null, false, function (entityMetadata) {
MetadataProperty.successRetrieveAttributes(entityMetadata, parentReferenceProperty, addAllProperties); }, MetadataProperty.errorRetrieveAttributes);
  };



MetadataProperty.successRetrieveAttributes = function(entityMetadata,parentReferenceProperty, addAllProperties ) {  

 try
 {
   
  
 


   $("select[id$=metadataproperty] > option").remove();

   if (addAllProperties == true) {
      N52.HTMLWebResourceHelper.InsertOptionSetItem("metadataproperty", "All Properties","");
   }

   if (entityMetadata.Attributes != null)
   {

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

    if (attribute.DisplayName.UserLocalizedLabel != null)    
      {
           if (attribute.DisplayName.UserLocalizedLabel.Label != null)
             {
                    text = attribute.DisplayName.UserLocalizedLabel.Label
                    value = attribute.LogicalName;

                  N52.HTMLWebResourceHelper.InsertOptionSetItem('metadataproperty', text,value);
             }
        }
    }  
  }

    $("#metadataproperty option").sort(MetadataProperty.Sort).appendTo("#metadataproperty");
    currentAttribute = parent.Xrm.Page.getAttribute(parentReferenceProperty).getValue();
    if (currentAttribute !== null && currentAttribute!=="") {  
             $("#metadataproperty").val(currentAttribute);
            MetadataProperty.DropDownChanged(currentAttribute  );
      }
     else {
        $('#metadataproperty option:first-child').attr("selected", "selected");        
        currentAttribute = $("#metadataproperty option:selected").val();
        MetadataProperty.DropDownChanged(currentAttribute  );
    }

      $('#loading').hide(); 
      $('#metadataproperty').show(); 

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

};


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

MetadataProperty.DropDownChanged= function(value) {  
    if (value !== null) { 
        parent.Xrm.Page.data.entity.attributes.get(MetadataProperty.parentReferenceProperty).setValue(value); 
     }
} ; 

MetadataProperty.Reset= function() {  
 
        $('#metadataproperty option:first-child').attr("selected", "selected");     
} ; 

MetadataProperty.LoadFromQuery = function(query) {  

   $("select[id$=metadataproperty] > option").remove();

  

   var attributes = query.split(',');
                  
   for (var i = 0; i < attributes.length; i++) 
   {

     N52.HTMLWebResourceHelper.InsertOptionSetItem('metadataproperty', attributes[i],attributes[i]);

     }  

   $("#metadataproperty option").sort(MetadataProperty.Sort).appendTo("#metadataproperty");   

    currentAttribute  = parent.Xrm.Page.getAttribute("north52_targetentityproperty").getValue();
    if (currentAttribute !== null && currentAttribute!=="") {  
             $("#metadataproperty").val(currentAttribute);
             MetadataProperty.DropDownChanged(currentAttribute);
      }
     else {
        $('#metadataproperty option:first-child').attr("selected", "selected");        
        currentAttribute = $("#metadataproperty option:selected").val();
        MetadataProperty.DropDownChanged(currentAttribute  );
    }

  
  $('#loading').hide(); 
  $('#metadataproperty').show(); 
};

</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 onload="MetadataProperty.Init();" bgColor="transparent">&nbsp;</body></html>