var a = 0;
var ah1 = 0;
var ah2 = 0;
var b = 0;
var back = new Image();
var backx = new Image();
var startgif = new Image();
var blank = 0;
var c = 0;
var circle = new Image();
var cirs = 0;
var clicks = 0;
var complet = 0;
var cross = new Image();
var crss = 0;
var dtype = 1;
var ends = 0;
var hits = 0;
var i = 0;
var m = 0;
var matches = new Array(0,0,0,0,0,0);
var mx = 5;
var o = 0;
var p = 0;
var p1 = 0;
var q1 = 0;
var r = 0;
var ran = 0;
var resp1 = 0;
var responses = new Array(0,0,0,0,0,0);
var square = new Image();
var sqrs = 0;
var star = new Image();
var started = 0;
var strs = 0;
var telcv = 1;
var targets = new Array(0,0,0,0,0,0);
var tm1 = 0;
var tog = "";
var totn = 25;
var trials = 0;
var feed = 1;
var v = 0;
var waves = new Image();
var wavs = 0;
var x = 0;
var x1a = 0.31938153;
var x2 = -0.356563782;
var x3 = 1.781477937;
var x4 = -1.821255978;
var x5 = 1.330274429;
var z = 0;
var sig="";
var outp = new Array (1002);
var outp2 = new Array (1002);
var clos = new Array(6);

back.src = "Images/ESPTest/cardback.gif";
backx.src = "Images/ESPTest/cardbackx.gif";
circle.src = "Images/ESPTest/circle.gif";
cross.src = "Images/ESPTest/cross.gif";
waves.src = "Images/ESPTest/waves.gif";
square.src = "Images/ESPTest/square.gif";
star.src = "Images/ESPTest/star.gif";
startgif.src = "Images/ESPTest/five.gif";

for (x=0; x<=6; x++) {
targets[x] = 0;
responses[x] = 0;
matches[x] = 0;
}

function control(){
if (document.forms.matrix.Button1.value==""){return;}
if (document.forms.matrix.Button1.value=="Reset"){ends=1;resetall();return;}
if(document.forms.matrix.radtype[0].checked==true){telcv=1;}else{telcv=2;}
if(document.forms.matrix.raddeck[0].checked==true){dtype=1;}else{dtype=2;}
if(document.forms.matrix.radfeed[0].checked==true){feed=1;}else{feed=2;}
if(document.forms.matrix.radtrials[0].checked==true){totn=25;}
if(document.forms.matrix.radtrials[1].checked==true){totn=50;}
if(document.forms.matrix.radtrials[2].checked==true){totn=100;}
if(document.forms.matrix.radtrials[3].checked==true){totn=200;}
if(document.forms.matrix.radtrials[4].checked==true){totn=500;}
if(document.forms.matrix.radtrials[5].checked==true){totn=1000;}
mx=totn/5;
if ((dtype==2)&&(feed==1)&&(telcv==1)){
document.forms.matrix.Messages.style.color="white";
document.forms.matrix.Messages.style.backgroundColor="red";document.forms.matrix.Messages.value="Procedure not allowed";return;}
if ((telcv==2)&&(feed==2)){
document.forms.matrix.Messages.style.color="white";
document.forms.matrix.Messages.style.backgroundColor="red";document.forms.matrix.Messages.value="Procedure not allowed";return;}
if (document.forms.matrix.Button1.value=="Cancel"){ends=1;resetall();return;}
ends=0;
if (telcv==1) {
document.forms.matrix.Messages.style.color="black";
document.forms.matrix.Messages.style.backgroundColor="#CCCCCC";document.forms.matrix.Messages.value = "Click symbols to guess cards";}
if (telcv==2) {
document.forms.matrix.Messages.style.color="black";
document.forms.matrix.Messages.style.backgroundColor="#CCCCCC";document.forms.matrix.Messages.value = "Sender views cards and records guesses";}
document.forms.matrix.Button1.value="Reset";
resetmats();
ranfull();
routetest();
}

function resetmats(){
for(x=1;x<=totn;x++){
outp2[x]=-1;
}
for(x=1;x<=5;x++){
clos[x]=0;
}
}

function routetest(){
document.forms.matrix.Button1.value="Cancel";
begin();
window.document.Cardshow.src=back.src;
hitvalue();
trialvalue();
selecttarget();
}

function resetall(){
document.forms.matrix.Messages.style.color="black";
document.forms.matrix.Messages.style.backgroundColor="#CCCCCC";
document.forms.matrix.Messages.value="Welcome to Advanced ESP Test";
window.document.Cardshow.src=startgif.src;
document.forms.matrix.Button1.value="Start";
document.forms.matrix.hit.value="";
document.forms.matrix.trial.value="";
document.forms.matrix.Circlehits.value="";
document.forms.matrix.Crosshits.value="";
document.forms.matrix.Waveshits.value="";
document.forms.matrix.Squarehits.value="";
document.forms.matrix.Starhits.value="";
document.forms.matrix.Totalhits.value="";
document.forms.matrix.Circlecards.value="";
document.forms.matrix.Crosscards.value="";
document.forms.matrix.Wavescards.value="";
document.forms.matrix.Squarecards.value="";
document.forms.matrix.Starcards.value="";
document.forms.matrix.Totalcards.value="";
document.forms.matrix.Evidence2.value="";
document.forms.matrix.zscore.value="";
document.forms.matrix.prob.value="";
begin();
}

function begin() {
tog = "";
tm1 = 0;
cirs = 0;
crss = 0;
wavs = 0;
sqrs = 0;
strs = 0;
trials = 0;
hits = 0;
clicks = 0;
for (x=0; x<=6; x++) {
targets[x] = 0;
responses[x] = 0;
matches[x] = 0;
}
resetmats();
}

function hitvalue() {
if (feed==1){document.forms.matrix.hit.value = hits;}else{document.forms.matrix.hit.value="?";}
}

function trialvalue() {
document.forms.matrix.trial.value = trials;
}

function ranfull(){
if(dtype==1){
for (x=1; x<=totn; x++) {
ran = (Math.floor(Math.random()*5)+1);
outp[x] = ran;
}
}
if(dtype==2){
for (x=1; x<=totn; x++) {
ran = (Math.floor(Math.random()*5)+1);
if (clos[ran]>=mx){x--;}else{outp[x]=ran;clos[ran]++;}
}
resort();
}
}

function resort(){
for(x=1;x<=totn;x++){
outp2[x]=-1;
}
for (x=1;x<=totn;x++){
ran = (Math.floor(Math.random()*totn)+1);
if(outp2[ran]==-1){outp2[ran]=outp[x];}else{x--;}
}
for (x=1;x<=totn;x++){
outp[x]=outp2[x];
}
}

function ran5(){
ran=outp[trials];
blank=0;
}

function analysis() {
document.forms.matrix.Circlecards.value = targets[1];
document.forms.matrix.Crosscards.value = targets[2];
document.forms.matrix.Wavescards.value = targets[3];
document.forms.matrix.Squarecards.value = targets[4];
document.forms.matrix.Starcards.value = targets[5];
document.forms.matrix.Circlehits.value = matches[1];
document.forms.matrix.Crosshits.value = matches[2];
document.forms.matrix.Waveshits.value = matches[3];
document.forms.matrix.Squarehits.value = matches[4];
document.forms.matrix.Starhits.value = matches[5];
document.forms.matrix.Totalhits.value = hits;
document.forms.matrix.Totalcards.value = totn;
critrat();
signif();
document.forms.matrix.zscore.value=z.toFixed(5);
document.forms.matrix.prob.value=p.toFixed(5);
document.forms.matrix.Evidence2.value=sig;
document.forms.matrix.hit.value=hits;
ends = 1;
}

function completed () {
analysis();
document.forms.matrix.Messages.style.color="black";
document.forms.matrix.Messages.style.backgroundColor="#CCCCCC";
document.forms.matrix.Messages.value="You scored "+String(hits)+"     Chance score = "+String(mx);
document.forms.matrix.Button1.value="Reset";
window.document.Cardshow.src=startgif.src;
}

function printtarget(callingField) {
if (ends == 1) {return;}
if (tog == "ready") {
tog = "";
clicks++;
responses[callingField]++;
resp1=eval(callingField);
if (eval(callingField) == ran) {
hits++;
matches[callingField]++;
hitvalue();
}
timeout = setTimeout('selecttarget()', 1000);
if (telcv==2) {window.document.Cardshow.src = back.src;return;}
if (feed==2) {
window.document.Cardshow.src = backx.src;
return;
}
displaycard();
}
}

function displaycard() {
if (ran == 1) {
window.document.Cardshow.src = circle.src;
}
if (ran == 2) {
window.document.Cardshow.src = cross.src;
}
if (ran == 3) {
window.document.Cardshow.src = waves.src;
}
if (ran == 4) {
window.document.Cardshow.src = square.src;
}
if (ran == 5) {
window.document.Cardshow.src = star.src;
}
}

function selecttarget() {
if (trials > (totn-1)) {
completed();
return;
}
if (tog == "") {
window.document.Cardshow.src = back.src;
trials++;
trialvalue();
blank = 1;
tm1=ran;
while(blank==1){ran5();}
targets[ran]++;
tog = "ready";
if (telcv==2) {displaycard();}
}
}

function critrat(){
p1= 0.2;
q1 = 0.8;
m = mx;
with (Math) {
v = sqrt(totn*p1*q1);
if (dtype == 2) {v = v*sqrt(totn/(totn-1))}
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";}
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!";}
}