var n = 0;
var s = 0;
var e = 0;
var r = 0;
var x = 0;
var y =0;
var ran = 0;
var range = 0;
var outp = new Array(100002);
var outp2= new Array(10000);
var out = "";
var ns = "";
var ss = "";
var es = "";
var freq = new Array(10000);
var clos = new Array(10000);
var expf=0;
var chi=0;
var nx=0;
var dfs=0;
var flag=0;
var need=0;
var timeout=0;
var f1=0;
var f2=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 crit = new Array(0,
2.706,4.605,6.251,7.779,9.236,
10.645,12.017,13.362,14.684,15.987,
17.275,18.549,19.812,21.064,22.307,
23.542,24.769,25.989,27.204,28.412,
29.615,30.813,32.007,33.196,34.382,
35.563,36.741,37.916,39.087,40.256,
41.422,42.585,43.745,44.903,46.059,
47.212,48.363,49.513,50.660,51.805,
52.949,54.090,55.230,56.369,57.505,
58.641,59.774,60.907,62.038,63.167,
64.295,65.422,66.548,67.673,68.796,
69.919,71.040,72.160,73.279,74.397,
75.514,76.630,77.745,78.860,79.973,
81.085,82.197,83.308,84.418,85.527,
86.635,87.743,88.850,89.956,91.061,
92.166,93.270,94.374,95.476,96.578,
97.680,98.780,99.880,100.980,102.079,
103.177,104.275,105.372,106.469,107.565,
108.661,109.756,110.850,111.944,113.038,
114.131,115.223,116.315,117.407,118.498);

var matrix = new Array(12);
for(x=0;x<=12;x++){
matrix[x]=new Array(12);
}

var rowsum = new Array(12);
var colsum = new Array(12);

for (x=0; x<100002; x++) {
outp[x] = 0;
}

for (x=0;x<10001;x++) {
clos[x]=0;
outp2[x]=-1;
}

function begin() {
document.forms.matrix.num.value = "";
document.forms.matrix.st.value = "";
document.forms.matrix.en.value = "";
out="";
document.forms.matrix.output.value = "";
document.forms.matrix.Messages.style.backgroundColor="#CCCCCC";
document.forms.matrix.Messages.value="";
for (x=0; x<100002; x++) {
outp[x] = 0;
}
for (x=0;x<10001;x++) {
clos[x]=0;
outp2[x]=-1;
}
clear2();
}

function clear2(){
document.forms.matrix.Ch1.value="";
document.forms.matrix.Ch2.value="";
document.forms.matrix.Chilab1.value="";
document.forms.matrix.Chilab2.value="";
document.forms.matrix.Dflab1.value="";
document.forms.matrix.Dflab2.value="";
document.forms.matrix.Ch1.style.backgroundColor="#CCCCCC";
document.forms.matrix.Ch2.style.backgroundColor="#CCCCCC";
document.forms.matrix.Ch1.style.color="black";
document.forms.matrix.Ch2.style.color="black";
}

function clearfields(){
document.forms.matrix.Ch1.value="";
document.forms.matrix.Ch2.value="";
out = "";
document.forms.matrix.output.value = out;
clear2();
}

function checknum() {
clearfields();

for (x=0;x<10001;x++) {
clos[x]=0;
outp2[x]=-1;
}

if((document.forms.matrix.num.value.length > 5)&&(document.forms.matrix.rpt.selectedIndex == 2)) {
document.forms.matrix.Messages.style.backgroundColor="red";
document.forms.matrix.Messages.style.color="white";
document.forms.matrix.Messages.value="No more than 10,000 unique integers can be generated";
return;
}

if((document.forms.matrix.num.value.length > 5)&&(document.forms.matrix.rpt.selectedIndex == 1)) {
document.forms.matrix.Messages.style.backgroundColor="red";
document.forms.matrix.Messages.style.color="white";
document.forms.matrix.Messages.value="No more than 10,000 closed integers can be generated";
return;
}

if(document.forms.matrix.num.value.length > 6) {
document.forms.matrix.Messages.style.backgroundColor="red";
document.forms.matrix.Messages.style.color="white";
document.forms.matrix.Messages.value="No more than 100,000 integers can be generated";
return;
}
if (!parseInt(document.forms.matrix.num.value, 10)) {
document.forms.matrix.Messages.style.backgroundColor="red";
document.forms.matrix.Messages.style.color="white";
document.forms.matrix.Messages.value="Type in WHOLE NUMBER of integers to generate";
return;
}
n = Math.ceil(parseInt(document.forms.matrix.num.value, 10));
if((n > 10000)&&(document.forms.matrix.rpt.selectedIndex == 1)) {
document.forms.matrix.Messages.style.color="white";
document.forms.matrix.Messages.style.backgroundColor="red";
document.forms.matrix.Messages.value="No more than 10,000 closed integers can be generated";
return;
}
if((n > 10000)&&(document.forms.matrix.rpt.selectedIndex == 2)) {
document.forms.matrix.Messages.style.color="white";
document.forms.matrix.Messages.style.backgroundColor="red";
document.forms.matrix.Messages.value="No more than 10,000 unique integers can be generated";
return;
}
if (n > 100000) {
document.forms.matrix.Messages.style.color="white";
document.forms.matrix.Messages.style.backgroundColor="red";
document.forms.matrix.Messages.value="No more than 100,000 integers can be generated";
return;
}
if (n < 1) {
document.forms.matrix.Messages.style.color="white";
document.forms.matrix.Messages.style.backgroundColor="red";
document.forms.matrix.Messages.value="Type in WHOLE NUMBER of integers to generate";
return;
}
ns = String(n);
if(document.forms.matrix.st.value.length > 7) {
document.forms.matrix.Messages.style.color="white";
document.forms.matrix.Messages.style.backgroundColor="red";
document.forms.matrix.Messages.value="Lowest integer must be between 0 and 9999999";
return;
}
if(document.forms.matrix.st.value == "") {
document.forms.matrix.Messages.style.color="white";
document.forms.matrix.Messages.style.backgroundColor="red";
document.forms.matrix.Messages.value="Type in WHOLE NUMBER for lowest integer";
return;
}
if(document.forms.matrix.st.value == 0) {
s = 0;
}
else{
if (!parseInt(document.forms.matrix.st.value, 10)) {
document.forms.matrix.Messages.style.color="white";
document.forms.matrix.Messages.style.backgroundColor="red";
document.forms.matrix.Messages.value="Type in WHOLE NUMBER for lowest integer";
return;
}
s = Math.ceil(parseInt(document.forms.matrix.st.value, 10));
}
if (s > 9999999) {
document.forms.matrix.Messages.style.color="white";
document.forms.matrix.Messages.style.backgroundColor="red";
document.forms.matrix.Messages.value="Lowest integer must be between 0 and 9999999";
return;
}
if (s < 0) {
document.forms.matrix.Messages.style.color="white";
document.forms.matrix.Messages.style.backgroundColor="red";
document.forms.matrix.Messages.value="Lowest integer must be 0 or greater";
return;
}
ss = String(s);
if(document.forms.matrix.en.value.length > 7) {
document.forms.matrix.Messages.style.color="white";
document.forms.matrix.Messages.style.backgroundColor="red";
document.forms.matrix.Messages.value="Highest integer must be between 0 and 9999999";
return;
}
if (!parseInt(document.forms.matrix.en.value, 10)) {
document.forms.matrix.Messages.style.color="white";
document.forms.matrix.Messages.style.backgroundColor="red";
document.forms.matrix.Messages.value="Type in WHOLE NUMBER for highest integer";
return;
}
e = Math.ceil(parseInt(document.forms.matrix.en.value, 10));
if (e > 9999999) {
document.forms.matrix.Messages.style.color="white";
document.forms.matrix.Messages.style.backgroundColor="red";
document.forms.matrix.Messages.value="Highest integer must be between 0 and 9999999";
return;
}
if (e < 1) {
document.forms.matrix.Messages.style.color="white";
document.forms.matrix.Messages.style.backgroundColor="red";
document.forms.matrix.Messages.value="Highest integer must be 1 or greater";
return;
}
if (e <= s) {
document.forms.matrix.Messages.style.color="white";
document.forms.matrix.Messages.style.backgroundColor="red";
document.forms.matrix.Messages.value="Highest integer must be greater than lowest integer";
return;
}
es = String(e);
range = e-s+1;
if (document.forms.matrix.rpt.selectedIndex == 2) {
if (n > range) {
document.forms.matrix.Messages.style.color="white";
document.forms.matrix.Messages.style.backgroundColor="red";
document.forms.matrix.Messages.value="Number of integers to generate exceeds number of different values";
return;
}
}

if (document.forms.matrix.rpt.selectedIndex == 1) {
if (parseInt(n/range, 10)!=n/range) {
document.forms.matrix.Messages.style.color="white";
document.forms.matrix.Messages.style.backgroundColor="red";
document.forms.matrix.Messages.value="Number of integers to generate must be a multiple of the different values";
return;
}
}

document.forms.matrix.Messages.style.backgroundColor="orange";
document.forms.matrix.Messages.value="Generating "+String(n)+" integers.   Please wait . . . .";
timeout=setTimeout("generate();",400);
}

function resort(){
for(x=1;x<=n;x++){
outp2[x]=-1;
}
for (x=1;x<=n;x++){
ran = (Math.floor(Math.random()*n)+1);
if(outp2[ran]==-1){outp2[ran]=outp[x];}else{x--;}
}
for (x=1;x<=n;x++){
outp[x]=outp2[x];
}
}

function generate(){

if(document.forms.matrix.rpt.selectedIndex == 1) {
for (x=1; x<=n; x++) {
ran = (Math.floor(Math.random()*range)+s);
if (clos[ran]>=(n/range)){x--;}else{outp[x]=ran;clos[ran]++;}
}
resort();
}

if (document.forms.matrix.rpt.selectedIndex == 2) {
for (x=1; x<=n; x++) {
ran = (Math.floor(Math.random()*range)+s);
outp[x] = ran;

if (x > 1) {
for (y=1; y<=x-1; y++) {
if (outp[x] == outp[y]) {x--;}
}
}
}
}
if (document.forms.matrix.rpt.selectedIndex == 0) {
for (x=1; x<=n; x++) {
ran = (Math.floor(Math.random()*range)+s);
outp[x] = ran;
}
}
for (x=1; x<=n; x++) {
out = out+String(outp[x])+ " ";
}

mydate= new Date();
theyear=mydate.getFullYear();
themonth=mydate.getMonth()+1;
theday=mydate.getDate();
thehour=mydate.getHours();
themin=mydate.getMinutes();
thesec=mydate.getSeconds();

document.forms.matrix.output.value = out+"\n\nRandom numbers 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.Messages.style.color="white";
document.forms.matrix.Messages.style.backgroundColor="green";
document.forms.matrix.Messages.value="Copy down these data or cut and paste them into your application";

if((range<3)&&((n/range)<10)){return;}
if((n/range)<5){return;}

if((document.forms.matrix.rpt.selectedIndex < 2)&&(range<11)){
f1=0;
f2=0;

if(document.forms.matrix.rpt.selectedIndex==0){
document.forms.matrix.Ch1.style.backgroundColor="#EAEAEA"
document.forms.matrix.Ch1.value="Click to check equiprobability";
}

document.forms.matrix.Ch2.style.backgroundColor="#EAEAEA"
document.forms.matrix.Ch2.value="Click to check independence";
}
}

function equi(){
if((document.forms.matrix.Ch1.value=="")||(f1==1)){return;}
for (y=s;y<=e;y++){
freq[y]=0;
for (x=1;x<=n;x++){
if(outp[x]==y){
freq[y]++;
}
}
}
expf=n/range;
if(range>2){need=5;}else{need=10;}
if(expf<need){document.forms.matrix.Ch1.value="Insufficient data for calculation";document.forms.matrix.Ch1.style.color="white";document.forms.matrix.Ch1.style.backgroundColor="red";return;}
chi=0;
for (y=s;y<=e;y++){
chi=chi+(((freq[y]-expf)*(freq[y]-expf))/expf);
}
document.forms.matrix.Chilab1.value="Chi-Square = "+String(chi.toFixed(3));
document.forms.matrix.Dflab1.value="df = "+String(range-1);
if (chi<crit[range-1]){document.forms.matrix.Ch1.value="Integers are equiprobable";document.forms.matrix.Ch1.style.backgroundColor="green";
document.forms.matrix.Ch1.style.color="white";
}else{document.forms.matrix.Ch1.value="Integers are NOT equiprobable";
document.forms.matrix.Ch1.style.color="white";
document.forms.matrix.Ch1.style.backgroundColor="red";}
f1=1;
}

function setmat(){
for(x=1;x<12;x++){
for(y=1;y<12;y++){
matrix[x][y]=0;
}
}
for(x=1;x<=n-1;x++){
for(y=1;y<=(e-s+1);y++){
nx=outp[x+1]-s+1;
if (outp[x]==(y+s-1)){matrix[y][nx]++;}
}
}
calcsums();
calcchi();
}

function indep(){
if((document.forms.matrix.Ch2.value=="")||(f2==1)){return;}
document.forms.matrix.Ch2.style.color="black";
document.forms.matrix.Ch2.style.backgroundColor="orange";
document.forms.matrix.Ch2.value="Calculating ...";
timeout=setTimeout("setmat();",400);
}

function calcsums(){
for(x=1;x<=(e-s+1);x++){
rowsum[x]=0;
for(y=1;y<=(e-s+1);y++){
rowsum[x]=rowsum[x]+matrix[x][y];
}
}
for(y=1;y<=(e-s+1);y++){
colsum[y]=0;
for(x=1;x<=(e-s+1);x++){
colsum[y]=colsum[y]+matrix[x][y];
}
}
}

function calcchi(){
chi=0;
flag=0;
if(range>2){need=5;}else{need=10;}
for(x=1;x<=(e-s+1);x++){
for(y=1;y<=(e-s+1);y++){
expf=(rowsum[x]*colsum[y])/(n-1);
if(expf<need){flag=1;}
chi=chi+((matrix[x][y]-expf)*(matrix[x][y]-expf))/expf;
}
}
if(flag==1){document.forms.matrix.Ch2.value="Insufficient data for calculation";document.forms.matrix.Ch2.style.color="white";
document.forms.matrix.Ch2.style.backgroundColor="red";return;}
document.forms.matrix.Chilab2.value="Chi-Square = "+String(chi.toFixed(3));
dfs=(range-1)*(range-1);
document.forms.matrix.Dflab2.value="df = "+String(dfs);
if (chi<crit[dfs]){document.forms.matrix.Ch2.value="Integers are independent";document.forms.matrix.Ch2.style.backgroundColor="green";
document.forms.matrix.Ch2.style.color="white";
}else{document.forms.matrix.Ch2.value="Integers are NOT independent";
document.forms.matrix.Ch2.style.color="white";
document.forms.matrix.Ch2.style.backgroundColor="red";
}
f2=1;
}