var ran = 0;
var ran0 =0;
var t = 2;
var x = 0;
var trials = 0;
var hits = 0;
var dcback = new Image();
var dc1 = new Image();
var dc1a = new Image();
var dc2 = new Image();
var dc2a = new Image();
var dc3 = new Image();
var dc3a = new Image();
var dc4 = new Image();
var dc4a = new Image();
var dc5 = new Image();
var dc5a = new Image();
var dc6 = new Image();
var dc6a = new Image();
var sp = 0;
var stopped=0;
var complet = 0;
var totn = 60;
var mx = 10;
var started = 0;
var timeout=0;
var adds="";
var sig="";
var x1a = 0.31938153;
var x2 = -0.356563782;
var x3 = 1.781477937;
var x4 = -1.821255978;
var x5 = 1.330274429;
var z=0;
var p=0;
var p1=0;
var q1=0;
var a=0;
var b=0;
var c=0;
var m=0;
var v=0;
var disab=0;

var topb=21; // LATEST DATA
var tops=22; // LATEST DATA
var topg=23; // LATEST DATA

dcback.src = "Images/Dice/diceback.png";
dc1.src = "Images/Dice/dice1.png";
dc1a.src = "Images/Dice/dice1a.png";
dc2.src = "Images/Dice/dice2.png";
dc2a.src = "Images/Dice/dice2a.png";
dc3.src = "Images/Dice/dice3.png";
dc3a.src = "Images/Dice/dice3a.png";
dc4.src = "Images/Dice/dice4.png";
dc4a.src = "Images/Dice/dice4a.png";
dc5.src = "Images/Dice/dice5.png";
dc5a.src = "Images/Dice/dice5a.png";
dc6.src = "Images/Dice/dice6.png";
dc6a.src = "Images/Dice/dice6a.png";

function checkchamp(){
if (hits<1){return;}
if (totn!=60){return;}
if (hits<topb){return;}
document.forms.matrix.Usern.disabled=false;
document.forms.matrix.Usern.value="Enter a Username (Optional)";
document.forms.matrix.B2.value="Submit";
document.forms.matrix.H1.value=hits;
document.forms.matrix.H2.value=z;
document.forms.matrix.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.");
}

function submt(){
if(document.forms.matrix.B2.value!="Submit"){return;}
document.forms.matrix.submit();
}

function user(){
if(document.forms.matrix.Usern.value=="Enter a Username (Optional)"){document.forms.matrix.Usern.value="";}
}

function critrat(){
m = mx;
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+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);
}

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<10){sig=sig+" (Misses)"}
}

function prntdata(){
document.forms.matrix.mess1.value="Statistical Analysis: z="+String(z.toFixed(5))+"    p="+String(p.toFixed(6))+"    Evidence for PK:  "+sig;
}


function begin2() {
if(disab==1){return;}
if (started==1){resetall();return;}
begin();
started=1;
document.forms.matrix.messages.value="Click dice. Try to roll SIXES. Make 60 rolls";
document.forms.matrix.reset.value="Reset";
window.document.diceshow.src=dc6a.src;
}

function hitvalue() {
document.forms.matrix.hit.value = hits;
}

function trialvalue() {
document.forms.matrix.trial.value = trials;
if ((totn-trials)<10){adds=" "}else{adds="";}
document.forms.matrix.messages.value="Click dice. Try to roll SIXES. Make "+adds+String(totn-trials)+ " rolls";
sp=0;
}

function trialno() {
trials++;
trialvalue();
}

function begin() {
disab=0;
z=0;
p=0;
sp=0;
p1= 1/6;
q1 = 5/6;
sig="";
trials = 0;
stopped=0;
hits = 0;
hitvalue();
trialvalue();
adds="";
complet=0;
document.forms.matrix.mess1.value="";
document.forms.matrix.Usern.value="";
document.forms.matrix.Usern.disabled=true;
document.forms.matrix.B2.value="";
document.forms.matrix.messages.value = "Welcome to PK Championship";
}

function resetall() {
begin();
window.document.diceshow.src = dcback.src;
document.forms.matrix.reset.value="Start";
document.forms.matrix.hit.value="";
document.forms.matrix.trial.value="";
started = 0;
}

function roll(){
if(stopped==1){return;}
if (complet==1){return;}
if(document.forms.matrix.reset.value=="Start"){return;}
disab=1;
trialno();
sp=0;
stopped=0;
spin();
}

function spin(){
if(sp>9){ran6();stopped=0;checkhit();showdiceend();
if (trials >(totn-1)){showend();}
return;
}
stopped=1;
timeout=setTimeout("showdice();",150);
}

function showdice(){
ran6();ran0=ran;
if (ran==1){window.document.diceshow.src=dc1.src;}
if (ran==2){window.document.diceshow.src=dc2.src;}
if (ran==3){window.document.diceshow.src=dc3.src;}
if (ran==4){window.document.diceshow.src=dc4.src;}
if (ran==5){window.document.diceshow.src=dc5.src;}
if (ran==6){window.document.diceshow.src=dc6.src;}
clearTimeout(timeout);
sp++;
spin();
}

function showdiceend(){
disab=0;
if (ran==1){window.document.diceshow.src=dc1a.src;}
if (ran==2){window.document.diceshow.src=dc2a.src;}
if (ran==3){window.document.diceshow.src=dc3a.src;}
if (ran==4){window.document.diceshow.src=dc4a.src;}
if (ran==5){window.document.diceshow.src=dc5a.src;}
if (ran==6){window.document.diceshow.src=dc6a.src;}
}

function showend(){
disab=0;
if (ran==1){window.document.diceshow.src=dc1a.src;}
if (ran==2){window.document.diceshow.src=dc2a.src;}
if (ran==3){window.document.diceshow.src=dc3a.src;}
if (ran==4){window.document.diceshow.src=dc4a.src;}
if (ran==5){window.document.diceshow.src=dc5a.src;}
if (ran==6){window.document.diceshow.src=dc6a.src;}
completed();
}

function ran6() {

while(ran==ran0){
ran = (Math.floor(Math.random()*6)+1);
}
}

function checkhit(){
trialvalue();
if(ran==6){
hits++;
hitvalue();}
}

function completed() {
disab=0;
document.forms.matrix.messages.value="You scored "+String(hits)+" hits   -   Score of 17 or more suggests PK";
complet=1;
critrat();
signif();
prntdata();
clearTimeout(timeout);
timeout=setTimeout("checkchamp();",1500);
document.forms.matrix.reset.value = "Reset";
}

