function InputSet = testinput(InputSet)
%SIMSOC Social Security Trust Fund simulation.
%   testinput looks at the InputSet structure which is passed in by the
%   Matlab webserver, and tests the user's input parameters for
%   errors.
  
%   For convenience, certain calculations get added to the InputSet 
%   structure
%
%   REVISED:  July 30, 2007

runid = InputSet.runid;
rundescr = InputSet.rundescr;
numbtraj   = str2double(InputSet.numbtraj); 
randtraj = InputSet.randtraj;
paytax      = str2double(InputSet.paytax);
plotend      = str2double(InputSet.plotend);
usessapop = InputSet.usessapop;
usedetint = InputSet.usedetint;
usedetprod = InputSet.usedetprod;
usedetsnp = InputSet.usedetsnp;
snpprop = str2double(InputSet.snpprop);
snpyear = str2double(InputSet.snpyear);
snpprop2 = str2double(InputSet.snpprop2);
snpyear2 = str2double(InputSet.snpyear2);
nra  = str2double(InputSet.nra);
nrayears = str2double(InputSet.nrayears);
%nrayear1 = str2double(InputSet.nrayear1);  NOW SET THIS TO 2000

nrayear1=2000;
InputSet.nrayear1 = nrayear1;

nrayear2 = str2double(InputSet.nrayear2);
nrayear3 = str2double(InputSet.nrayear3);
nrayear4 = str2double(InputSet.nrayear4);
alphain = str2double(InputSet.alphain);
betain = str2double(InputSet.betain);

% Check input arguments.
if ( (length(runid) < 6) | (length(runid) > 8) )
     error('The ID for your run must be at least 6 characters, no more than 8');
elseif (length(runid) == 6)
   if ((runid=='xxxxxx') | (runid=='      ') )
      error('Enter a unique ID for this run (something other than ''xxxxxx'')');
   end
elseif (length(runid) == 7)
   if (runid=='       ') 
      error('Enter a unique ID for this run, using only letters and digits');
   end
elseif (length(runid) == 8)
   if (runid=='        ') 
      error('Enter a unique ID for this run, using only letters and digits');
   end
end

% Check input arguments.
if (length(rundescr) > 53) 
     rundescr=rundescr(1:53);
end


if (nzmax(numbtraj)~=1)
  error('Enter a scalar number of trajectories');
elseif ( numbtraj <= 1 | numbtraj~=round(numbtraj) )
  error('The number of trajectories must be a positive integer greater than 1');
elseif ( numbtraj > 1000 )
  error('The number of trajectories must be 1000 or less');
end
InputSet.numbtraj = numbtraj;

if ((randtraj~='n')&(randtraj~='N')&(randtraj~='Y')&(randtraj~='y'))
   error('Enter "Y" or "N" to re-seed the random number generator')
elseif ((randtraj=='n')|(randtraj=='N'))
   getrand=0;
elseif ((randtraj=='y')|(randtraj=='Y'))
   getrand=1;
end

InputSet.getrand = getrand;

if (nzmax(plotend)~=1)
  error('Enter a scalar for the plotting endpoint');
elseif ( plotend < 2002 | plotend~=round(plotend) | plotend > 2101 )
  error('The plotting endpoint must be a positive integer from 2002 to 2101');
end

InputSet.plotend = plotend;

if ((usessapop~='n')&(usessapop~='N')&(usessapop~='Y')&(usessapop~='y'))
   error('Enter "Y" or "N" for using the deterministic population forecasts')
elseif ((usessapop=='n')|(usessapop=='N'))
   calib=0;
elseif ((usessapop=='y')|(usessapop=='Y'))
   calib=1;
end

InputSet.calib = calib;

if ((usedetint~='n')&(usedetint~='N')&(usedetint~='Y')&(usedetint~='y'))
   error('Enter "Y" or "N" for using the deterministic interest rate forecasts')
elseif ((usedetint=='n')|(usedetint=='N'))
   detint=0;
elseif ((usedetint=='y')|(usedetint=='Y'))
   detint=1;
end

InputSet.detint = detint;

if ((usedetprod~='n')&(usedetprod~='N')&(usedetprod~='Y')&(usedetprod~='y'))
   error('Enter "Y" or "N" for using the deterministic productivity rate forecasts')
elseif ((usedetprod=='n')|(usedetprod=='N'))
   detprod=0;
elseif ((usedetprod=='y')|(usedetprod=='Y'))
   detprod=1;
end

InputSet.detprod = detprod;
if ((usedetsnp~='n')&(usedetsnp~='N')&(usedetsnp~='Y')&(usedetsnp~='y'))
   error('Enter "Y" or "N" for using the deterministic equities returns')
elseif ((usedetsnp=='n')|(usedetsnp=='N'))
   detsnp=0;
elseif ((usedetsnp=='y')|(usedetsnp=='Y'))
   detsnp=1;
end

InputSet.detsnp = detsnp;

if (nzmax(paytax)~=1)
  error('Enter a scalar payroll tax rate (be sure to leave off the "%" sign)');
elseif ( paytax < 12.4 )
  error('The payroll tax rate must be at least 12.4%');
elseif ( paytax >= 30 )
  error('The payroll tax rate must be less than 30%');
end

InputSet.paytax = paytax;

if (nzmax(snpprop)~=1)
  error('Enter a scalar for the percentage of the fund to be invested (leave off "%" sign)');
elseif ( snpprop < 0 )
  error('The percentage of the fund to be invested must be positive');
elseif ( snpprop > 100 )
  error('The percentage of the fund to be invested must be less than 100');
end

InputSet.snpprop = snpprop;

if (nzmax(snpprop2)~=1)
  error('Enter a scalar for the percentage of the fund to be invested (leave off "%" sign)');
elseif ( snpprop2 < 0 )
  error('The percentage of the fund to be invested must be positive');
elseif ( snpprop2 > 100 )
  error('The percentage of the fund to be invested must be less than 100');
end

InputSet.snpprop2 = snpprop2 ;

if ((snpprop>0) | (snpprop2>0)),
   if (nzmax(snpyear)~=1)
     error('Enter an integer for the year to begin investing in equities');
   elseif ( snpyear < 2002 | snpyear > 2090 | snpyear~=round(snpyear) )
     error('The number of trajectories must be a positive integer from 2002 to 2090');
   end
end

InputSet.snpyear = snpyear;

if ((snpprop>0) | (snpprop2>0)),
   if (nzmax(snpyear2)~=1)
     error('Enter an integer for the year to begin investing in equities');
   elseif ( snpyear2 < 2003 | snpyear2 > 2090 | snpyear2~=round(snpyear2) )
     error('The number of trajectories must be a positive integer from 2003 to 2090');
   elseif (snpyear2 <= snpyear)
     error('The year after which investment is constant must be greater than the year in which investment is initiated');
   end
end

InputSet.snpyear2 = snpyear2;

if (nzmax(nra)~=1)
  error('Enter a scalar NRA');
elseif ( nra < 67 | nra > 69 )
  error('The NRA must be at least 67 and no more than 69');
elseif ( nra~=round(nra) )
   error('The NRA must be an integer from 67 to 69');
end

if (nzmax(nrayears)~=1)
  error('Enter a scalar for the number of years of each NRA phase-in');
elseif ( nrayears < 1 | nrayears > 6 | nrayears~=round(nrayears) )
  error('The number of years for each NRA phase-in must be an integer from 1 to 6');
end

InputSet.nra = nra;
InputSet.nrayears = nrayears;

if (nzmax(alphain)~=1)
  error('Enter a scalar for the percentage decrease in the hazard rate of retirement at age 62');
elseif ( (alphain < 0) | (alphain >100) )
  error('The percentage decrease in the hazard rate of retirement at 62 must be between 0 and 100');
end

if (alphain==0) 
   betain=0;
end

if (nzmax(betain)~=1)
  error('Enter a scalar for the percentage of those postponing retirement at 62 who roll over to DI');
elseif ( (betain < 0) | (betain >100) )
  error('The percentage of those postponing retirement at 62 who roll over to DI must be between 0 and 100');
end

InputSet.alphain = alphain;
InputSet.betain = betain;

numshifts=nra-65;
nrayrvec=[nrayear1 nrayear2 nrayear3 nrayear4];
nrayrvec=nrayrvec(1:numshifts);

if isnan(nrayrvec(numshifts))
   error('If you raised the NRA, remember to specify all the years in which each NRA shift begins to phase-in')
end

for (k5 = 2:numshifts),
   junktemp=nrayrvec(k5);
   if (nzmax(junktemp)~=1)
      error('Enter scalars for the years in which NRA phase-ins begin')
   elseif ((junktemp~=round(junktemp)) | (junktemp<2007) | (junktemp>2090))
      error('Enter integers from 2007 to 2090 for the years in which NRA phase-ins begin')
   end
end

if (sum(diff(nrayrvec)<nrayears)>0)
   error('There are not enough years separating the start of each NRA increase phase-in')
end

InputSet.nrayear2 = nrayear2;
InputSet.nrayear3 = nrayear3;
InputSet.nrayear4 = nrayear4;

