

function compute(){
var outwin=document.theform.textout.value;

var r=0, n=0, R=0, cl=95;
r=parseInt(document.theform.successes.value);
n=parseInt(document.theform.samsize.value);
R=parseInt(document.theform.popsuc.value);
cl=parseFloat(document.theform.conf.value);


if (isNaN(n))
        {
        alert("The sample size is not a number.");
        return;
        }
if (n<=0)
        {
        alert("The sample size must be at least 1.");
        return;
        }
if (isNaN(r))
        {
        alert("The number of observed successes is not a number.");
        return;
        }
if ((r<0)||(r>n))
        {
        alert("The number of observed successes must be between zero "+
                "and the sample size, inclusive.");
        return;
        }
if (isNaN(R))
        {
        alert("The number of population successes is not a number.");
        return;
        }
if (R<r)
        {
        alert("The number of population successes "+
                "must be at least the number of "+
                "successes observed.");
        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 "+r+" successes were observed in a "+
        "sample of size "+n+
        " drawn from a population with "+
        R+" successes, without replacement.\n";
if (r==0)
{
out+="The MLE does not exist.\n";
}
else if ((Math.floor(R*n/r)==R*n/r)&&(r>0))
{
out +="The MLEs of the population size are "+
        (R*n/r-1)+" and "+(R*n/r)+".\n";
}
else
{
out +="The MLE of the population size is "+
        Math.floor(R*n/r)+".\n";
}
out +="The lower endpoint of a one sided "+
        cl+"% confidence interval for the ";
out +="population size is "+
        hgnlower(R, n, r, (100-cl)/100)+".\n";
out +="The upper endpoint of a one sided "+
        cl+"% confidence interval for the ";
out +="population size is "+
        hgnupper(R, n, r, (100-cl)/100)+".\n";
out +="The endpoints of a two sided "+
        cl+"% confidence interval for the ";
out +="population size are "+
        hgnlower(R, n, r, (100-cl)/200)+" and ";
out +=""+hgnupper(R, n, r, (100-cl)/200)+".\n\n";

document.theform.textout.value=out+outwin;
}



function hyperprob(N, R, n, m) {
var frac=1.0;
var i=0;
for(i=0;i<=m-1;i++)
{
frac=frac*(R-i)*(n-i)/( (N-i)*(m-i) );
}
for(i=m;i<=n-1;i++)
{
frac=frac*(n-i)*(N-R-i+m)/ ( (N-i)*(n-m-i+m) );
}
return frac;
}


function hypertail(N, R, n, m)
{
if (m>Math.min(R,n)) return 0.0;
if ( m<Math.max(0,n-(N-R)) ) return 1.0;
var prob=0.0;
var i=0;
var end=Math.min(R,n);
for(i=m;i<=end;i++)
{
prob=prob+hyperprob(N,R,n,i);
}
return prob;
}


function hgnlower(R, n,  r, alpha)
{
var high, low, test;
var testprob;
high=R+n-r;
testprob=1.0-hypertail(high,R,n,r+1);
if (testprob>alpha) return high; 
while(testprob<=alpha)
        {
        high=2*high;
        testprob=1.0-hypertail(high,R,n,r+1);
        }
low=Math.floor(high/2);

while(high-low>1)
        {
        test=Math.floor((high+low)/2);
        testprob=1.0-hypertail(test,R,n,r+1);
        if(testprob<=alpha) low=test;
        if(testprob>alpha) high=test;
        }
return high;
}


function hgnupper(R,  n,  r,  alpha)
{
if (r==0) return Number.POSITIVE_INFINITY;
var  high, low, test;
var testprob;
high=R+n-r;
testprob=1.0;
while(testprob>alpha)
        {
        high=2*high;
        testprob=hypertail(high,R,n,r);
        }
low=Math.floor(high/2);

while(high-low>1)
        {
        test=Math.floor((high+low)/2);
        testprob=hypertail(test,R,n,r);
        if(testprob>alpha) low=test;
        if(testprob<=alpha) high=test;
        }
return low;
}


