function [k,qt,tc] = erlces(A,h,N,Q,I,K0)
%Markov Chain Event Simulator for Erlang C configuration
% Call
%Where A = offered traffic in Erlang (for M/x queue lambda = A/h)
%      h = holding time is seconds   (For M/x queue mu =1/h)
%      N = Number of resources
%      Q =Queue length
%      I = number of attempts/arrivals to process
%      K0 = warm up/cool down period
%      K = sequence of states visited

if (nargin == 5)
  K0 = 0; 
endif
I = I + K0
g = h/A; % Average Call Gap

% Initialise start times 
ts =  cumsum([0;g*(-log(rand(I-1,1)))]);
td =  h*(-log(rand(I,1)));
te =  ts + td;

[t,it] = sort([ts;te]); %it = original location
ic = ones(2*I,1);
ic(find(it>I)) =-1;

q=zeros(1,Q)

k=0;
qt=0
for i=1:2*I
  if (ic(i)==1)            %call start
    if (k==N)
      if any(q==0)         % queue call, queue place available
        j = find(q==0)    
        q(j) = i
        qt(i) = length(j)
      else                 % queue full - block call
        ic(i) =0;
        ic(find(it==(it(i)+I)))=0;
      endif
    else                   % normal start
      k =k+1
    endif      
  elseif (ic == -1)  %end
    if (k==N)
      if any(q>0)          % queued call exists
        j = find(q==0)     % de-queue
        te(i) = ???        %recalculate end time
        qt(i) = qt(i) -1
      else                 % queue empty, normal end
        k =k+1
      endif 
  endif       
endfor



