var agt=navigator.userAgent.toLowerCase()
var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1))
var obj_copy, method=1, on_off=1, changed=false
var typicalKeyCode = new Array(
97, 226, 259, 101, 234, 105, 111, 244, 417, 117, 432, 121,
65, 194, 258, 69, 202, 73, 79, 212, 416, 85, 431, 89);
function notWord(word) {
return ("\ \r\n#,\\;.:-_()<>+-*/=?!\"$%{}[]\'~|^\@\&".indexOf(word)>=0)
}
function start(obj, key) {
var uni
obj_copy=obj
if (method==4) uni=uni_auto
else if (method==1) uni=uni_telex
else if (method==2) uni=uni_vni
else if (method==3) uni=uni_viqr
if (!is_ie) {
var i=obj.selectionStart
uni(obj, obj.value.substr(i-3,1), i-2)
i=obj.selectionStart
uni(obj, obj.value.substr(i-2,1), i-1)
i=obj.selectionStart
uni(obj, obj.value.substr(i-1,1), i)
}
else {
var caret=obj.document.selection.createRange()
var word=""
while(1) {
caret.moveStart("character",-1)
if (word.length == caret.text.length) break
word=caret.text
if (notWord(word.charAt(0))) {
if (word.charCodeAt(0)==13) word=word.substr(2)
else word=word.substr(1)
break
}
}
if (word.length) {
caret.collapse(false)
caret.moveStart("character",-word.length)
obj.curword=caret.duplicate()
word=ie_telex(key, word)
obj.curword.text=word
if (changed) {
changed=false
window.event.keyCode = 0
}
}
}
}
function ie_telex(key, word) {
var upkey=key.toUpperCase()
if ('SFJRX'.indexOf(upkey)>=0) {
var return_word=typicalReplace(word, key, null)
if (return_word) return return_word
}
else if (upkey=='D') {
var searchFor=new Array('d', 'D')
pos=findCharToChange(word,key,null,searchFor)
if (pos) {
var charCode=(key=='d')?273:272
return ie_replaceChar(word, pos, charCode)
}
}
else if (upkey=='A') {
var searchFor=new Array('a', 'A')
pos=findCharToChange(word,key,null,searchFor)
if (pos) {
var charCode=(key=='a')?226:194
return ie_replaceChar(word, pos, charCode)
}
}
else if (upkey=='W') {
var charCode
var searchFor=new Array('a','A','o','O','u','U')
pos=findCharToChange(word,key,null,searchFor)
if (pos) {
var poschar=word.substr(word.length-pos,1).toUpperCase()
if (poschar=='A') charCode=(key=='w')?259:258
else if (poschar=='O') charCode=(key=='w')?417:416
else if (poschar=='U') charCode=(key=='w')?432:431
return ie_replaceChar(word, pos, charCode)
}
}
else if (upkey=='E') {
var searchFor=new Array('e','E')
pos=findCharToChange(word,key,null,searchFor)
if (pos) {
var charCode=(key=='e')?234:202
return ie_replaceChar(word, pos, charCode)
}
}
else if (upkey=='O') {
var searchFor=new Array('o','O')
pos=findCharToChange(word,key,null,searchFor)
if (pos) {
var charCode=(key=='o')?244:212
return ie_replaceChar(word, pos, charCode)
}
}
return word
}
function ie_replaceChar(word, pos, charCode) {
changed=true
return word.substr(0,word.length-pos)+String.fromCharCode(charCode)+word.substr(word.length-pos+1)
}
function uni_telex(obj, key, i) {
var pos
var upkey=key.toUpperCase()
if ('SFJRX'.indexOf(upkey)>=0) {
typicalReplace(obj, key, i)
}
else if (upkey=='D') {
var searchFor=new Array('d','D')
pos=findCharToChange(obj, key, i, searchFor)
if (pos) {
var charCode=(key=='d')?273:272
replaceChar(obj, i-pos, charCode, i)
}
}
else if (upkey=='A') {
var searchFor=new Array('a','A')
pos=findCharToChange(obj, key, i, searchFor)
if (pos) {
var charCode=(key=='a')?226:194
replaceChar(obj, i-pos, charCode, i)
}
}
else if (upkey=='W') {
var charCode
var searchFor=new Array('a','A','o','O','u','U')
pos=findCharToChange(obj, key, i, searchFor)
if (pos) {
var poschar=obj.value.substr(i-pos,1).toUpperCase()
if (poschar=='A') charCode=(key=='w')?259:258
else if (poschar=='O') charCode=(key=='w')?417:416
else if (poschar=='U') charCode=(key=='w')?432:431
replaceChar(obj, i-pos, charCode, i)
}
}
else if (upkey=='E') {
var searchFor=new Array('e','E')
pos=findCharToChange(obj, key, i, searchFor)
if (pos) {
var charCode=(key=='e')?234:202
replaceChar(obj, i-pos, charCode, i)
}
}
else if (upkey=='O') {
var searchFor=new Array('o','O')
pos=findCharToChange(obj, key, i, searchFor)
if (pos) {
var charCode=(key=='o')?244:212
replaceChar(obj, i-pos, charCode, i)
}
}
}
function findCharToChange(obj, key, i, searchFor) {
var g, str=""
for (g=0; g<searchFor.length; g++) {
str+=searchFor[g]
}
if (!is_ie) {
for (g=2; g<7; g++) {
if (i-g>=0) {
if (notWord(obj.value.substr(i-g, 1))) break
else if (str.indexOf(obj.value.substr(i-g, 1))>=0) return g
}
else break
}
}
else {
for (g=1; g<=obj.length; g++) {
if (str.indexOf(obj.substr(obj.length-g,1))>=0) return g
}
}
return false
}
function typicalReplace(obj, key, i) {
var searchFor=new Array()
for (var g=0; g<typicalKeyCode.length; g++) searchFor.push(String.fromCharCode(typicalKeyCode[g]))
pos=findCharToChange(obj, key, i, searchFor)
if (pos) {
var charCode=returnUniCode(obj, key, i, pos)
if (!is_ie) replaceChar(obj, i-pos, charCode, i)
else return ie_replaceChar(obj, pos, charCode)
}
return false
}
function returnUniCode(obj, key, i, pos) {
var uni_array=returnKeyCode(key.toUpperCase())
var charCode=(!is_ie)?(obj.value.charCodeAt(i-pos)):(obj.charCodeAt(obj.length-pos))
for (var g=0; g<typicalKeyCode.length; g++) if (typicalKeyCode[g]==charCode) return uni_array[g]
}
function returnKeyCode(key) {
if (key=='S') return new Array(225, 7845, 7855, 233, 7871, 237, 243, 7889, 7899, 250, 7913, 253,
193, 7844, 7854, 201, 7870, 205, 211, 7888, 7898, 218, 7912, 221);
else if (key=='F') return new Array(224, 7847, 7857, 232, 7873, 236, 242, 7891, 7901, 249, 7915, 7923,
192, 7846, 7856, 200, 7872, 204, 210, 7890, 7900, 217, 7914, 7922);
else if (key=='J') return new Array(7841, 7853, 7863, 7865, 7879, 7883, 7885, 7897, 7907, 7909, 7921, 7925,
7840, 7852, 7862, 7864, 7878, 7882, 7884, 7896, 7906, 7908, 7920, 7924);
else if (key=='R') return new Array(7843, 7849, 7859, 7867, 7875, 7881, 7887, 7893, 7903, 7911, 7917, 7927,
7842, 7848, 7858, 7866, 7874, 7880, 7886, 7892, 7902, 7910, 7916, 7926);
else if (key=='X') return new Array(227, 7851, 7861, 7869, 7877, 297, 245, 7895, 7905, 361, 7919, 7929,
195, 7850, 7860, 7868, 7876, 296, 213, 7894, 7904, 360, 7918, 7928);
}
function replaceChar(obj, pos, charCode, i) {
var oldlen=obj.value.length
var fix=obj.selectionStart
obj.setSelectionRange(pos, pos+1)
obj.value = obj.value.substr(0, obj.selectionStart) + String.fromCharCode(charCode) + obj.value.substr(obj.selectionEnd)
obj.setSelectionRange(i-1, i)
obj.value = obj.value.substr(0, obj.selectionStart) + obj.value.substr(obj.selectionEnd)
if (fix > i) obj.setSelectionRange(i+(fix-i)-1, i+(fix-i)-1)
else obj.setSelectionRange(i-(oldlen-obj.value.length), i-(oldlen-obj.value.length))
scrollIntoView(obj)
}
function scrollIntoView(obj) {
var factor = 36
if ((obj_copy.scrollHeight <= obj.scrollTop) || (obj_copy.scrollHeight >= obj.scrollTop + obj.offsetHeight))
obj.scrollTop = obj_copy.scrollHeight - obj_copy.offsetHeight + factor;
}
function statusMessage() {
var str='Mode: '
if(!on_off) str+='NONE'
else if(method==1) str+='TELEX'
else if(method==2) str+='VNI'
else if(method==3) str+='VIQR'
else if(method==4) str+='AUTO'
str+=" [F9=TypingMode; F12=On/Off]"
window.status=str
}
document.onkeydown = function(e) {
var key = (!is_ie) ? e.which : window.event.keyCode
if (key==120) {
on_off=true
if (method==4) method=1
else method++
}
else if (key==123) on_off=!on_off
statusMessage()
}
document.onkeyup = function(e) {
if ((!on_off) || (is_ie)) return
var el=e.target
if ((el.type != 'textarea') && (el.type != 'text')) return
start(el, null)
}
document.onkeypress = function() {
if (!is_ie) return
var el=window.event.srcElement
var code=window.event.keyCode
if (((el.type != 'textarea') && (el.type != 'text')) || (!on_off) || (code<47) || (code==145) || (code==255)) return
start(el, String.fromCharCode(window.event.keyCode))
}
statusMessage()