var _cluster = false; 

function add(ch) { 
	// ch: string, the text to be added
	// _cluster: boolean, global variable, set if this is a consonant cluster (used for vowels that surround base)
	// _view: string, indicates which view is showing - this is important, since non-intelligent ordering is needed in the default view
	
	if (document.getElementById('output').style.display == 'none') { return; }
	var outputNode = document.getElementById( 'output' ); // points to the output textarea

	// check for left-positioned glyph
	var leftglyph = ch.match(/[\u0EC0\u0EC1\u0EC4\u0EC2\u0EC3]/);

	//IE support
	if (document.selection) { 
		outputNode.focus();
	    range = document.selection.createRange();
		
		if (leftglyph != null && outputNode.value != ''  && _view == 'phonic') { // wrap previous character with vowels
			range.moveStart('character', -1);  // need  to figure out how to prevent this if at beginning of the text area
			if (range.text.match(/[\u0EC8\u0EC9\u0ECA\u0ECB]/) != null && outputNode.value.length > 0) {
				range.moveStart('character', -1);
				}
			if (_cluster &&  outputNode.value.length > 1) {
				range.moveStart('character', -1);
				}
			var initial = ch.charAt(0); 
			var rest = ch.substr(1);

			ch = initial+range.text+rest;
			}

		range.text = ch; 
	    range.select(); 
		outputNode.focus();
		}
	// Mozilla and Safari support
	else if (outputNode.selectionStart || outputNode.selectionStart == '0') {
		var startPos = outputNode.selectionStart;
		var endPos = outputNode.selectionEnd;
		var cursorPos = startPos;
		var scrollTop = outputNode.scrollTop;
		var baselength = 0;
		
		if (leftglyph != null && startPos > 0  && _view == 'phonic') {  // if there's a left-side glyph and at least one character before highlight
			startPos--; cursorPos--; baselength++;
			// check for tone mark, and if present move back again
			if (outputNode.value.substr(startPos, 1).match(/[\u0EC8\u0EC9\u0ECA\u0ECB]/) && startPos > 0) {
				startPos--; cursorPos--; baselength++; 
				}
			// check for a cluster and if so move startPos back again
			if (_cluster && startPos > 0) { 
				startPos--; cursorPos--; baselength++; 
				}
			var initial = ch.charAt(0); 
			var rest = ch.substr(1);
			ch = initial+outputNode.value.substr(startPos, baselength)+rest;
			}

		outputNode.value = outputNode.value.substring(0, startPos)
              + ch
              + outputNode.value.substring(endPos, outputNode.value.length);
		cursorPos += ch.length;

		outputNode.focus();
		outputNode.selectionStart = cursorPos;
		outputNode.selectionEnd = cursorPos;
		outputNode.scrollTop = scrollTop;
		}
	else {
		outputNode.value += ch;
		outputNode.focus();
		}
		
	// normalize
	// swap the tone mark with any combining character if in wrong order
	var pairs = outputNode.value.match(/[\u0EC8\u0EC9\u0ECA\u0ECB][\u0EB1\u0ECD\u0ECC\u0EB4\u0EB5\u0EB6\u0EB7\u0EB8\u0EB9\u0EBB]/g);
	if (pairs != null) {
		for (var i=0; i<pairs.length; i++) { 
			outputNode.value = outputNode.value.replace(pairs[i], pairs[i].charAt(1)+pairs[i].charAt(0));
			}
		}

	if (! ch.match(/[\u0EC8\u0EC9\u0ECA\u0ECB]/))	{ 
		_cluster = false; 
		document.getElementById('clusterindicator').src = "images/clusteroff.png";
		}

	// normalize for any other characters
	if (_n11n=='nfc') { outputNode.value = nfc(outputNode.value); }
	else if (_n11n=='nfd') { outputNode.value = nfd(outputNode.value);}
	}

function setcluster () {
	image = document.getElementById('clusterindicator');
	if (_cluster == false) {
		image.src = 'images/clusteron.png';
		_cluster = true;
		}
	else {
		image.src = 'images/clusteroff.png';
		_cluster = false;
		}
	}

function localInitialise () {
	}