function pageInit() {
  changeSelectSL();
  changeSelectTL();
  if (def_sl && def_tl) {
    var selSL = document.getElementById('src_lang');
    var selTL = document.getElementById('lang_to');
    var selQ  = document.getElementById('mtqual');
    var tmpS  = getOptionIndex(selSL, def_sl);
    var tmpT  = getOptionIndex(selTL, def_tl);
    var tmpQ;
    if (def_sl == selSL.item(tmpS).value) {
      selSL.selectedIndex = tmpS;
      changeSelectSL();
      tmpT = getOptionIndex(selTL, def_tl);
      selTL.selectedIndex = tmpT;
      changeSelectTL();
    } else if (def_tl == selTL.item(tmpT).value) {
      selTL.selectedIndex = tmpT;
      changeSelectTL();
      tmpS = getOptionIndex(selSL, def_sl);
      if (def_sl == selSL.item(tmpS).value) {
	selSL.selectedIndex = tmpS;
	changeSelectSL();
      }
    }
    if (def_q) {
      tmpQ  = getOptionIndex(selQ,  def_q);
      if (def_q == selQ.item(tmpQ).value) {
        selQ.selectedIndex = tmpQ;
        changeQuality();
      }
    }
    if (def_tt) {
      setTranslationType(def_tt);
      changeTranslationType(def_tt);
    }
  }
  return;
}
function calculateUnitsRequired() {
  var aMultiCoef   = 1;
  var aObjInput    = document.getElementById('ot_input');
  var aType        = getTranslationType();
  var aUnits       = 0;
  var selSrc       = document.getElementById('src_lang');
  var selQuality   = document.getElementById('mtqual');
  fileUploadHandle();
  if (!selQuality) return;
  if (aObjInput) aUnits = countWords(aObjInput.value);
  quality = selQuality.item(selQuality.selectedIndex) ? selQuality.item(selQuality.selectedIndex).value : '';
  if (quality.search(/^\D+(\d+)$/) != -1 )
    aMultiCoef = parseInt(quality.replace(/^\D+(\d+)$/, '$1')) + 1;
  if (aType == 'mt-he') {
    aMultiCoef += 6;
    aType = 'paid';
  } else if (aType == 'ht') {
    aMultiCoef = 18;
    aType = 'paid';
  }
  swapServicePriceType(aType);
  return aMultiCoef * aUnits;
}
function getTranslationType() {
  var aObj  = document.getElementsByName('ttype');
  var aType = '';
  if (!aObj) return '';
  for (var cycle = 0; cycle < aObj.length; cycle++) {
    if (aObj.item(cycle).checked) {
      return aObj.item(cycle).value;
    }
  }
  return '';
}
function setTranslationType(aType) {
  var aObj  = document.getElementsByName('ttype');
  if (!aObj) {
    return;
  }
  for (var cycle = 0; cycle < aObj.length; cycle ++) {
    if (aObj.item(cycle).value == aType) {
      aObj.item(cycle).checked = true;
      cycle = aObj.length;
    }
  }
  return aType;
}
function Language($aCode, $aName) {
  this.code = $aCode;
  this.name = $aName;
}
function SL(aLang) {
  this.code    = aLang;
  //
  // function/method target(aCode)
  //   get TL object for specified language code
  //
  this.target  = new Function('aCode',
			      'return this[aCode];');
  for (cycle = 1; cycle < SL.arguments.length; cycle++) {
    this[SL.arguments[cycle].code] = SL.arguments[cycle];
  }
}
function TL(aLang) {
  this.code    = aLang;
  //
  // function/method supports(aQuality)
  //   detect if translation quality is supported by current TL object
  //
  this.supports = new Function('aQuality',
			       'return this[aQuality] ? 1 : 0;');
  for (cycle = 1; cycle < TL.arguments.length; cycle++) {
    this[TL.arguments[cycle]] = TL.arguments[cycle];
  }
}
function getLanguageName(aCode) {
  for (var cycle = 0; cycle < lang_names.length; cycle += 2) {
    if (lang_names[cycle] == aCode) {
      return lang_names[cycle + 1];
    }
  }
  return '[' + aCode + ']';
}
function getQualityName(aCode) {
  for (var cycle = 0; cycle < lang_names.length; cycle += 2) {
    if (quality_names[cycle] == aCode) {
      return quality_names[cycle + 1];
    }
  }
  return '[' + aCode + ']';
}
function changeQuality() {
  updatePriceInfo();
  return;
}
function changeSelectTL() {
  var selSL = document.getElementById('src_lang');
  var selTL = document.getElementById('lang_to');
  var aSL   = selSL.item(selSL.selectedIndex) ? selSL.item(selSL.selectedIndex).value : 'auto';
  var aTL   = selTL.item(selTL.selectedIndex) ? selTL.item(selTL.selectedIndex).value : '';
  var tmp   = new Array();
  var aType = getTranslationType();
  var matrix;
  clearSelectOptions(selSL);
  ssl.length = 0;
  ssl.push('auto', getLanguageName('auto'));
  if (aType == 'mt-he') {
  } else {
    if (aType == 'mt') {
      matrix = mt;
    } else if (aType == 'ht') {
      matrix = ht;
    } else {
      return;
    }
    for (var i = 0; i < matrix.length; i++) {
      if (matrix[i][aTL] &&
	  matrix[i].code != aTL) {
	tmp.push(matrix[i].code);
      }
    }
    for (var i = 0; i < lang_names.length; i += 2) {
      if (containItem(tmp, lang_names[i])) {
	ssl.push(lang_names[i], getLanguageName(lang_names[i]));
      }
    }
    appendSelectOptions(selSL, ssl);
    selSL.selectedIndex = getOptionIndex(selSL, aSL);
  }
  if ('ht' != aType) updateQuality();
  updatePriceInfo();
}
function changeSelectSL() {
  var selSL = document.getElementById('src_lang');
  var selTL = document.getElementById('lang_to');
  var aSL   = selSL.item(selSL.selectedIndex) ? selSL.item(selSL.selectedIndex).value : 'auto';
  var aTL   = selTL.item(selTL.selectedIndex) ? selTL.item(selTL.selectedIndex).value : '';
  var aType = getTranslationType();
  var tmp   = new Array();
  var matrix;
  clearSelectOptions(selTL);
  stl.length = 0;
  if (aType == 'mt-he') {
  } else {
    if (aType == 'mt') {
      matrix = mt;
    } else if (aType == 'ht') {
      matrix = ht;
    } else {
      return;
    }
    //
    // Fill temporary array with supported languages (iso code)
    //
    for (var i = 0; i < matrix.length; i++) {
      if (matrix[i].code == aSL ||
	  aSL == 'auto') {
	for (var j in matrix[i]) {
	  if (j != 'code' &&
	      !containItem(tmp, j)) {
	    tmp.push(j);
	  }
	}
	if (aSL != 'auto') break;
      }
    }
    //
    // use lang_names to create stl array with correct languages order
    //
    for (var i = 0; i < lang_names.length; i += 2) {
      if (containItem(tmp, lang_names[i])) {
	stl.push(lang_names[i], getLanguageName(lang_names[i]));
      }
    }
  }
  appendSelectOptions(selTL, stl);
  selTL.selectedIndex = getOptionIndex(selTL, aTL);
  if ('ht' != aType) updateQuality();
  updatePriceInfo();
}
function changeTranslationType(aType) {
  var selQuality   = document.getElementById('mtqual');
  changeSelectSL();
  changeSelectTL();
  if (aType != 'ht') {
    updateQuality();
    if (aType == 'mt-he') {
      for (cycle = selQuality.childNodes.length - 1; cycle >= 0; cycle--) {
	if (selQuality.childNodes[cycle].value.replace(/^\D+(\d+)$/, '$1') < human_level) {
	  selQuality.removeChild(selQuality.childNodes[cycle]);
	}
      }
    }
  }
  updatePriceInfo();
}
function updateQuality() {
  var selSL = document.getElementById('src_lang');
  var selTL = document.getElementById('lang_to');
  var selQ  = document.getElementById('mtqual');
  var aSL   = selSL.item(selSL.selectedIndex) ? selSL.item(selSL.selectedIndex).value : 'auto';
  var aTL   = selTL.item(selTL.selectedIndex) ? selTL.item(selTL.selectedIndex).value : '';
  var aQ    = selQ.item(selQ.selectedIndex)   ? selQ.item(selQ.selectedIndex).value   : '';
  var tmp   = new Array();
  clearSelectOptions(selQ);
  sql.length = 0;
  if (aSL == 'auto') {
    var countLang  = 0;
    var level      = new Array();
    for (var i = 0; i < quality_names.length / 2; i++) level[i] = 0;
    for (var i = 0; i < mt.length; i++) {
      if (mt[i][aTL]) {
	countLang++;
	for (var j in mt[i][aTL]) {
	  if (j.search(/^\D+(\d+)$/) != -1 ) {
	    level[j.replace(/^\D+(\d+)$/, '$1')]++;
	  }
	}
      }
    }
    for (var i = 0; i < (quality_names.length / 2); i++) {
      if (level[i] &&
	  level[i] == countLang) {
	tmp.push(quality_names[i * 2]);
      }
    }
  } else {
    for (var i = 0; i < mt.length; i++) {
      if (mt[i].code == aSL &&
	  mt[i][aTL]) {
	for (var j in mt[i][aTL]) {
	  if (j.search(/^\D+(\d+)$/) != -1 ) tmp.push(j);
	}
      }
    }
  }
  for (var i = 0; i < quality_names.length; i += 2) {
    if (containItem(tmp, quality_names[i])) {
      sql.push(quality_names[i], getQualityName(quality_names[i]));
    }
  }
  appendSelectOptions(selQ, sql);
  enableMTHE(selQ);
  selQ.selectedIndex = suggestQualityIndex(aQ);
}
function suggestQualityIndex(aQuality) {
  var aSelect  = document.getElementById('mtqual');
  var idxOld   = parseInt(aQuality.replace(/^\D+(\d+)$/, '$1'));
  var possible = new Array ();
  for (var i = 0; i < quality_names.length / 2; i++) possible[i] = 0;
  for (var i = 0; i < aSelect.childNodes.length; i++) {
    possible[aSelect.childNodes[i].value.replace(/^\D+(\d+)$/, '$1')]++;
  }
  if (possible[idxOld]) return getOptionIndex(aSelect, 'level' + idxOld);
  for (var dist = 1; dist <= quality_names.length / 4; dist ++) {
    var tmpUp = idxOld + dist;
    var tmpDn = idxOld - dist;
    if (possible[tmpUp]) {
      return getOptionIndex(aSelect, 'level' + tmpUp);
    } else if (possible[tmpDn]) {
      return getOptionIndex(aSelect, 'level' + tmpDn);
    }
  }
  return 0;
}
function enableMTHE(aSelect) {
  var enable = 0;
  var aObj   = document.getElementById('mthe');
  var selSrc = document.getElementById('src_lang');
  if (!aObj) return;
  for (cycle = aSelect.childNodes.length - 1; cycle >= 0; cycle--) {
    if (aSelect.childNodes[cycle].value.replace(/^\D+(\d+)$/, '$1') >= human_level) {
      enable = 1;
      break;
    }
  }
  if (enable) {
    aObj.style.visibility = 'visible';
  } else {
    aObj.style.visibility = 'hidden';
    if (getTranslationType() == 'mt-he') setTranslationType('mt');
  }
}
function updatePriceInfo() {
  var objCount    = document.getElementById('cost_count');
  var objPrice    = document.getElementById('cost_price');
  var aUnits      = calculateUnitsRequired();
  var aUnitsLng   = langident_calculateUnitsRequired();
  var aPrice      = calculateServicePrice(aUnits, unit_price, used_vat);
  var aPriceLng   = calculateServicePrice(aUnitsLng, unit_price_lng, used_vat);
  var aObjLang    = document.getElementById('src_lang');
  var aType       = getTranslationType();
  if (!objCount ||
      !objPrice ||
      !aObjLang) {
    return;
  }
  if (!aObjLang.childNodes.length) return;
  if (aType == 'ht') {
    var aObjInput = document.getElementById('ot_input');
    if (aObjInput) aUnits = countWords(aObjInput.value);
    aPrice = aUnits * 0.06;
  }
  if (aObjLang &&
      aObjLang[aObjLang.selectedIndex].value == 'auto') {
    if (aType == 'ht') {
      objCount.innerHTML = aPrice.toFixed(precision) + ' ' + base_currency + ' [' +
	text_service_translate +']; ' + aUnitsLng + ' ' + text_units + ' [' +
	text_service_langident + ']';
      objPrice.innerHTML = aPrice.toFixed(precision) + ' + ' +
	aPriceLng.toFixed(precision) + ' ' + base_currency;
    } else if (aType == 'mt') {
      objCount.innerHTML = aUnits + ' [' + text_service_translate + '] + ' +
	aUnitsLng + ' [' + text_service_langident + '] ' + text_units;
      objPrice.innerHTML = aPrice.toFixed(precision) + ' + ' +
	aPriceLng.toFixed(precision) + ' ' + base_currency;
    } else if (aType == 'mt-he') {
    }
  } else {
    if (aType == 'ht') {
      objCount.innerHTML = aPrice.toFixed(precision) + ' ' + base_currency;
      objPrice.innerHTML = aPrice.toFixed(precision) + ' ' + base_currency;
    } else if (aType == 'mt') {
      objCount.innerHTML = aUnits + ' [' + text_service_translate + '] ' + text_units;
      objPrice.innerHTML = aPrice.toFixed(precision) + ' ' + base_currency;
    } else if (aType == 'mt-he') {
    }
  }
}