var timeout0=0;
var x=0;
var trials=0;
var rn=0;
var tr=0;
var c1=0; //guess stare to stare - Correct1
var x2=0; // guess stare to blank - Incorrect 2
var c2=0; // guess blank to blank - Correct 2
var x1=0; // guess blank to stare - Incorrect 1
var hold=0;

var pc=0;
var px=0;

var a=0;
var b=0;
var c=0;
var z=0;
var p=0;
var v=0;

var p1=0.5;
var q1=0.5; 

var hits=0;
var m=0;
var totn=0;
var sig="";

var x1a = 0.31938153;
var x2a = -0.356563782;
var x3 = 1.781477937;
var x4 = -1.821255978;
var x5 = 1.330274429;
var sig="";


var eyes =new Image();
var back = new Image();
var nextt = new Image();
var start = new Image();

back.src = "Images/Staring/eyesblank.png";
eyes.src = "Images/Staring/eyes.png";
nextt.src = "Images/Staring/staretrial.png";
start.src = "Images/Staring/eyesstart.png";

function resetall(){
if(document.forms.prec.Reset.value=="Reset"){begin2();return;}
if (document.forms.prec.Reset.value=="Start"){begin();findtrials();
document.forms.prec.Stare.value="Staring";
document.forms.prec.Nostare.value="Blank";
window.document.scren.src=nextt.src;
document.forms.prec.Messages.value="Guess whether the screen will show Staring or Blank";
document.forms.prec.trialn.value=trials;
rnd();
}
}

function begin2(){
clearTimeout(timeout0);
tr=0;
c1=0;c2=0;x1=0;x2=0;
hold=0;
window.document.scren.src=start.src;
document.forms.prec.Stare.value="";
document.forms.prec.Nostare.value="";
document.forms.prec.trialn.value="";
document.forms.prec.Reset.value="Start";
document.forms.prec.Messages.value="   Test your precognition by sensing whether you will be stared at";
resetres();
}

function findtrials(){
if(document.forms.prec.radtr[0].checked==true){trials=10;}
if(document.forms.prec.radtr[1].checked==true){trials=50;}
if(document.forms.prec.radtr[2].checked==true){trials=100;}
if(document.forms.prec.radtr[3].checked==true){trials=200;}
if(document.forms.prec.radtr[4].checked==true){trials=400;}

}

function begin(){
clearTimeout(timeout0);
tr=0;
c1=0;c2=0;x1=0;x2=0;
hold=0;
window.document.scren.src=start.src;
document.forms.prec.Stare.value="";
document.forms.prec.Nostare.value="";
document.forms.prec.Reset.value="Reset";
resetres();
}

function stare(){
if ((document.forms.prec.Stare.value=="")||(hold==1)){return;}
if (rn==1){c1++;corr();}else{x2++;incorr();}
showscreen();
}

function nostare(){
if ((document.forms.prec.Nostare.value=="")||(hold==1)){return;}
if (rn==2){c2++;corr();}else{x1++;incorr();}
showscreen();
}

function corr(){
document.forms.prec.Messages.value="   CORRECT";
}

function incorr(){
document.forms.prec.Messages.value="   WRONG";
}

function showscreen(){
tr++;
if (rn==1){window.document.scren.src=eyes.src;}else{window.document.scren.src=back.src;}
document.forms.prec.Stare.value="";
document.forms.prec.Nostare.value="";
hold=1;
timeout0=setTimeout('nexttrial()',2000);
}

function nexttrial(){
if (tr>=trials){ends();return;}
hold=0;
document.forms.prec.trialn.value=trials-c1-c2-x1-x2;
clearTimeout(timeout0);
window.document.scren.src=nextt.src;
document.forms.prec.Messages.value="   Guess whether the screen will show Staring or Blank";
document.forms.prec.Stare.value="Staring";
document.forms.prec.Nostare.value="Blank";
rnd();
}

function resetres(){
document.forms.prec.Finres.value="";
document.forms.prec.Corgs.value="";
document.forms.prec.Incgs.value="";
document.forms.prec.Statan.value="";
document.forms.prec.Nlab1.value="";
document.forms.prec.Nlab2.value="";
document.forms.prec.PClab1.value="";
document.forms.prec.PClab2.value="";
document.forms.prec.Zlab.value="";
document.forms.prec.Plab.value="";
document.forms.prec.Evlab1.value="";
document.forms.prec.Evlab2.value="";

document.forms.prec.nc.value="";
document.forms.prec.nx.value="";
document.forms.prec.pcc.value="";
document.forms.prec.pcx.value="";
document.forms.prec.zt.value="";
document.forms.prec.pt.value="";
document.forms.prec.evt.value="";
}

function ends(){
pc=((c1+c2)*100)/trials;
px=((x1+x2)*100)/trials;
document.forms.prec.trialn.value="";
document.forms.prec.Messages.value="Results now shown";
window.document.scren.src=start.src;
document.forms.prec.nc.value=c1+c2;
document.forms.prec.nx.value=x1+x2;
document.forms.prec.pcc.value=pc.toFixed(2);
document.forms.prec.pcx.value=px.toFixed(2);

document.forms.prec.Finres.value="Final Results";
document.forms.prec.Corgs.value="Correct Guesses";
document.forms.prec.Incgs.value="Incorrect Guesses";
document.forms.prec.Statan.value="Statistical Analysis (two-tailed)";
document.forms.prec.Nlab1.value="N";
document.forms.prec.Nlab2.value="N";
document.forms.prec.PClab1.value="%";
document.forms.prec.PClab2.value="%";
document.forms.prec.Zlab.value="z";
document.forms.prec.Plab.value="p <";
document.forms.prec.Evlab1.value="Evidence for";
document.forms.prec.Evlab2.value="Precognition";

calcstt();
if(totn>=25){
document.forms.prec.zt.value=z.toFixed(5);
document.forms.prec.pt.value=p.toFixed(10);
document.forms.prec.evt.value=sig;}
else{
document.forms.prec.zt.value="n / a";
document.forms.prec.pt.value="n / a";
document.forms.prec.evt.value="None";
}
}

function calcstt(){
hits=c1+c2;
m=trials/2;
totn=trials;
critrat();
signif();
}

function rnd() {
rn=(Math.floor(Math.random()*2)+1);
}

function critrat(){
with (Math) {
v = sqrt(totn*p1*q1);
if (hits >= m) {
z = (hits-m-0.5)/v;
} else {
z = ((hits-m+0.5)/v);
}
}
ndist();
}

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(x1a*b+x2a*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);
}

function signif(){
if (p>0.10){sig="None";}
if (p<=0.10){sig="Marginal";}
if (p<=0.05){sig="Fair";}
if (p<=0.01){sig="Good";}
if (p<=0.005){sig="Very Good";}
if (p<=0.001){sig="Excellent";}
if (p<=0.0001){sig="Amazing!";}
}