

function compute(){
var outwin=document.theform.textout.value;

var k=0,p=0.5, cl=95;
k=parseInt(document.theform.successes.value);
p=parseFloat(document.theform.sucprob.value);
cl=parseFloat(document.theform.conf.value);


if (isNaN(k))
        {
        alert("The number of successes is not a number.");
        return;
        }
if (k<0)
        {
        alert("The number of successes must be at least zero.");
        return;
        }

if (isNaN(p))
        {
        alert("The success probability is not a number.");
        return;
        }
if ((p<=0)||(p>=1))
        {
        alert("The success probability must be between "+
                "zero and one, exclusive.");
        return;
        }

if (isNaN(cl))
        {
        alert("The confidence level is not a number.");
        return;
        }
if ((cl<=0)||(cl>=100))
        {
        alert("The confidence level must be between "+
                "zero and 100, exclusive.");
        return;
        }


var out="";
out +="Here "+k+" successes were observed, "+
        "each with success probability "+p+".\n";
if((Math.floor(k/p)==k/p)&&(k>0))
{
out +="The MLEs of the number of trials are "+
        (k/p-1)+" and "+(k/p)+".\n";
}
else
{
out +="The MLE of the number of trials is "+
        Math.floor(k/p)+".\n";
}
out +="The lower endpoint of a one sided "+
        cl+"% confidence interval for the ";
out +="number of trials is "+bnlower(k,p,(100-cl)/100)+".\n";
out +="The upper endpoint of a one sided "+
        cl+"% confidence interval for the ";
out +="number of trials is "+bnupper(k,p,(100-cl)/100)+".\n";
out +="The endpoints of a two sided "+
        cl+"% confidence interval for the ";
out +="number of trials are "+bnlower(k,p,(100-cl)/200)+" and ";
out +=""+bnupper(k,p,(100-cl)/200)+".\n\n";

document.theform.textout.value=out+outwin;
}



function loggamma(x) {
var a1=Math.log(2*Math.PI)/2;
var a2= 1/1680;
var a3= 1/1260;
var a4= 1/360;
var a5= 1/12;
var f,xx,z;
f=1;
if (x <=0) return(Number.NaN);
for(xx=x;xx < 7;xx++) f=f*xx;
f=-Math.log(f);
z=1/(xx*xx);
return(f+(xx-0.5)*Math.log(xx)-xx+a1+(((-a2*z+a3)*z-a4)*z+a5)/xx);
}


function dfbeta(x, p, q)
{

var one=1.0;
var zero=0.0;
var acu=0.0000001;
if ( (p<=zero) || (q<=zero) ) return(Number.NaN);
if (x<=zero) return(0.0);
if (x>=one) return(1.0);


var ai, beta, betain, psq, cx, ns, pp, qq, rx, temp, term, xx ;
var index;
betain=x;
psq=p+q;
cx=one-x;
if (p>= psq*x)
        {
        xx=x;
        pp=p;
        qq=q;
        index=0;
        }
else
        {
        xx=cx;
        cx=x;
        pp=q;
        qq=p;
        index=1;
        }
term=one;
ai=one;
betain=one;
ns=qq+cx*psq;


rx=xx/cx;
temp=qq-ai;
if (Math.floor(ns)==0)  rx=xx;
for(;;)
        {
        term=term*temp*rx/(pp+ai);
        betain=betain+term;
        temp=Math.abs(term);
        if  ( (temp <=acu) && (temp<=acu*betain) ) break;
        ai=ai+one;
        ns=ns-1;
        if (Math.floor(ns) >=0)
                {
                temp=qq-ai;
                if (Math.floor(ns) ==0) rx=xx;
                }
        else
                {
                temp=psq;
                psq=psq+one;
                }
        }

beta=loggamma(p)+loggamma(q)-loggamma(p+q);
betain=betain*Math.exp( pp*Math.log(xx) +(qq-one)*Math.log(cx) -beta)/pp;
if (index==1) betain=one-betain;
return(betain);
}


function bintail(n, p, k){
if (k>n) return 0.0;
if (k<=0) return 1.0;
return dfbeta(p, k, (n-k+1));
}


function bnlower(k, p, alpha)
{
var high, low, test;
var testprob;
high=k;
testprob=bintail(high, p, k);

if(testprob>alpha) return high;

while( testprob <=alpha)
        {
        high=2*high;
        testprob=bintail(high, p, k);
        }
low=Math.floor(high/2);


while(high-low>1)
        {
        test=Math.floor((high+low)/2);
        testprob=bintail(test, p, k);
        if(testprob>alpha) high=test;
        if(testprob<=alpha) low=test;
        }

return high;
}


function bnupper(k, p, alpha)
{
var high, low, test;
var testprob;
high=k;
testprob=1.0-bintail(high, p, k+1);
while( testprob >alpha)
        {
        if (high==0) high=1;
        else
        high=2*high;
        testprob=1.0-bintail(high, p, k+1);
        }
low=Math.floor(high/2);
while(high-low>1)
        {
        test=Math.floor((high+low)/2);
        testprob=1.0-bintail(test, p, k+1);
        if(testprob>alpha) low=test;
        if(testprob<=alpha) high=test;
        }
return low;
}


