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

North52.ExecuteCommand.TimerCount = 0;
North52.ExecuteCommand.Completed = false;


North52.ExecuteCommand.Ok = function() {

  try
   { 
        $('#loading').show(); 
        $('#topRow').css("display", "none")
        var command = parent.parent.Xrm.Page.data.entity.attributes.get("north52_shortcode").getValue();

          var targetId = '';
 var options= "$select=north52_Result&$filter=north52_name eq " + "'"+ command  + "' and north52_Parameters eq '<north52_command></north52_command>' and north52_TargetEntityID eq '" + targetId + "' and north52_FormulaType eq '99'"; 


  SDK.REST.retrieveMultipleRecords("north52_formulacalculation", options, North52.ExecuteCommand.retrieveCommandCallBack, function (error) { North52.ExecuteCommand.cleanUp(); 
 $("#lblResult").text("North52 Formula Manager: " + error.message ); }, North52.ExecuteCommand.configSuccess);

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

};

North52.ExecuteCommand.retrieveCommandCallBack= function (retrievedResult) {

   try
   {
    var displayResult = '';
    North52.ExecuteCommand.Completed = true;

    if(retrievedResult == null) 
         displayResult = "An internal failure has occurred. Please enable server side tracing & check the N52 Trace Log entity.";
    else
    {
       var formulaResult = retrievedResult[0];

       if (formulaResult.north52_Result == 'NoOp')
            displayResult = 'Command Completed.';
       else
       {
           var result= JSON.parse(formulaResult.north52_Result);
           displayResult  = result.PropertyValueAction;
       }
    }
       $('#loading').hide(); 
       $("#lblResult").text(displayResult);

     
   }

  catch (ex)
  {
    North52.ExecuteCommand.Completed = true;
    $("#lblResult").text('North52 Formula Manager Error: ' + ex.message);

   }
};

 North52.ExecuteCommand.Timer= function () {
       
       if (North52.ExecuteCommand.Completed == false)
       {
           $("#lblResult").text('Execution Time: ' + North52.ExecuteCommand.TimerCount);
           setTimeout(North52.ExecuteCommand.Timer, 1000);
 
            North52.ExecuteCommand.TimerCount += 1;
        }
    };

 North52.ExecuteCommand.cleanUp= function () {
            North52.ExecuteCommand.Completed = true;
           $('#loading').hide(); 
          
     };


 North52.ExecuteCommand.configSuccess= function () {

     };

 North52.ExecuteCommand.configError= function (XmlHttpRequest, textStatus, errorThrown) {
                      North52.ExecuteCommand.Completed = true;
                       $("#lblResult").text('North52 Formula Manager: ' + XmlHttpRequest.message );
                       North52.ExecuteCommand.cleanUp();
       };