//accepts an ID reference to a textarea
//draws a formatted table containing rows for each line of code (uses ".code-snippet" classes)
//note: the "copy to clipboard" link is only drawn for IE (the clipboard function won't work by default in Mozilla due to tighter security settings)
//uses the utility functions below
function drawCodeTable(whichTextareaID) {
	var curTextArea = document.getElementById(whichTextareaID);
	var code = curTextArea.value;
	var lines = code.split("\n");
	
	openCodeTable(whichTextareaID);
	for (var i=0; i<lines.length; i++) {
		var curLine = lines[i];
		var curLineNum = i + 1;
		addCodeLine(curLineNum, curLine);
	}
	closeCodeTable();
}
function openCodeTable(whichTextareaID) {
	document.write('<DIV class=code-snippet><table class="dp-c" cellSpacing=0 cellPadding=0 border=1>');	
	document.write('<TR><TD class=tools-corner></TD>');
	if (window.clipboardData) {
		document.write('<TD class=tools><A onclick="copyToClipboard(\'' + whichTextareaID + '\'); return false;" href="#">copy to clipboard</A></TD></TR>');
	} else {
		document.write('<TD class=tools>&nbsp;</TD></TR>');
	}
}
function addCodeLine(whichLineNum, whichText) {
	whichText = whichText.replace(/[\f\n\r]/, '');
	whichText = whichText.replace(/\t/g, '&nbsp;&nbsp;&nbsp;&nbsp;');
	whichText = whichText.replace(/</g, '&lt;');
	whichText = whichText.replace(/>/g, '&gt;');
	if (whichText != '') {
		document.write('<tr><TD class="gutter">' + whichLineNum + '</TD><td class="line">' + whichText + '</td></tr>');
	}
}
function closeCodeTable(whichText) {
	document.write('</table></div>');
}

//accepts an ID reference to a textarea
//copies the contents of the textarea to the clipboard (IE only)
function copyToClipboard(whichTextareaID)
{
	var curTextArea = document.getElementById(whichTextareaID);
	var code = curTextArea.value;
	if (window.clipboardData) {
		window.clipboardData.setData('text', code);
		alert('The code has been copied to your clipboard.');
	}
}