

function compute(){
var outwin=document.theform.textout.value;

var r=0, n=0, N=0, cl=95;
r=parseInt(document.theform.successes.value);
n=parseInt(document.theform.samsize.value);
N=parseInt(document.theform.popsize.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 successes is not a number.");
        return;
        }
if ((r<0)||(r>n))
        {
        alert("The number of successes must be between zero "+
                "and the sample size, inclusive.");
        return;
        }
if (isNaN(N))
        {
        alert("The population size is not a number.");
        return;
        }
if (N<n)
        {
        alert("The population size must be "+
                "at least the sample size.");
        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 of size "+
        N+" without replacement.\n";
if((Math.floor(r*(N+1)/n)==r*(N+1)/n)&&(r>0))
{
out +="The MLEs of the number of successes are "+
        (r*(N+1)/n-1)+" and "+(r*(N+1)/n)+".\n";
}
else
{
out +="The MLE of the number of successes is "+
        Math.floor(r*(N+1)/n)+".\n";
}
out +="The lower endpoint of a one sided "+
        cl+"% confidence interval for the ";
out +="number of successes is "+
        hgrlower(N, n, r, (100-cl)/100)+".\n";
out +="The upper endpoint of a one sided "+
        cl+"% confidence interval for the ";
out +="number of successes is "+
        hgrupper(N, n, r, (100-cl)/100)+".\n";
out +="The endpoints of a two sided "+
        cl+"% confidence interval for the ";
out +="number of successes are "+
        hgrlower(N, n, r, (100-cl)/200)+" and ";
out +=""+hgrupper(N, 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 hgrlower(N, n, r, alpha)
{
var high, low, test;
var testprob;
high=N;
low=r;
testprob=hypertail(N,low,n,r);
if (testprob>alpha) return low;
while(high-low>1)
        {
        test=Math.floor((high+low)/2);
        testprob=hypertail(N,test,n,r);
        if(testprob<=alpha) low=test;
        if(testprob>alpha) high=test;
        }
return high;
}


function hgrupper(N, n, r, alpha)
{
var high, low, test;
var testprob;
high=N;
low=r;
testprob=1.0-hypertail(N,high,n,r+1);
if (testprob>alpha) return high;
while(high-low>1)
        {
        test=Math.floor((high+low)/2);
        testprob=1.0-hypertail(N,test,n,r+1);
        if(testprob>alpha) low=test;
        if(testprob<=alpha) high=test;
        }
return low;
}


