﻿// JavaScript Document


var phonetic1 = null;
var typewriter = null;
var phonetic2 = null;
var layouts = null;
var layoutBox = null;
var armFlag = null;
var textArea = null;
var isNumPadKey = false;

function handleKeyDown(){
textArea.onkeydown=function(e)
{
	var keyEvent=(window.event)?event : e;		
	var keyCode=keyEvent.keyCode;
	isNumPadKey = (keyCode>95 && keyCode<112) ? true : false;
	if(keyCode == 27){
		armFlag.checked = !armFlag.checked;
		armFlag.focus();     // not to delete already written text  in FireFox when Esc key is pressed.
		((keyEvent.target) ? keyEvent.target : keyEvent. srcElement).focus();
		return true;
	}
}
}


function handleKeyPress(){
textArea.onkeypress=function(e)
{	
	if(!armFlag.checked || isNumPadKey){
		return true;
	}

	if (e) {                                                            //Mozila & Opera
		if(e.altKey || e.ctrlKey) return true;
		if(e.which<32) return true;		
		textArea = e.target;
		var symb =  layouts[layoutBox.selectedIndex][ e.which];
		if('scrollTop' in textArea)
			var scrollTop=textArea.scrollTop;
		if(symb){
			var start=textArea.selectionStart;
			if(start != null){
				textArea.value=textArea.value.substring(0, start)+ symb +  textArea.value.substring(textArea.selectionEnd); 
				textArea.setSelectionRange(++start,start);
				if('scrollTop' in textArea)
					textArea.scrollTop=scrollTop;
				return false;
			}
		}
	}
	
	else{                                                      //IE
		var symb =  layouts[layoutBox.selectedIndex][window.event.keyCode];
		if(symb){
			window.event.keyCode =symb.charCodeAt(0);
		}
	}
}
}

function initLayouts(){

	phonetic1 = new Array(127);
	phonetic2 = new Array(127);
	typewriter = new Array(127);
	layouts =  new Array(3);

	layouts[0] = phonetic1;

phonetic1 [96] = '՝';
phonetic1 [49] = 'է';
phonetic1 [50] = 'թ';
phonetic1 [51] = 'փ';
phonetic1 [52] = 'ձ';
phonetic1 [53] = 'ջ';
phonetic1 [54] = ')';
phonetic1 [55] = 'և';
phonetic1 [56] = 'ր';
phonetic1 [57] = 'չ';
phonetic1 [48] = 'ճ';
phonetic1 [45] = '-';
phonetic1 [61] = 'ժ';
phonetic1 [113] = 'ք';
phonetic1 [119] = 'ո';
phonetic1 [101] = 'ե';
phonetic1 [114] = 'ռ';
phonetic1 [116] = 'տ';
phonetic1 [121] = 'ը';
phonetic1 [117] = 'ւ';
phonetic1 [105] = 'ի';
phonetic1 [111] = 'օ';
phonetic1 [112] = 'պ';
phonetic1 [91] = 'խ';
phonetic1 [93] = 'ծ';
phonetic1 [92] = 'շ';
phonetic1 [ 97] = 'ա';
phonetic1 [115] = 'ս';
phonetic1 [100] = 'դ';
phonetic1 [102] = 'ֆ';
phonetic1 [103] = 'գ';
phonetic1 [104] = 'հ';
phonetic1 [106] = 'յ';
phonetic1 [107] = 'կ';
phonetic1 [108] = 'լ';
phonetic1 [59] = ':';
phonetic1 [39] = '՛';
phonetic1 [122] = 'զ';
phonetic1 [120] = 'ղ';
phonetic1 [99] = 'ց';
phonetic1 [118] = 'վ';
phonetic1 [98] = 'բ';
phonetic1 [110] = 'ն';
phonetic1 [109] = 'մ';
phonetic1 [44] = ',';
phonetic1 [46] = '.';
phonetic1 [47] = '/';
phonetic1 [126] = '՜';
phonetic1 [33] = 'Է';
phonetic1 [64] = 'Թ';
phonetic1 [35] = 'Փ';
phonetic1 [36] = 'Ձ';
phonetic1 [37] = 'Ջ';
phonetic1 [94] = '(';
phonetic1 [38] = '%';
phonetic1 [42] = 'Ր';
phonetic1 [40] = 'Չ';
phonetic1 [41] = 'Ճ';
phonetic1 [95] = '–';
phonetic1 [43] = 'Ժ';
phonetic1 [81] = 'Ք';
phonetic1 [87] = 'Ո';
phonetic1 [69] = 'Ե';
phonetic1 [82] = 'Ռ';
phonetic1 [84] = 'Տ';
phonetic1 [89] = 'Ը';
phonetic1 [85] = 'Ւ';
phonetic1 [73] = 'Ի';
phonetic1 [79] = 'Օ';
phonetic1 [80] = 'Պ';
phonetic1 [123] = 'Խ';
phonetic1 [125] = 'Ծ';
phonetic1 [124] = 'Շ';
phonetic1 [65] = 'Ա';
phonetic1 [83] = 'Ս';
phonetic1 [68] = 'Դ';
phonetic1 [70] = 'Ֆ';
phonetic1 [71] = 'Գ';
phonetic1 [72] = 'Հ';
phonetic1 [74] = 'Յ';
phonetic1 [75] = 'Կ';
phonetic1 [76] = 'Լ';
phonetic1 [58] = '…';
phonetic1 [34] = '\"';
phonetic1 [90] = 'Զ';
phonetic1 [88] = 'Ղ';
phonetic1 [67] = 'Ց';
phonetic1 [86] = 'Վ';
phonetic1 [66] = 'Բ';
phonetic1 [78] = 'Ն';
phonetic1 [77] = 'Մ';
phonetic1 [60] = '«';
phonetic1 [62] = '»';
phonetic1 [63] = '՞';
}

function addTextarea(textareaId){
	if(layouts == null){
		armFlag = document.getElementById('armFlag');
		layoutBox = document.getElementById('layout');
		initLayouts();
	}
	textArea = document.getElementById(textareaId);
	handleKeyPress();
	handleKeyDown();
}