var hw = '';
function helpWindow(what, aLang) {
  if (!aLang) aLang = 'en';
  if (!hw.closed && hw.location) {
    hw.location.href = '/' + aLang + '/mb/help/' + what;
  } else {
    hw = window.open('/' + aLang + '/mb/help/' + what, 'Help',
		     'resizable=1,height=600,width=500,scrollbars');
    if (!hw.opener) hw.opener = self;
  }
  if (hw) hw.focus();
}
function toOldWindow(aURL) {
  opener.location.href = aURL;
}
function charWindow(aLang) {
  if (!aLang) aLang = 'en';
  cw = window.open('/' + aLang + '/chars.cgi', 'Chars',
   'resizable=1,height=200,width=400,scrollbars');
  if (cw) cw.focus();
}
function getOptionIndex(aSelect, aValue) {
  aIndex = 0;
  for (cycle = aSelect.childNodes.length - 1; cycle >= 0; cycle--) {
    if (aSelect.childNodes[cycle].value &&
	aSelect.childNodes[cycle].value == aValue) {
      aIndex = cycle;
    }
  }
  return aIndex;
}
function changeAfterConfirm(aSelect, aOldValue, aFunction, aQuestion) {
  if (confirm(aQuestion)) {
    aFunction(aSelect[aSelect.selectedIndex].value, aOldValue);
  } else {
    aSelect.selectedIndex = getOptionIndex(aSelect, aOldValue);
  }
}
function changeSiteLang(aNew, aOld) {
  var url    = document.location.href;
  var offset = url.indexOf(':');
  var newUrl = url.substring(0, offset + 3);
  url     = url.substring(offset + 3);
  offset  = url.indexOf('/');
  newUrl += url.substring(0, offset + 1)
  url     = url.substring(offset + 1);
  offset = url.indexOf(aOld);
  url = (offset == 0) ? url.substring(aOld.length) : '/' + url;
  newUrl += aNew + url;
  document.location = newUrl;
}
function changeSiteTheme() {
  var theme_elem = document.getElementsByName('new_site_theme')[0];
  var params        = '?new_site_theme=' + theme_elem[theme_elem.selectedIndex].value;
  reloadWithParams(params);
}
function reloadWithParams(params) {
  var url    = document.location.href;
  var offset = url.indexOf('?');
  if (offset == -1) {
    document.location = url + params;
  } else {
    document.location = url.substring(0, offset) + params;
  }
}
function countWords(aText) {
  aCount = 0;
  aText = aText.replace(/(^\W+)|(\W+$)/g, '');
  if (aText.length) {
    aList  = aText.split(/\W+/);
    aCount = aList.length;
  }
  return aCount;
}
var wordCount = 0;
function updateWordCount(aText) {
  var aCount = countWords(aText);
  var obj    = document.getElementById('wordCount');
  if (obj) {
    obj.innerHTML = aCount;
  } else {
    alert('elem not found');
  }
}
function calculateServicePrice(aUnits, aUnitPrice, aVat) {
  aPrice     = 0;
  aUnits     = parseInt(aUnits);
  aUnitPrice = parseFloat(aUnitPrice);
  aVat       = parseFloat(aVat);
  if (!isNaN(aVat) ||
      !isNaN(aUnits) ||
      !isNaN(aUnitPrice)) {
    aPrice = aUnitPrice * aUnits;
    if (aVat != 0) {
      aPrice += aPrice * aVat / 100;
    }
  }
  return aPrice;
}
function updatePriceInfo() {
  objCount = document.getElementById('cost_count');
  objPrice = document.getElementById('cost_price');
  aUnits   = calculateUnitsRequired();
  aPrice   = calculateServicePrice(aUnits, unit_price, used_vat);
  if (!objCount || !objPrice) return;
  objCount.innerHTML = aUnits + ' ' + text_units;
  objPrice.innerHTML = aPrice.toFixed(precision) + ' ' + base_currency;
}
function appendSelectOptions(aSelect, aOptions, aSkip) {
  if (!aSelect || !aOptions) {
    return
  }
  for (cycle = 0; cycle < aOptions.length / 2; cycle ++) {
    idx = cycle * 2;
    if (!aSkip ||
	aSkip != aOptions[idx]) {
      option       = document.createElement('option');
      option.value = aOptions[idx];
      option.appendChild( document.createTextNode( aOptions[idx + 1] ) );
      aSelect.appendChild( option );
    }
  }
}
function clearSelectOptions(aSelect) {
  if (!aSelect) return;
  for (cycle = aSelect.childNodes.length - 1; cycle >= 0; cycle--) {
    aSelect.removeChild(aSelect.childNodes[cycle]);
  }
}
function  updateDstSelect(aSrcSelect,aSrcList,aDstSelect,aDstList) {
  var skip_value;
  var select_val;
  if (!aSrcSelect         ||
      !aSrcSelect.options ||
      !aSrcList.length    ||
      !aSrcSelect         ||
      !aDstSelect.options ||
      !aDstList.length) {
    return;
  }
  dst_val  = aDstSelect[aDstSelect.selectedIndex] ? aDstSelect[aDstSelect.selectedIndex].value : '';
  src_skip = aSrcSelect[aSrcSelect.selectedIndex] ? aSrcSelect[aSrcSelect.selectedIndex].value : '';
  src_val  = aSrcSelect[aSrcSelect.selectedIndex] ? aSrcSelect[aSrcSelect.selectedIndex].value : '';
  clearSelectOptions(aSrcSelect);
  clearSelectOptions(aDstSelect);
  if (aDstList.length == 2) {
    appendSelectOptions(aSrcSelect, aSrcList, aDstSelect[0].value);
  } else {
    appendSelectOptions(aSrcSelect, aSrcList);
  }
  if (src_skip &&
      src_skip == dst_val) {
    for (cycle = 0; cycle < aDstList.length / 2; cycle ++) {
      idx = cycle * 2;
      if (skip_value != aDstList[idx]) {
        dst_val = aDstList[idx];
        cycle   = aDstList.length / 2;
      }
    }
  }
  appendSelectOptions(aDstSelect, aDstList, src_skip);
  setSelectedOption(aDstSelect, dst_val);
  setSelectedOption(aSrcSelect, src_val);
}
function setSelectedOption(aSelect, aValue) {
  if (!aSelect) {
    return;
  }
  selected = 0;
  for (cycle = 0; cycle < aSelect.options.length; cycle++) {
    if (aSelect[cycle].value == aValue) {
      aSelect.selectedIndex = cycle;
      selected = 1;
    }
  }
  if (!selected) {
    aSelect.selectedIndex = 0;
  }
}
var langident_method_dict = 2;
var langident_method_ngram = 1;
var langident_method_nvect = 2;
var langident_method_smart = 2;
function fileUploadHandle() {
  aObjFileUpload   = document.getElementById('ot_upload');
  aObjFileType     = document.getElementById('data_src_type');
  aObjFileEncoding = document.getElementById('data_src_encoding');
  if (aObjFileUpload) {
    aObjUpload = document.getElementById('cost_upload');
    aObjNormal = document.getElementById('cost_normal');
    if (aObjNormal &&
	aObjFileUpload &&
	aObjFileUpload.value) {
      aObjUpload.style.display = 'inline';
      aObjNormal.style.display = 'none';
      if (aObjFileType)     aObjFileType.style.visibility     = 'visible';
      if (aObjFileEncoding) aObjFileEncoding.style.visibility = 'visible';
    } else {
      aObjUpload.style.display = 'none';
      aObjNormal.style.display = 'inline';
      if (aObjFileType)     aObjFileType.style.visibility     = 'hidden';
      if (aObjFileEncoding) aObjFileEncoding.style.visibility = 'hidden';
    }
  }
}
function langident_calculateUnitsRequired() {
  aObjMethod = document.getElementById('rec_method');
  aObjInput  = document.getElementById('ot_input');
  aMultiCoef = langident_method_smart;
  aUnits     = 0;
  if (aObjMethod) {
    var aoidx = aObjMethod[aObjMethod.selectedIndex];
    if ('dict' == aoidx.value)       aMultiCoef = langident_method_dict;
    else if ('ngram' == aoidx.value) aMultiCoef = langident_method_ngram;
    else if ('nvect' == aoidx.value) aMultiCoef = langident_method_nvect;
    else if ('smart' == aoidx.value) aMultiCoef = langident_method_smart;
  }
  if (aObjInput) aUnits = countWords(aObjInput.value);
  return aUnits * aMultiCoef;
}
  function setFormAction(formName,formAction) {
    var sForm = eval('document.'+formName);
    if (!sForm) return false;
    sForm.action = formAction;
    return true;
  }
function swapServicePriceType(aType) {
  var aObjFree = document.getElementById('free_service');
  var aObjPaid = document.getElementById('paid_service');
  if (aObjFree && aObjPaid) {
    aObjFree.style.display = 'inline';
    aObjPaid.style.display = 'none';
    if (aType && aType == 'paid') {
      aObjFree.style.display = 'none';
      aObjPaid.style.display = 'inline';
    }
  }
}
function getListIndex(aList, aValue) {
  for (cycle = aList.length - 1; cycle >= 0; cycle --) {
    if (aList[cycle] == aValue) {
      return cycle;
    }
  }
  return 0;
}
function containItem(aList, aItem) {
  for (cycle = 0; cycle < aList.length; cycle ++) {
    if (aList[cycle] == aItem) {
      return 1;
    }
  }
  return 0;
}
function describeObject(aObj) {
  var elem;
  var text = '';
  for (elem in aObj) {
    text += elem + ' => ' + aObj[elem] + '\n';
  }
  alert(text)
}