var tries = 0;
var finished = 0;

images = new Array;
images[1] = new Image();
images[1].src = "lit_window.jpg";
images[2] = new Image();
images[2].src = "unlit_window.jpg";
images[3] = new Image();
images[3].src = "lit_door.jpg";
images[4] = new Image();
images[4].src = "unlit_door.jpg";
////////////////////////////////////////////////
function setLights(){
do{
var randnum = Math.round(Math.random() * 2);
}while ((randnum < 1) || (randnum > 2))
document.tl_window.src = images[randnum].src;

randnum = 0;
do{
randnum = Math.round(Math.random() * 2);
}while ((randnum < 1) || (randnum > 2))
document.tm_window.src = images[randnum].src;

randnum = 0;
do{
var randnum = Math.round(Math.random() * 2);
}while ((randnum < 1) || (randnum > 2))
document.tr_window.src = images[randnum].src;

randnum = 0;
do{
randnum = Math.round(Math.random() * 2);
}while ((randnum < 1) || (randnum > 2))
document.ml_window.src = images[randnum].src;

randnum = 0;
do{
randnum = Math.round(Math.random() * 2);
}while ((randnum < 1) || (randnum > 2))
document.mm_window.src = images[randnum].src;

randnum = 0;
do{
randnum = Math.round(Math.random() * 2);
}while ((randnum < 1) || (randnum > 2))
document.mr_window.src = images[randnum].src;

randnum = 0;
do{
randnum = Math.round(Math.random() * 2);
}while ((randnum < 1) || (randnum > 2))
document.bl_window.src = images[randnum].src;

randnum = 0;
do{
var randnum = Math.round(Math.random() * 4);
}while ((randnum < 3) || (randnum > 4))
document.door.src = images[randnum].src;

randnum = 0;
do{
var randnum = Math.round(Math.random() * 2);
}while ((randnum < 1) || (randnum > 2))
document.br_window.src = images[randnum].src;
}
///////////////////////////////////////////////////////
function checkLights(){
if (finished == 0){
if (	(document.tl_window.src == images[2].src) &&
	(document.tm_window.src == images[2].src) &&
	(document.tr_window.src == images[2].src) &&
	(document.ml_window.src == images[2].src) &&
	(document.mm_window.src == images[2].src) &&
	(document.mr_window.src == images[2].src) &&
	(document.bl_window.src == images[2].src) &&
	(document.door.src == images[4].src) &&
	(document.br_window.src == images[2].src)){
		alert("Way to go, You Did It!!\nIt only took you " + tries + " clicks!");
		finished = 1;
}}
else if ((document.tl_window.src == images[2].src) &&
	(document.tm_window.src == images[2].src) &&
	(document.tr_window.src == images[2].src) &&
	(document.ml_window.src == images[2].src) &&
	(document.mm_window.src == images[2].src) &&
	(document.mr_window.src == images[2].src) &&
	(document.bl_window.src == images[2].src) &&
	(document.door.src == images[4].src) &&
	(document.br_window.src == images[2].src))
		alert("You cheater! Refresh the page to play again!!");
}

function statusBar(){
tries++;
window.status = "You have made " + tries + " clicks.";

}

function tl_click(){
if (document.tl_window.src == images[1].src)
	document.tl_window.src = images[2].src;

else if (document.tl_window.src == images[2].src)
	document.tl_window.src = images[1].src;
tm();
ml();
statusBar();
checkLights();
}

function tm_click(){
if (document.tm_window.src == images[1].src)
	document.tm_window.src = images[2].src;

else if (document.tm_window.src == images[2].src)
	document.tm_window.src = images[1].src;
tl();
tr();
mm();
statusBar();
checkLights();
}

function tr_click(){
if (document.tr_window.src == images[1].src)
	document.tr_window.src = images[2].src;

else if (document.tr_window.src == images[2].src)
	document.tr_window.src = images[1].src;
tm();
mr();
statusBar();
checkLights();
}

function ml_click(){
if (document.ml_window.src == images[1].src)
	document.ml_window.src = images[2].src;

else if (document.ml_window.src == images[2].src)
	document.ml_window.src = images[1].src;
tl();
bl();
mm();
statusBar();
checkLights();
}

function mm_click(){
if (document.mm_window.src == images[1].src)
	document.mm_window.src = images[2].src;

else if (document.mm_window.src == images[2].src)
	document.mm_window.src = images[1].src;
tm();
ml();
mr();
bm();
statusBar();
checkLights();
}

function mr_click(){
if (document.mr_window.src == images[1].src)
	document.mr_window.src = images[2].src;

else if (document.mr_window.src == images[2].src)
	document.mr_window.src = images[1].src;
tr();
mm();
br();
statusBar();
checkLights();
}

function bl_click(){
if (document.bl_window.src == images[1].src)
	document.bl_window.src = images[2].src;

else if (document.bl_window.src == images[2].src)
	document.bl_window.src = images[1].src;
ml();
bm();
statusBar();
checkLights();
}

function bm_click(){
if (document.door.src == images[3].src)
	document.door.src = images[4].src;

else if (document.door.src == images[4].src)
	document.door.src = images[3].src;
bl();
br();
mm();
statusBar();
checkLights();
}

function br_click(){
if (document.br_window.src == images[1].src)
	document.br_window.src = images[2].src;

else if (document.br_window.src == images[2].src)
	document.br_window.src = images[1].src;
mr();
bm();
statusBar();
checkLights();
}
////////////////////////////////////////////////////////////////////
function tl(){
if (document.tl_window.src == images[1].src)
	document.tl_window.src = images[2].src;

else if (document.tl_window.src == images[2].src)
	document.tl_window.src = images[1].src;
}

function tm(){
if (document.tm_window.src == images[1].src)
	document.tm_window.src = images[2].src;

else if (document.tm_window.src == images[2].src)
	document.tm_window.src = images[1].src;
}

function tr(){
if (document.tr_window.src == images[1].src)
	document.tr_window.src = images[2].src;

else if (document.tr_window.src == images[2].src)
	document.tr_window.src = images[1].src;
}

function ml(){
if (document.ml_window.src == images[1].src)
	document.ml_window.src = images[2].src;

else if (document.ml_window.src == images[2].src)
	document.ml_window.src = images[1].src;
}

function mm(){
if (document.mm_window.src == images[1].src)
	document.mm_window.src = images[2].src;

else if (document.mm_window.src == images[2].src)
	document.mm_window.src = images[1].src;
}

function mr(){
if (document.mr_window.src == images[1].src)
	document.mr_window.src = images[2].src;

else if (document.mr_window.src == images[2].src)
	document.mr_window.src = images[1].src;
}

function bl(){
if (document.bl_window.src == images[1].src)
	document.bl_window.src = images[2].src;

else if (document.bl_window.src == images[2].src)
	document.bl_window.src = images[1].src;
}

function bm(){
if (document.door.src == images[3].src)
	document.door.src = images[4].src;

else if (document.door.src == images[4].src)
	document.door.src = images[3].src;
}

function br(){
if (document.br_window.src == images[1].src)
	document.br_window.src = images[2].src;

else if (document.br_window.src == images[2].src)
	document.br_window.src = images[1].src;
}