/*vumaps.js [optional]
* this file is part of the VIETUNI typing tool 
* by Tran Anh Tuan [tuan@physik.hu-berlin.de]
* Copyright (c) 2001, 2002 AVYS e.V.. All Rights Reserved.
*/


if (typeof(initCharMap) != 'undefined') {
  initCharMap = selectMap;
  if (theTyper) theTyper.charmap = initCharMap();
  vumaps = 1;
}

function selectMap(id) {
  var map = id? id: charmapid;
  switch (map) {
     case 1: return new CVietUniCodeMap();
     case 2: return new CVietCombUCMap();
     case 3: return new CVietUTF8Map();
     case 4: return new CVietEscUCMap();
     case 5: return new CVietVniMap();
     case 6: return new CVietTCVNMap();
     case 7: return new CVietVISCIIMap();
     case 8: return new CVietVPSMap();
     case 9: return new CVietViqrMap();
     default: return new CVietUniCodeMap();
  }
}

function detectFormat (txt, alrt) {
  var id = detectEncoding (txt);
  if (!alrt) return id;
  var encname= ["Unicode", "Unicode to^? ho+.p", "UTF-8", "Unicode, HTML-encoded",
      "VNI-Win", "TCVN3 (ABC)", "VISCII", "VPS-Win", "VIQR (VietNet)" ];
  if (!id) alert("Xin lo^~i, VIETUNI kho^ng tha`nh co^ng...");
  else alert("Ba?ng ma~ ddu+o+.c du`ng trong va(n ba?n tre^n la`: "+ encname[id-1]);
  return id;
}

function detectEncoding (txt) {
//  my $txt = shift;

  if(/[\s\-"\(]®/g.test(txt)&&!(/[\S]®/g.test(txt))){return 6;}
  if(/[\s\-"\(]ð/g.test(txt)&&!(/[\S]ð/g.test(txt))){return 7;}
  if(/[\s\-"\(]Ç/g.test(txt)&&!(/[\S]Ç/g.test(txt))){return 8;}
  if(/(áº|á»)[¥¤§¦¬©¨«ª¯®±°·¶³²º½¼¾¿¡£¢]/gi.test(txt)){return 3;}
  if(/­[¬íêîëì]|hoÆc|o[¸µ¹¶·]/gi.test(txt)){return 6; } 
  if(/[öô][ùøïûõ]|oa[ëùøïûõ]|ñ[aoeuôö][äàáåãùøïûõ]/gi.test(txt)){return 5;}
  if(/ß[½¾¶þ·Þ]|ho£c|oÕ/gi.test(txt)){return 7;} 
  if(/Ü[Ö§©®ª«]|ho¥c|oå/gi.test(txt)){return 8;} 
  if(/u[\+\*]o[\+\*]|dda[\(\^~'`]|[aoe]\^[~`'\.\?]|[uo]\+[`'~\.\?]|a\(['`~\.\?]/gi.test(txt)){return 9;}
  var vs= String.fromCharCode(7840), ve= String.fromCharCode(7929);
  var regexp= new RegExp("["+vs+"-"+ve+"]",'g')
  if (regexp.test(txt)) {return 1;}
  vs= String.fromCharCode(768)+ String.fromCharCode(769)+String.fromCharCode(771);
  vs+= String.fromCharCode(777)+ String.fromCharCode(803);
  regexp= new RegExp("["+vs+"]",'g');
  if(regexp.test(txt)) {return 2;}
  if(/&#\d+;/gi.test(txt)) {return 4;}

//## return 0 for "unkhown" if it really could pass the bs-check
  return 0; 
}


///////////////////////////

function CVietTCVNMap() { var map = new CVietCharMap();
map.vietchars = new Array(
 "TCVN-3",
 97, 169, 168, 101, 170, 105, 111, 171, 172, 117, 173, 121, 
 65, 162, 161, 69, 163, 73, 79, 164, 165, 85, 166, 89,
 184, 202, 190, 208, 213, 221, 227, 232, 237, 243, 248, 253,
 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 181, 199, 187, 204, 210, 215, 223, 229, 234, 239, 245, 250,
 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 185, 203, 198, 209, 214, 222, 228, 233, 238, 244, 249, 254,
 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 182, 200, 188, 206, 211, 216, 225, 230, 235, 241, 246, 251,
 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 183, 201, 189, 207, 212, 220, 226, 231, 236, 242, 247, 252,
 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 100, 174, 68, 167);
return map;
}

function CVietVISCIIMap() { var map = new CVietCharMap();
map.vietchars = new Array(
"VISCII",
97, 226, 229, 101, 234, 105, 111, 244, 189, 117, 223, 121,
65, 194, 197, 69, 202, 73, 79, 212, 180, 85, 191, 89,
225, 164, 237, 233, 170, 237, 243, 175, 190, 250, 209, 253,
193, 8222, 129, 201, 352, 205, 211, 143, 8226, 218, 186, 221,
224, 165, 162, 232, 171, 236, 242, 176, 182, 249, 215, 207,
192, 8230, 8218, 200, 8249, 204, 210, 144, 8211, 217, 187, 376,
213, 167, 163, 169, 174, 184, 247, 181, 254, 248, 241, 220,
8364, 8225, 402, 8240, 381, 732, 353, 8220, 8221, 382, 185, 0,
228, 166, 198, 235, 172, 239, 246, 177, 183, 252, 216, 214,
196, 8224, 0, 203, 338, 8250, 8482, 8216, 8212, 339, 188, 0,
227, 231, 199, 168, 173, 238, 245, 178, 222, 251, 230, 219,
195, 0, 0, 710, 141, 206, 0, 8217, 179, 157, 255, 0,
100, 240, 68, 208);
return map;
}


function CVietVPSMap() { var map = new CVietCharMap();
map.vietchars = new Array(
"VPS-Win",
97, 226, 230, 101, 234, 105, 111, 244, 214, 117, 220, 121,
65, 194, 710, 69, 202, 73, 79, 212, 247, 85, 208, 89,
225, 195, 161, 233, 8240, 237, 243, 211, 167, 250, 217, 353,
193, 402, 141, 201, 144, 180, 185, 8211, 157, 218, 173, 221,
224, 192, 162, 232, 352, 236, 242, 210, 169, 249, 216, 255,
0, 8222, 0, 215, 8220, 181, 188, 8212, 0, 168, 175, 178,
229, 198, 165, 203, 338, 206, 8224, 182, 174, 248, 191, 339,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
228, 196, 163, 200, 8249, 204, 213, 176, 170, 251, 186, 8250,
129, 8230, 0, 222, 8221, 183, 189, 732, 376, 209, 177, 0,
227, 197, 164, 235, 205, 239, 245, 8225, 171, 219, 187, 207,
8218, 0, 0, 254, 8226, 184, 0, 8482, 166, 172, 0, 0,
100, 199, 68, 241);
return map;
}


function CVietMultibyteMap(superior) { 
var map= superior? superior: new CVietCharMap();

map.maxchrlen = 3;

map.charAt = function (ind) { 
  return this.vietchars[ind];
}

// return an array of num vietchars with variable length: [[chr,len][]..]
// extracted from str in reversed order
//
map.lastCharsOf = function (str, num) {
  var vchar = null;
  var i= this.maxchrlen;
  var mystr = str;
  while (!vchar && (i > 1)) {
    var c = mystr.substring(mystr.length-i);
    if (this.indexOf(c)) vchar=[c, i]; 
    --i;
  }
  if (!vchar) vchar=[mystr.charAt(mystr.length-1), 1]; 
  if (!num) return vchar;
  var vchars = new Array(num);
  vchars[0]= vchar; 
  for ( i=1; i< num; i++) {
    mystr= mystr.substring(0,mystr.length-vchars[i-1][1]);  
    vchars[i]= this.lastCharsOf(mystr);
  }
  return vchars;
}

return map;
}

function CVietVniMap() { var map = new CVietMultibyteMap();
map.maxchrlen = 2;
map.vietchars = new Array(
"VNI-WIN",
"a", "aâ", "aê", "e", "eâ", "i", "o", "oâ", "ô", "u", "ö", "y",
"A", "AÂ", "AÊ", "E", "EÂ", "I", "O", "OÂ", "Ô", "U", "Ö", "Y",
"aù", "aá", "aé", "eù", "eá", "í", "où", "oá", "ôù", "uù", "öù", "yù", 
"AÙ", "AÁ", "AÉ", "EÙ", "EÁ", "Í", "OÙ", "OÁ", "ÔÙ", "UÙ", "ÖÙ", "YÙ", 
"aø", "aà", "aè", "eø", "eà", "ì", "oø", "oà", "ôø", "uø", "öø", "yø", 
"AØ", "AÀ", "AÈ", "EØ", "EÀ", "Ì", "OØ", "OÀ", "ÔØ", "UØ", "ÖØ", "YØ", 
"aï", "aä", "aë", "eï", "eä", "ò", "oï", "oä", "ôï", "uï", "öï", "î", 
"AÏ", "AÄ", "AË", "EÏ", "EÄ", "Ò", "OÏ", "OÄ", "ÔÏ", "UÏ", "ÖÏ", "Î", 
"aû", "aå", "aú", "eû", "eå", "æ", "oû", "oå", "ôû", "uû", "öû", "yû", 
"AÛ", "AÅ", "AÚ", "EÛ", "EÅ", "Æ", "OÛ", "OÅ", "ÔÛ", "UÛ", "ÖÛ", "YÛ", 
"aõ", "aã", "aü", "eõ", "eã", "ó", "oõ", "oã", "ôõ", "uõ", "öõ", "yõ", 
"AÕ", "AÃ", "AÜ", "EÕ", "EÃ", "Ó", "OÕ", "OÃ", "ÔÕ", "UÕ", "ÖÕ", "YÕ",
"d", "ñ", "D", "Ñ");
return map;
}

function CVietViqrMap() { var map = new CVietMultibyteMap();
map.vietchars = new Array(
 "VIQR",
 "a", "a^", "a(", "e", "e^", "i", "o", "o^", "o+", "u", "u+", "y",
 "A", "A^", "A(", "E", "E^", "I", "O", "O^", "O+", "U", "U+", "Y",
 "a'", "a^'", "a('", "e'", "e^'", "i'", "o'", "o^'", "o+'", "u'", "u+'", "y'", 
 "A'", "A^'", "A('", "E'", "E^'", "I'", "O'", "O^'", "O+'", "U'", "U+'", "Y'", 
 "a`", "a^`", "a(`", "e`", "e^`", "i`", "o`", "o^`", "o+`", "u`", "u+`", "y`", 
 "A`", "A^`", "A(`", "E`", "E^`", "I`", "O`", "O^`", "O+`", "U`", "U+`", "Y`", 
 "a.", "a^.", "a(.", "e.", "e^.", "i.", "o.", "o^.", "o+.", "u.", "u+.", "y.", 
 "A.", "A^.", "A(.", "E.", "E^.", "I.", "O.", "O^.", "O+.", "U.", "U+.", "Y.", 
 "a?", "a^?", "a(?", "e?", "e^?", "i?", "o?", "o^?", "o+?", "u?", "u+?", "y?", 
 "A?", "A^?", "A(?", "E?", "E^?", "I?", "O?", "O^?", "O+?", "U?", "U+?", "Y?", 
 "a~", "a^~", "a(~", "e~", "e^~", "i~", "o~", "o^~", "o+~", "u~", "u+~", "y~", 
 "A~", "A^~", "A(~", "E~", "E^~", "I~", "O~", "O^~", "O+~", "U~", "U+~", "Y~",
 "d", "dd", "D", "DD");

map.regExpAt = function(ind, isD) {
  var c=this.charAt(ind);
  if (!c) return null;
  c = c.replace(/\+/g, "[\\+\\*]");
  c = c.replace(/'/g, "['´]");
  c = c.replace(/([\-\?\.\(\^])/g, "\\$1");
  return new RegExp(c,'g');
}

return map;
}


// Unicode Transformation Format - 8b
//
function CVietUTF8Map() { var map= new CVietMultibyteMap(new CVietUniCodeMap());
for (var i = 1; i < map.length; i++ ) {
  var c = map.vietchars[i], utf;
  if (c < 128) {
    utf = String.fromCharCode(c);
  }
  else if (c < 2048) {
    utf = String.fromCharCode(192 | c >> 6 );
    utf += String.fromCharCode(128 | c & 63);
  }
  else if (c < 65536) {
    utf = String.fromCharCode(224 | c >> 12);
    utf += String.fromCharCode(128 | c >> 6 & 63);
    utf += String.fromCharCode(128 | c & 63);
  }
  map.vietchars[i] = utf;
}
return map;
}

// HTML-Encoded Unicode Format
//
function CVietEscUCMap() { var map= new CVietMultibyteMap(new CVietUniCodeMap());
  map.maxchrlen = 7;
  for (var i = 1; i < map.length; i++ ) {
    var c = map.vietchars[i];
    if (c < 256) map.vietchars[i] = String.fromCharCode(c);
    else map.vietchars[i] = "&#"+ c +';';
  }
  return map;
}

// Combined Unicode (somewhat like VNI): character + ton mark
//
function CVietCombUCMap() { var map = new CVietMultibyteMap(new CVietUniCodeMap());
  map.maxchrlen = 2;
  var viettm = new Array("UNICODE-C", 769, 768, 803, 777, 771); // ('`.?~)

  for (var i = 1; i < map.length-4; i++ ) {
    var i_char = (i-1)%24;
    var i_tm = (i - i_char - 1)/24;
    var base_c = map.vietchars[i_char+1];
    if (i<25) base_c = String.fromCharCode(base_c);
    var tonmark = String.fromCharCode(viettm[i_tm]);
    map.vietchars[i] = i_tm? base_c + tonmark : base_c;
  }
  for (var i = map.length-4; i < map.length; i++ ) {
    map.vietchars[i] = String.fromCharCode(map.vietchars[i]);
  }
  return map;
}
