//Wald's Sequential Probabilty Ratio Test online. (SPRT)
//copyright: Daan G Uitenbroek PhD

function addspace(real,lenght)  // changes scientific into normal notation and cuts to size
{
var str=""+real;
for (var index=str.length;index<=lenght;index++)
      str+=" ";
pt=str.indexOf(".");
if (pt==0) str="0"+str    //solves that newer versions of Netscape give inconsistent numeric notations
return str.substring(0,lenght);
}

function format(integer,fieldlenght) // places leading spaces
{
var str=""+integer;
var r="";
for (var index=1;index<fieldlenght-str.length+1;index++)
 r+=" ";
str=r+str;
pt=str.indexOf(".");
if (pt==0) str="0"+str    //solves that newer versions of Netscape give inconsistent numeric notations
return str;
}

function sprt(p1,p2,alfa,power,r)
{
var writeln="\n";
if (navigator.appVersion.lastIndexOf('Win') != -1) writeln="\r\n"; 

if (p1>1.00) p1=p1/100;
if (p2>1.00) p2=p2/100;
if (alfa>1.00) alfa=alfa/100;
if (power>1.00) power=power/100

beta=1-power*1;
var s=Math.log((1-p1)/(1-p2))/Math.log((p2/p1)*(1-p1)/(1-p2));
var h1=Math.log((1-alfa)/beta)/Math.log((p2/p1)*(1-p1)/(1-p2));
var h2=Math.log((1-beta)/alfa)/Math.log((p2/p1)*(1-p1)/(1-p2));

document.form1.outp.value+=writeln;
document.form1.outp.value+="General Info"+writeln+"Regression Coefficient: "+addspace(s,5)+writeln;
document.form1.outp.value+="Low Intercept: "+addspace(h1,4)+"; High Intercept: "+addspace(h2,4)+writeln+writeln;
document.form1.outp.value+="       Sequential Probability Ratioos"+writeln;
document.form1.outp.value+="trial\t-  lower limit    -	  higher limit"+writeln;


for (index=1;index<=r;index++)
  {   
     var upper=((s*index+h2));
     var lower=((s*index-h1));
     document.form1.outp.value+="#"+format(index,3)+" >  "+addspace(s*index-h1,5);
     if (lower>=0)
      document.form1.outp.value+="  ("+addspace(lower/index*100,5)+"%)  "
      else document.form1.outp.value+="  continue  ";
     document.form1.outp.value+="	"+addspace(s*index+h2,5);
     if (index>=upper) 
       document.form1.outp.value+="  ("+addspace(upper/index*100,5)+"%)"
       else document.form1.outp.value+="  continue";
     document.form1.outp.value+=writeln;
   }
}

function ClearOutp()
{
var writeln="\n";
if (navigator.appVersion.lastIndexOf('Win') != -1) writeln="\r\n"; 
document.form1.outp.value="cleared; "+writeln;
}

