var scores = new makearray(14);
var dice = new makearray(5);
var rolls = 3;
var mover = 0;
var cnt = 0;
var totalscored = 0;
p1 = new Image();
p1.src = "1.gif";
p2 = new Image();
p2.src = "2.gif";
p3 = new Image();
p3.src = "3.gif";
p4 = new Image();
p4.src = "4.gif";
p5 = new Image();
p5.src = "5.gif";
p6 = new Image();
p6.src = "6.gif";
o1 = new Image();
o1.src = "dropcard.gif";
o2 = new Image();
o2.src = "holdcard.gif";
blank = new Image();
blank.src = "blank.gif";

function makearray(n) {
        this.length = n;
        for(var i = 1; i <= n; i++)
        this[i] = 0;
    return this;
}

function over(n) {
	mover = n;
}

function sum(n) {
	var sm = 0
	if (n == 1) {
		for(x = 1; x <= 14; x++)
		sm = sm + scores[x];
	}
	if (n == 2) {
		for(x = 1; x <= 6; x++) {
			if (scores[x] > 0) {
				sm = sm + scores[x];
			}
		}
	}
	if (n == 3) {
		for(x = 1; x <= 5; x++)
			sm = sm + dice[x];
	}
	return sm;
}

function restart() {
	for(x = 1; x <= 12; x++) {
		document.score.elements[x * 2 - 1].value = "";
	}
	document.score.elements[26].value = "";
	document.score.totalscore.value = "";
	document.score.subscore.value = "";
	rolls = 3;
	mover = 0;
	cnt = 0;
	totalscored = 0;
	for(x = 1; x <= 14; x++)
	scores[x] = 0
	for(x = 1; x <= 5; x++) {
		dice[x] = 0;
	}
	for(x = 0; x <= 4; x++) {
		document.images[x + 3].src = blank.src;
	}
	for(x = 5; x <= 9; x++) {
		document.images[x + 3].src = o1.src;
	}
}

function endofgame() {
	bonus = ""
	if (sum(2) >= 63) {
		scores[14] = scores[14] + 35;
		document.score.totalscore.value = sum(1);
		bonus = " with 35 bonus points"
	}
	playagain = window.confirm("Game over\r You scored: " + sum(1) + bonus + "\r Play again?");
	if (playagain) {
		restart();
	}
}

function instruct() {
	instructions = window.open("yahtzeerules.htm", "instruct", "scrollbars=yes,resizable=yes,height=400,width=550");
}

function sort() {
	for(x = 1; x <= 5; x++)
		dice[x] = Math.abs(dice[x])
	olddice = new makearray(5);
	for(x = 1; x <= 5; x++)
		olddice[x] = dice[x];
	temp = new makearray(5);
	tempval = new makearray(5);
	for(x = 1; x <= 5; x++)
		tempval[x] = 7;
	for(x = 1; x <= 5; x++) {
		for(i = 1; i <= 5; i++) {
			if (dice[i] < tempval[x]) {
				temp[x] = i;
				tempval[x] = dice[i];
			}
		}
	dice[temp[x]] = 8;
	}
	for(x = 1; x <= 5; x++)
		dice[x] = olddice[temp[x]];
	}

function rolla() {
	for(i = 0; i <= 4; i++) {
		if (dice[i + 1] > -1) {
			dice[i + 1] = Math.round(Math.random() * 5) + 1;
			document.images[i + 3].src = eval("p" + dice[i + 1] + ".src");
		}
	}
	cnt = cnt + 1
	animate()
}

function animate() {
	if (cnt != 10) {
		timer = setTimeout("rolla()", 5);
	} else {
		clearTimeout(timer);
	rollb();
	}
}

function roll() {

document.dice.roller.disabled=true;
setTimeout('document.dice.roller.disabled=false;',2000);

	// banner handler
	var lbd = document.getElementById('leaderbd');
	lbd.src = lbd.src;
	var pzn = document.getElementById('pzone');
	pzn.src = pzn.src;
	var ssky = document.getElementById('shrtsky');
	ssky.src = ssky.src;
	var tsky_1 = document.getElementById('tsky1');
	tsky_1.src = tsky_1.src;
	var tsky_2 = document.getElementById('tsky2');
	tsky_2.src = tsky_2.src;
	var cb2 = document.getElementById('cube1');
	cb2.src = cb2.src;


// play the rollong sound
playSound1();

	if (rolls > 0 && rolls < 4) {
		rolls--;
		document.dice.rolls.value = rolls;

		// removed option for animation on or off - 1-2003 by WLDM
		//if (document.options.anm[0].checked == false) {
			animate();
		//} else {
		//	rollb();
		//}
	}
}

function rollb() {
	cnt = 0;
	for(var i = 0; i <= 4; i++) {
		if (dice[i + 1] > -1) {
			dice[i + 1] = Math.round(Math.random() * 5) + 1;
			document.images[i + 3].src = eval("p" + dice[i + 1] + ".src");
		}
	}
}

function hold() {

//refresh IFRAME after 2 clicks
clickcounter++;
if ( clickcounter==2) {
  document.ADFRAME.location.href="http://www.stupidstuff.org/com/sjsheader.htm";
clickcounter = 0;
}

	if (dice[mover] < 0) {
		document.images[mover + 7].src = o1.src;
	}
	if (dice[mover] > 0) {
		document.images[mover + 7].src = o2.src;
	}
	dice[mover] = -dice[mover];
}

function scr(n) {
	if ((scores[n] == 0 && rolls != 3) || (n == 13 && rolls != 3 && scores[n] != -1)) {
		sort();
		rolls = 3;
		totalscored++

		if (n <= 6) {
			for(x = 1; x <= 5; x++) {
				if (dice[x] == n) {
				scores[n] = scores[n] + n;
				}
			}
		}
	
		if (n == 7) {
			for(x = 0; x <= 2; x++) {
				if (dice[1 + x] == dice[2 + x] && dice[2 + x] == dice[3 + x]) {
					scores[n] = sum(3);
				}
			}
		}
	
		if (n == 8) {
			for(x = 0; x <= 1; x++) {
				if (dice[1 + x] == dice[2 + x] && dice[2 + x] == dice[3 + x] && dice[3 + x] == dice[4 + x]) {
					scores[n] = sum(3);
				}
			}
		}
	
		if (n == 9) {
			for(x = 1; x <= 4; x++) {
				if (dice[x] == dice[x + 1]) {
					for(i = x; i <= 4; i++) {
						dice[i] = dice[i + 1];
					}
				}
			}
			for(x = 0; x <= 1; x++) {
				if (dice[1 + x] + 1 == dice[2 + x] && dice[2 + x] + 1 == dice[3 + x] && dice[3 + x] + 1 == dice[4 + x]) {
					scores[n] = 30;
				}
			}
		}
		
		if (n == 10) {
			if (dice[1] + 1 == dice[2] && dice[2] + 1 == dice[3] && dice[3] + 1 == dice[4] && dice[4] + 1 == dice[5]) {
				scores[n] = 40;
			}
		}
		
		if (n == 11) {
			if (dice[1] == dice[2] && dice[2] == dice[3] && dice[4] == dice[5]) {
				scores[n] = 25;
			} 
			if (dice[3] == dice[4] && dice[4] == dice[5] && dice[1] == dice[2]) {
				scores[n] = 25;
			} 
		}
	
		if (n == 12) {
			scores[n] = sum(3);
		}
	
		if (n == 13 && rolls != 4) {
			if (dice[1] == dice[2] && dice[2] == dice[3] && dice[3] == dice[4] && dice[4] == dice[5]) {
				if (scores[n] >= 50) {
					scores[n] = 100 + scores[n];
					totalscored--;
					rolls = 5;
				} else {
					scores[n] = 50;
				}
			}
		}
	
		if (n <= 12) {
			document.score.elements[(n * 2) - 1].value = scores[n];
		} else {
			document.score.elements[26].value = scores[13];
	}
	document.score.totalscore.value = sum(1);
	document.score.subscore.value = sum(2);
	if (scores[n] == 0) {
		scores[n] = -1
		scores[14] = scores[14] + 1
	}
	for(x = 5; x <= 9; x++)
		document.images[x + 3].src = o1.src;
	}
	if (totalscored == 13) {
		endofgame()
	}
}	