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

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

North52.TargetProperty.Disable= function() {  
   $("#targetproperty").attr("disabled", true);
}

North52.TargetProperty.Load= function(type, entity, parentReferenceProperty, addAllPropertiesQuery) {  
  
   $("#targetproperty").hide();
  
       if (type == 'load')
       {
           TargetProperty.parentReferenceProperty = parentReferenceProperty ;

           North52.TargetProperty.LoadLocal(entity , parentReferenceProperty , addAllPropertiesQuery);
       }
       else if (type == 'query')
       {
             TargetProperty.parentReferenceProperty = parentReferenceProperty ;

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

      


   

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

   $('#loading-targetproperty').hide(); 
   $('#targetproperty').show(); 

}

North52.TargetProperty.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;
}

North52.TargetProperty.LoadLocal = function(entity, parentReferenceProperty, addAllProperties) {  

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


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



North52.TargetProperty.successRetrieveAttributes = function(entityMetadata,parentReferenceProperty, addAllProperties ) {  

 try
 {

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

   if (addAllProperties == true) {
      N52.HTMLWebResourceHelper.InsertOptionSetItem("targetproperty", "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('targetproperty', text,value);
             }
        }
    }  
  }

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

      $('#loading-targetproperty').hide(); 
      $('#targetproperty').show(); 

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

};


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

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

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

North52.TargetProperty.LoadFromQuery = function(query) {  

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

  

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

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

     }  

   $("#targetproperty option").sort(North52.TargetProperty.Sort).appendTo("#targetproperty");   

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

  
  $('#loading-targetproperty').hide(); 
  $('#targetproperty').show(); 
};
