// Jonathan Gaedke
// Final for Web Design (2004)
// If you like the following script please ask to use it
// mold_it@hotmail.com In the subject text area, enter the name of the web site that posted it.
// Example:  www.codygokarts.com

// declare the variables
var pictures = new Array(16);
var good = new Array();
var b = 0;
var c = 0;
var b2 = 0;
var b3 = 0;
var ran = 0;
var chk = 0;
var counter = 0;
var flag = 0;
var flag2 = 0;
// set the array to zero's
for (c = 0; c <= 16; c++)
{
	good[c] = 0;
}

// go button resets everything
function resets2()
{
	b2 = 0;
	flag = 0;
	for (c = 0; c <= 16; c++)
	{
		good[c] = 0;
	}
	chk = 0;
	b = 0;
	counter = 0;
	document.theform.cout.value = "";
	resets();
}

// pick random number
function pickran() 
{
if(b2 >= 16)
{
	ran = 17;
}
if(b2 <= 15)
{
chk = 0;
while(chk <= 2)
{
if(chk < 1)
{
	ran = Math.floor(Math.random() * 16) + 1;
}
for (b3 = 0; b3 <= b2; b3++)
{
	
	if (ran == good[b3])
	{
		chk = 0;
		break;
	}
	chk++;
}
}
}
document.theform.image00.src = pictures[ran].src
}

//get the pictures put into an array.
function getpics()
{

var i = 0;
for (i = 1; i <= 16; i++)
{
	pictures[i] = new Image(100, 75);
	pictures[i].src = "game/img" + i + ".jpg"  ;
	document.theform.cout.value = "Getting images " + i + " of 16.";
}
pictures[17] = new Image(100, 75);
pictures[17].src = "images/space.gif";
pickran();
}

// this actually only resets the program the first time
// after that it shows the images that i have created
function resets(picID)
{
if(picID == null)
{
getpics();
window.document.theform.image1.src = "images/unknown.gif"
window.document.theform.image2.src = "images/unknown.gif"
window.document.theform.image3.src = "images/unknown.gif"
window.document.theform.image4.src = "images/unknown.gif"
window.document.theform.image5.src = "images/unknown.gif"
window.document.theform.image6.src = "images/unknown.gif"
window.document.theform.image7.src = "images/unknown.gif"
window.document.theform.image8.src = "images/unknown.gif"
window.document.theform.image9.src = "images/unknown.gif"
window.document.theform.image10.src = "images/unknown.gif"
window.document.theform.image11.src = "images/unknown.gif"
window.document.theform.image12.src = "images/unknown.gif"
window.document.theform.image13.src = "images/unknown.gif"
window.document.theform.image14.src = "images/unknown.gif"
window.document.theform.image15.src = "images/unknown.gif"
window.document.theform.image16.src = "images/unknown.gif"
document.theform.cout.value = "";
flag2 = 1;
}
if(picID != null)
{
	if((picID == 1) || (ran == 17))
	{
		window.document.theform.image1.src = "images/space.gif"
		good[b2] = 1;
		b2++;
	}
	if((picID == 2) || (ran == 17))
	{
		window.document.theform.image2.src = "images/space.gif"
		good[b2] = 2;
		b2++;
	}
	if((picID == 3) || (ran == 17))
	{
		window.document.theform.image3.src = "images/space.gif"
		good[b2] = 3;
		b2++;
	}
	if((picID == 4) || (ran == 17))
	{
		window.document.theform.image4.src = "images/space.gif"
		good[b2] = 4;
		b2++;
	}
	if((picID == 5) || (ran == 17))
	{
		window.document.theform.image5.src = "images/space.gif"
		good[b2] = 5;
		b2++;
	}
	if((picID == 6) || (ran == 17))
	{
		window.document.theform.image6.src = "images/space.gif"
		good[b2] = 6;
		b2++;
	}
	if((picID == 7) || (ran == 17))
	{
		window.document.theform.image7.src = "images/space.gif"
		good[b2] = 7;
		b2++;
	}
	if((picID == 8) || (ran == 17))
	{
		window.document.theform.image8.src = "images/space.gif"
		good[b2] = 8;
		b2++;
	}
	if((picID == 9) || (ran == 17))
	{
		window.document.theform.image9.src = "images/space.gif"
		good[b2] = 9;
		b2++;
	}
	if((picID == 10) || (ran == 17))
	{
		window.document.theform.image10.src = "images/space.gif"
		good[b2] = 10;
		b2++;
	}
	if((picID == 11) || (ran == 17))
	{
		window.document.theform.image11.src = "images/space.gif"
		good[b2] = 11;
		b2++;
	}
	if((picID == 12) || (ran == 17))
	{
		window.document.theform.image12.src = "images/space.gif"
		good[b2] = 12;
		b2++;
	}
	if((picID == 13) || (ran == 17))
	{
		window.document.theform.image13.src = "images/space.gif"
		good[b2] = 13;
		b2++;
	}
	if((picID == 14) || (ran == 17))
	{
		window.document.theform.image14.src = "images/space.gif"
		good[b2] = 14;
		b2++;
	}
	if((picID == 15) || (ran == 17))
	{
		window.document.theform.image15.src = "images/space.gif"
		good[b2] = 15;
		b2++;
	}
	if((picID == 16) || (ran == 17))
	{
		window.document.theform.image16.src = "images/space.gif"
		good[b2] = 16;
		b2++;
	}
}
pickran();
}

// this has my counter attached to it. This is also the main 
// function that calls all the other functions through a daisy
// chain.
function clicks(imgID)
{
	if(flag2 == 0)
	{
		alert("Click the Go Button When ready");
	}
	if(flag2 != 0)
	{
		dogame(imgID);
	}
}	

function dogame(imgID)
{
	counter++;
	if(imgID == ran)
	{
		resets(imgID);
	}
	if(b2 < 15)
	{
		pickran();
	}
	if(ran == 17)
	{
		counter = counter - b2;
		alert("You win!! with " + counter + " error(s).");
		flag = 1;
	}
	document.theform.cout.value = (counter - b2) + " Errors";
	if(flag == 1)
	{
		document.theform.cout.value = "";
		flag2 = 0;
		flag = 0;
		counter = 0;
		b2 = 0;
	}
}