colors = {
	1: {'hex':'#000080', 'code':'|b'},
	2: {'hex':'#800000', 'code':'|r'},
	3: {'hex':'#008080', 'code':'|c'},
	4: {'hex':'#800080', 'code':'|m'},
	5: {'hex':'#808000', 'code':'|y'},
	6: {'hex':'#C0C0C0', 'code':'|w'},
	7: {'hex':'#00B300', 'code':'|g'},
	8: {'hex':'#808080', 'code':'|D'},
	
	9: {'hex':'#0000FF', 'code':'|B'},
	10:{'hex':'#FF0000', 'code':'|R'},
	11:{'hex':'#00FFFF', 'code':'|C'},
	12:{'hex':'#FF00FF', 'code':'|M'},
	13:{'hex':'#FFFF00', 'code':'|Y'},
	14:{'hex':'#FFFFFF', 'code':'|W'},
	15:{'hex':'#00FF00', 'code':'|G'}
};
limits = {
	'short':{'codes':10, 'min':5, 'max':60},
	'long':{'codes':10, 'min':5, 'max':120},
	'keyword':{'codes':999, 'min':5, 'max':120}
};
revColors = {
	'rgb(0, 0, 128)':'|b',
	'rgb(128, 0, 0)':'|r',
	'rgb(0, 128, 128)':'|c',
	'rgb(128, 0, 128)':'|m',
	'rgb(128, 128, 0)':'|y',
	'rgb(192, 192, 192)':'|w',
	'rgb(0, 179, 0)':'|g',
	'rgb(128, 128, 128)':'|D',
	
	'rgb(0, 0, 255)':'|B',
	'rgb(255, 0, 0)':'|R',
	'rgb(0, 255, 255)':'|C',
	'rgb(255, 0, 255)':'|M',
	'rgb(255, 255, 0)':'|Y',
	'rgb(255, 255, 255)':'|W',
	'rgb(0, 255, 0)':'|G',
	
	'#000080':'|b',
	'#800000':'|r',
	'#008080':'|c',
	'#800080':'|m',
	'#808000':'|y',
	'#c0c0c0':'|w',
	'#00b300':'|g',
	'#808080':'|D',
	
	'#0000ff':'|B',
	'#ff0000':'|R',
	'#00ffff':'|C',
	'#ff00ff':'|M',
	'#ffff00':'|Y',
	'#ffffff':'|W',
	'#00ff00':'|G'
};
last = 'It\'s JOHNNY!';
changed = false;
rte = null;
function init() {
	rte = document.getElementById('edit').contentWindow.document;
	rte.designMode = 'on';
	//window.setTimeout(function () {
		document.getElementById('edit').contentWindow.focus();
		setDefaults();
		rte.body.style.backgroundColor = '#000000';
		rte.body.style.border = 'none';
	//}, 100);
	document.getElementById('output').value = "";
	document.getElementById('slotShort').checked = true;
	setSlot(document.getElementById('slotShort'));
	timer = window.setInterval('processFrame();', 1000);
}
function setDefaults() {
	rte.execCommand('fontname', 0, 'Courier New');
	rte.execCommand('ForeColor', 0, '#c0c0c0');
	rte.execCommand('backcolor', 0, '#000000');
}
function setSlot(t) {
	cSlot = t.value;
	var e = document.getElementById('content').getElementsByTagName('label');
	for (var i = 0; i < e.length; i++) {
		if (e[i].htmlFor == t.id) {
			e[i].style.fontWeight = 'bold';
			document.getElementById(e[i].htmlFor + 'Desc').style.display = 'block';
		} else {
			e[i].style.fontWeight = 'normal';
			document.getElementById(e[i].htmlFor + 'Desc').style.display = 'none';
		}
	}
	hasChanged();
}
function hasChanged() {
	last = 'It\'s JOHNNY!';
}
function color(n) {
	rte.execCommand('forecolor', false, colors[n].hex);
	rte.execCommand('fontname', false, 'Courier New');
}
function undo() {
	rte.execCommand('undo', false, null);
	processFrame();
}
function redo() {
	rte.execCommand('redo', false, null);
	processFrame();
}
function HTML2MMcode(src, text){
	var modString = src;
	var stack = new Array();
	var token = "";
	var left = "";
	var tag = "";
	var out = "";
	var modString = modString.replace(/\|/g, '||');
	if (/<FONT/.test(src)) {
		var re = /^(.*?)(<\/*FONT.*?>)(.*)$/;
		var cre = /color="?(#[0-9A-Fa-f]{6})"?/;
		var open = /<FONT/;
		var end = /<\/FONT/;
	} else if (/<span/.test(src)) {
		var re = /^(.*?)(<\/*span.*?>)(.*)$/;
		var cre = /(rgb\(.+?\))/;
		var open = /<span/;
		var end = /<\/span/;
	} else if (src.replace(/<.*>/g, '').length == 0) {
		setDefaults();
		return '';
	} else {
		return "Error!";
	}

	modString = modString.replace(/<br>/, "");
	pointer = 0;
	stack[0] = '#c0c0c0';
	lastColour = '';
	//document.getElementById("out").innerHTML += (src + "\n\n").replace(/</g, "&lt;");

	while(modString.length> 0) {
		mat = re.exec(modString);
		left = mat[1];
		token = mat[2];
		modString = mat[3];
		//document.getElementById("out").innerHTML += '<pre>' + (pointer + ': ' + left + ' +++ ' + token + ' +++ ' + modString + ' /// ' + stack[pointer] + ' --- ' + lastColour + "\n\n").replace(/</g, '&lt;') + '</pre>';
		if (left.length > 0) {
			if (lastColour != stack[pointer] && !/^\s+$/.test(left)) {
				out += revColors[stack[pointer]];
				lastColour = stack[pointer];
			}
			out += left;
		}
		if (cre.test(token)) {
			stack[++pointer] = cre.exec(token)[1];
		} else if (end.test(token)) {
			--pointer;
		} else if (open.test(token)) {
			t = stack[pointer];
			stack[++pointer] = t;
		}
	}
	return out.replace(/^\|w/, '');
}
function processFrame() {
	var codes = 0;
	var length = 0;
	var errors = '';
	var bErrors = false;
	var notes = '';
	var itemName = document.getElementById('itemName').value;
	if (!rte.body) { return ''; }
	var text = rte.body.innerHTML;
	if (last != text) {
		last = text;
		u = HTML2MMcode(text);
		document.getElementById('output').value = u;

		codes = 0;
		var results = u.match(/\|./g);
		for (n in results) if (results[n] != "||") codes++;

		length = u.replace(/\|\|/g, "|").length - codes * 2;

		document.getElementById('codes').innerHTML = codes;
		document.getElementById('length').innerHTML = length;

		if (itemName.length == 0)
			errors += "<li>You didn't specify an item name!</li>";
		if (!/^\".+\"$/.test(itemName) && !/^\'.+\'$/.test(itemName) && itemName.indexOf(' ') > -1)
			errors += "<li>The item name must be either a quoted name (such as 'a brain in a vat') or a keyword without any spaces.</li>";
		if (/^[\'\"]/.test(itemName) && !/^\"[^\"]+\"$/.test(itemName) && !/^\'[^\']+\'$/.test(itemName))
			errors += "<li>For quoted item names, you must use the same quote character at the beginning and end and mustn't use the quote character you used within the name again. For example, 'an ogre's belt' is not right, while \"an ogre's belt\" is.</li>";

		if (errors.length == 0) {
			document.getElementById('e1').innerHTML = '';
		} else {
			bErrors = true;
			document.getElementById('e1').innerHTML = '<ul>' + errors + '</ul>';
		}

		errors = '';

		if (length < limits[cSlot]['min'])
			errors += "<li>Your text is " + (limits[cSlot]['min'] - length) + " characters too short!	The lower limit is " + limits[cSlot]['min'] + ".</li>";
		if (length > limits[cSlot]['max'])
			errors += "<li>Your text is " + (length - limits[cSlot]['max']) + " characters too long!	The upper limit is " + limits[cSlot]['max'] + ".</li>";
		if (codes > limits[cSlot]['codes'])
			errors += "<li>Your has " + (codes - limits[cSlot]['codes']) + " too many color codes!	The upper limit is " + limits[cSlot]['codes'] + ".</li>";
		if (/[@%\^`;&\*\$~]/.test(u) && cSlot == 'short')
			errors += "<li>You may not use the following characters in a short: $, ~, @, %, ^, `, ;, &, or *</li>";

		if (errors.length == 0) {
			document.getElementById('e3').innerHTML = '';
		} else {
			bErrors = true;
			document.getElementById('e3').innerHTML = '<ul>' + errors + '</ul>';
		}

		if (cSlot == 'keyword') {
			notes += '<li>Note: all color codes will be stripped, as you are using the keyword slot';
			u = u.replace(/\|./g, '');
		}

		if (notes.length == 0) {
			document.getElementById('n2').innerHTML = '';
		} else {
			document.getElementById('n2').innerHTML = '<ul>' + notes + '</ul>';
		}

		if (!bErrors) {
			document.getElementById('command').value = 'shop restring ' + document.getElementById('itemName').value + ' ' + cSlot + ' ' + u;
		} else {
			document.getElementById('command').value = 'Your command will be shown here once all the errors have been sorted out.';
		}
	}
	return true;
}
window.onload = init;