//statistical correlation analysis online
//copyright: Daan G Uitenbroek PhD

function truncate(real,places)  // truncates 'real' numbers n 'places' behind the decimal dot
{
var multi=Math.pow(10,places);
return (Math.round(real*multi)/multi)
}

function cut(real,lenght)  // changes scientific into normal notation and cuts to size
{
var str=""+real;
var r="0.";
var pt=str.indexOf("e");
if (pt>0 ) 
    {
     var a=str.substring(0,1)+str.substring(2,pt+1);
     for (var index=1;index<=(str.substring(pt+2,pt+5)*1-1);index++)
        r+=0;
     var st=r+a;
     for (var index=st.length;index<=lenght;index++)
      st+="0";
     return (st.substring(0,lenght));
     };
for (var index=str.length;index<=lenght;index++)
      str+="0";
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 addspace(real,lenght) 
{
var str=""+real;
for (var index=str.length;index<=lenght;index++)
      str=" "+str;
var pt=str.indexOf(".");
if (pt==0) str="0"+str    //solves that newer versions of Netscape give inconsistent numeric notations
return str;
}

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;
return str;
}

function zprob(zval)  
{ 
if ((zval>5)||(zval<-5)) outp=tprob(zval,4000)
else
{
if (zval == 0.0)     
     var x  = 0.0;        
else      
     {  
     var y = 0.5 * Math.abs (zval);        
     if (y >= (10000* 0.5))        
             x=1.0;  
     else if (y < 1.0)         
     {         
     var w = y*y;          
     x = ((((((((0.000124818987 * w  
          -0.001075204047) * w +0.005198775019) * w  
          -0.019198292004) * w +0.059054035642) * w  
          -0.151968751364) * w +0.319152932694) * w  
          -0.531923007300) * w +0.797884560593) * y * 2.0;     

    
     }         
     else      
     {  
     y -= 2.0;         
     x  = (((((((((((((-0.000045255659 * y  
          +0.000152529290) * y -0.000019538132) * y  
          -0.000676904986) * y +0.001390604284) * y  
          -0.000794620820) * y -0.002034254874) * y  
          +0.006549791214) * y -0.010557625006) * y  
          +0.011630447319) * y -0.009279453341) * y  
          +0.005353579108) * y -0.002141268741) * y  
          +0.000535310849) * y +0.999936657524*1;      
     }          
     }  
var outp=((zval > 0.0 ? ((x*1+1.0*1) * 0.5) : ((1.0 -x) * 0.5))) 
if ((outp>1)||(outp<-1)) (outp=1) 
}
return (outp) 
} 

function tprob(tval,df)
{

function probv(b,s,tval,ioe,df)  //support function for tprob
{
  var a=tval/Math.sqrt(df);
  if (ioe>0.5)
     {
       if (df==1) s=0;
       var hulp= 0.5*1+(a*b*s+Math.atan(a))/Math.PI;
       return hulp
     }
  else
   var hulp=0.5*1+0.5*a*Math.sqrt(b)*s;
   return hulp
}

if (df>4000) df=4000;
var s=1/1;
var c=1/1;
var f=1/1;
var ioe=Math.round(Math.round(df/2+0.1*1)-df/2+0.1*1); //takes the modules
var b=df/(df*1+tval*tval);
var fk=2*1+ioe*1;
var pval=989;
if ((df-2)<2) 
   pval=probv(b,s,tval,ioe,df);
for (index=fk;index<(df-1);index+=2)
  {
    c=c*b*(fk-1)/fk;
    s=s*1+c*1;
    if (s==f) pval=probv(b,s,tval,ioe,df);
    f=s;
    fk=fk*1+2*1;
  }
if (pval==989) pval=probv(b,s,tval,ioe,df);

return pval
}

function zcor(r)
{
var zval=0.5*Math.log((1*1+r*1)/(1-r))
return zval
}

function contraz(z)
{
var expz=Math.exp(z*2)
var rval=(expz*1-1*1)/(expz*1+1*1)
return rval
}

function depdif(r1,r2,r3,n)
{

function dept(r1,r2,r3,n)
{
//var tvalue=(r1-r2)*Math.sqrt((n-3)*(1*1+r3*1)/(2*(1-(r1*r1)-(r3*r3)-(r2*r2)+(2*r1*r2*r3)*1)))
// above old method by Blalock (1960)
// below the Hotelling William test
var R=(1-(r1*r1)-(r3*r3)-(r2*r2)+(2*r1*r2*r3)*1)
var meanr=(r1*1+r2*1)/2
var denominator=(r1-r2)*Math.sqrt((n-1)*(1*1+r3*1))
var numerator=Math.sqrt((2*(n-1)/(n-3)*R)+meanr*meanr*(1*1-r3*1)*(1*1-r3*1)*(1*1-r3*1))
var tvalue=denominator/numerator

return tvalue;
} //end of dept

function stdev(r1,r2,r3,n)
{
var zvalue=(1-(r1*r1))*(1-(r1*r1))+(1-(r2*r2))*(1-(r2*r2))-(2*r3*r3*r3)
zvalue=zvalue-2*(r3-r1*r2)*(1-(r1*r1)-(r2*r2)-(r3*r3))  
var sdz=Math.sqrt(n/zvalue)
return sdz;
}
 
writeln="\n";
if (navigator.appVersion.lastIndexOf('Win') != -1) writeln="\r\n";

document.form1.outp.value+=writeln+"dependent (single sample) differences"+writeln
                          +"the correlations must be: rxy, rxz & ryz"+writeln

var dif=r1-r2
document.form1.outp.value+=writeln+"t-values for all differences:"+writeln+writeln

if (r1!=0){
   var tval=dept(r1,r2,r3,n)
   document.form1.outp.value+="r1-r2= "+truncate(dif,5)+"; t= "+truncate(tval,5)+" (p="+truncate(tprob(tval,n-3),5)+"; 1-p="+truncate(1-tprob(tval,n-3),4)+")"+writeln
  }
if (r2!=0)
  {var tval=dept(r1,r3,r2,n)
   document.form1.outp.value+="r1-r3= "+truncate((r1-r3),5)+"; t= "+truncate(tval,5)+" (p="+truncate(tprob(tval,n-3),5)+"; 1-p="+truncate(1-tprob(tval,n-3),4)+")"+writeln
  }
if (r3!=0)
  {var tval=dept(r2,r3,r1,n)
   document.form1.outp.value+="r2-r3= "+truncate((r2-r3),5)+"; t= "+truncate(tval,5)+" (p="+truncate(tprob(tval,n-3),5)+"; 1-p="+truncate(1-tprob(tval,n-3),4)+")"+writeln
                               +"all with "+(n-3)+" degrees of freedom"+writeln
                               +"(multiply p-values with 2 for double sided testing)"+writeln
  }

document.form1.outp.value+=writeln+"Confidence Intervals for the difference"+writeln
                          +"between r1 and r2"+writeln+writeln
document.form1.outp.value+="C.I. \t lower \t\t d(r) \t\t upper"+writeln
                          +"\t---------------------------------------"+writeln;
if (n<21) document.form1.outp.value+="Confidence intervals only if the"+writeln
                                    +"number of cases is larger than 20"+writeln;
else
for (var index=1;index<=4;index++)  //make the confidence intervals
{
document.form1.outp.value+=ConfInt[index].interval
var lower=(dif-ConfInt[index].intzval/stdev(r1,r2,r3,n))
document.form1.outp.value+="\t"+truncate(lower,6)+"   < \t"+truncate((dif),4)
var higher=(dif+ConfInt[index].intzval/stdev(r1,r2,r3,n))
document.form1.outp.value+="    < \t"+truncate(higher,6)+writeln
}
}

function inddif(r1,r2,N1,N2)
{
writeln="\n";
if (navigator.appVersion.lastIndexOf('Win') != -1) writeln="\r\n";

document.form1.outp.value+=writeln+"independent (two sample) difference"+writeln+writeln

var z1=zcor(r1)
var z2=zcor(r2)

var zr=z1-z2
document.form1.outp.value+="\t Confidence Intervals for the difference"+writeln+writeln
document.form1.outp.value+="C.I. \t lower \t\t d(r) \t\t upper"+writeln
                          +"\t---------------------------------------"+writeln;
if (N1<11) document.form1.outp.value+="Confidence intervals only if the"+writeln
                                    +"number of cases is larger than 10"+writeln;
else
for (var index=1;index<=4;index++)  //make the confidence intervals
{
document.form1.outp.value+=ConfInt[index].interval
var lower=contraz(zr-ConfInt[index].intzval*Math.sqrt((1/(N1-3))+(1/(N2-3))))
document.form1.outp.value+="\t"+truncate(lower,6)+"   < \t"+truncate((r1-r2),4)
var higher=contraz(zr*1+ConfInt[index].intzval*Math.sqrt((1/(N1-3))+(1/(N2-3))))
document.form1.outp.value+="    < \t"+truncate(higher,6)+writeln
}
document.form1.outp.value+=writeln


var sdz=Math.sqrt((1/(N1-3))+(1/(N2-3)))
var z=(z1-z2)/sdz
document.form1.outp.value+="r1-r2= "+truncate((r1-r2),5)+"; z= "+truncate(z,5)+" (p="
                         +truncate(zprob(z),5)+"; 1-p="+truncate(1-zprob(z),4)+")"+writeln
                           +"(multiply p-value with 2 for double sided testing)"+writeln+writeln
}

function popdif(r1,r2,N1)
{
writeln="\n";
if (navigator.appVersion.lastIndexOf('Win') != -1) writeln="\r\n";

document.form1.outp.value+=writeln+"\t difference between a population and"+writeln
                          +"\t     a sample correlation"+writeln+writeln

var z1=zcor(r1)
var z2=zcor(r2)

var zr=z1-z2
document.form1.outp.value+="\t Confidence Intervals for the difference"+writeln+writeln
document.form1.outp.value+="C.I. \t lower \t\t d(r) \t\t upper"+writeln
                          +"\t---------------------------------------"+writeln;
if (N1<11) document.form1.outp.value+="Confidence intervals only if the"+writeln
                                    +"number of cases is larger than 10"+writeln;
else
for (var index=1;index<=4;index++)  //make the confidence intervals
{
document.form1.outp.value+=ConfInt[index].interval
var lower=contraz(zr-ConfInt[index].intzval*Math.sqrt((1/(N1-3))))
document.form1.outp.value+="\t"+truncate(lower,6)+"   < \t"+truncate((r1-r2),4)
var higher=contraz(zr*1+ConfInt[index].intzval*Math.sqrt((1/(N1-3))))
document.form1.outp.value+="    < \t"+truncate(higher,6)+writeln
}
document.form1.outp.value+=writeln

var sdz=Math.sqrt(1/(N1-3))
var z=(z1-z2)/sdz
document.form1.outp.value+="r1-r2= "+truncate((r1-r2),5)+"; z= "+truncate(z,5)+" (p="
                         +truncate(zprob(z),5)+"; 1-p="+truncate(1-zprob(z),4)+")"+writeln
                         +"(multiply p-value with 2 for double sided testing)"+writeln+writeln
}

function makepartials(r1,r2,r3,n)
{

function partial(cor1,cor2,cor3)
{
var part=(cor1-(cor2*cor3))/(Math.sqrt(1-(cor2*cor2))*Math.sqrt(1-(cor3*cor3)));
return part;
}

function fpr(F,df1,df2){if (F < 0.2 || df1 < 1 || df2 < 1)
  return (1.0);                          //10
else
{
var a = df1%2 ? 1 : 2;
var b = df2%2 ? 1 : 2;
var w = (F * df1) / df2;
var z = 1.0 / (1.0*1 + w*1);

if (a == 1)
  {if (b == 1)
     {               //20
      var p = Math.sqrt (w);
      var y = 1 / Math.PI
      var d = y * z / p;
      var p = 2.0 * y * Math.atan(p)
     }
  else
     {
      var p = Math.sqrt (w * z);
      var d = 0.5 * p * z / w
     }}
else 
   {if (b == 1)
     {
      var p = Math.sqrt (z);
      var d = 0.5 * z * p;
      var p = 1.0 - p
     }
   else
     {
      var d = z * z;
      var p = w * z
     }}

y = 2.0 * w / z;

for (j = b*1 + 2*1; j <= df2; j += 2)
   {
    d *= (1.0 + a / (j - 2.0)) * z;
    p = (a == 1 ? p*1 + d * y / (j - 1.0) : (p*1 + w*1) * z);
   } //Tolmans version replaces this loop

y = w * z;
z = 2.0 / z;
b = df2 - 2;

for (i = a*1 + 2*1; i <= df1; i += 2)
   {
    j = i*1 + b*1;
    d *= y * j / (i - 2.0);
    p -= z * d / j;
   }

if (p < 0.0) p = 0.0;
   else 
   if (p > 1.0) p = 1.0;
return (1-p)}}  

writeln="\n";
if (navigator.appVersion.lastIndexOf('Win') != -1) writeln="\r\n";

document.form1.outp.value+="R1 is the correlation between Y and X"+writeln
                          +"R2 is the correlation between Y and Z"+writeln
                          +"R3 is the correlation between X and Z"+writeln+writeln

if (r1!=0)
  {document.form1.outp.value+="correlation between Y and X"+writeln
      +" Simple (r(yx)): "+r1
   if ((r1!=0)&(r2!=0)&(r3!=0)) 
         document.form1.outp.value+="  Partial (r(yx.z)): "+truncate(partial(r1,r2,r3),4)+writeln
  }

if (r2!=0)
  {document.form1.outp.value+="correlation between Y and Z"+writeln
      +" Simple (r(yz)): "+r2
   if ((r1!=0)&(r2!=0)&(r3!=0)) 
         document.form1.outp.value+="  Partial (r(yz.x)): "+truncate(partial(r2,r1,r3),4)+writeln
  }

if (r3!=0)
  {document.form1.outp.value+="correlation between X and Z"+writeln
      +" Simple (r(xz)): "+r3
   if ((r1!=0)&(r2!=0)&(r3!=0)) 
         document.form1.outp.value+="  Partial (r(xz.y)): "+truncate(partial(r3,r2,r1),4)+writeln+writeln
  }

var R1sq=r1*r1
var PartZsq=partial(r2,r1,r3)
PartZsq=PartZsq*PartZsq
BigR=R1sq*1+(PartZsq*(1-R1sq))
document.form1.outp.value+="ANOVA table (variance explained in Y by x & z):"+writeln
                          +"------------------------------------------"+writeln
                          +"\tTotal\t= expl.\t+ added\t* not expl."+writeln
                          +"\tin Y \t= by X \t+  by Z\t*  by X   "+writeln
                          +"------------------------------------------"+writeln
                          +"R-sq:\t|"+truncate(BigR,4)+"\t="+truncate(R1sq,4)+"\t+"
                          +truncate(PartZsq,4)+"\t*"+truncate((1-R1sq),4)+writeln
if (n>5)
{
var FY=(BigR/(1-BigR))*((n-3)/2)
var FX= (R1sq/(1-R1sq))*(n-3)
var FZ= (PartZsq/(1-PartZsq))*(n-3)
document.form1.outp.value+="F:\t|"+truncate(FY,1)+"\t|"+truncate(FX,1)+"\t|"+truncate(FZ,1)+writeln
                         +"df:\t|"+((n-3)/2)+"\t|"+(n-3)+"\t|"+(n-3)+writeln
                         +"p-val:\t|"+truncate(fpr(FY,2,((n-3)/2)),4)+"\t|"+truncate(fpr(FX,1,(n-3)),4)+"\t|"
                          +truncate(fpr(FZ,1,(n-3)),4)+writeln

}
document.form1.outp.value+="--------------------------------------more"+writeln+writeln

var Yxz=Math.sqrt(BigR)
var ZYxz=zcor(Yxz)
if (n>5)
{
document.form1.outp.value+="\tConfidence Intervals for R (Y.xz)"+writeln
                          +"(Optimistic C.I. for correlation for Y on x&z)"+writeln
document.form1.outp.value+="C.I.\tlower \t\tRy.xz\t\tupper"+writeln
                          +"\t---------------------------------------"+writeln;
if (n<11) document.form1.outp.value+="Confidence intervals only if the"+writeln
                                    +"number of cases is larger than 10"+writeln;
else
for (var index=1;index<=4;index++)  //make the confidence intervals
{
document.form1.outp.value+=ConfInt[index].interval
var lower=contraz(ZYxz-ConfInt[index].intzval/Math.sqrt(n-5))
document.form1.outp.value+="\t"+truncate(lower,6)+"   < \t"+truncate(Yxz,4)
var higher=contraz(ZYxz*1+ConfInt[index].intzval/Math.sqrt(n-5))
document.form1.outp.value+="    < \t"+truncate(higher,6)+writeln
}
document.form1.outp.value+=writeln
}
}

function array(len)
{
  this.length=len;
   for (var i=1;i<=len;i++)
      this[i]=0;
  return this
}

function alphaval()
{
this.peas=1.283
this.intzval=0.20
}

function alphavalues(length)

{
this.length=length
   for (var j=1;j<=length;j++)
      this[j]=new alphaval();

this[1].peas=1.283; //start blok for double sided
this[1].tail=0.20;
this[2].peas=1.6449; 
this[2].tail=0.10;
this[3].peas=1.960;
this[3].tail=0.05;
this[4].peas=2.576;
this[4].tail=0.01;
this[5].peas=3.291;
this[5].tail=0.001;
this[6].peas=0.847; //start blok for single sided
this[6].tail=0.20;
this[7].peas=1.282; 
this[7].tail=0.10;
this[8].peas=1.6449;
this[8].tail=0.05;
this[9].peas=2.326;
this[9].tail=0.01;
this[10].peas=3.090;
this[10].tail=0.001;
}

//following is a statement to innitialize the alpha values
alphavalue=new alphavalues(10)  //because we allow for 10 different alpha values

function power(r,r2,n,indep)
{

function zbeta(r,n,alpha,indep)
{
ztr=(zcor(r))
zpr=ztr*1+(r/(2*(n-1)))*1
zb=(zpr*Math.sqrt((n-3)/indep))-alpha
return zb
}

function zbet(r1,r2,n,alpha,indep)
{
var zr1=zcor(r1)+(r1/(2*(n-1)))*1
var zr2=zcor(r2)+(r2/(2*(n-1)))*1
var zpr=Math.abs(zr1-zr2)
var zb=(zpr*Math.sqrt((n-3)/indep))-alpha
return zb
}

writeln="\n";
if (navigator.appVersion.lastIndexOf('Win') != -1) writeln="\r\n";

if ((r==0)&(r2!=0))
   {r=r2
    r2=0}

document.form1.outp.value+="power of r= "+r+" against "+r2+" with N= "+n+writeln
if (indep==1) document.form1.outp.value+="r2 is a population correlation"+writeln+writeln
   else document.form1.outp.value+="both correlations are from a sample"+writeln+writeln
document.form1.outp.value+="\tRESULTS for double sided"+writeln
document.form1.outp.value+="alpha\tz-power\tpower\t   %"+writeln
document.form1.outp.value+="--------------------------------------------------"+writeln
                        // +"zrho= "+zbeta(r,n,1.6449)+writeln;

// end of preperation start of calculations

for (var index=2;index<=5;index++)  //this for statement does clasical model
   {str=""+alphavalue[index].tail
    pt=str.indexOf(".");
    if (pt==0) str="0"+str    //solves that newer versions of Netscape give inconsistent numeric notations
    document.form1.outp.value+=str+"\t"
    if (r2==0)
      var zvalue=zbeta(r,n,alphavalue[index].peas,indep);
    if (r2!=0)
      zvalue=zbet(r,r2,n,alphavalue[index].peas,indep);
    var tvalue=tprob(zvalue,3000)
    document.form1.outp.value+=truncate(zvalue,3)+"\t"+truncate(tvalue,4)+"\t"+truncate(tvalue*100,2)
    document.form1.outp.value+=writeln; 
   }
document.form1.outp.value+=writeln;       //end of double sided output

document.form1.outp.value+="\tRESULTS for single sided"+writeln
document.form1.outp.value+="alpha\tz-power\tpower\t   %"+writeln
document.form1.outp.value+="--------------------------------------------------"+writeln;

for (var index=7;index<=10;index++)
   {str=""+alphavalue[index].tail
    pt=str.indexOf(".");
    if (pt==0) str="0"+str    //solves that newer versions of Netscape give inconsistent numeric notations
    document.form1.outp.value+=str+"\t"
    if (r2==0)
      var zvalue=zbeta(r,n,alphavalue[index].peas,indep);
    if (r2!=0)
      var zvalue=zbet(r,r2,n,alphavalue[index].peas,indep);
    var tvalue=tprob(zvalue,3000)
    document.form1.outp.value+=truncate(zvalue,3)+"\t"+truncate(tvalue,4)+"\t"+truncate(tvalue*100,2)
    document.form1.outp.value+=writeln; 
   }
document.form1.outp.value+=writeln;   //end of single sided output
}

function samplesize(r1,r2,indep) 
{
function initn(alpha,beta,q,mult)
{
var n=(((alpha*1+beta*1)/q)*((alpha*1+beta*1)/q)*mult)+3*1
return n
}

function nofcas(r1,r2,alpha,beta,indep)
{
var zr1=zcor(r1)
var zr2=zcor(r2)
var q=Math.abs(zr1-zr2)
var n=initn(alpha,beta,q,indep)
return n
}

function nofcases(r1,alpha,beta,indep)
{
var Nnew=0
var q=zcor(r1)
var Nold=initn(alpha,beta,q,indep)
for (var jndex=1;jndex<=30;jndex++)
{
q=zcor(r1)*1+(r1/(2*(Nold-1)))*1
Nnew=initn(alpha,beta,q,indep)
if (((Nold-Nnew)<2)||((Nold-Nnew)>-2)) jndex=31
Nold=Nnew
} 
return Nnew //Nnew
}

writeln="\n";
if (navigator.appVersion.lastIndexOf('Win') != -1) writeln="\r\n";

betaas=new array(5);

betaas[1]=0.252;  //power 60%
betaas[2]=0.522;  //power 70%
betaas[3]=0.847;  //power 80%
betaas[4]=1.282;  //power 90%
betaas[5]=1.645;  //power 95%

if ((r1==0)&(r2!=0))
   {r1=r2
    r2=0}

document.form1.outp.value+="SAMPLE SIZE CALCULATION"+writeln
                          +"for r1= "+r1+" against r2= "+r2+writeln
if (indep==1) document.form1.outp.value+="r2 is a population correlation"+writeln+writeln
   else document.form1.outp.value+="both correlations are from a sample"+writeln+writeln

document.form1.outp.value+="\tRESULTS for double sided"+writeln
document.form1.outp.value+="alpha\t\t  power"+writeln
document.form1.outp.value+="\t 0.6\t 0.7\t 0.8\t 0.9"+writeln;
document.form1.outp.value+="--------------------------------------------------"+writeln;

// end of preperation start of calculations

for (var index=2;index<=5;index++)  //statement does clasical model
   {str=""+alphavalue[index].tail
    pt=str.indexOf(".");
    if (pt==0) str="0"+str    //solves that newer versions of Netscape give inconsistent numeric notations
    document.form1.outp.value+=str
    for (var jndex=1;jndex<=4;jndex++)               
        {document.form1.outp.value+="\t"          
         if (r2==0) document.form1.outp.value+=
           addspace(Math.round(nofcases(r1,alphavalue[index].peas,betaas[jndex],indep)+0.5*1),4)
         if (r2!=0) document.form1.outp.value+=
           addspace(Math.round(nofcas(r1,r2,alphavalue[index].peas,betaas[jndex],indep)+0.5*1),4)}
    document.form1.outp.value+=writeln;
   }
document.form1.outp.value+=writeln;       //end of double sided output
document.form1.outp.value+="\tRESULTS for single sided"+writeln
document.form1.outp.value+="alpha\t\t  power"+writeln;
document.form1.outp.value+="\t 0.6\t 0.7\t 0.8\t 0.9"+writeln;
document.form1.outp.value+="--------------------------------------------------"+writeln;

for (var index=7;index<=10;index++)
   {str=""+alphavalue[index].tail
    pt=str.indexOf(".");
    if (pt==0) str="0"+str    //solves that newer versions of Netscape give inconsistent numeric notations
    document.form1.outp.value+=str
    for (var jndex=1;jndex<=4;jndex++)               
        {document.form1.outp.value+="\t"          
         if (r2==0) document.form1.outp.value+=
           addspace(Math.round(nofcases(r1,alphavalue[index].peas,betaas[jndex],indep)+0.5*1),4)
         if (r2!=0) document.form1.outp.value+=
           addspace(Math.round(nofcas(r1,r2,alphavalue[index].peas,betaas[jndex],indep)+0.5*1),4)}
    document.form1.outp.value+=writeln;
   }
document.form1.outp.value+=writeln;   //end of single sided output
 }

function currentCI()
{
this.interval="95%"
this.intzval=1.96
}

function CI(length)
{
this.length=length
   for (var j=1;j<=length;j++)
      this[j]=new currentCI();

this[1].interval="80%"
this[1].intzval=1.282
this[2].interval="90%"
this[2].intzval=1.645
this[3].interval="95%"
this[3].intzval=1.96
this[4].interval="99%"
this[4].intzval=2.575
}

//following is a statement to innitialize the confidence interval
ConfInt=new CI(4)  //because we allow for four different Confidence Intervals

function rprob(corr,n)
{
writeln="\n";
if (navigator.appVersion.lastIndexOf('Win') != -1) writeln="\r\n";

var zr=zcor(corr)
document.form1.outp.value+="\t Confidence Intervals for r= "+corr+writeln
document.form1.outp.value+="C.I. \t lower \t\t r \t\t upper"+writeln
                          +"\t---------------------------------------"+writeln;
if (n<11) document.form1.outp.value+="Confidence intervals only if the"+writeln
                                    +"number of cases is larger than 10"+writeln;
else
for (var index=1;index<=4;index++)  //make the confidence intervals
{
document.form1.outp.value+=ConfInt[index].interval
var lower=contraz(zr-ConfInt[index].intzval/Math.sqrt(n-3))
document.form1.outp.value+="\t"+truncate(lower,6)+"   < \t"+corr
var higher=contraz(zr*1+ConfInt[index].intzval/Math.sqrt(n-3))
document.form1.outp.value+="    < \t"+truncate(higher,6)+writeln
}
document.form1.outp.value+=writeln

var tval=(corr*Math.sqrt(n-2))/Math.sqrt(1-(corr*corr))
document.form1.outp.value+="statistical significance of r= "+corr+" against 0"+writeln+
                           "t= "+truncate(tval,5)+"; df= "+(n-2)+"; p= "+truncate(tprob(tval,n-2),6)+
                           "; 1-p= "+truncate(1-tprob(tval,n-2),6)+writeln+
                           "(multiply p-value with 2 for double sided testing)"+writeln+writeln
}

function cor(r1,r2,rn3,rn4) //MAIN
{
writeln="\n";
if (navigator.appVersion.lastIndexOf('Win') != -1) writeln="\r\n";

if ((r1>1)||(r1<-1)) document.form1.outp.value+=writeln+"WARNING, FIRST PARAMETER IS NOT A CORRELATION"+writeln;
if ((r2>1)||(r2<-1)) document.form1.outp.value+=writeln+"WARNING, SECOND PARAMETER IS NOT A CORRELATION"+writeln;

if ((r1!=0)&(r2!=0)&(Math.abs(rn3)<1)&(rn3!=0)&(Math.abs(rn4)==0))
       makepartials(r1,r2,rn3,0)

if ((r1!=0)&(r2!=0)&(Math.abs(rn3)<1)&(rn3!=0)&(Math.abs(rn4)>1))
    depdif(r1,r2,rn3,rn4)

if ((r1!=0)&(r2!=0)&(Math.abs(rn3)>1)&(Math.abs(rn4)>1))
    inddif(r1,r2,rn3,rn4)

if ((r1!=0)&(r2!=0)&(rn3!=0)&(rn3>1)&(rn4==0))
      popdif(r2,r1,rn3)

if ((r1!=0)&(r2==0)&(rn3>1)&(rn4==0))
      rprob(r1,rn3)

if ((r1!=0)&(rn3==0)&(rn4>1))
      power(r1,r2,rn4,1)

if ((r1!=0)&(rn3==0)&(rn4==0))
      samplesize(r1,r2,1)
}

function alternat(r1,r2,rn3,rn4) //MAIN
{

if ((r1!=0)&(r2!=0)&(Math.abs(rn3)<1)&(rn3!=0)&(Math.abs(rn4)==0))
       makepartials(r1,r2,rn3,0)

if ((r1!=0)&(r2!=0)&(Math.abs(rn3)<1)&(rn3!=0)&(Math.abs(rn4)>1))
       makepartials(r1,r2,rn3,rn4)

if ((r1!=0)&(r2!=0)&(Math.abs(rn3)>1)&(Math.abs(rn4)>1))
       inddif(r1,r2,rn3,rn4)

if ((r1!=0)&(rn3>1)&(rn4==0))
      power(r1,r2,rn3,1)

if ((r1!=0)&(rn3==0)&(rn4==0))
      samplesize(r1,r2,2)

if ((r1!=0)&(rn3==0)&(rn4>1))
      power(r1,r2,rn4,2)
}

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

