function privateslcbsampletime(dialog, value, varargin)
%PRIVATESLCBSAMPLETIME Validates the sample time entry in the T&M SL blocks.
%
%    PRIVATESLCBSAMPLETIME(DIALOG, VALUE) validates the sample time entry
%    for the T&M Simulink block.
%    PRIVATESLCBSAMPLETIME(DIALOG, VALUE, ALLOWINF) validates the sample time entry
%    for the T&M Simulink blocks which allow Inf and -1 as sample time
%    values.

%    SS 04-01-08
%    Copyright 2008-2011 The MathWorks, Inc.
%    $Revision: 1.1.6.3 $  $Date: 2012/10/08 03:27:27 $

% Get the dialog source object. 
obj = dialog.getDialogSource;

% Convert sample time to number. 
sampleTime = str2num(value); %#ok<ST2NM>

% Initialize
isErr = false;

% Allow variable names but do not allow NaN, i and j (complex) as
% variables. If they are left undefined, it yields incorrect results.
if isvarname(value) && ~ismember(lower(value), {'nan', 'i', 'j', 'inf'})
    % Assign the sample time from the block to the source. 
    obj.SampleTime = obj.Block.SampleTime;    
    return;
end

% Check for non-numeric and negative values.
if  isempty(sampleTime) || ~isscalar(sampleTime) || ~isnumeric(sampleTime) || ...
        isnan(sampleTime) 
    isErr = true;
end

if ~isErr
    if (nargin == 3) %if user requires inf and -1 to be allowed.
        allowInf = varargin{1};
        if allowInf
            if (isinf(sampleTime) && sampleTime<0) || ...
                    (sampleTime<=0 && sampleTime~=-1)
                isErr = true;
            end
        else
            if (isinf(sampleTime) || sampleTime<=0)
                isErr = true;
            end
        end
    else
        if (isinf(sampleTime) || sampleTime<=0)
            isErr = true;
        end
    end
end

% Call error if present.
if isErr
    % Call T&M function to display error dialog.
    privatesldialogbox( dialog, ...
                sprintf('Sample time must be a positive real number.'), ...
                sprintf('Configuration error') );
    % Restore the previous value on the dialog.                                
    obj.Block.SampleTime = obj.SampleTime;
    return;
end

% Assign the sample time from the block to the source. 
obj.SampleTime = obj.Block.SampleTime;