function fcp_dice_roller(container_id) {
	var container = document.getElementById(container_id);
	if (!container) return;

	var fieldset = document.createElement('fieldset');
	container.appendChild(fieldset);
	fieldset.style.textAlign = 'center';
	fieldset.style.width = '12em';
	var legend = document.createElement('legend');
	fieldset.appendChild(legend);
	legend.innerHTML = 'FCP Dice Roller';

	var dice_field = document.createElement('input');
	fieldset.appendChild(dice_field);
	dice_field.style.width = '7em';
	dice_field.value = '3d6';
	dice_field.title = 'Dice to roll';

	var times_label = document.createElement('label');
	fieldset.appendChild(times_label);
	times_label.innerHTML = '&nbsp;x&nbsp;';

	var times_field = document.createElement('input');
	fieldset.appendChild(times_field);
	times_field.style.width = '3em';
	times_field.value = '6';
	times_field.title = 'Times to roll';

	var br1 = document.createElement('br');
	fieldset.appendChild(br1);

	var link_to_wikipedia = document.createElement('a');
	fieldset.appendChild(link_to_wikipedia);
	link_to_wikipedia.href = 'http://en.wikipedia.org/wiki/Dice_notation';
	link_to_wikipedia.innerHTML = 'Dice notation';

	var br2 = document.createElement('br');
	fieldset.appendChild(br2);

	var roll_button = document.createElement('button');
	fieldset.appendChild(roll_button);
	roll_button.innerHTML = 'Roll dice';
	//roll_button.style.width = '100%';

	var results = document.createElement('ul');
	fieldset.appendChild(results);
	results.style.textAlign = "left";

	var parse_dice = function (expr) {
		var matches;
		if (matches = expr.match(/^(\d+)d(\d+)$/)) {
			return {count: matches[1],
				faces: matches[2],
				multiplier: 1,
				modifier: 0};
		}
		if (matches = expr.match(/^(\d+)d(\d+)([+-])(\d+)$/)) {
			var modif = parseInt(matches[4]);
			if (matches[3] == '-') modif = -modif;
			return {count: matches[1],
				faces: matches[2],
				multiplier: 1,
				modifier: modif};
		}
		if (matches = expr.match(/^(\d+)d(\d+)[xX*](\d+)$/)) {
			var mult = parseInt(matches[3]);
			return {count: matches[1],
				faces: matches[2],
				multiplier: mult,
				modifier: 0};
		}
		var expr_mul_modif = /^(\d+)d(\d+)[xX*](\d+)([+-])(\d+)$/;
		if (matches = expr.match(expr_mul_modif)) {
			var mult = parseInt(matches[3]);
			var modif = parseInt(matches[5]);
			if (matches[4] == '-') modif = -modif;
			return {count: matches[1],
				faces: matches[2],
				multiplier: mult,
				modifier: modif};
		}

		return false;
	}

	var dice_roll = function(faces) {
		return Math.floor(Math.random() * faces + 1);
	}

	roll_button.onclick = function () {
		var dice = parse_dice(dice_field.value);
		if (!dice) {
			results.innerHTML = "<li>Bad dice expression</li>";
			return;
		}

		var times = parseInt(times_field.value);
		if (!times) times = 1;

		results.innerHTML = "";
		for (var i = 0; i < times; i++) {
			var result = 0;
			var partials = "";
			for (var j = 0; j < dice.count; j++) {
				var roll = dice_roll(dice.faces);
				result += roll;
				partials += (roll + " ");
			}
			result = result * dice.multiplier + dice.modifier;
			partials += " = " + result;
			results.innerHTML += ("<li>" + partials + "</li>\n");
		}
	}
}

