//Survey Response Rates calculation online. According to CASRO, council of 
//american survey research organisations
//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 currentCI()
{
this.interval="95"
this.intzval=1.96
this.upper=0.975
this.lower=0.025
this.chi2=3.841 //0.00393
}

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[1].upper=0.9
this[1].lower=0.1
this[1].chi2=1.642  //0.0642
this[2].interval="90"
this[2].intzval=1.645
this[2].upper=0.95
this[2].lower=0.05
this[1].chi2=2.706     //0.0158
this[3].interval="95"
this[3].intzval=1.96
this[3].upper=0.975
this[3].lower=0.025
this[1].chi2=3.841  //0.00393
this[4].interval="99"
this[4].intzval=2.575
this[4].upper=0.995
this[4].lower=0.005
this[1].chi2=10.827  //0.000157
}

function changeCI(prop)
{
currentoption=prop.selectedIndex;
currentoption+=1*1;
CIcurrent=allCI[currentoption];
}

//following are statements to innitialize the confidence interval
currentoption=3  //3 is for 95 CI
CIcurrent=new currentCI()
allCI=new CI(4)  //because we allow for four different Confidence Intervals

function Wilson(p,n,z,ul) //continuity corrected version!!!
{
var outcome=2*n*p+z*z+ul+ul*z*Math.sqrt(z*z+ul*2-1/n+4*p*(n*(1-p)-1*ul))
outcome/=(2*(n*1+z*z))
return outcome
}

function simplevar(p1,N)
{
var variance=p1*(1-p1)/N
return variance
}

function casro(PC,C,compl,R,NC,O,eli,inel,UN,UC,unkn,S,e)
{
writeln="\n";
if (navigator.appVersion.lastIndexOf('Win') != -1) writeln="\r\n";

//document.form1.outp.value+="PC "+PC +writeln
//document.form1.outp.value+="C "+C +writeln
//document.form1.outp.value+="compl "+compl +writeln
//document.form1.outp.value+="R "+R +writeln
//document.form1.outp.value+="NC "+NC +writeln
//document.form1.outp.value+="O "+O +writeln
//document.form1.outp.value+="eli "+eli +writeln
//document.form1.outp.value+="inel"+inel +writeln
//document.form1.outp.value+="UN "+UN +writeln
//document.form1.outp.value+="UC "+UC +writeln
//document.form1.outp.value+="unkn "+unkn +writeln
//document.form1.outp.value+="S "+S +writeln
//document.form1.outp.value+="e "+e +writeln

if ((C==0)&&(compl>0)) 
   {C=compl-PC
    document.form1.C.value=C}
if ((PC==0)&&(compl>0)) 
   {PC=compl-C
    document.form1.PC.value=PC}
if ((PC>0)&&!((compl*1)==(C*1+PC*1))) 
   {document.form1.outp.value+="WARNING. Total Completed (A) not equal to 1+2. Total completed changed."+writeln+writeln
    compl=C*1+PC*1
    document.form1.compl.value=compl}
if ((R==0)&&(eli>0)&&(O*1+NC*1+R*1!=eli*1)) 
   {R=eli-O-NC
    document.form1.R.value=R}
if ((eli>0)&&!((eli*1)==(O*1+NC*1+R*1))) 
   {document.form1.outp.value+="WARNING. Total refused (B) not equal to 3+4+5. Total refused changed."+writeln+writeln
    eli=O*1+NC*1+R*1
    document.form1.eli.value=eli}
if ((UN==0)&&(unkn>0)) 
   {UN=unkn-UC
    document.form1.UN.value=UN}
if ((UC==0)&&(unkn>0)) 
   {UC=unkn-UN
    document.form1.UC.value=UC}
if ((unkn>0)&&!((unkn*1)==(UN*1+UC*1))) 
   {document.form1.outp.value+="WARNING. Total Unknown (C) not equal to 6a+6b. Total unknown changed."+writeln+writeln
    unkn=UN*1+UC*1
    document.form1.unkn.value=unkn}
if ((compl*1)==(0*1)) 
   {compl=PC*1+C*1
    document.form1.compl.value=compl}
if ((eli*1)==(0*1)) 
   {eli=R*1+NC*1+O*1
    document.form1.eli.value=eli}
if ((unkn*1)==(0*1)) 
   {unkn=UN*1+UC*1
    document.form1.unkn.value=unkn}
var total=eli*1+compl*1;
e2=total/(total*1+inel*1)
if ((e*1)==(0*1)) 
   {e=e2
    document.form1.e.value=e*1}
if ((S*1)==(0*1)) 
   {S=compl*1+eli*1+inel*1+unkn*1
    document.form1.S.value=S}
if (!((S*1)==(compl*1+eli*1+inel*1+unkn*1))) 
   {document.form1.outp.value+="WARNING. Sample size (N) not equal to total of totals (A+B+C+D). N changed"+writeln+writeln
    S=compl*1+eli*1+inel*1+unkn*1
    document.form1.S.value=S}

document.form1.outp.value+="  ***  Sample Confidence Intervals  ***"+writeln

if (C>1)
   {var completed=C/S
    var variance=simplevar(completed,S)
    var lower=Wilson(completed,S,CIcurrent.intzval,-1)
    var upper=Wilson(completed,S,CIcurrent.intzval,+1)
    document.form1.outp.value+="Completed [1/N]: "+truncate(completed,4)+writeln
                         +CIcurrent.interval+"% CI: "+truncate((completed-CIcurrent.intzval*Math.sqrt(variance))*1,3)+"<1<"
                         +truncate((completed+CIcurrent.intzval*Math.sqrt(variance))*1,3)+"; Wilson: "+truncate(lower*1,3)+"<1<"+truncate(upper*1,3)+writeln}
if (compl>1)
   {var completed=compl/S
    var variance=simplevar(completed,S)
    var lower=Wilson(completed,S,CIcurrent.intzval,-1)
    var upper=Wilson(completed,S,CIcurrent.intzval,+1)
    document.form1.outp.value+="Compl+Part [A/N]: "+truncate(completed,4)+writeln
                         +CIcurrent.interval+"% CI: "+truncate((completed-CIcurrent.intzval*Math.sqrt(variance))*1,3)+"<A<"
                         +truncate((completed+CIcurrent.intzval*Math.sqrt(variance))*1,3)+"; Wilson: "+truncate(lower*1,3)+"<A<"+truncate(upper*1,3)+writeln}
if (eli>1)
   {var completed=eli/S
    var variance=simplevar(completed,S)
    var lower=Wilson(completed,S,CIcurrent.intzval,-1)
    var upper=Wilson(completed,S,CIcurrent.intzval,+1)
    document.form1.outp.value+="Refused [B/N]: "+truncate(completed,4)+writeln
                         +CIcurrent.interval+"% CI: "+truncate((completed-CIcurrent.intzval*Math.sqrt(variance))*1,3)+"<B<"
                         +truncate((completed+CIcurrent.intzval*Math.sqrt(variance))*1,3)+"; Wilson: "+truncate(lower*1,3)+"<B<"+truncate(upper*1,3)+writeln}
if (unkn>1)
   {var completed=unkn/S
    var variance=simplevar(completed,S)
    var lower=Wilson(completed,S,CIcurrent.intzval,-1)
    var upper=Wilson(completed,S,CIcurrent.intzval,+1)
    document.form1.outp.value+="Unknown [C/N]: "+truncate(completed,4)+writeln
                         +CIcurrent.interval+"% CI: "+truncate((completed-CIcurrent.intzval*Math.sqrt(variance))*1,3)+"<C<"
                         +truncate((completed+CIcurrent.intzval*Math.sqrt(variance))*1,3)+"; Wilson: "+truncate(lower*1,3)+"<C<"+truncate(upper*1,3)+writeln}
if (inel>1)
   {var completed=inel/S
    var variance=simplevar(completed,S)
    var lower=Wilson(completed,S,CIcurrent.intzval,-1)
    var upper=Wilson(completed,S,CIcurrent.intzval,+1)
    document.form1.outp.value+="Ineligible [D/N]: "+truncate(completed,4)+writeln
                         +CIcurrent.interval+"% CI: "+truncate((completed-CIcurrent.intzval*Math.sqrt(variance))*1,3)+"<D<"
                         +truncate((completed+CIcurrent.intzval*Math.sqrt(variance))*1,3)+"; Wilson: "+truncate(lower*1,3)+"<D<"+truncate(upper*1,3)+writeln}
if ((S-inel)>1)
   {var completed=(S-inel)/S
    var variance=simplevar(completed,S)
    var lower=Wilson(completed,S,CIcurrent.intzval,-1)
    var upper=Wilson(completed,S,CIcurrent.intzval,+1)
    document.form1.outp.value+="ISER eligibility rate: "+truncate(completed,4)+writeln
                         +CIcurrent.interval+"% CI: "+truncate((completed-CIcurrent.intzval*Math.sqrt(variance))*1,3)+"<ER<"
                         +truncate((completed+CIcurrent.intzval*Math.sqrt(variance))*1,3)+"; Wilson: "+truncate(lower*1,3)+"<ER<"+truncate(upper*1,3)+writeln}

document.form1.outp.value+=writeln+"  ***  CASRO Response Rates  ***"+writeln
document.form1.outp.value+="simple [unknowns eligible]: "+truncate(compl/(total*1+unkn*1),4)+writeln;
document.form1.outp.value+="simple [unknowns not eligible]: "+truncate(compl/total,4)+writeln;
document.form1.outp.value+="CASRO [unknowns devided]: "+truncate(compl/(total*1+e2*unkn),4)+writeln;
document.form1.outp.value+="e for CASRO [proportion eligible]: "+truncate(e2,4)+writeln+writeln;

document.form1.outp.value+="  ***  AAPO Response Rates  ***"+writeln
if (C>1) {document.form1.outp.value+="RR1 [1/(A+B+C)]: "+truncate(C/(compl*1+eli*1+unkn*1),4)+writeln}
document.form1.outp.value+="RR2 [A/A+B+C]: "+truncate(compl/(compl*1+eli*1+unkn*1),4)+writeln;
if (C>1) document.form1.outp.value+="RR3 [1/(A+B+e*C)]: "+truncate(C/(compl*1+eli*1+unkn*e),4)+writeln;
document.form1.outp.value+="RR4 [A/(A+B+e*C)]: "+truncate(compl/(compl*1+eli*1+unkn*e),4)+writeln;
if (C>1) document.form1.outp.value+="RR5 [1/(A+B)]: "+truncate(C/(compl*1+eli*1),4)+writeln;
document.form1.outp.value+="RR6 [A/(A+B)]: "+truncate(compl/(compl*1+eli*1),4)+writeln+writeln;

document.form1.outp.value+="  ***  AAPO Cooperation Rates  ***"+writeln
if (C>1) {document.form1.outp.value+="CR1 [1/(A+4+5)]: "+truncate(C/(compl*1+R*1+O*1),4)+writeln;}
document.form1.outp.value+="CR2 [A/(A+4+5)]: "+truncate(compl/(compl*1+R*1+O*1),4)+writeln;
if (C>1) {document.form1.outp.value+="CR3 [1/(A+4)]: "+truncate(C/(compl*1+R*1),4)+writeln;}
document.form1.outp.value+="CR4 [A/(A+4)]: "+truncate(compl/(compl*1+R*1),4)+writeln+writeln;

if (R>1) 
{document.form1.outp.value+="  ***  AAPO Refusal Rates  ***"+writeln
 document.form1.outp.value+="RefR1 [4/(A+B+C)]: "+truncate(R/(compl*1+eli*1+unkn*1),4)+writeln;
 document.form1.outp.value+="RefR2 [4/(A+B+e*C)]: "+truncate(R/(compl*1+eli*1+unkn*e),4)+writeln;
 document.form1.outp.value+="RefR3 [4/(A+B)]: "+truncate(R/(compl*1+eli*1),4)+writeln+writeln;}

document.form1.outp.value+="  ***  AAPO Contact Rates  ***"+writeln
document.form1.outp.value+="ConR1 [(A+4+5)/(A+B+C)]: "+truncate((compl*1+R*1+O*1)/(compl*1+eli*1+unkn*1),4)+writeln;
document.form1.outp.value+="ConR2 [(A+4+5)/(A+B+e*C)]: "+truncate((compl*1+R*1+O*1)/(compl*1+eli*1+unkn*e),4)+writeln;
document.form1.outp.value+="ConR3 [(A+4+5)/(A+B)]: "+truncate((compl*1+R*1+O*1)/(compl*1+eli*1),4)+writeln+writeln;

document.form1.outp.value+="  ***  ISER Rates  ***"+writeln
document.form1.outp.value+="Response-o [A/(A+B+e*C)]: "+truncate(compl/(compl*1+eli*1+unkn*e),4)+writeln;
document.form1.outp.value+="Response-f [1/(A+B+e*C)]: "+truncate(C/(compl*1+eli*1+unkn*e),4)+writeln;
document.form1.outp.value+="Co-operation [A/(A+4+5+e*6a)]: "+truncate(compl/(compl*1+R*1+O*1+UN*e),4)+writeln;
document.form1.outp.value+="Contact [(A+4+5+e*6a)/(A+B+e*C)]: "+truncate((compl*1+R*1+O*1+UN*e)/(compl*1+eli*1+unkn*e),4)+writeln;
if (R>1) document.form1.outp.value+="Refusal [4/(A+B+e*C)]: "+truncate(R/(compl*1+eli*1+unkn*e),4)+writeln;
}

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

document.form1.outp.value ="cleared"+writeln;
}

