// DEFINE VARIABLES
var z = 0;
var x1 = 0.31938153;
var x2 = -0.356563782;
var x3 = 1.781477937;
var x4 = -1.821255978;
var x5 = 1.330274429;
var a = 0;
var b = 0;
var c = 0;
var p = 0;
var o = 0;
var t = 0;
var h = 0;
var i = 0;
var p1 = 0;
var q1 = 0;
var v = 0;
var m = 0;
var r4 = "";
var r5 = "";

var tst=0;

function begin() {
document.forms.matrix.trials.value = "";
document.forms.matrix.hits.value = "";
document.forms.matrix.rep1.value = "";
document.forms.matrix.rep2.value = "";
document.forms.matrix.rep3.value = "";
document.forms.matrix.rep4.value = "";
document.forms.matrix.rep5.value = "";
document.forms.matrix.rep6.value = "";
document.forms.matrix.Messages.value="";
document.forms.matrix.Messages1.value="";
}

function check() {
document.forms.matrix.Messages.value="";
document.forms.matrix.Messages1.value="";
if (!parseInt(document.forms.matrix.trials.value, 10)) {
document.forms.matrix.Messages.value="Enter no. of trials";
return;
}
if (document.forms.matrix.trials.value <1) {
document.forms.matrix.Messages.value="Enter no. of trials";
document.forms.matrix.Messages1.value="Positive number only";
return;
}

if((document.forms.matrix.hits.value!=0)&&(!parseInt(document.forms.matrix.hits.value, 10)) ){
document.forms.matrix.Messages.value="Enter no. of hits";
return;
}

if (document.forms.matrix.hits.value < 0) {
document.forms.matrix.Messages.value="Enter no. of hits";
document.forms.matrix.Messages1.value="Positive number only";
return;
}

t = Math.ceil(parseInt(document.forms.matrix.trials.value, 10));
h = Math.ceil(parseInt(document.forms.matrix.hits.value, 10));

if(document.forms.matrix.radtype[0].checked==true){tst=1;}else{tst=2;}

if (h > t) {
document.forms.matrix.Messages.value="Hits exceed trials";
return;
}

if(document.forms.matrix.radprob[0].checked==true){i=2;}
if(document.forms.matrix.radprob[1].checked==true){i=3;}
if(document.forms.matrix.radprob[2].checked==true){i=4;}
if(document.forms.matrix.radprob[3].checked==true){i=5;}
if(document.forms.matrix.radprob[4].checked==true){i=6;}
if(document.forms.matrix.radprob[5].checked==true){i=8;}
if(document.forms.matrix.radprob[6].checked==true){i=10;}
if(document.forms.matrix.radprob[7].checked==true){i=12;}

if (t/i < 10) {
document.forms.matrix.Messages.value="Insufficient trials";
document.forms.matrix.Messages1.value="At least " + String(i*10) + " needed";
return;
}
document.forms.matrix.Messages.value="";
document.forms.matrix.Messages1.value="";
critrat();
}

function critrat(){
p1= 1/i;
q1 = 1-p1;
m = t/i;
with (Math) {
v = sqrt(t*p1*q1);
if (tst == 2) {v = v*sqrt(t/(t-1))}
if (h >= m) {
z = (h-m-0.5)/v;
} else {
z = ((h-m+0.5)/v);
}
}
ndist();
report();
}

function ndist() {
a=Math.abs(z)
b=1/(1+a*0.2316419)
c=(1/Math.sqrt((2*Math.PI)))*Math.exp(-Math.pow(a,2)/2)
p=1-c*(parseFloat(x1*b+x2*Math.pow(b,2))+parseFloat(x3*Math.pow(b,3))+parseFloat(x4*Math.pow(b,4))+parseFloat(x5*Math.pow(b,5)))
p=2*(1-p);
o=Math.floor(1/p);
}

function signif() {
if (p>0.05) {
r4 = "Not significant";
r5="None";
}
if (p<=0.05) {
r4 = "0.05";
r5="Fair";
}
if (p<=0.01) {
r4 = "0.01";
r5="Good";
}
if (p<=0.005) {
r4 = "0.005";
r5="Good";
}
if (p<=0.001) {
r4 = "0.001";
r5="Very good";
}
if (p<=0.0005) {
r4 = "0.0005";
r5="Very good";
}
if (p<=0.0001) {
r4 = "0.0001";
r5="Excellent";
}
if (p<=0.00001) {
r4 = "0.00001";
r5="Excellent";
}
if (p<=0.000001) {
r4 = "0.000001";
r5="Amazing !";
}
if (p<=0.0000001) {
r4 = "0.0000001";
r5="Amazing !";
}
if (p<=0.00000001) {
r4 = "0.00000001";
r5="Amazing !";
}
if (p<=0.000000001) {
r4 = "0.000000001";
r5="Amazing !";
}
if (p<=0.0000000001) {
r4 = "0.0000000001";
r5="Amazing !";
}
if (p<=0.00000000001) {
r4 = "0.00000000001";
r5="Amazing !";
}
if (p<=0.000000000001) {
r4 = "0.000000000001";
r5="Amazing !";
}
if (p<=0.0000000000001) {
r4 = "0.0000000000001";
r5="Amazing !";
}
if (p<=0.00000000000001) {
r4 = "0.00000000000001";
r5="Amazing !";
}
if (p<=0.000000000000001) {
r4 = "0.000000000000001";
r5="Amazing !";
}
if (p<=0.0000000000000001) {
r4 = "0.0000000000000001";
r5="Amazing !";
}
if (p<=0.00000000000000001) {
r4 = "0.00000000000000001";
r5="Amazing !";
}
if (p<=0.000000000000000001) {
r4 = "0.000000000000000001";
r5="Amazing !";
}
if (p<=0.0000000000000000001) {
r4 = "0.0000000000000000001";
r5="Amazing !";
}
if (p<=0.00000000000000000001) {
r4 = "0.00000000000000000001";
r5="Amazing !";
}
if (p<=0.000000000000000000001) {
r4 = "0.000000000000000000001";
r5="Amazing !";
}
}

function report() {

if(document.forms.matrix.tail[1].checked==true){p=p/2;}
signif();
document.forms.matrix.rep1.value = String(m);
document.forms.matrix.rep2.value = String(z.toFixed(5));
document.forms.matrix.rep3.value = String(p);
document.forms.matrix.rep4.value = r4;
if(h>=m){document.forms.matrix.rep5.value = r5;}else{document.forms.matrix.rep5.value = "n/a";}
if(h<m){document.forms.matrix.rep6.value = r5;}else{document.forms.matrix.rep6.value = "n/a";}
}
