function initVU (form) {

  for (var i=1; i<maps.length; i++) {
    var mstr = maps.names[i];
    var sel = (i==1);
    form.curmap.options[i-1] = new Option(mstr, mstr, sel, sel);
    form.destmap.options[i-1] = new Option(mstr, mstr, sel, sel);
  }

  setTypingMode(1);
  form.TELEX.onclick= function(){ setTypingMode(1); }
  form.VNI.onclick= function(){ setTypingMode(2); }
  form.VIQR.onclick= function(){ setTypingMode(3); }
  form.LOAN.onclick= function(){ setTypingMode(4); }
  form.OFF.onclick= function(){ setTypingMode(0); }

  form.onclick= function(){
    this.spella.disabled = !(this.TELEX.checked || this.LOAN.checked);
    return true;
  }
  form.checkAccent.onclick= function(){
    convertAtOnce(this.form.vArea);
    return true;
  }
  form.spella.onclick= function(){
    loadSpellA(this.checked);
    return true;
  }
  form.spellb.onclick= function(){
    loadSpellB(this.form.vArea);
    return true;
  }
  form.replace.onclick= function(){
    loadSpellB(this.form.vArea, 1);
    return true;
  }
  form.curmap.onclick= function() {
    setCharMap(this.options[this.options.selectedIndex].value);
    return true;
  }
  form.detectEnc.onclick= function(){
    var id= detectMap(this.form.vArea);
    if (id) this.form.curmap.options[id-1].selected=true; 
    return true;
  }
  form.convertTo.onclick= function() {  
    var sel = this.form.destmap.options.selectedIndex;
    var destID= this.form.destmap.options[sel].value;
    var converted= convertTo(this.form.vArea, destID);
    if (!converted) return true;
    this.form.curmap.options[sel].selected=true;
    setCharMap(destID);
    return true;
  }
  form.xoa.onclick= function() {
    this.form.reset();
    setTypingMode(1);
    setCharMap("UNICODE");
  }
  form.toUni.onclick= function() {
    if (!convertArea(this.form.vArea, "UNICODE")) {
      return alert("Sorry, VietUni kho^ng xa'c ddi.nh duoc ma dung trong ba`i");
    }
    this.form.curmap.options[0].selected=true;
    setCharMap("UNICODE");
    return true;
  }                                    
  form.correction.onclick= function() {
    correctArea(this.form.vArea);
    return true;
  }
  form.preview.onclick= function() {
    preview(this.form.vArea);
    return true;
  }
  form.save.onclick= function() {
    saveFile(this.form.vArea);
    return true;
  }

  return true;
}


function preview(txtarea) {
  var win = open("");
  if (!win || !txtarea) return;
  if (!txtarea.form.curmap.options[0].selected) { win.document.charset= "iso-8859-1"; }
  var closebutton= "<p><center><a href='javascript:close();'><b>[close]</b></a></center>"
  win.document.open();
  win.document.write(txtarea.value+ closebutton);
  win.document.close();
}

function saveFile(txtarea) {
  if (!document.all) {
    alert("Xin lo^~i, tri`nh duye^.t web cu?a ba.n kho^ng cho phe'p thu+.c hie^.n thao ta'c na`y...");
    return;
  }
  var win = open("", "", "resizable=no,width=500,height=300");
  if (!txtarea.form.curmap.options[0].selected) { win.document.charset= "iso-8859-1"; }
  win.document.body.innerText = txtarea.value;
  win.document.execCommand("SaveAs", null, "vietunidoc.txt");
  win.close();
}
