/*vietuni5.js
* by Tran Anh Tuan [tuan@physik.hu-berlin.de] - R. 26.04.01
* Copyright (c) 2001, 2002 AVYS e.V.. All Rights Reserved.
*
* Originally published and documented at http://www.avys.de/
* This code is free for you to use on non-commercial web sites.
* If you want to, keep this copyright notice intact to make friends with AVYS :-)
*/


// interface for HTML
//

function VNOff() { return 0; }

var disabled = false;
var charmapID = 0;
var typingMode = VNOff;
var theTyper = null;

// for HTML-call:
//------------------------

telexingVietUC = initTyper;

function setCharMap(mapID) { 
   charmapID = mapID; 
   if(theTyper) theTyper.charmap = initCharMap();
}

function setTypingMode(mode) {
  switch (mode) {
     case 1: typingMode = telexTyping; break;
     case 2: typingMode = vniTyping; break;
     case 3: typingMode = viqrTyping; break;
     default: typingMode = VNOff;
  }
  if (theTyper) theTyper.typing = typingMode;
  if (disabled) return true;
  if (!document.all && !document.getElementById) {
     errmsg = "Xin loi, trinh duyet web cua ban khong cho phep dung VietTyping.\n"
     errmsg += "Mac du vay ban van co the go~ chu Viet nhu binh thuong\n"
     errmsg += "roi bam vao nut \"chuyen ddoi\" khi dda~ xong.";
     alert(errmsg);
     disabled = true;  
  }
}

function convertAtOnce(txtarea) {
  if(!txtarea) return;
  if(!theTyper) theTyper = new vietString("");
  txtarea.value = theTyper.doConvertIt(txtarea.value);
}
// end from HTML


function initCharMap() { 
  switch (charmapID) {
     case 0: return new VietUniCodeMap();
     case 1: return new VietVniMap();
     case 2: return new VietTCVNMap();
     case 3: return new VietVISCIIMap();
     case 4: return new VietVPSMap();
     default: return new VietUniCodeMap();
  }
}

function initTyper(txtarea,evt) {
  if (document.all) telexingVietUC = ieTyping; 
  else if (document.getElementById) telexingVietUC = ns6Typing;
  else telexingVietUC = VNOff;
}

// event & typing:
//------------------------

function ieTyping(txtarea,evt) {
  if(!txtarea) return; 
  var c = event.keyCode;
  var curword= getCurrentWord(txtarea);
  if(!theTyper) theTyper = new vietString(curword);
  else theTyper.setValue(curword);
  if (c < 49) {
     if (theTyper.onEndWord(curword)) replaceWord(txtarea, theTyper.value);
     return;
  }
  if (theTyper.typing()) replaceWord(txtarea, theTyper.value);
}

function ns6Typing(txtarea,evt) {
  if(!txtarea) return; 
  var c = evt ? evt.keyCode: 50;
  if(!theTyper) theTyper = new vietString(txtarea.value);
  else theTyper.setValue(txtarea.value);
  if (c < 49) {
     if (theTyper.onEndWord(txtarea.value)) txtarea.value = theTyper.value; 
     return;
  }
  if (theTyper.typing()) txtarea.value = theTyper.value;
}


// for IE4+,5+: 
function getCurrentWord(txtarea) {
  var caret = document.selection.createRange();
  var backward = -6;
  do {
     var caret2 = caret.duplicate();
     caret2.moveStart("character", backward++);    
  } while (caret2.parentElement() != txtarea && backward <0);
  txtarea.curword = caret2.duplicate(); 
  return caret2.text;
}

function replaceWord(txtarea, newword) {
  txtarea.curword.text = newword;
  txtarea.curword.collapse(false);
}
// end IE-special

///////////////////////////////////////////
// end interface



// vietString Obj
// 
function vietString(str) {
  var i = str.length-1;
  while((i>=0) && (str.charCodeAt(i) <= 32)) --i;
  var tmp =  new String(str);
  if(i>=0) tmp = tmp.substring(0,i+1);

  this.value = new String(tmp);
  this.charmap = initCharMap();
  this.changed = 0;

  this.setValue = setValue;
  this.typing = typingMode;
  this.telexDau = telexDau;
  this.telexAEOWD = telexAEOWD;
  this.onEndWord = onEndWord;
  this.findCharToChange = findCharToChange;
  this.do                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
