// check all items
function checkAll(type){
   var i;
   var checkID = 'check_';
   var checkedItem;
   for(i = 1; i <= getAllRows() ; i++) {
      checkedItem = ExGetElementByID (checkID + i)
      if (checkedItem == null)
         break;
      else {
         switch (type) {
            case 1:
               checkedItem.checked = true;
               break;
            case 0:
               checkedItem.checked = false;
               break
         }
      }
   }
}

// check one item
function checkChosenOne(nID){
   ExGetElementByID ('check_' + nID).checked = true;
}

// count of checked items
function getCheckedRows(){
   var i;
   var result = 0
   var checkID = 'check_';
   for(i = 1; i <= getAllRows() ; i++) {
      checkedItem = ExGetElementByID (checkID + i)
      if (checkedItem == null)
         break;
      else {
         if (checkedItem.checked) result++;
      }
   }
   return result;
}

// count of all items
function getAllRows() {
   var nCount = ExGetElementByID ('nAllRows');
   if (nCount != null)
      return nCount.value;
   else
      return 0;
}

// serialize checked items
function getSerializeItems() {
   var i;
   var result  = '';
   var checkID = 'check_';
   for(i = 1; i <= getAllRows() ; i++) {
      checkedItem = ExGetElementByID (checkID + i);
      if (checkedItem == null)
         break;
      else {
         if (checkedItem.checked) {
            if(result == '')
               result = checkedItem.value;
            else
               result = result + ',' + checkedItem.value;
         }
      }
   }
   return result;
}

// serialize amounts of checked items
function getSerializeAmounts() {
   var i;
   var result   = '';
   var checkID  = 'check_';
   var amountID = 'amount_';
   for(i = 1; i <= getAllRows() ; i++) {
      checkedItem = ExGetElementByID (checkID + i);
      amountItem  = ExGetElementByID (amountID + i);
      if (checkedItem == null)
         break;
      else {
         if (checkedItem.checked) {
            if(result == '')
               result = amountItem.value;
            else
               result = result + ',' + amountItem.value;
         }
      }
   }
   return result;
}

// forward (extended)
function goItem(nActionID, nItem, nSelID, sMessage) {
/****************************************************************************************************************************
 nActionID :  1 - Form - Browser - products overview
              2 - Form - Browser - groups overview
              3 - Form - Browser - parameters overview
              4 - Form - Browser - photos overview
              5 - Form - Browser - users overview
              6 - Form - Webpage
              7 - Form - Browser - relation between products and groups
              8 - Form - Add new item
              9 - Form - Add new item
             10 - Form - Browser - manuals overview
             11 - DML - recalculate amount of chosen item
             12 - DML - recalculate amounts of checked/all items
             13 - DML - delete chosen item
             14 - DML - delete checked items
             15 - DML - delete all items
             16 - DML - insert new item data
             17 - DML - update changed data
             18 - Form - Browser - revert changes and forward to products
             19 - Form - Detail - add new item
             20 - Form - Detail - edit chosen item
             21 - Form - Detail - detail of chosen item
             22 - DML - insert new item data
             23 - Form - Browser - revert changes and forward to parameters
             24 - Form - Browser - relation between products and parameters
             25 - DML - update changed data
             26 -
             27 -
             28 -
             29 -
             30 -
             31 - DML - delete chosen item
             32 - DML - delete/restore checked items
             33 - DML - delete all items
             34 - DML - insert new item data
             35 - DML - update changed data
             36 - Form - Browser - revert changes and forward to groups
             37 - Form - Detail - add new item
             38 - Form - Detail - edit chosen item
             39 - Form - Detial - detail of chosen item
             40 - DML - delete/restore checked items
             41 - DML - delete checked items
             42 - DML - delete checked items
             43 - DML - delete checked items
             44 - DML - delete/restore checked items
             45 - Form - Detail - edit chosen item
             46 - DML - delete chosen item
             47 - Form - Detail - detail of chosen item
             50 - Form - Browser - parameters overview
             51 - Form - Detail - add new item
*****************************************************************************************************************************/

   var nAction     = ExGetElementByID ('nAction');
   var form        = ExGetElementByID ('sForm');
   var nItemLink   = ExGetElementByID ('nItemLink');
   var nItemAmount = ExGetElementByID ('nItemAmount');
   var bContinue   = false;

   switch (nActionID) {

      case 1: // Form - Browser - products overview
         bContinue = false;
         location.href = 'b_product.php';
         break;

      case 2: // Form - Browser - groups overview
         bContinue = false;
         location.href = 'b_group.php';
         break;

      case 3: // Form - Browser - parameters overview
         bContinue = false;
         location.href = 'b_param.php';
         break;

      case 4: // Form - Browser - photos overview
         bContinue = false;
         location.href = 'b_foto.php';
         break;

      case 5: // Form - Browser - users overview
         bContinue = false;
         location.href = 'b_users.php';
         break;

      case 6: // Form - Webpage
         bContinue = false;
         location.href = '../index.php';
         break;

      case 7: // Form - Browser - relation between products and groups
         bContinue = false;
         location.href = 'b_prodgroup.php';
         break;

      case 8: // Form - Add new item
         bContinue = false;
         location.href = '../obr/file.php';
         break;
         
      case 9: // Form - Add new item
         bContinue = false;
         location.href = '../manual/file.php';
         break;

      case 10: // Form - Browser - manuals overview
         bContinue = false;
         location.href = 'b_manual.php';
         break;

      case 11: // DML - recalculate amount of chosen item
         bContinue = true;
         nItemLink.value = nItem;
         checkChosenOne(nSelID);
         nItemAmount.value = getSerializeAmounts();
         break;

      case 12: // DML - recalculate amounts of checked/all items
         if (getCheckedRows() == 0)
         {
            if (confirm("Nie sú vybrané žiadne položky. Označiť všetky a pokračovať?")) {
               checkAll(1);
               bContinue = true;
            }
            else {
               bContinue = false;
            }
         }
         else
         {
            bContinue = true;
         }

         if (bContinue)
         {
               nItemLink.value   = getSerializeItems();
               nItemAmount.value = getSerializeAmounts();
         }

         break;

      case 13: // DML - delete chosen item
         if (confirm(sMessage))
         {
            checkChosenOne(nSelID);
            nItemLink.value = nItem;
            bContinue = true;
         }
         else
         {
            bContinue = false;
         }
         break;

      case 14: // DML - delete checked items
         if (getCheckedRows() != 0) {
            bContinue = confirm(sMessage);
            // send values
            nItemLink.value = getSerializeItems();
            //alert(nItem.value);
         }
         else {
            alertNoItem();
         }
         break;

      case 15: // DML - delete all items
         bContinue = confirm(sMessage);
         checkAll(1);
         nItemLink.value = -1;
         break;

      case 16: // DML - insert new item data
         location.href = 'd_product.php';
         bContinue = true;
         break;

      case 17: // DML - update changed data
         nItemLink.value = nItem;
         bContinue = true;
         break;

      case 18: // Form - Browser - revert changes and forward to products
         bContinue = false;
         if (sMessage.length != 0) {
            if (confirm(sMessage))
            {
               goItemEx(1);
            }
         }
         else
         {
            goItemEx(1);
         }
         break;

      case 19: // Form - Detail - add new item
         bContinue = false;
         location.href = 'd_product.php';
         break;

      case 20: // Form - Detail - edit chosen item
         bContinue = false;
         location.href = 'd_product.php?id='+nItem+'&action=edit';
         break;

      case 21: // Form - Detail - detail of chosen item
         bContinue = false;
         location.href = 'd_product.php?id='+nItem+'&action=detail';
         break;

      case 22: // DML - insert new item data
         location.href = 'd_parameter.php';
         bContinue = true;
         break;

      case 23: // Form - Browser - revert changes and forward to parameters
         bContinue = false;
         if (sMessage.length != 0) {
            if (confirm(sMessage))
            {
               goItemEx(50);
            }
         }
         else
         {
            goItemEx(50);
         }
         break;
         
      case 24: // Form - Browser - relation between products and parameters
         bContinue = false;
         location.href = 'b_prodparam.php';
         break;

      case 25: // DML - update changed data
         nItemLink.value = nItem;
         bContinue = true;
         break;

      case 31: // DML - delete chosen item
         if (confirm(sMessage))
         {
            checkChosenOne(nSelID);
            nItemLink.value = nItem;
            bContinue = true;
         }
         else
         {
            bContinue = false;
         }
         break;

      case 32: // DML - delete/restore checked items
         if (getCheckedRows() != 0) {
            bContinue = confirm(sMessage);
            // send values
            nItemLink.value = getSerializeItems();
            //alert(nItem.value);
         }
         else {
            alertNoItem();
         }
         break;

      case 33: // DML - delete all items
         bContinue = confirm(sMessage);
         checkAll(1);
         nItemLink.value = -1;
         break;

      case 34: // DML - insert new item data
         location.href = 'd_group.php';
         bContinue = true;
         break;

      case 35: // DML - update changed data
         nItemLink.value = nItem;
         bContinue = true;
         break;

      case 36: // Form - Browser - revert changes and forward to groups
         bContinue = false;
         if (sMessage.length != 0) {
            if (confirm(sMessage))
            {
               goItemEx(2);
            }
         }
         else
         {
            goItemEx(2);
         }
         break;

      case 37: // Form - Detail - add new item
         bContinue = false;
         location.href = 'd_group.php';
         break;

      case 38: // Form - Detail - edit chosen item
         bContinue = false;
         location.href = 'd_group.php?id='+nItem+'&action=edit';
         break;

      case 39: // Form - Detail - detail of chosen item
         bContinue = false;
         location.href = 'd_group.php?id='+nItem+'&action=detail';
         break;

      case 40: // DML - delete/restore checked items
         bContinue = confirm(sMessage);
         // send values
         nItemLink.value = getSerializeItems();
         //alert(nItem.value);
         break;

      case 41: // DML - delete/restore checked items
        if (getCheckedRows() != 0) {
            bContinue = confirm(sMessage);
            // send values
            nItemLink.value = getSerializeItems();
            //alert(nItem.value);
         }
         else {
            alertNoItem();
         }
         break;

      case 42: // DML - delete/restore checked items
        if (getCheckedRows() != 0) {
            bContinue = confirm(sMessage);
            // send values
            nItemLink.value = getSerializeItems();
            //alert(nItem.value);
         }
         else {
            alertNoItem();
         }
         break;

      case 43: // DML - delete/restore checked items
        if (getCheckedRows() != 0) {
            bContinue = confirm(sMessage);
            // send values
            nItemLink.value = getSerializeItems();
            //alert(nItem.value);
         }
         else {
            alertNoItem();
         }
         break;
         
      case 44: // DML - delete/restore checked items
         bContinue = confirm(sMessage);
         // send values
         nItemLink.value = getSerializeItems();
         //alert(nItem.value);
         break;

      case 45: // Form - Detail - edit chosen item
         bContinue = false;
         location.href = 'd_parameter.php?id='+nItem+'&action=edit';
         break;

      case 46: // DML - delete chosen item
         if (confirm(sMessage))
         {
            checkChosenOne(nSelID);
            nItemLink.value = nItem;
            bContinue = true;
         }
         else
         {
            bContinue = false;
         }
         break;

      case 47: // Form - Detail - detail of chosen item
         bContinue = false;
         location.href = 'd_parameter.php?id='+nItem+'&action=detail';
         break;

      case 50: // Form - Browser - parameters overview
         bContinue = false;
         location.href = 'b_parameter.php';
         break;

      case 51: // Form - Detail - add new item
         bContinue = false;
         location.href = 'd_parameter.php';
         break;
   }

   if (bContinue)
   {
      nAction.value = nActionID;
      form.submit();
   }
}

// filter on
function applyFilter(nCatID, sSearch, sUrl) {
   location.href = sUrl + '?category=' + nCatID + '&search=' + sSearch;
}

// filter off
function clearFilter(sUrl) {
   location.href = sUrl;
}

// import a new prices
function doImport(sMessage) {
   var pokracuj = false;
       pokracuj = confirm(sMessage);
       
   if (pokracuj)
   { location.href = 'import.php'; }
}

// edit group template
function GTemplate(nID,sMessage)
{
   var pokracuj = false;
       pokracuj = confirm(sMessage);

   if (pokracuj)
   { location.href = './editor/articles/htmlg/?Toolbar=Custom&id='+nID; }
}

// edit product template
function PTemplate(nID,sMessage)
{
   var pokracuj = false;
       pokracuj = confirm(sMessage);
       
   if (pokracuj)
   { location.href = './editor/articles/htmlp/?Toolbar=Custom&id='+nID; }
}

// no data found
function alertNoItem() {
   alert("Nie sú vybrané žiadne položky");
}

// alert and forward
function goAlert(nActionID, nSelID, sMessage)
{
   goItem(nActionID, 0, nSelID, sMessage)
}

// forward
function goItemEx(nActionID)
{
   goItem(nActionID, 0, 0, "")
}

// return only numbers
function onlyNumber(nSelID,vField)
{
   if (isNaN(vField) || (vField>255) || (vField<0))
   {
      alert('Nezadali ste číslo v rozmedzí 0-255');
      ExGetElementByID ('amount_' + nSelID).value = 0;
   }
   else
   {
      checkChosenOne(nSelID);
   }
}

function changeImg(sNewFileName)
{
   ExGetElementByID ('image').src = '../lib/thumbs.php?im=' + sNewFileName + '&size=70';
   ExGetElementByID ('image').alt = 'ilustračný obrázok produktu je nedostupný';
   ExGetElementByID ('image').OnClick = '';
}

function nullSelect(combobox)
{
   // ak je prazdne pole comboboxu tak zvolime default value
   if (ExGetElementByID (combobox).selectedIndex == 1)
   {
      ExGetElementByID (combobox).selectedIndex = 0;
   }
}

function changeSelectC(combobox,nCatID)
{
   // nacitanie produktov pre vybranu kategoriu
   location.href = 'b_prodgroup.php?category=' + nCatID;
}

function changeSelectP(combobox,nProdID)
{
   // nacitanie parametrov pre vybrany produkt
   location.href = 'b_prodparam.php?product=' + nProdID;
}
