function calcNumbers(pObj, pIndex) {
  with (formByName('details')) {
    var ElemSP = elements['sellingprice[]'][pIndex];
    var ElemPP = elements['purchaseprice[]'][pIndex];
    var ElemD = elements['discount[]'][pIndex];
    var ElemM = elements['margin'][pIndex];
    if (typeof(ElemSP) == 'undefined') {
      ElemSP = elements['sellingprice[]'];
      ElemPP = elements['purchaseprice[]'];
      ElemD = elements['discount[]'];
      ElemM = elements['margin'];
    }
  }
  if (pObj.name == 'margin')
    ElemSP.value = Math.round(ElemPP.value * (1 + (ElemPP.value < 0 ? - ElemM.value : ElemM.value) / 100) / (1 - ElemD.value / 100) * 100) / 100;
  else
    ElemM.value = ElemPP.value == 0 ? 0 : Math.round((ElemSP.value * (1 - ElemD.value / 100) - ElemPP.value) / Math.abs(ElemPP.value) * 10000) / 100;
}

function doActionAddDelivery(pObj, pPattern) {
  var S = getSelectSelValue(pObj);
  if (S == '')
    pObj.selectedIndex = 0;
  else {
    if (pPattern != '')
      try {
        var List = pPattern.split('|');
        eval("formByName('details').elements['invoicenum'].value = pObj.options[pObj.selectedIndex].text.replace(" + List[1] + ', List[0]);');
      } catch (Err) {}
    submitAndAction('details', 'add');
  }
}

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

function doConfirmActionDeletePrintout(pMsg, pIndex) {
  loadPageConfirm(pMsg, '&page=printouts&action=delete&index=' + pIndex);
}

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

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

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

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

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

function doPopupActionSelItems() {
  doPopupActionItem();
}

function doPopupActionText() {
  doPopupActionItem();
}

function doPopupActionRet(pCustomer, pNum, pPattern, pName) {
  if (!opener.closed) {
    var Form = formByNameOpener('details').elements;
    var Elem = Form['customer'];
    if (Elem.value == '-1')
      Elem.value = pCustomer;
    if (pPattern != '')
      try {
        var List = pPattern.split('|');
        eval("Form[pName].value = pNum.replace(" + List[1] + ', List[0]);');
      } catch (Err) {}
    submitFormOpener('details');
  }
  window.close();
}

function doPopupActionRetOffer(pCustomer, pQuotationNum, pPattern) {
  doPopupActionRet(pCustomer, pQuotationNum, pPattern, 'ordernum');
}

function doPopupActionRetOrder(pCustomer, pOrderNum, pPattern) {
  doPopupActionRet(pCustomer, pOrderNum, pPattern, 'deliverynum');
}

function doPopupActionRetRequest(pCustomer, pRequestNum, pPattern) {
  doPopupActionRet(pCustomer, pRequestNum, pPattern, 'quotationnum');
}

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

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

function dragEndMoveItem(pIndex) {
  if (_DragParam != '' && _DragParam - pIndex != 0) {
    with (formByName('details')) {
      elements['subindex'].value = pIndex;
      elements['offset'].value = _DragParam - pIndex;
    }
    submitAndAction('details', 'move');
  }
}

function dragOutMoveItem(pObj) {
  pObj.style.cursor = 'move';
}

function dragOverMoveItem(pObj) {
  if (_DragAction != '')
    pObj.style.cursor = 'url(bin/crm/cases/img/dragmove.ico),move';
}

function dragStartMoveItem(pIndex) {
  _DragAction = "dragEndMoveItem('" + pIndex + "');";
  getElemBody().style.cursor = 'url(bin/crm/cases/img/dragmove.ico),move';
}

function dragStopMoveItem(pIndex) {
  _DragParam = pIndex;
}

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

function getDynCustomer() {
  var Form = formByName('details').elements;
  Form['customer'].value = '';
  var S = Form['dyn_customer'].value;
  if (S == '')
    displayElemById('ok', 'inline');
  else {
    displayElemById('ok', 'none');
    doHttpRequest('plain.php?umi=crm_billing&page=customer.dyn', 'showDynCustomer', '', 'name=' + encodeURIComponent(S));
  }
}

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

function hideDynCustomer() {
  window.setTimeout("displayElemById('dyn_customer', 'none');", 300);
}

function loadSelSerials(pSubIndex) {
  if (!opener.closed) {
    var List = new Array();
    var Elem;
    var I = 0;
    while (Elem = formByNameOpener('details').elements['serials[' + 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 popupCustomer() {
  popupWindow('plain.php?umi=crm_opportunities&page=customer.pop', 300, 420);
}

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

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

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

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

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

function popupItem(pItemsFile, pIndex) {
  popupWindow('plain.php?umi=crm_billing&page=item.pop&index=' + pIndex + '&itemsfile=' + pItemsFile, 160, 500);
}

function popupRetOffer(pItemsFile) {
  popupWindow('plain.php?umi=crm_billing&page=retoffer.pop&itemsfile=' + pItemsFile, 300, 500);
}

function popupRetOrder(pItemsFile) {
  popupWindow('plain.php?umi=crm_billing&page=retorder.pop&itemsfile=' + pItemsFile, 300, 500);
}

function popupRetRequest(pItemsFile) {
  popupWindow('plain.php?umi=crm_billing&page=retrequest.pop&itemsfile=' + pItemsFile, 300, 500);
}

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

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

function popupText(pItemsFile, pIndex) {
  popupWindow('plain.php?umi=crm_billing&page=text.pop&index=' + pIndex + '&itemsfile=' + pItemsFile, 300, 500);
}

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

function setDynCustomer(pIndex, pName) {
  var Form = formByName('details').elements;
  Form['customer'].value = pIndex;
  Form['dyn_customer'].value = pName;
  displayElemById('ok', 'inline');
}

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

function viewInfoBoxCustomer(pIndex) {
  if (pIndex != '-1')
    viewInfoBox('customer' + pIndex, 'plain.php?umi=crm_customers&page=customer.info&index=' + pIndex);
}

function viewInfoBoxCustomer2() {
  viewInfoBoxCustomer(formByName('details').elements['customer'].value);
}

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']));
}