var _cluster = false; 

function localInitialise () {
	}

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(/[\u0E40\u0E41\u0E44\u0E42\u0E43]/);

	//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(/[\u0E48\u0E49\u0E4A\u0E4B]/) != 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(/[\u0E48\u0E49\u0E4A\u0E4B]/) && 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
	var pairs = outputNode.value.match(/[\u0E48\u0E49\u0E4A\u0E4B][\u0E31\u0E4D\u0E47\u0E4C\u0E4E\u0E38\u0E39\u0E34\u0E35\u0E36\u0E37\u0E3A]/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(/[\u0E48\u0E49\u0E4A\u0E4B]/))	{ 
		_cluster = false; 
		document.getElementById('clusterindicator').src = "images/clusteroff.png";
		}
	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;
		}
	}
