﻿function transcribe (node, direction) { 
	var chstring=''; // the text to be converted
	
	// get the highlighted text, or default to all text
	//IE support
	if (document.selection) { 
		//outputNode.focus();
	    chstring = document.selection.createRange().text;
		}
	// Mozilla and Safari support
	else if (node.selectionStart || node.selectionStart == '0') {
		chstring = node.value.substring(node.selectionStart, node.selectionEnd);
		}

	// add a space to avoid breaking on lookahead; if no selection, try whole field; if still nothing, abort
	chstring = chstring.toLowerCase()+' ';

if (chstring==' ') { chstring = node.value.toLowerCase()+' '; }
	if (chstring==' ') { return; }

	if (direction == 'tolatin') {
		output = maptolatn(chstring);
		}
	else { 
		chstring = normalise(chstring); 

		// build the output from the map array
    	var output = ''; var latin = ''; var ch;
		for (var i=0; i<chstring.length-1; i++) {
			latin = chstring.charAt(i);
		
			ch = map[latin];
			if (ch) { if (ch !='null') {output += ch;} }
			else { output += latin; }
			}
		}

	node.value = node.value+output;
	node.focus();
	}
	
	          