if (typeof reportFields != 'undefined') {
	var insertAtCursor = function(myField, myValue)
	{
		if (document.selection) {
			myField.focus();
			sel = document.selection.createRange();
			sel.text = myValue;
		}
		else if (myField.selectionStart || myField.selectionStart == '0') {
			var startPos = myField.selectionStart;
			var endPos = myField.selectionEnd;
			myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
		}
		else {
			myField.value += myValue;
		}
	}
	
	var insertText = function(o) {
		if (typeof reportFieldOptions[o.options[o.selectedIndex].value] != 'undefined') {
			insertAtCursor(document.getElementById(o.textareaId), reportFieldOptions[o.options[o.selectedIndex].value].text);
		}
		
		o.selectedIndex = 0;
	}
	
	var initializeTextareas = function() {
		var select;
		var option;
		
		for (var i in reportFields) {
			if (o = document.getElementById(i)) {
				select = document.createElement('select');
				select.onchange = function() {
					insertText(this);
					};
				select.textareaId = i;
				
				if (reportFields[i].length > 0) {
					option = document.createElement('option');
					option.value = -1;
					option.appendChild(document.createTextNode(''));
						
					select.appendChild(option);
				}
				
				for (var j in reportFields[i]) {
					if (typeof reportFieldOptions[reportFields[i][j]] != 'undefined') {
						option = document.createElement('option');
						option.value = reportFields[i][j];
						option.appendChild(document.createTextNode(reportFieldOptions[reportFields[i][j]].description));
						
						select.appendChild(option);
					}
				}
				
				o.parentNode.insertBefore(select, o);
				o.parentNode.insertBefore(document.createTextNode('Options: '), select);
			}
		}
	}
	
	initializeTextareas();
}