function toggleMText(idx){
  o1 = document.getElementById('ttext' + idx);
  o2 = document.getElementById('mtext' + idx);
  if(o1.style.visibility == 'hidden'){
    o = o2;
    o2 = o1;
    o1 = o;
  }
  o1.style.visibility = 'hidden';
  o1.style.position = 'absolute';
  o2.style.visibility = 'visible';
  o2.style.position = 'static';
}

function adjustSize(idx){
  document.getElementById('idx' + idx).rows += 1;
}

function setValue(fidx, tidx){
  o = document.getElementById('idx' + fidx);
  if(o.value == ''){
    o.value = document.getElementById('idx' + tidx).value.toLowerCase();
  }
}

function addSpin(idx){
  txt = getSelText(idx);
  if(txt.length==0){
    o = document.getElementById('idx' + idx);
    otext = o.value;
  }
  else{
    otext = txt;
    o = new Object();
    o.value = txt;
  }
  c = false;
  vals = [];        
  text = '';
  if(otext[0]=='{'){
    if(otext.length > 2){
      otext = o.value.substring(1, otext.length - 1);
      vals = otext.split('|');
      c = vals.length;
      otext = '';
    }
  }
  else{
    c = 0;
  }
  while(1){
    val = prompt('Enter Another Version:');
    if(val == null){
      text = otext;
      c = true;
      break;
    }
    else if(val.length == 0){
      break;
    }
    else{
      vals[c] = val;
      c++;
    }
  }
  if(vals.length > 0){          
    text = '{';
    if(otext.length > 0){
      text += otext + '|';
    }
    for(i=0;i<vals.length;i++){
      text += vals[i] + '|';
    }
    text = text.substring(0, text.length - 1) + '}';
  }
  else if (!c){
    text = '';
  }
  if(txt.length==0){
    o.value = text;
  }
  else{
    putSelText(idx, text);
  }
}

function httpobj(){
  if (typeof XMLHttpRequest == 'undefined') {
    XMLHttpRequest = function () {
      try { return new ActiveXObject('Msxml2.XMLHTTP.6.0'); }
        catch (e1) {}
      try { return new ActiveXObject('Msxml2.XMLHTTP.3.0'); }
        catch (e2) {}
      try { return new ActiveXObject('Msxml2.XMLHTTP'); }
        catch (e3) {}
      //Microsoft.XMLHTTP points to Msxml2.XMLHTTP.3.0 and is redundant
      throw new Error('This browser does not support XMLHttpRequest.');
    };
  }
  else{
    return new XMLHttpRequest();
  }
}

function getSynonyms(idx){
  req = httpobj();
  txt = getSelText(idx);
  if(txt.length > 0){
    params = 'action=synonyms&sel=-1&body=' + escape(txt);
  }
  else{
    params = 'action=synonyms&sel=0&body=' + escape(document.getElementById('idx' + idx).value);
  }
  req.open('POST', '/', false);
  req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  req.setRequestHeader('Content-length', params.length);
  req.send(params);
  if(req.responseText){
    if(txt.length > 0){
      if(txt != req.responseText){
        putSelText(idx, req.responseText);
      }
      else{
        alert('No synonyms were found for the selected field.');
      }
    }
    else{
      if(document.getElementById('idx' + idx).value != req.responseText){
        document.getElementById('idx' + idx).value = req.responseText;
      }
      else{
        alert('No synonyms were found for the selected field.');
      }
    }
  }
}

function upUses(id){
  req = httpobj();
  params = 'action=upuses&id=' + id;
  req.open('POST', '/', false);
  req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  req.setRequestHeader('Content-length', params.length);
  req.send(params);
}

function getSelText(idx)
{
  var txt = '';
  if(document.selection)
  {
    txt = document.selection.createRange().text;
  }
  else{
    o = document.getElementById('idx' + idx)
    len = o.value.length;
    start = o.selectionStart;
    end = o.selectionEnd;
    txt = o.value.substring(start, end);
  }

  return txt;
}

function putSelText(idx, txt)
{
  o = document.getElementById('idx' + idx)
  o.focus();
  if (document.selection)
  {
    sel = document.selection.createRange();
    sel.text = txt;
  }
  else{
    len = o.value.length;
    start = o.selectionStart;
    end = o.selectionEnd;
    sel = o.value.substring(start, end);
    o.value = o.value.substring(0, start) + txt + o.value.substring(end, len);
  }
}