var agt=navigator.userAgent.toLowerCase();
var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var temp, method = 1
var obj_copy
var typicalSearch=new Array(
'a', 'aa', 'aw', 'e', 'ee', 'i', 'o', 'oo', 'ow', 'u', 'uw', 'y',
'A', 'AA', 'AW', 'E', 'EE', 'I', 'O', 'OO', 'OW', 'U', 'UW', 'Y');
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 start(obj) {
obj_copy=obj
i=obj.selectionStart
key=obj.value.substr(i-1, 1)
if (method == 0) //auto
{
uni_auto(obj, key, i)
}
else if (method == 1) //telex
{
uni_telex(obj, key, i, true)
}
else if (method == 2) //vni
{
uni_vni(obj, key, i)
}
else if (method == 3) //viqr
{
uni_viqr(obj, key, i)
}
}
function uni_telex(obj, key, i, first)
{
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)
}
}
if (first) {
uni_telex(obj, obj.value.substr(i-2,1), i-1, false)
}
}
function findCharToChange(obj, key, i, searchFor) {
var g, str=""
for (g=0; g<searchFor.length; g++) {
str+=searchFor[g]
}
for (g=2; g<7; g++) {
if (i-g>=0) {
if (obj.value.substr(i-g, 1)==" ") break
else if (str.indexOf(obj.value.substr(i-g, 1))>=0) return g
}
else break
}
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)
replaceChar(obj, i-pos, charCode, i)
}
}
function returnUniCode(obj, key, i, pos) {
var uni_array=returnKeyCode(key.toUpperCase())
for (var g=0; g<typicalKeyCode.length; g++) if (typicalKeyCode[g]==obj.value.charCodeAt(i-pos)) 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) {
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 (temp.length < obj.value.length) obj.setSelectionRange(i+(obj.value.length-temp.length), i+(obj.value.length-temp.length))
else obj.setSelectionRange(i, i)
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;
}
document.onkeydown = function(e) {
var el = (!is_ie) ? e.target : window.event.srcElement
temp = el.value
}
document.onkeyup = function(e) {
var el = (!is_ie) ? e.target : window.event.srcElement
start(el)
}