function calcOutput(pObj, pReload) {
  with (formByName('details'))
    setHTML(elements['output'].value * elements['target'].value, 'calc');
  if (pReload)
    submitFormIndicator('details');
}

function doActionAddOrder(pObj) {
  var S = getSelectSelValue(pObj);
  if (S == '')
    pObj.selectedIndex = 0;
  else
    submitAndAction('details', 'add');
}

function doConfirmAction(pAction, pMsg, pStorage, pIndex) {
  loadPageConfirm(pMsg, '&action=' + pAction + '&index=' + pIndex + '&storage=' + pStorage);
}

function doConfirmActionCancelOrder(pMsg, pIndex, pSubIndex) {
  loadPageConfirm(pMsg, '&page=details_item&action=cancelorder&index=' + pIndex + '&subindex=' + pSubIndex + '&tab=2');
}

function doConfirmActionCancelOrder2(pMsg, pIndex) {
  loadPageConfirm(pMsg, '&page=orders&action=cancel&index=' + pIndex);
}

function doConfirmActionDeleteCategory(pMsg) {
  if (formByName('index').elements['category'].selectedIndex > 0 && confirm(pMsg))
    submitAndAction('index', 'delete');
}

function doConfirmActionRemoveFile(pMsg, pIndex, pSubIndex) {
  loadPageConfirm(pMsg, '&page=details_item&action=removefile&index=' + pIndex + '&subindex=' + pSubIndex + '&tab=6');
}

function doConfirmActionRemoveItem(pMsg, pIndex) {
  if (confirm(pMsg)) {
    formByName('details').elements['index'].value = pIndex;
    submitAndAction('details', 'remove');
  }
}

function doConfirmActionRemoveSupplier(pMsg, pIndex, pSubIndex) {
  loadPageConfirm(pMsg, '&page=details_item&action=removesupplier&index=' + pIndex + '&subindex=' + pSubIndex);
}

function doConfirmActionStorages(pAction, pMsg, pIndex) {
  loadPageConfirm(pMsg, '&page=storages&action=' + pAction + '&index=' + pIndex);
}

function doConfirmActionTemplates(pAction, pMsg, pIndex) {
  loadPageConfirm(pMsg, '&page=templates&action=' + pAction + '&index=' + pIndex);
}

function doIndexConfirmAction(pAction, pMsg, pStorage) {
  doConfirmAction(pAction, pMsg, pStorage, getOptSelValue(formByName('index').elements['index']));
}

function doIndexConfirmActionStorages(pAction, pMsg) {
  doConfirmActionStorages(pAction, pMsg, getOptSelValue(formByName('index').elements['index']));
}

function doIndexConfirmActionTemplates(pAction, pMsg) {
  doConfirmActionTemplates(pAction, pMsg, getOptSelValue(formByName('index').elements['index']));
}

function doPopupActionAddFile(pIndex, pSubIndex) {
  goOpener('index.php?umi=erp_stockcalc&page=details_item&action=addfile&index=' + pIndex + '&subindex=' + pSubIndex + '&tab=6');
  window.close();
}

function doPopupActionAddSupplier(pIndex, pSubIndex) {
  goOpener('index.php?umi=erp_stockcalc&page=details_item&action=addsupplier&index=' + pIndex + '&subindex=' + pSubIndex);
  window.close();
}

function doPopupActionSelItems() {
  if (!opener.closed)
    submitFormOpener('details');
  window.close();
}

function doPopupActionSelSerials() {
  if (!opener.closed)
    formByNameOpener('details').elements['serials'].value = getAllOptSelValues(formByIndex(0).elements['serials']).join(', ');
  window.close();
}

function doPopupActionSelSerials2(pSubIndex) {
  if (!opener.closed) {
    var List = getAllOptSelValues(formByIndex(0).elements['serials']);
    var I = 0;
    while (Elem = formByNameOpener('details').elements['serials2[' + pSubIndex + '_' + I + ']']) {
      Elem.value = List.length > I ? List[I] : '';
      I++;
    }
  }
  window.close();
}

function doPopupActionScanSerials() {
  if (!opener.closed) {
    var List = formByIndex(0).elements['serials'].value.replace(/[,\r\t]/g, '').split("\n");
    var List2 = new Array();
    for (var I = 0; I < List.length; I++)
      if (List[I] != '')
        List2.push(List[I]);
    formByNameOpener('details').elements['serials'].value = List2.join(', ');
  }
  window.close();
}

function doPromptActionRenameSerial(pTab, pMsg, pIndex, pSerial) {
  var S = prompt(pMsg, pSerial);
  if (S != null && S != '')
    loadPage('&page=details_item&action=renameserial&index=' + pIndex + '&newserial=' + encodeURIComponent(S) + '&oldserial=' + pSerial + '&tab=' + pTab);
}

function doPromptActionResetPrice(pTab, pMsg, pIndex, pSubIndex) {
  var S = prompt(pMsg, '');
  if (S != null && S != '')
    loadPage('&page=details_item&action=resetprice&index=' + pIndex + '&resetprice=' + encodeURIComponent(S) + '&subindex=' + pSubIndex + '&tab=' + pTab);
}

function doPromptActionSetInfo(pMsg) {
  var Form = formByName('details').elements;
  var Elem = Form['info'];
  if (getSelectSelValue(Elem) == '')
    Elem.selectedIndex = 0;
  else {
    var S = prompt(pMsg, '');
    if (S == null)
      Elem.selectedIndex = 0;
    else {
      Form['action'].value = 'setinfo';
      Form['input'].value = S;
      submitFormIndicator('details');
    }
  }
}

function doPromptActionSetMailDate(pMsg, pIndex) {
  var S = prompt(pMsg, '');
  if (S != null && S != '')
    loadPage('&page=printouts&action=setmaildate&date=' + encodeURIComponent(S) + '&index=' + pIndex);
}

function focusNextSerial(pId) {
  if (_KeyCode == 13)
    formByName('details').elements['serials[' + pId + ']'].select();
}

function getDynItem() {
  displayElemById('ok', 'none');
  var Form = formByName('details').elements;
  Form['subitem'].value = '';
  var S = Form['dyn_item'].value;
  if (S != '')
    doHttpRequest('plain.php?umi=erp_stockcalc&page=item.dyn', 'showDynItem', '', 'name=' + encodeURIComponent(S));
}

function getDynPreview(pIndex) {
  if (elementById('index_preview').style.display != 'none')
    doHttpRequest('plain.php?umi=erp_stockcalc&page=preview.dyn&index=' + pIndex, 'setHTML', 'index_preview', '');
}

function getDynSupplier() {
  var Form = formByName('details').elements;
  Form['supplier'].value = '';
  var S = Form['dyn_supplier'].value;
  if (S == '')
    displayElemById('ok', 'inline');
  else {
    displayElemById('ok', 'none');
    doHttpRequest('plain.php?umi=erp_stockcalc&page=supplier.dyn', 'showDynSupplier', '', 'name=' + encodeURIComponent(S));
  }
}

function goAssignedUser() {
  var S = getSelectSelValue(formByName('details').elements['assigneduser']);
  if (S != '')
    go('index.php?umi=admin_users&page=details&id=' + S);
}

function goPreviewTemplate() {
  go('plain.php?umi=erp_stockcalc&page=print.pdf&template=' + getOptSelValue(formByName('index').elements['index']));
}

function goStorage() {
  loadPage('&page=details_storage&index=' + getSelectSelValue(formByName('details').elements['storage']));
}

function hideDynItem() {
  window.setTimeout("displayElemById('dyn_item', 'none');", 300);
}

function hideDynSupplier() {
  window.setTimeout("displayElemById('dyn_supplier', 'none');", 300);
}

function loadScan(pMsg, pIndex) {
  if (pIndex != '')
    if (pIndex == '-1')
      window.alert(pMsg);
    else {
      goOpener('index.php?umi=erp_stockcalc&page=details_item&index=' + pIndex);
      window.close();
    }
  formByIndex(0).elements['barcode'].select();
}

function loadScanSerials() {
  if (!opener.closed)
    with (formByIndex(0).elements['serials']) {
      value = formByNameOpener('details').elements['serials'].value.replace(/\s*,\s*/g, "\n");
      if (value != '')
        value += "\n";
      focus();
    }
}

function loadSelSerials() {
  if (!opener.closed) {
    var List = formByNameOpener('details').elements['serials'].value.replace(/\s*,\s*/g, ',').split(',');
    var Elem = formByIndex(0).elements['serials'];
    for (var I = 0; I < List.length; I++)
      if (List[I] != '')
        if (typeof(Elem.length) == 'number') {
          for (var J = 0; J < Elem.length; J++)
            if (Elem[J].value == List[I])
              Elem[J].checked = true;
        } else if (Elem.value == List[I])
          Elem.checked = true;
  }
}

function loadSelSerials2(pSubIndex) {
  if (!opener.closed) {
    var List = new Array();
    var Elem;
    var I = 0;
    while (Elem = formByNameOpener('details').elements['serials2[' + pSubIndex + '_' + I + ']']) {
      List.push(Elem.value);
      I++;
    }
    Elem = formByIndex(0).elements['serials'];
    for (var I = 0; I < List.length; I++)
      if (List[I] != '')
        if (typeof(Elem.length) == 'number') {
          for (var J = 0; J < Elem.length; J++)
            if (Elem[J].value == List[I])
              Elem[J].checked = true;
        } else if (Elem.value == List[I])
          Elem.checked = true;
  }
}

function popupAddFile(pIndex) {
  popupWindow('plain.php?umi=erp_stockcalc&page=addfile.pop&index=' + pIndex, 300, 420);
}

function popupAddSupplier(pIndex) {
  popupWindow('plain.php?umi=erp_stockcalc&page=addsupplier.pop&index=' + pIndex, 300, 420);
}

function popupHistoryItem(pIndex) {
  popupWindow('plain.php?page=history.pop&entity=erp_stockcalc_items&index=' + pIndex, 240, 500);
}

function popupHistoryStorage(pIndex) {
  popupWindow('plain.php?page=history.pop&entity=erp_stockcalc_storages&index=' + pIndex, 240, 500);
}

function popupHistoryTemplate(pIndex) {
  popupWindow('plain.php?page=history.pop&entity=erp_stockcalc_templates&index=' + pIndex, 240, 500);
}

function popupItem() {
  popupWindow('plain.php?umi=erp_stockcalc&page=item.pop', 300, 420);
}

function popupScan() {
  popupWindow('plain.php?umi=erp_stockcalc&page=scan.pop', 140, 220);
}

function popupScanSerials() {
  popupWindow('plain.php?umi=erp_stockcalc&page=scanserials.pop', 300, 220);
}

function popupSelItems(pItemsFile) {
  popupWindow('plain.php?umi=erp_stockcalc&page=selitems.pop&itemsfile=' + pItemsFile, 300, 500);
}

function popupSelSerials(pIndex) {
  popupWindow('plain.php?umi=erp_stockcalc&page=selserials.pop&index=' + pIndex, 300, 220);
}

function popupSelSerials2(pIndex, pSubIndex) {
  popupWindow('plain.php?umi=erp_stockcalc&page=selserials.pop&index=' + pIndex + '&subindex=' + pSubIndex, 300, 220);
}

function selCategory() {
  var Form = formByName('index').elements;
  var Elem = Form['category'];
  if (Elem.selectedIndex > 0) {
    Form['action'].value = 'update';
    Form['update'].value = Form['label1'].value;
    Form['original'].value = Form['number'].value = getSelectSelValue(Elem);
    Form['name'].value = Elem.options[Elem.selectedIndex].text;
  } else {
    Form['action'].value = 'add';
    Form['update'].value = Form['label2'].value;
    Form['original'].value = Form['number'].value = '';
    Form['name'].value = '';
  }
}

function selTemplate(pObj) {
  if (getSelectSelValue(pObj) == '')
    pObj.selectedIndex = 0;
  else
    submitFormIndicator('printout');
}

function setDynItem(pIndex, pUseSerials, pName) {
  var Form = formByName('details').elements;
  Form['subitem'].value = pIndex;
  Form['dyn_item'].value = pName;
  displayElemById('ok', 'inline');
  displayElemById('serials', pUseSerials == '0' ? 'none' : 'table-row');
}

function setDynSupplier(pIndex, pName) {
  var Form = formByName('details').elements;
  Form['supplier'].value = pIndex;
  Form['dyn_supplier'].value = pName;
  displayElemById('ok', 'inline');
}

function showDynItem(pValue, pParam) {
  setHTML(pValue, 'dyn_item');
  fadeElemById('dyn_item', pValue == '' ? 'none' : 'block');
}

function showDynSupplier(pValue, pParam) {
  setHTML(pValue, 'dyn_supplier');
  fadeElemById('dyn_supplier', pValue == '' ? 'none' : 'block');
}

function viewInfoBoxSupplier() {
  var S = formByName('details').elements['supplier'].value;
  if (S != '-1')
    viewInfoBox('supplier' + S, 'plain.php?umi=erp_suppliers&page=supplier.info&index=' + S);
}

function viewInfoBoxSupplier2() {
  var S = getSelectSelValue(formByName('details').elements['supplier']);
  if (S != '-1')
    viewInfoBox('supplier' + S, 'plain.php?umi=erp_suppliers&page=supplier.info&index=' + S);
}

function viewInfoBoxUser(pId) {
  if (pId != '')
    viewInfoBox('user' + pId, 'plain.php?umi=admin_users&page=user.info&id=' + pId);
}

function viewInfoBoxUser2() {
  viewInfoBoxUser(getSelectSelValue(formByName('details').elements['assigneduser']));
}