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 disab=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 eyes =new Image();
var back = new Image();
var nextt = new Image();
var start = new Image();

var topb=36; // LATEST DATA
var tops=37; // LATEST DATA
var topg=39; // LATEST DATA

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(disab==1){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.Messages0.value="Guess whether the screen will show Staring or Blank";
document.forms.prec.Messages.value="                 Guesses Remaining:  "+String(trials-c1-c2-x1-x2);
document.forms.prec.hit.value=String(hits);
document.forms.prec.trial.value="1";
rnd();
}
}

function begin2(){
clearTimeout(timeout0);
tr=0;
c1=0;c2=0;x1=0;x2=0;
hits=0;
hold=0;
disab=0;
document.forms.prec.B2.disabled=true;
document.forms.prec.Usern.disabled=true;
document.forms.prec.B2.value="";
document.forms.prec.Usern.value="";
window.document.scren.src=start.src;
document.forms.prec.Stare.value="";
document.forms.prec.Nostare.value="";
document.forms.prec.Reset.value="Start";
document.forms.prec.Messages.value="                Welcome to Precognition Championship";
document.forms.prec.Messages0.value="";
document.forms.prec.trial.value="";
document.forms.prec.hit.value="";
document.forms.prec.Restr.value="";
}

function findtrials(){
trials=50;
}

function begin(){
clearTimeout(timeout0);
tr=0;
c1=0;c2=0;x1=0;x2=0;
hits=0;
hold=0;
disab=0;
window.document.scren.src=start.src;
document.forms.prec.Stare.value="";
document.forms.prec.Nostare.value="";
document.forms.prec.Reset.value="Reset";
document.forms.prec.B2.disabled=true;
document.forms.prec.Usern.disabled=true;
document.forms.prec.B2.value="";
document.forms.prec.Usern.value="";
document.forms.prec.trial.value="";
document.forms.prec.hit.value="";
}

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.Restr.value="CORRECT";
}

function incorr(){
document.forms.prec.Restr.value="WRONG";
}

function showscreen(){
document.forms.prec.hit.value=String(c1+c2);
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;
disab=1;
timeout0=setTimeout('nexttrial()',2000);
}

function nexttrial(){
if (tr>=trials){ends();return;}
document.forms.prec.trial.value=String(tr+1);
hold=0;
disab=0;
clearTimeout(timeout0);
window.document.scren.src=nextt.src;
document.forms.prec.Messages0.value="Guess whether the screen will show Staring or Blank";
document.forms.prec.Messages.value="               Guesses Remaining:  "+String(trials-c1-c2-x1-x2);
document.forms.prec.Stare.value="Staring";
document.forms.prec.Nostare.value="Blank";
document.forms.prec.Restr.value="";
rnd();
}

function ends(){
disab=0;
pc=((c1+c2)*100)/trials;
px=((x1+x2)*100)/trials;
window.document.scren.src=start.src;
calcstt();
document.forms.prec.Restr.value="";
document.forms.prec.Messages.value="You scored "+String(hits)+" hits - Score of 33 or more suggests precognition";
document.forms.prec.Messages0.value="Statistical Analysis: z="+String(z.toFixed(5))+"    p="+String(p.toFixed(6))+"    Evidence for precognition:  "+sig;
timeout=setTimeout("checkchamp();",1500);
}

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";return;}
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!";}
if (hits<25){sig=sig+" (Misses)"}
}

function submt(){
if(document.forms.prec.B2.value!="Submit"){return;}
document.forms.prec.submit();
}

function user(){
if(document.forms.prec.Usern.value=="Enter a Username (Optional)"){document.forms.prec.Usern.value="";}
}

function checkchamp(){
if (hits<1){return;}
if (totn!=50){return;}
if (hits<topb){return;}
document.forms.prec.Usern.disabled=false;
document.forms.prec.B2.disabled=false;
document.forms.prec.Usern.value="Enter a Username (Optional)";
document.forms.prec.B2.value="Submit";
document.forms.prec.H1.value=hits;
document.forms.prec.H2.value=z;
document.forms.prec.H3.value=p;
if (hits>topg){topnew();return;}
if (hits>=topg){topgold();return;}
if (hits>=tops){topsilv();return;}
if (hits>=topb){topbron();return;}
}

function topnew(){
alert("MANY CONGRATULATIONS!\n\nYou are our NEW BEST EVER SCORER!\n\nPlease enter a Username and\nclick Submit to send us your data\n\n(We do not collect any other information about you)\n\n\nOr you can click Reset to keep your score private.\n\n\nBy submitting your data, you agree to allow us\nto publish your Username and score on our website.");
}

function topgold(){
alert("CONGRATULATIONS!\n\nYou are one of our GOLD MEDAL SCORERS!\n\nPlease enter a Username and\nclick Submit to send us your data\n\n(We do not collect any other information about you)\n\n\nOr you can click Reset to keep your score private.\n\n\nBy submitting your data, you agree to allow us\nto publish your Username and score on our website.");
}

function topsilv(){
alert("CONGRATULATIONS!\n\nYou are one of our SILVER MEDAL SCORERS!\n\nPlease enter a Username and\nclick Submit to send us your data\n\n(We do not collect any other information about you)\n\n\nOr you can click Reset to keep your score private.\n\n\nBy submitting your data, you agree to allow us\nto publish your Username and score on our website.");
}


function topbron(){
alert("CONGRATULATIONS!\n\nYou are one of our BRONZE MEDAL SCORERS!\n\nPlease enter a Username and\nclick Submit to send us your data\n\n(We do not collect any other information about you)\n\n\nOr you can click Reset to keep your score private.\n\n\nBy submitting your data, you agree to allow us\nto publish your Username and score on our website.");
}

