scoreColors = new Array("#bbbbbb","#bb2222","#FFFF9C","#22bb22");

function SetUpField (level) {
document.game.level.value = level;
if (level == 0){
	document.game.game.value = "shifty2";
	document.getElementById("LDisplay").innerHTML = "0";
	}
else {
	document.game.game.value = "shifty2"+level;
	document.getElementById("LDisplay").innerHTML = level;
	}

for (col=0;col<5;col++) {
	if (Levels[level][col] == 0) {
		document.getElementById("ti"+col).src = "../images/empty.gif";
		document.getElementById("bi"+col).src = "../images/empty.gif";
		document.getElementById("tl"+col).href = "#void";
		document.getElementById("bl"+col).href = "#void";
		}
	else {
		document.getElementById("ti"+col).src = "../images/top.gif";
		document.getElementById("bi"+col).src = "../images/bottom.gif";
		document.getElementById("tl"+col).href = ActiveLinks["t"+col] ;
		document.getElementById("bl"+col).href = ActiveLinks["b"+col] ;
		}
	}
for (row=0;row<5;row++) {
	if (Levels[level][row+5] == 0) {
		document.getElementById("li"+row).src = "../images/empty.gif";
		document.getElementById("ri"+row).src = "../images/empty.gif";
		document.getElementById("ll"+row).href = "#void";
		document.getElementById("rl"+row).href = "#void";
		}
	else {
		document.getElementById("li"+row).src = "../images/left.gif";
		document.getElementById("ri"+row).src = "../images/right.gif";
		document.getElementById("ll"+row).href = ActiveLinks["l"+row] 
		document.getElementById("rl"+row).href = ActiveLinks["r"+row] 
		}
	}
}


function shift(a,b,c,d,e,f,g,dir) {
scorecard++;

// crosswipe script
crosswipeA(document.getElementById("a"+a), document.getElementById("a"+b).src, '1', dir, '','A');
crosswipeB(document.getElementById("a"+b), document.getElementById("a"+c).src, '1', dir, '','B');
crosswipeC(document.getElementById("a"+c), document.getElementById("a"+d).src, '1', dir, '','C');
crosswipeD(document.getElementById("a"+d), document.getElementById("a"+e).src, '1', dir, '','D');
crosswipeE(document.getElementById("a"+e), document.getElementById("a"+f).src, '1', dir, '','E');
crosswipeF(document.getElementById("a"+f), document.getElementById("a"+g).src, '1', dir, '','E');
crosswipeG(document.getElementById("a"+g), document.getElementById("a"+a).src, '1', dir, '','E');

// update score
if (GameOver == -1) {
	setTimeout("UpdateScore();",500);
	}
}


function rotate(i) {
scorecard++;

// Rotate the clicked button
temp = document.getElementById("a"+i).src.charAt(document.getElementById("a"+i).src.length-5);
temp++;
temp = (temp % 4);
crosswipeA(document.getElementById("a"+i), "../images/"+temp+".gif" , '1', 'lr', '','A');

// Rotate the surrounding buttons
Rot = exch[i-1].split(",");

if (Rot[0] != "-") {
	temp = document.getElementById("a"+Rot[0]).src.charAt(document.getElementById("a"+Rot[0]).src.length-5);
	temp++;
	temp = (temp % 4);
	crosswipeB(document.getElementById("a"+Rot[0]), "../images/"+temp+".gif" , '1', 'lr', '','A');
	}

if (Rot[1] != "-") {
	temp = document.getElementById("a"+Rot[1]).src.charAt(document.getElementById("a"+Rot[1]).src.length-5);
	temp++;
	temp = (temp % 4);
	crosswipeC(document.getElementById("a"+Rot[1]), "../images/"+temp+".gif" , '1', 'bt', '','A');
	}

if (Rot[2] != "-") {
	temp = document.getElementById("a"+Rot[2]).src.charAt(document.getElementById("a"+Rot[2]).src.length-5);
	temp++;
	temp = (temp % 4);
	crosswipeD(document.getElementById("a"+Rot[2]), "../images/"+temp+".gif" , '1', 'tb', '','A');
	}

if (Rot[3] != "-") {
	temp = document.getElementById("a"+Rot[3]).src.charAt(document.getElementById("a"+Rot[3]).src.length-5);
	temp++;
	temp = (temp % 4);
	crosswipeE(document.getElementById("a"+Rot[3]), "../images/"+temp+".gif" , '1', 'rl', '','A');
	}
if (GameOver == -1) {
	setTimeout("UpdateScore();",500);
	}
}


function UpdateScore() {
//update score
document.getElementById("scoretotal").innerHTML = scorecard;
S0 = 0;
S1 = 0;
S2 = 0;
S3 = 0;

for (m=1;m<46;m++){
	if (document.getElementById("a"+m).src.charAt(document.getElementById("a"+m).src.length-5) == 0) {S0++;}
	if (document.getElementById("a"+m).src.charAt(document.getElementById("a"+m).src.length-5) == 1) {S1++;}
	if (document.getElementById("a"+m).src.charAt(document.getElementById("a"+m).src.length-5) == 2) {S2++;}
	if (document.getElementById("a"+m).src.charAt(document.getElementById("a"+m).src.length-5) == 3) {S3++;}
	}

document.getElementById("score0").innerHTML = "<font color='"+ scoreColors[Math.floor(S0/10)] +"'>"+ S0 +"</font>";
document.getElementById("score1").innerHTML = "<font color='"+ scoreColors[Math.floor(S1/10)] +"'>"+ S1 +"</font>";
document.getElementById("score2").innerHTML = "<font color='"+ scoreColors[Math.floor(S2/10)] +"'>"+ S2 +"</font>";
document.getElementById("score3").innerHTML = "<font color='"+ scoreColors[Math.floor(S3/10)] +"'>"+ S3 +"</font>";

if ((S0 == 45) || 
    (S1 == 45) || 
    (S2 == 45) || 
    (S3 == 45)) {
	if (S0 == 45) {document.game.pic.value="../images/0.gif";}
	if (S1 == 45) {document.game.pic.value="../images/1.gif";}
	if (S2 == 45) {document.game.pic.value="../images/2.gif";}
	if (S3 == 45) {document.game.pic.value="../images/3.gif";}
	document.game.score.value=scorecard;
	msgWindow=window.open("endofgame.asp","displayWindow","toolbar=no,width=600,height=300,directories=no,status=no,scrollbars=yes,resize=no,menubar=no")
	GameOver = 1;
	}
}


function resetgame() {
if (level == 9) {
	// random start color
	Color= Math.floor(Math.random() * 4);
	for (l=1;l<46;l++){ 
		document.getElementById("a"+l).src="../images/"+ Color + ".gif";
		}

	// Scramble marbles
	M = 25;  // number of moves
	P = 25;  // number of marbles

	for (xy=0;xy<M;xy++) {
		Thisone = (Math.floor(Math.random() * P))+1;
		Mix(Thisone);
		}
	}
else {
	for (l=1;l<46;l++) {
		document.getElementById("a"+l).src="../images/"+Math.floor(Math.random() * 4) + ".gif";
		}
	}

GameOver = -1;
scorecard=0;
UpdateScore();
}



function Mix(i) {
// change center button
temp = document.getElementById("a"+i).src.charAt(document.getElementById("a"+i).src.length-5);
temp++;
document.getElementById("a"+i).src="../images/"+ (temp) % 4 +".gif";

// change surrounding buttons
Rot = exch[i-1].split(",");
for (x=0;x<4;x++) {
	if (Rot[x] != "-") { 
		tem = document.getElementById("a"+ Rot[x]).src.charAt(document.getElementById("a"+ Rot[x]).src.length-5);
		tem++;
		document.getElementById("a"+ Rot[x]).src = "../images/"+(tem) % 4 +".gif";
		}
	}
}