var pimages = new Array(6);
var timeout = new Array(22);
var sett=0;
var phot=0;
var phid="";
var p=0;
var q=0;
var x=0;
var z=0;
var tm=1;
var tm1=0;
var ms=60000;
var started=0;
var seltest=1;
var PhA=0;
var PhB=0;
var PhC=0;
var PhD=0;
var rkA="";
var rkB="";
var rkC="";
var rkD="";
var locked=0;
var paused=0;
var r=0;
var t=0;

for (x = 0; x < 8; x++) {pimages[x] = new Image;}

//resetall();

function radtest(r){
loadphotos();
resetall();
}

function radtime(t){
loadphotos();
resetall();
}

function control(){
if (paused==1){return;}
if(document.forms.ESPT.Button.value==""){return;}
if(document.forms.ESPT.Button.value=="Continue"){ranking();return;}
if (document.forms.ESPT.Button.value=="Start"){starttest();return;}
if (document.forms.ESPT.Button.value=="Confirm"){confranks();return;}
if (document.forms.ESPT.Button.value=="Reset"){resetall();return;}
if (document.forms.ESPT.Button.value=="Stop transmitting"){endtrans();return;}
setranks();
if(document.forms.ESPT.radtype[0].checked==true){seltest=1;}else{seltest=2;}
if(document.forms.ESPT.radsess[0].checked==true){tm=1;}
if(document.forms.ESPT.radsess[1].checked==true){tm=5;}
if(document.forms.ESPT.radsess[2].checked==true){tm=10;}
if(document.forms.ESPT.radsess[3].checked==true){tm=15;}
if(document.forms.ESPT.radsess[4].checked==true){tm=20;}
if (seltest==1){document.forms.ESPT.Messages.value="When you click 'Start', a hidden photo will be transmitted.";document.forms.ESPT.Messages0.value="Use the time to gain psychic impressions about this photo.";}
if (seltest==2){document.forms.ESPT.Messages.value="When you click 'Start' the sender should transmit";document.forms.ESPT.Messages0.value="the displayed photo telepathically to the receiver.";}
document.forms.ESPT.Button.value="Start";
document.forms.ESPT.Photoshow.src="Images/ESPTrainer/photgrey.gif";
pausing();
}

function starttest(){
chooseset();
started=1;
if(seltest==1){clairvoyance();}
if(seltest==2){telepathy();}
pausing();
}

function chooseset(){
sett = Math.floor(Math.random()*100);
phot = (Math.floor(Math.random()*4)+1);
loadphotos();
phid = pimages[phot].src;
}

function loadphotos(){
pimages[0].src = "Images/ESPTrainer/photgrey.gif";
pimages[5].src = "Images/ESPTrainer/phottrans.gif";
pimages[6].src = "Images/ESPTrainer/photrank.gif";
pimages[7].src = "Images/ESPTrainer/phottitle.gif";
if (sett > 9) {
for (x = 1; x < 5; x++) {
pimages[x].src = "Images/ESPTrainer/ph"+String(sett)+String(x)+".jpg";
}
} else {
for (x = 1; x < 5; x++) {
pimages[x].src = "Images/ESPTrainer/ph"+"0"+String(sett)+String(x)+".jpg";
}
}
}

function clairvoyance(){
document.forms.ESPT.Photoshow.src=pimages[5].src;
document.forms.ESPT.Messages0.value="Try to form impressions about the photo being transmitted.";
document.forms.ESPT.Button.value="Stop transmitting";
controlsession();
}

function telepathy(){
document.forms.ESPT.Messages0.value="Transmit this photo telepathically to the receiver.";
document.forms.ESPT.Photoshow.src=phid;
document.forms.ESPT.Button.value="Stop transmitting";
controlsession();
}

function controlsession(){
tm1 = tm*ms;
clearTimeout(timeout[0]);
timeout[0] = setTimeout("endtrans();",tm1);
countdown();
}

function endtrans(){
clrtime();
if(seltest==1){document.forms.ESPT.Messages.value="End of transmission.";document.forms.ESPT.Messages0.value="Click 'Continue' to view the four possible target photos.";}
if(seltest==2){document.forms.ESPT.Messages.value="End of transmission.";document.forms.ESPT.Messages0.value="Click 'Continue' to allow the receiver to view the four possible target photos.";}
document.forms.ESPT.Photoshow.src=pimages[0].src;
document.forms.ESPT.Button.value="Continue";
pausing();
}

function clrtime() {
for (z = 0; z < 22; z++) {clearTimeout(timeout[z]);}
}

function countdown() {
document.forms.ESPT.Messages.value = 'Time left:    ' + String(tm) + ' min';
timeout[1] = setTimeout("document.forms.ESPT.Messages.value = 'Time left:    ' + String(tm-1) + ' min';", ms);
timeout[2] = setTimeout("document.forms.ESPT.Messages.value = 'Time left:    ' + String(tm-2) + ' min';", 2*ms);
timeout[3] = setTimeout("document.forms.ESPT.Messages.value = 'Time left:    ' + String(tm-3) + ' min';", 3*ms);
timeout[4] = setTimeout("document.forms.ESPT.Messages.value = 'Time left:    ' + String(tm-4) + ' min';", 4*ms);
timeout[5] = setTimeout("document.forms.ESPT.Messages.value = 'Time left:    ' + String(tm-5) + ' min';", 5*ms);
timeout[6] = setTimeout("document.forms.ESPT.Messages.value = 'Time left:    ' + String(tm-6) + ' min';", 6*ms);
timeout[7] = setTimeout("document.forms.ESPT.Messages.value = 'Time left:    ' + String(tm-7) + ' min';", 7*ms);
timeout[8] = setTimeout("document.forms.ESPT.Messages.value = 'Time left:    ' + String(tm-8) + ' min';", 8*ms);
timeout[9] = setTimeout("document.forms.ESPT.Messages.value = 'Time left:    ' + String(tm-9) + ' min';", 9*ms);
timeout[10] = setTimeout("document.forms.ESPT.Messages.value = 'Time left:    ' + String(tm-10) + ' min';", 10*ms);
timeout[11] = setTimeout("document.forms.ESPT.Messages.value = 'Time left:    ' + String(tm-11) + ' min';", 11*ms);
timeout[12] = setTimeout("document.forms.ESPT.Messages.value = 'Time left:    ' + String(tm-12) + ' min';", 12*ms);
timeout[13] = setTimeout("document.forms.ESPT.Messages.value = 'Time left:    ' + String(tm-13) + ' min';", 13*ms);
timeout[14] = setTimeout("document.forms.ESPT.Messages.value = 'Time left:    ' + String(tm-14) + ' min';", 14*ms);
timeout[15] = setTimeout("document.forms.ESPT.Messages.value = 'Time left:    ' + String(tm-15) + ' min';", 15*ms);
timeout[16] = setTimeout("document.forms.ESPT.Messages.value = 'Time left:    ' + String(tm-16) + ' min';", 16*ms);
timeout[17] = setTimeout("document.forms.ESPT.Messages.value = 'Time left:    ' + String(tm-17) + ' min';", 17*ms);
timeout[18] = setTimeout("document.forms.ESPT.Messages.value = 'Time left:    ' + String(tm-18) + ' min';", 18*ms);
timeout[19] = setTimeout("document.forms.ESPT.Messages.value = 'Time left:    ' + String(tm-19) + ' min';", 19*ms);
}

function ranking(){
document.forms.ESPT.Photoshow.src=pimages[0].src;
mixphotos();
document.forms.ESPT.PhotoA.src=pimages[PhA].src;
document.forms.ESPT.PhotoB.src=pimages[PhB].src;
document.forms.ESPT.PhotoC.src=pimages[PhC].src;
document.forms.ESPT.PhotoD.src=pimages[PhD].src;

document.forms.ESPT.Messages.value="Rank the four photos in terms of which you think may be the target photo.";document.forms.ESPT.Messages0.value="Then click 'Confirm'";
document.forms.ESPT.Button.value="Confirm";
document.forms.ESPT.Photoshow.src=pimages[6].src;

document.forms.ESPT.RankA.options[0].text="Rank this photo";
document.forms.ESPT.RankA.options[1].text="First choice";
document.forms.ESPT.RankA.options[2].text="Second choice";
document.forms.ESPT.RankA.options[3].text="Third choice";
document.forms.ESPT.RankA.options[4].text="Fourth choice";

document.forms.ESPT.RankB.options[0].text="Rank this photo";
document.forms.ESPT.RankB.options[1].text="First choice";
document.forms.ESPT.RankB.options[2].text="Second choice";
document.forms.ESPT.RankB.options[3].text="Third choice";
document.forms.ESPT.RankB.options[4].text="Fourth choice";

document.forms.ESPT.RankC.options[0].text="Rank this photo";
document.forms.ESPT.RankC.options[1].text="First choice";
document.forms.ESPT.RankC.options[2].text="Second choice";
document.forms.ESPT.RankC.options[3].text="Third choice";
document.forms.ESPT.RankC.options[4].text="Fourth choice";

document.forms.ESPT.RankD.options[0].text="Rank this photo";
document.forms.ESPT.RankD.options[1].text="First choice";
document.forms.ESPT.RankD.options[2].text="Second choice";
document.forms.ESPT.RankD.options[3].text="Third choice";
document.forms.ESPT.RankD.options[4].text="Fourth choice";

pausing();
}

function mixphotos(){
PhA=(Math.floor(Math.random()*4)+1);
PhB=(Math.floor(Math.random()*4)+1);
while(PhB==PhA){PhB=(Math.floor(Math.random()*4)+1);}
PhC=(Math.floor(Math.random()*4)+1);
while((PhC==PhA)||(PhC==PhB)){PhC=(Math.floor(Math.random()*4)+1);}
PhD=(Math.floor(Math.random()*4)+1);
while((PhD==PhA)||(PhD==PhB)||(PhD==PhC)){PhD=(Math.floor(Math.random()*4)+1);}
}

function enlarge(p){
if(document.forms.ESPT.PhotoA.src==pimages[0].src){return;}
if (locked==1){return;}
if (p==1){document.forms.ESPT.Photoshow.src=document.forms.ESPT.PhotoA.src;}
if (p==2){document.forms.ESPT.Photoshow.src=document.forms.ESPT.PhotoB.src;}
if (p==3){document.forms.ESPT.Photoshow.src=document.forms.ESPT.PhotoC.src;}
if (p==4){document.forms.ESPT.Photoshow.src=document.forms.ESPT.PhotoD.src;}
}

function confranks(){
checkranks();
pausing();
}

function checkranks(){
rkA=document.forms.ESPT.RankA.value;
rkB=document.forms.ESPT.RankB.value;
rkC=document.forms.ESPT.RankC.value;
rkD=document.forms.ESPT.RankD.value;
if ((rkA=="0")||(rkB=="0")||(rkC=="0")||(rkD=="0")){document.forms.ESPT.Messages.value="PLEASE USE ALL FOUR RANKS";return;}  
if ((rkA==rkB)||(rkA==rkC)||(rkA==rkD)||(rkB==rkC)||(rkB==rkD)||(rkC==rkD)){document.forms.ESPT.Messages.value="PLEASE USE ALL FOUR RANKS";return;}
locked=1;
results();
}

function results(){
document.forms.ESPT.Photoshow.src=phid;
if ((rkA==1)&&(document.forms.ESPT.PhotoA.src==phid)){firstchoice();}
if ((rkB==1)&&(document.forms.ESPT.PhotoB.src==phid)){firstchoice();}
if ((rkC==1)&&(document.forms.ESPT.PhotoC.src==phid)){firstchoice();}
if ((rkD==1)&&(document.forms.ESPT.PhotoD.src==phid)){firstchoice();}

if ((rkA==2)&&(document.forms.ESPT.PhotoA.src==phid)){secondchoice();}
if ((rkB==2)&&(document.forms.ESPT.PhotoB.src==phid)){secondchoice();}
if ((rkC==2)&&(document.forms.ESPT.PhotoC.src==phid)){secondchoice();}
if ((rkD==2)&&(document.forms.ESPT.PhotoD.src==phid)){secondchoice();}

if ((rkA==3)&&(document.forms.ESPT.PhotoA.src==phid)){thirdchoice();}
if ((rkB==3)&&(document.forms.ESPT.PhotoB.src==phid)){thirdchoice();}
if ((rkC==3)&&(document.forms.ESPT.PhotoC.src==phid)){thirdchoice();}
if ((rkD==3)&&(document.forms.ESPT.PhotoD.src==phid)){thirdchoice();}

if ((rkA==4)&&(document.forms.ESPT.PhotoA.src==phid)){fourthchoice();}
if ((rkB==4)&&(document.forms.ESPT.PhotoB.src==phid)){fourthchoice();}
if ((rkC==4)&&(document.forms.ESPT.PhotoC.src==phid)){fourthchoice();}
if ((rkD==4)&&(document.forms.ESPT.PhotoD.src==phid)){fourthchoice();}
document.forms.ESPT.Button.value="Reset";
}

function firstchoice(){
document.forms.ESPT.Messages.value="CONGRATULATIONS!";document.forms.ESPT.Messages0.value="The actual photo (now shown) was your FIRST choice";
}

function secondchoice(){
document.forms.ESPT.Messages.value="BAD LUCK!";document.forms.ESPT.Messages0.value="The actual photo (now shown) was your SECOND choice";
}

function thirdchoice(){
document.forms.ESPT.Messages.value="BAD LUCK!";document.forms.ESPT.Messages0.value="The actual photo (now shown) was your THIRD choice";
}

function fourthchoice(){
document.forms.ESPT.Messages.value="BAD LUCK!";document.forms.ESPT.Messages0.value="The actual photo (now shown) was your FOURTH choice";
}

function resetall(){
clrtime();
started=0;
locked=0;
paused=0;
document.forms.ESPT.Button.value="Begin";
document.forms.ESPT.Messages.value="Practice and test your clairvoyance and telepathy!";document.forms.ESPT.Messages0.value="Choose a procedure, then click 'Begin'.";
document.forms.ESPT.Photoshow.src=pimages[7].src;
document.forms.ESPT.PhotoA.src=pimages[0].src;
document.forms.ESPT.PhotoB.src=pimages[0].src;
document.forms.ESPT.PhotoC.src=pimages[0].src;
document.forms.ESPT.PhotoD.src=pimages[0].src;
setranks();
pausing();
}

function setranks(){
for (x=0;x<5;x++){
document.forms.ESPT.RankA.options[x].text = "";
document.forms.ESPT.RankB.options[x].text = "";
document.forms.ESPT.RankC.options[x].text = "";
document.forms.ESPT.RankD.options[x].text = "";
}
document.forms.ESPT.RankA.selectedIndex=0;
document.forms.ESPT.RankB.selectedIndex=0;
document.forms.ESPT.RankC.selectedIndex=0;
document.forms.ESPT.RankD.selectedIndex=0;
}

function pausing(){
clearTimeout(timeout[20])
paused=1;
timeout[20] = setTimeout("paused=0;", 1000);
}