<html><head><title>North52 Formula Manager Business Unit</title>
<script src="ClientGlobalContext.js.aspx"></script>

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

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

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

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


<script type="text/javascript">

var BusinessUnit = {}; 


BusinessUnit.Init= function() {  

try
{
    $("#ddlBusinessUnit").hide();
    $("select[id$=ddlBusinessUnit] > option").remove();
       
    var options= "$select=Name&$filter=Name ne ''&$orderby= Name asc";

       SDK.REST.retrieveMultipleRecords("BusinessUnit", options, BusinessUnit.RetrieveEntityData, function (error) { window['n52error'] = error; window['executionContext'] = null; alert("North52 Formula Manager: An error has occurred please contact your system administrator."  ); }, BusinessUnit.RetrieveComplete);

   
 }
 catch(ex)
 {
    alert('North52 Formula Manager: Communication issue, please click Ok & then F5 to refresh the screen. Message: ' + ex.message);

 }

};



BusinessUnit.RetrieveEntityData= function(data) {  

 try
 {

    for (var i = 0; i < data.length; i++)
    {
      N52.HTMLWebResourceHelper.InsertOptionSetItem("ddlBusinessUnit", data[i].Name, data[i].Name);    
     }

    var savedBusinessUnit = parent.Xrm.Page.getAttribute("north52_businessunitkey").getValue();
     N52.HTMLWebResourceHelper.InsertOptionSetItemAtTop("ddlBusinessUnit", '' , '' );     

    if (savedBusinessUnit !== null && savedBusinessUnit !=="") 
    {  
            $("#ddlBusinessUnit").val(savedBusinessUnit);
      }
      else
      {
         $("#ddlBusinessUnit").val('');
         parent.Xrm.Page.data.entity.attributes.get("north52_businessunitkey").setValue("");

      }
     
   

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

 }
 catch(ex)
 {
   alert('North52 Formula Manager: Communication issue, please click Ok & then F5 to refresh the screen. Message: ' + ex.message);
 }

  };



BusinessUnit.DropDownChanged= function(value) {  

  try
  {
    if (value != null && value!="")
    {  
       parent.Xrm.Page.data.entity.attributes.get("north52_businessunitkey").setValue(value);
     }
     else
     {
        parent.Xrm.Page.data.entity.attributes.get("north52_businessunitkey").setValue("");
     }

   }
   catch(ex)
   {
      alert('North52 Formula Manager: Communication issue, please click Ok & then F5 to refresh the screen. Message: ' + ex.message);
   }
} ; 


BusinessUnit.RetrieveComplete= function(value) {  

         var msg = 'Complete';
  };


</script>

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


</style>

<meta charset="utf-8">
</head><body style="margin: 0px; border: 0px currentColor; padding-top: 0px; padding-left: 0px; font-family: Segoe UI, Tahoma, Arial; font-size: 11px; font-weight: normal;" onload="BusinessUnit.Init();" bgColor="transparent">
<div id="container">
<div id="loading"><img id='loading-image"' alt="Loading" src="north52_image_progress"> </div>
<div id="main"><select style="width: 100%; font-family: Segoe UI; font-size: 11px; font-weight: normal;" id="ddlBusinessUnit" onchange="BusinessUnit.DropDownChanged(this.value)" class="ms-crm-SelectBox"></select> </div></div></body></html>