var x=0;
var y=0;
var n=0;
var r=0;
var ch=0;
var st=0;
var d=0;
var f=0;
var tm=0;
var inp=new Array(10002);
var oup=new Array(10002);
var oux=new Array(10002);
var timeout1=0;

var months=new Array("","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
var mydate= new Date();
var theyear=mydate.getFullYear();
var themonth=mydate.getMonth()+1;
var theday=mydate.getDate();
var thehour=mydate.getHours();
var themin=mydate.getMinutes();
var thesec=mydate.getSeconds();

var str$="";
var str1$="";
var s1=0;


function clr(){
begin();
document.forms.matrix.output.value="";
}

function cleardata1(){
if(document.forms.matrix.resett.value==""){return;}
cleardata();
}

function cleardata(){
begin();
document.forms.matrix.go.value="START";
document.forms.matrix.Mess1.value="Randomize your own custom lists";
document.forms.matrix.Mess3.value="(lists are NOT sent to our servers) *";
document.forms.matrix.Mess2.style.backgroundColor="#CCCCCC";
document.forms.matrix.Mess2.value="";
document.forms.matrix.resett.style.backgroundColor="#CCCCCC";
document.forms.matrix.resett.value="";
document.forms.matrix.M3.value="Your data remain confidential";
document.forms.matrix.output.disabled=true;
document.forms.matrix.output.style.backgroundColor="#CCCCCC";
document.forms.matrix.orig.value="";
document.forms.matrix.orig.style.backgroundColor="#CCCCCC";
}

function begin(){
document.forms.matrix.inpopt[0].disabled=false;
document.forms.matrix.inpopt[1].disabled=false;
if((document.forms.matrix.inpopt[0].checked==false)&&(document.forms.matrix.inpopt[1].checked==false)){document.forms.matrix.inpopt[0].checked==true;};
if((document.forms.matrix.outpopt[0].checked==false)&&(document.forms.matrix.outpopt[1].checked==false)){document.forms.matrix.outpopt[0].checked==true;};

clearTimeout(timeout1);
begin2();
st=0;
str$="";
str1$="";
document.forms.matrix.output.value="";
document.forms.matrix.go.value="RANDOMIZE";
document.forms.matrix.resett.value="Reset";
document.forms.matrix.resett.style.backgroundColor="red";
document.forms.matrix.Mess1.value="Type or paste your list in the box";
document.forms.matrix.Mess3.value="Then click RANDOMIZE";
document.forms.matrix.Mess2.value="";
document.forms.matrix.M3.value="( Maximum 10,000 items )";
document.forms.matrix.output.disabled=false;
document.forms.matrix.output.style.backgroundColor="white";
}

function begin2(){
y=0;
n=0;
r=0;
len=0;
ch=0;
s1=0;
d=0;
f=0;
tm=0;
for (x=0;x<10002;x++){
inp[x]="";
oup[x]="";
oux[x]=0;
}
x=0;
}

function gorand(){
if(document.forms.matrix.go.value=="Calculating ..."){return;};
if(document.forms.matrix.go.value=="START"){begin();return;}

str1$=document.forms.matrix.output.value;
if(str1$.length==0){return;}

document.forms.matrix.go.value="Calculating ...";
document.forms.matrix.Mess1.value="";
document.forms.matrix.Mess3.value="";
document.forms.matrix.M3.value="";
document.forms.matrix.resett.value="";
document.forms.matrix.resett.style.backgroundColor="#CCCCCC";
begin2();
clearTimeout(timeout1);
timeout1=setTimeout("paus();",400);
}

function paus(){
if(st==0){checkstring();}
putarray();
}

function checkline(){
while(ch==0){
if (str$.substring(0,1)=="\n"){str$=str$.substring(1);}
else{ch=1;}
}
ch=0;
while(ch==0){
if (str$.substring(str$.length-1)=="\n"){str$=str$.substring(0,str$.length-1);}
else{ch=1;}
}
str$=str$.replace(/\n\n\n\n\n\n\n\n\n\n\n\n/g,"\n");
str$=str$.replace(/\n\n\n\n\n\n\n\n\n\n\n/g,"\n");
str$=str$.replace(/\n\n\n\n\n\n\n\n\n\n/g,"\n");
str$=str$.replace(/\n\n\n\n\n\n\n\n\n/g,"\n");
str$=str$.replace(/\n\n\n\n\n\n\n\n/g,"\n");
str$=str$.replace(/\n\n\n\n\n\n\n/g,"\n");
str$=str$.replace(/\n\n\n\n\n\n/g,"\n");
str$=str$.replace(/\n\n\n\n\n/g,"\n");
str$=str$.replace(/\n\n\n\n/g,"\n");
str$=str$.replace(/\n\n\n/g,"\n");
str$=str$.replace(/\n\n/g,"\n");
if(str$.length==0){return;st=1;}
}

function checkcomma(){
while(ch==0){
if (str$.substring(0,1)==","){str$=str$.substring(1);}
else{ch=1;}
}
ch=0;
while(ch==0){
if (str$.substring(str$.length-1)==","){str$=str$.substring(0,str$.length-1);}
else{ch=1;}
}
str$=str$.replace(/,,,,,,,,,,,,/g,",");
str$=str$.replace(/,,,,,,,,,,,/g,",");
str$=str$.replace(/,,,,,,,,,,/g,",");
str$=str$.replace(/,,,,,,,,,/g,",");
str$=str$.replace(/,,,,,,,,/g,",");
str$=str$.replace(/,,,,,,,/g,",");
str$=str$.replace(/,,,,,,/g,",");
str$=str$.replace(/,,,,,/g,",");
str$=str$.replace(/,,,,/g,",");
str$=str$.replace(/,,,/g,",");
str$=str$.replace(/,,/g,",");
if(str$.length==0){return;st=1;}
}

function checkstring(){
str$=document.forms.matrix.output.value;
ch=0;
if(document.forms.matrix.inpopt[0].checked==true){checkline();}
if(document.forms.matrix.inpopt[1].checked==true){stripline();checkcomma();}
st=1;
str$=str$+"\nEndofString";

document.forms.matrix.inpopt[0].disabled=true;
document.forms.matrix.inpopt[1].disabled=true;

}

function putarray(){
s1=0;
n=1;
for (x=1;x<str$.length-1;x++){
if(n>10000){alert("Your list has too many items to randomize");cleardata();return;}

if(str$.substring(x,x+12)=="\nEndofString"){inp[n]=str$.substring(s1,x);ran();f=1;tm=0;disp();return;}

if(document.forms.matrix.inpopt[0].checked==true){
if(str$.substring(x,x+1)=="\n"){inp[n]=str$.substring(s1,x);s1=x+1;n++;}
}

if(document.forms.matrix.inpopt[1].checked==true){
if(str$.substring(x,x+1)==","){inp[n]=str$.substring(s1,x);s1=x+1;n++;}
}
}
}

function ran(){
for (x=1; x<=n; x++) {
r=(Math.floor(Math.random()*n)+1);
oux[x]=r;oup[x]=inp[r];
if (x>1){for(y=1;y<x;y++){if(oux[x]==oux[y]){x--;}}}
}
}

function disp(){
if (f==0){return;}

if(n<2){alert("Insufficient items to randomize");cleardata();return;}
str1$="";



document.forms.matrix.orig.value="View original list";
document.forms.matrix.orig.style.backgroundColor="#F9F9F9";



d=document.forms.matrix.D1.value;
if (d==0){d=n;}

if(document.forms.matrix.outpopt[0].checked==true){
for (x=1; x<=d; x++) {
if(document.forms.matrix.numopt.checked==true){str1$=str1$+String(x)+". "+oup[x]+"\n";}else{str1$=str1$+oup[x]+"\n";}
}
}

if(document.forms.matrix.outpopt[1].checked==true){
for (x=1; x<=d; x++) {
if(document.forms.matrix.numopt.checked==true){str1$=str1$+String(x)+"."+oup[x]+",";}else{str1$=str1$+oup[x]+",";}
}
}

if (tm==0){timestamp();tm=1;}

document.forms.matrix.go.value="RANDOMIZE";
document.forms.matrix.output.value = str1$+"\n\nRandomized list generated "+months[themonth]+" "+String(theday)+" "+String(theyear)+" at "+String(thehour)+":"+String(themin)+":"+String(thesec)+" by www.psychicscience.org  \nFree educational resources for parapsychology, psychical research & mind magic.";
document.forms.matrix.Mess1.value="Click RANDOMIZE again to resort";
document.forms.matrix.Mess3.value="You can also change output";
document.forms.matrix.M3.value="or Reset to start over";
document.forms.matrix.Mess2.value="Copy down these randomized data or cut and paste them into your application";
document.forms.matrix.Mess2.style.backgroundColor="green";
document.forms.matrix.resett.value="Reset";
document.forms.matrix.resett.style.backgroundColor="red";
}

function chinp(){
}

function choutp(){
if (document.forms.matrix.orig.value=="View random list"){origview();}else{disp();}
}

function chnum(){
if (document.forms.matrix.orig.value=="View random list"){origview();}else{disp();}
}

function chdisp(){
if (document.forms.matrix.orig.value=="View random list"){origview();}else{disp();}
}

function view(){
if(document.forms.matrix.orig.value==""){return;}
if (document.forms.matrix.orig.value=="View original list"){
document.forms.matrix.orig.value="View random list";origview();return;
}
if (document.forms.matrix.orig.value=="View random list"){document.forms.matrix.orig.value="View original list";disp();return;}
}

function origview(){
str1$="";
d=document.forms.matrix.D1.value;
if (d==0){d=n;}
if(document.forms.matrix.outpopt[0].checked==true){
for (x=1; x<=d; x++) {
if(document.forms.matrix.numopt.checked==true){str1$=str1$+String(x)+". "+inp[x]+"\n";}else{str1$=str1$+inp[x]+"\n";}
}
document.forms.matrix.output.value = str1$;
}
if(document.forms.matrix.outpopt[1].checked==true){
for (x=1; x<=d; x++) {
if(document.forms.matrix.numopt.checked==true){str1$=str1$+String(x)+"."+inp[x]+",";}else{str1$=str1$+inp[x]+",";}
}
document.forms.matrix.output.value = str1$;
}
document.forms.matrix.Mess2.value="Data from original list now shown";

}

function timestamp(){
mydate= new Date();
theyear=mydate.getFullYear();
themonth=mydate.getMonth()+1;
theday=mydate.getDate();
thehour=mydate.getHours();
themin=mydate.getMinutes();
thesec=mydate.getSeconds();
}

function stripline(){
str$=str$.replace(/\n/g,"");
}