

function GetAjaxFrebelStockValues(ArticleNames, SessionID, Country, startrow, frebellUser, SetPricesCountry, ConvertCurrency){
//         ShowLoading();
         if (Country == 'Finland') {
             new Ajax.Updater('ape_saldoa', 'ajax_ape_saldot.php', {
                 parameters: { etsit: ArticleNames , sid: SessionID, Country: Country },
                 onComplete: function(transport) {
                     HideLoading();
                     SetAtoyPrices(startrow);
                 }});
         } else {
            new Ajax.Updater('ape_saldoa', 'ajax_ape_saldot.php', {
                    parameters: { etsit: ArticleNames, sid: SessionID,  frebellUser: frebellUser, Country: Country, ConvertCurrency: ConvertCurrency },
                    onComplete: function(transport) {
                        HideLoading();
                        if (SetPricesCountry == "Finland") {
                            SetAtoyPrices(startrow);
                        } else {
                            SetPrices(startrow);
                        }
                        
                    }
                });
         }

}

function GetAjaxFrebelStockValuesTemot(ArticleNames, SessionID, Country, BlockId, startrow){
         if (Country == 'Finland') {
             new Ajax.Updater('ape_saldo_' + BlockId, 'ajax_ape_saldot.php', { parameters: { etsit: ArticleNames , sid: SessionID, Country: Country }, onComplete: function(transport) {HideLoading();  SetAtoyPricesTemot(BlockId);    }});
         } else {
             new Ajax.Updater('ape_saldo_' + BlockId, 'ajax_ape_saldot.php', { parameters: { etsit: ArticleNames , sid: SessionID }, onComplete: function(transport) {HideLoading();   SetPricesTemot(BlockId,startrow);  }});
         }

}

function SetAtoyPrices(startrow) {
	var i = 0;
	var ArtId, Price, NetPrice, TaxPrice, Saldo, ArtInfo, AvailableCustomer, ReplacedItemCode;

	var result_arrays = $('ape_saldoa').innerHTML.split(';');
    var UserRole;
    var z;
    if (startrow) {
        z = startrow;
    } else {
        z = i;
    }

    if ($('UserRole')) {
  	  UserRole = $('UserRole').value;
    } else {
  	  UserRole = 0;
    }

    var UserId;
    if ($('UserId')) {
  	  UserId = $('UserId').value;
    } else {
  	  UserId = 0;
    }

	while(result_arrays[i] != null){
	  ArtInfo = result_arrays[i].split('$');
	  ArtId = ArtInfo[0];
	  if (ArtId != ""){
          if (ArtInfo[1] != '?'){
            Price =  ArtInfo[1].replace('.', ',');		//Math.round(ArtInfo[1]);
          } else {
            Price = ArtInfo[1];
          }


          Saldo = ArtInfo[2].replace(/^\s+|\s+$/g, '');
          if (Saldo == ''){
              Saldo = 0;
          }
          NetPrice = ArtInfo[3].replace('.', ',');
          Discount = ArtInfo[4];
          ABCCode = ArtInfo[5];

          if (ArtInfo[6] != '?'){
            TaxPrice = ArtInfo[6];
          } else {
            TaxPrice = ArtInfo[6];
          }

          TaxPrice = TaxPrice.replace(/^\s+|\s+$/g, '');
          TaxPrice = new Number(TaxPrice);
          TaxPrice = TaxPrice.toFixed(2);
          DTaxPrice = TaxPrice + '';
          DTaxPrice = DTaxPrice.replace('.', ',');

          Text = ArtInfo[7];
          AvailableCustomer = ArtInfo[8].replace(/^\s+|\s+$/g, '') ;

          GarageTooltip = ArtInfo[7];

          var garageuser;
          if ($('GarageUser')) {
                garageuser = $('GarageUser').value;
          } else {
              garageuser = 0;
          }
          var noSale = '';

          var saldoText = 'Keskusvarasto ' + Text;
          if (Saldo == 0) {
              if ((UserRole == 'wholesaler' || UserRole == 'cs') && ABCCode.match('(C|D).*2')) {
                  saldoText = 'Tehdastilaustuote ' + Text;
                  Saldo = "<img style='cursor:pointer; height: 17px;' SRC='pictures/more_information_icon.gif' alt='0' onclick=\"GetByAjax({file : 'ajax.php', PageName : 'GetStockValues', element : 'ArticleStockValues', TailFunction: 'ShowArticleStockValues', PreFunction : 'ShowArticleStockValues', 'LineNumber' : '" + i + "', ArtId: '" + ArtId + "'});\"/>";
                  if ((AvailableCustomer == '') && (UserRole=='wholesaler')) {
                      noSale = "<span title='Olisin tilannut tuotteen jos sitä olisi ollut varastossa.' onclick=\"GetByAjax({file : 'ajax.php', PageName : 'MissedSale', element : 'MissedSale', ArtId: '" + ArtId + "', UserId: '" + UserId + "'});alert('Kiitos!'); _gaq.push(function() { var pageTracker = _gat._getTracker('UA-9150002-2'); pageTracker._trackPageview('/NoSale/" + ArtId + "'); });\"><img src=\"images/nosales.gif\" alt=\"Olisin tilannut tuotteen jos sitä olisi ollut varastossa.\" style='cursor:pointer' ></span>";
                  }
              }
              else if ((AvailableCustomer == '') && (garageuser == 1)) {
                  Saldo = "<img src=\"images/telephoneIcon.gif\" alt=\"0\"/>";
              }
          }
          if (garageuser == 1) {
              NetPrice = '';
          }
          // show customersalesprice if there is one

          if ($('PriceType').value == 1){
              DTaxPrice = NetPrice;
          }


          if (NetPrice && NetPrice != ''){
              Title = NetPrice;
          } else if (garageuser == 1){
              Title = GarageTooltip;
          }

          var discountCode = ArtInfo[10];
          var discountCodeChar = ArtInfo[11];
          discountCode = discountCode.replace(/^\s+|\s+$/g, '') ;
          if (discountCodeChar){
            discountCodeChar = discountCodeChar.replace(/^\s+|\s+$/g, '') ;
          } else {
            discountCodeChar = "";
          }
          if (discountCode != "" || discountCodeChar != "") {
              Title = Title + "   Alennustiedot: " + discountCode + " / " + discountCodeChar + "  ";
          }

          //var warehouseCode = ABCCode.trim();
          var warehouseCode = ABCCode.replace(/^\s+|\s+$/g, '') ;
              
          if ((warehouseCode == 'D') && (Saldo > 0) && (1==0)) {
              $('ape_vat_price'+z).innerHTML = "<span style='color: red;' title='" + Title + "' >" + DTaxPrice + "</span><br/>Hinta koskee loppuvarastoa";
          } else {
              $('ape_vat_price'+z).innerHTML = "<span title='" + Title + "' >" + DTaxPrice + "</span>";
          }


          if (AvailableCustomer != ''){
              $('ape_saldo'+z).innerHTML= "<span style='white-space:nowrap'>"
                                        + "<span TITLE='Liikkeen varasto'>"
                                        + "<b style='color:green'>"
                                        + AvailableCustomer + "</b></span>"
                                        + " &nbsp;"
                                        + "<span TITLE='"
                                        + saldoText + "'>" + Saldo + "</span></span>";
            if ($('no_sale_'+(z+1))) {
                $('no_sale_'+(z+1)).innerHTML = noSale;
            }
            if ($('no_sale_npu_'+(z+1))) {
                $('no_sale_npu_'+(z+1)).innerHTML = noSale;
            }
          } else {
                $('ape_saldo'+z).innerHTML = "<span TITLE='"
                                        + saldoText + "'>" + Saldo + "</span>";
                if ($('no_sale_'+(z+1))) {
                    $('no_sale_'+(z+1)).innerHTML = noSale;
                }
                if ($('no_sale_npu_'+(z+1))) {
                    $('no_sale_npu_'+(z+1)).innerHTML = noSale;
                }
          }

          j = z + 1;
          j = parseFloat(j);
          if ($('Price'+j) && $('VatPrice'+j)){
              $('Price'+j).value = TaxPrice;
              $('VatPrice'+j).value = TaxPrice;
              $('StockValue'+j).value = Saldo;
          }
      }
      i++;
	  z++;

	}

}



function SetPrices(startrow) {
    var i = 0;
    var j = 1;
    var ArtId;
    var Price;
    var NetPrice;
    var TaxPrice;
    var Saldo;
    var ArtInfo;
    var ItemClass;
    var HandlingStatus;
    var AvailableCustomer;
    var z;
    if (startrow) {
        z = startrow;
    } else {
        z = i;
    }

    var result_arrays = $('ape_saldoa').innerHTML.split(';');
    var UserRole;
    if ($('UserRole')) {
  	  UserRole = $('UserRole').value;
    } else {
  	  UserRole = 0;
    }
    var HideSaldoInfoForPrivateCustomers;
    if ($('HideSaldoInfoForPrivateCustomers')) {
  	  HideSaldoInfoForPrivateCustomers = $('HideSaldoInfoForPrivateCustomers').value;
    } else {
  	  HideSaldoInfoForPrivateCustomers = 0;
    }
    var UserId;
    if ($('UserId')) {
  	  UserId = $('UserId').value;
    } else {
  	  UserId = 0;
    }
    var garageuser;
    if ($('GarageUser')) {
            garageuser = $('GarageUser').value;
    } else {
          garageuser = 0;
    }
        
    while(result_arrays[i] != null){
      ArtInfo = result_arrays[i].split('$');
      ArtId = ArtInfo[0];
      if (ArtInfo[1] != '?'){
            Price =  parseFloat(ArtInfo[1]);		//Math.round(ArtInfo[1]);
          //  Price =  Price.round();		//Math.round(ArtInfo[1]);
            Price =  Price.toFixed(2);		//Math.round(ArtInfo[1]);
      } else {
            Price = ArtInfo[1];
      }
      Saldo = ArtInfo[2];
      NetPrice = ArtInfo[3];
      Discount = ArtInfo[4];
      ABCCode = ArtInfo[5];
      ItemClass = ArtInfo[10];
      HandlingStatus = ArtInfo[11];

      if (ArtInfo[6] != '?'){

        if ($('LanguageCode')) {
            if ($('LanguageCode').value == 35) {
                TaxPrice = (parseFloat(ArtInfo[6]) / 1.25)*1.20;
                //TaxPrice = Math.round(TaxPrice*100)/100;
                TaxPrice = TaxPrice.toFixed(2);
            } else {
                TaxPrice = parseFloat(ArtInfo[6]);
                //TaxPrice = Math.round(TaxPrice*100)/100;
                TaxPrice = TaxPrice.round();
                TaxPrice = TaxPrice.toFixed(2);
            }
        } else {
            TaxPrice = parseFloat(ArtInfo[6]);
            //TaxPrice = Math.round(TaxPrice*100)/100;
            TaxPrice = TaxPrice.round();
            TaxPrice = TaxPrice.toFixed(2);
        }

	  } else {
            TaxPrice = 'Ny artikel';
            Price = '';
            Saldo = '0';
            ABCCode = '-';
            Discount = '-';
	 }

	  Text = ArtInfo[7];
	  AvailableCustomer = ArtInfo[8].replace(/^\s+|\s+$/g, '') ;
	  AvailableRMA = ArtInfo[9].replace(/^\s+|\s+$/g, '') ;

      var noSale = '<span id="Test"></span>';
      var HandlingText = '';
      if ($('teccom_' + ArtId)) {
          if ($('teccom_' + ArtId).value == 1) {
              var teccom_supplier = true;
          } else {
              teccom_supplier = false;
          }
      } else {
          teccom_supplier = false;
      }
      var more_info = false;
      if (ItemClass == 2) {
          if (teccom_supplier) {
              more_info = true;
          }
          if (HandlingStatus.match("STOPP")) {
              HandlingText = '<br/><span style="color: red;">Tillfälligt handelsstoppad</span>';
              more_info = false;
          }
          if (HandlingStatus.match("UTGÅR") || HandlingStatus.match("UTGÅR1")) {
              HandlingText = '<br/><span style="color: red;">Utgående</span>';
              more_info = false;
          }
      }
      var warehouseCode = ABCCode.replace(/^\s+|\s+$/g, '') ;
      if ($('ape_price'+z)) {
          if (warehouseCode.charAt(0) == 'D' && Saldo != 0) {
              $('ape_price'+z).innerHTML = "<span style='color: red;'>" + Price.replace('.',',') + "</span><br/>Priset gäller så länge lagret räcker" + HandlingText;
              $('ape_vat_price'+z).innerHTML = "<span style='color: red;'>" + TaxPrice.replace('.',',') + "</span><br/>Priset gäller så länge lagret räcker" + HandlingText;
          } else {
              $('ape_price'+z).innerHTML=Price.replace('.',',');
              $('ape_vat_price'+z).innerHTML = TaxPrice.replace('.',',') + HandlingText;
          }
      }
      var saldoText = '';
      if (UserRole == 'guest') {
        if (HideSaldoInfoForPrivateCustomers > 0) {
            $('ape_saldo'+z).innerHTML = '';
        }
        else if (AvailableCustomer != '' && AvailableCustomer > 0) {
                $('ape_saldo'+z).innerHTML = "<img src='pictures/green_dot.gif' alt='' id='toolTip_" + z + "'/>";
                new Tooltip("toolTip_" + z, 'toolTipText', { offset: {x: -5, y: -90 }, start_effect: function(element) { element.appear({ duration: 0.5 }); } });
        } else if (Saldo.replace('+', '') > 0) {
                $('ape_saldo'+z).innerHTML = "<img src='pictures/yellow_dot.gif' alt='' id='toolTip_" + z + "'/>";
                new Tooltip("toolTip_" + z, 'toolTipText', { offset: {x: -5, y: -90 }, start_effect: function(element) { element.appear({ duration: 0.5 }); } });
        } else {
                $('ape_saldo'+z).innerHTML = "<img src='pictures/red_dot.gif' alt='' id='toolTip_" + z + "'/>";
                new Tooltip("toolTip_" + z, 'toolTipText', { offset: {x: -5, y: -90 }, start_effect: function(element) { element.appear({ duration: 0.5 }); } });
        }
      } else {
          if (Saldo == 0) {
              // user admin_id is not null => show telephone icon if Saldo = 0

              if (garageuser == 1) {
                  Saldo = "<img SRC=\"images/telephoneIcon.gif\" alt=\"0\"/>";
                  saldoText = "Centrallagers lagersaldo - " + Text;
              } else if ((UserRole == 'wholesaler' || UserRole == 'bds_wholesaler' || UserRole == 'cs') && more_info) {
                  Saldo = "<img style='cursor:pointer; height: 17px;' SRC='pictures/more_information_icon.gif' alt='0' onclick=\"GetByAjax({file : 'ajax.php', PageName : 'GetStockValues', element : 'ArticleStockValues', TailFunction: 'ShowArticleStockValues', PreFunction : 'ShowArticleStockValues', 'LineNumber' : '" + i + "', ArtId: '" + ArtId + "'});\"/>";
                  saldoText = "Denna artikel lagerförs ej hos Atoy. Klicka på i:et för saldoinformation hos leverantören";
              } else {
                  saldoText = "Centrallagers lagersaldo - " + Text;
              }

              if (UserRole == 'wholesaler' || UserRole == 'bds_wholesaler') {
                  noSale = "<span title='Skulle ha beställt om artikeln hade funnits i lager.' onclick=\"GetByAjax({file : 'ajax.php', PageName : 'MissedSale', element : 'MissedSale', ArtId: '" + ArtId + "', UserId: '" + UserId + "'});alert('Tack!');_gaq.push(function() { var pageTracker = _gat._getTracker('UA-9150002-1'); pageTracker._trackPageview('/NoSale/" + ArtId + "'); });\"><img src=\"images/nosales.gif\" alt=\"Skulle ha beställt om artikeln hade funnits i lager.\" style='cursor:pointer' /></span>";
              }

          }
          if ($('ape_saldo'+z)) {
          	  if (AvailableCustomer != ''){
                  $('ape_saldo'+z).innerHTML= "<span style='white-space:nowrap'>"
                                            + "<span TITLE='Butikens lagersaldo'>"
                                            + "<b style='color:green'>"
                                            + AvailableCustomer + "</b></span>"
                                            + " &nbsp;"
                                            + "<span TITLE='" + saldoText
                                            + "'>" + Saldo + "</span></span>";
                if ($('no_sale_'+(z+1))) {
                    $('no_sale_'+(z+1)).innerHTML = noSale;
                }
                if ($('no_sale_npu_'+(z+1))) {
                    $('no_sale_npu_'+(z+1)).innerHTML = noSale;
                }
              } else {
                  $('ape_saldo'+z).innerHTML = "<span TITLE='" + saldoText
                                            + "'>" + Saldo + "</span>";
                if ($('no_sale_'+(z+1))) {
                    $('no_sale_'+(z+1)).innerHTML = noSale;
                }
                if ($('no_sale_npu_'+(z+1))) {
                    $('no_sale_npu_'+(z+1)).innerHTML = noSale;
                }
                  //$('ape_saldo'+i).innerHTML = "<span TITLE='Centrallagers lagersaldo - "
                  //                          + Text + "'>" + Saldo + "&nbsp;</span>";
              }
          }
      }

	  if ((AvailableRMA != '') && (UserRole == 'wholesaler' || UserRole == 'bds_wholesaler')){
                if ($('in_rma_'+(z+1))) {
		  $('in_rma_'+(z+1)).innerHTML = '<span title="Artikeln markerad för retur, men har inte anlänt till APE"><img src="images/rmaicon.gif"></span>';
                }
	  }

	  $('ape_code'+z).innerHTML = ABCCode+ "  "+Discount;
//	  if ($('ape_c_saldo'+i)){
//	    $('ape_c_saldo'+i).innerHTML="(" + AvailableCustomer + ")";
//	  }

	  j = z + 1;
      j = parseFloat(j);

	  if ($('Price'+j) && $('VatPrice'+j)){
		  $('Price'+j).value = Price;
	      $('VatPrice'+j).value = TaxPrice;
	      $('StockValue'+j).value = Saldo;
      }

	  i++;
      z++;

	}

}

function SetAtoyPricesTemot(BlockId) {
    var i = 0;
    var ArtId;
    var NetPrice;
    var TaxPrice;
    var Saldo;
    var ArtInfo;
    var AvailableCustomer;

    var result_arrays = $('ape_saldo_' + BlockId).innerHTML.split(';');
    var UserRole;

    if ($('UserRole')) {
  	  UserRole = $('UserRole').value;
    } else {
  	  UserRole = 0;
    }

    var UserId;
    if ($('UserId')) {
  	  UserId = $('UserId').value;
    } else {
  	  UserId = 0;
    }

	while(result_arrays[i] != null){
	  ArtInfo = result_arrays[i].split('$');
	  ArtId = ArtInfo[0];
	  if (ArtId != ""){
	  if (ArtInfo[1] != '?'){
		Price =  ArtInfo[1].replace('.', ',');		//Math.round(ArtInfo[1]);
	  } else {
		Price = ArtInfo[1];
	  }


	  Saldo = ArtInfo[2].replace(/^\s+|\s+$/g, '');
	  if (Saldo == ''){
		  Saldo = 0;
	  }
	  NetPrice = ArtInfo[3].replace('.', ',');
	  Discount = ArtInfo[4];
      ABCCode = ArtInfo[5];

      if (ArtInfo[6] != '?'){
    	TaxPrice = ArtInfo[6];
	  } else {
        TaxPrice = ArtInfo[6];
	  }

      TaxPrice = TaxPrice.replace(/^\s+|\s+$/g, '');
      TaxPrice = new Number(TaxPrice);
      TaxPrice = TaxPrice.toFixed(2);
      DTaxPrice = TaxPrice + '';
      DTaxPrice = DTaxPrice.replace('.', ',');

	  Text = ArtInfo[7];
	  AvailableCustomer = ArtInfo[8].replace(/^\s+|\s+$/g, '') ;

	  GarageTooltip = ArtInfo[10] + " " + ArtInfo[11] + " " + ArtInfo[12];

	  var garageuser;
      if ($('GarageUser')) {
            garageuser = $('GarageUser').value;
      } else {
          garageuser = 0;
      }
      var noSale = '';
      var saldoText = 'Keskusvarasto ' + Text;
      if (Saldo == 0) {
          if ((UserRole == 'wholesaler' || UserRole == 'cs') && ABCCode.match('(C|D).*2')) {
              saldoText = 'Tehdastilaustuote ' + Text;
              Saldo = "<img style='cursor:pointer; height: 17px;' SRC='pictures/more_information_icon.gif' alt='0' onclick=\"GetByAjax({file : 'ajax.php', PageName : 'GetStockValues', element : 'ArticleStockValues', TailFunction: 'ShowArticleStockValues', PreFunction : 'ShowArticleStockValues', 'LineNumber' : '" + i + "', ArtId: '" + ArtId + "'});\"/>";
              if ((AvailableCustomer == '') && (UserRole=='wholesaler')) {
                  noSale = "<span title='Olisin tilannut tuotteen jos sitä olisi ollut varastossa.' onclick=\"GetByAjax({file : 'ajax.php', PageName : 'MissedSale', element : 'MissedSale', ArtId: '" + ArtId + "', UserId: '" + UserId + "'});alert('Kiitos!'); _gaq.push(function() { var pageTracker = _gat._getTracker('UA-9150002-2'); pageTracker._trackPageview('/NoSale/" + ArtId + "'); });\"><img src=\"images/nosales.gif\" alt=\"Olisin tilannut tuotteen jos sitä olisi ollut varastossa.\" style='cursor:pointer' ></span>";
              }
          }
          else if ((AvailableCustomer == '') && (garageuser == 1)) {
              Saldo = "<img src=\"images/telephoneIcon.gif\" alt=\"0\"/>";
          }
      }

      if (garageuser == 1) {
    	  NetPrice = '';
      }
      // show customersalesprice if there is one

      if ($('PriceType').value == 1){
    	  DTaxPrice = NetPrice;
      }

      if (NetPrice){
    	  Title = NetPrice;
      } else if (garageuser == 1){
    	  Title = GarageTooltip;
      }

	  //var warehouseCode = ABCCode.trim();
	  var warehouseCode = ABCCode.replace(/^\s+|\s+$/g, '') ;
	  if ((warehouseCode == 'D') && (Saldo > 0)) {
		  $('ape_' + BlockId + '_vat_price'+i).innerHTML = "<span style='color: red;' TITLE='" + Title + "' >" + DTaxPrice + "</span><br/>Hinta koskee loppuvarastoa";
	  } else {
		  $('ape_' + BlockId + '_vat_price'+i).innerHTML = "<span TITLE='" + Title + "' >" + DTaxPrice + "</span>";
	  }


	  if (AvailableCustomer != ''){
		  $('ape_' + BlockId + '_saldo'+i).innerHTML= "<span style='white-space:nowrap'>"
                                    + "<span TITLE='Liikkeen varasto'>"
                                    + "<b style='color:green'>"
                                    + AvailableCustomer + "</b></span>"
                                    + " &nbsp;"
                                    + "<span TITLE='"
                                    + saldoText + "'>" + Saldo + "</span></span>";
		if ($('no_sale_' + BlockId + '_'+(i+1))) {  $('no_sale_' + BlockId + '_'+(i+1)).innerHTML = noSale; }
	  } else {
            $('ape_' + BlockId + '_saldo'+i).innerHTML = "<span TITLE='"
                                    + saldoText + "'>" + Saldo + "</span>";
            if ($('no_sale_' + BlockId + '_'+(i+1))) {  $('no_sale_' + BlockId + '_'+(i+1)).innerHTML = noSale; }
	  }
	  j = i + 1;
	  if ($('Price_' + BlockId + '_'+j) && $('VatPrice_' + BlockId + '_'+j)){
		  $('Price_' + BlockId + '_'+j).value = TaxPrice;
	      $('VatPrice_' + BlockId + '_'+j).value = TaxPrice;
	      $('StockValue_' + BlockId + '_'+j).value = Saldo;
      }
	  }
	  i++;

	}

}



function SetPricesTemot(BlockId, startrow)
{
    var i = 0;
    var ArtId;
    var Price;
    var TaxPrice;
    var Saldo;
    var ArtInfo;
    var AvailableCustomer;
    var z;
    if (startrow) {
        z = startrow;
    } else {
        z = i;
    }

    var result_arrays = $('ape_saldo_' + BlockId).innerHTML.split(';');
    var UserRole;
    if ($('UserRole')) {
  	  UserRole = $('UserRole').value;
    } else {
  	  UserRole = 0;
    }
    var HideSaldoInfoForPrivateCustomers;
    if ($('HideSaldoInfoForPrivateCustomers')) {
  	  HideSaldoInfoForPrivateCustomers = $('HideSaldoInfoForPrivateCustomers').value;
    } else {
  	  HideSaldoInfoForPrivateCustomers = 0;
    }
    var UserId;
    if ($('UserId')) {
  	  UserId = $('UserId').value;
    } else {
  	  UserId = 0;
    }
      var garageuser;
      if ($('GarageUser')) {
            garageuser = $('GarageUser').value;
      } else {
          garageuser = 0;
      }

	while(result_arrays[i] != null){
	  ArtInfo = result_arrays[i].split('$');
	  ArtId = ArtInfo[0];
          if (ArtInfo[1] != '?'){
              Price =  parseFloat(ArtInfo[1]);
//              Price =  Price.round();
              Price = Price.toFixed(2);
          }
          else{
              Price =  ArtInfo[1];
          }
          Price = ArtInfo[1];
	  Saldo = ArtInfo[2];
	  NetPrice = ArtInfo[3];
	  Discount = ArtInfo[4];
          ABCCode = ArtInfo[5];
          ItemClass = ArtInfo[10];
          HandlingStatus = ArtInfo[11];

      if (ArtInfo[6] != '?'){
    	if ($('LanguageCode')) {
	    	if ($('LanguageCode').value == 35) {
                    TaxPrice = (parseFloat(ArtInfo[6]) / 1.25)*1.20;
                    TaxPrice = TaxPrice.toFixed(2);
	    	} else {
                    TaxPrice = parseFloat(ArtInfo[6]);
                    TaxPrice = TaxPrice.round();
                    TaxPrice = TaxPrice.toFixed(2);
	    	}
    	} else {
            TaxPrice = parseFloat(ArtInfo[6]);
            TaxPrice = TaxPrice.round();
            TaxPrice = TaxPrice.toFixed(2);
    	}

	  } else {
        TaxPrice = ArtInfo[6];
	 }

	  Text = ArtInfo[7];
          if (ArtInfo[8]) {
            AvailableCustomer = ArtInfo[8].replace(/^\s+|\s+$/g, '') ;
	  }
          else {
              AvailableCustomer = '';
          }
          if (ArtInfo[9]) {
            AvailableRMA = ArtInfo[9].replace(/^\s+|\s+$/g, '') ;
          }
          else {
              AvailableRMA = '';
          }

      var noSale = '';
      var HandlingText = '';
      if ($('teccom_' + ArtId)) {
          if ($('teccom_' + ArtId).value == 1) {
              var teccom_supplier = true;
          }
          else {
              teccom_supplier = false;
          }
      }
      else {
          teccom_supplier = false;
      }
      var more_info = false;
      if (ItemClass == 2) {
          if (teccom_supplier) {
              more_info = true;
          }
          if (HandlingStatus.match("STOPP")) {
              HandlingText = '<br/><span style="color: red;">Tillfälligt handelsstoppad</span>';
              more_info = false;
          }
          if (HandlingStatus.match("UTGÅR") || HandlingStatus.match("UTGÅR1")) {
              HandlingText = '<br/><span style="color: red;">Utgående</span>';
              more_info = false;
          }
      }

          if (ABCCode == 'undefined') {
            var warehouseCode = ABCCode.replace(/^\s+|\s+$/g, '') ;
          }
          else {
              warehouseCode = '';
          }
	  if (warehouseCode.charAt(0) == 'D' && Saldo != 0) {
		  $('ape_' + BlockId + '_price'+z).innerHTML = "<span style='color: red;'>" + Price.replace('.',',') + "</span><br/>Priset gäller så länge lagret räcker" + HandlingText;
		  $('ape_' + BlockId + '_vat_price'+z).innerHTML = "<span style='color: red;'>" + TaxPrice.replace('.',',') + "</span><br/>Priset gäller så länge lagret räcker" + HandlingText;
	  } else {
		  $('ape_' + BlockId + '_price'+z).innerHTML=Price.replace('.',',');
		  $('ape_' + BlockId + '_vat_price'+z).innerHTML = TaxPrice.replace('.',',') + HandlingText;
	  }
      var saldoText = '';
      if (UserRole == 'guest') {
          //$('toolTipText').style.display = 'inline';
        if (HideSaldoInfoForPrivateCustomers > 0) {
            $('ape_' + BlockId + '_saldo'+z).innerHTML = '';
        }
        else if (AvailableCustomer != '' && AvailableCustomer > 0) {
                $('ape_' + BlockId + '_saldo'+z).innerHTML = "<img src='pictures/green_dot.gif' alt='' id='toolTip_" + z + "'>";
                new Tooltip("toolTip_" + z, 'toolTipText', { offset: {x: -5, y: -90 }, start_effect: function(element) { element.appear({ duration: 0.5 }); } });
        } else if (Saldo.replace('+', '') > 0) {
                $('ape_' + BlockId + '_saldo'+z).innerHTML = "<img src='pictures/yellow_dot.gif' alt='' id='toolTip_" + z + "'>";
                new Tooltip("toolTip_" + z, 'toolTipText', { offset: {x: -5, y: -90 }, start_effect: function(element) { element.appear({ duration: 0.5 }); } });
        } else {
                $('ape_' + BlockId + '_saldo'+z).innerHTML = "<img src='pictures/red_dot.gif' alt='' id='toolTip_" + z + "'>";
                new Tooltip("toolTip_" + z, 'toolTipText', { offset: {x: -5, y: -90 }, start_effect: function(element) { element.appear({ duration: 0.5 }); } });
        }
      } else {
          if (Saldo == 0) {
              // user admin_id is not null => show telephone icon if Saldo = 0

              if (garageuser == 1) {
                  Saldo = "<img SRC=\"images/telephoneIcon.gif\" alt=\"0\">";
                  saldoText = "Centrallagers lagersaldo - " + Text;
//              } else if (UserId == 62 && more_info) {
              } else if ((UserRole == 'wholesaler' || UserRole == 'bds_wholesaler' || UserRole == 'cs') && more_info) {
                  Saldo = "<img style='cursor:pointer; height: 17px;' SRC='pictures/more_information_icon.gif' alt='0' onclick=\"GetByAjax({file : 'ajax.php', PageName : 'GetStockValues', element : 'ArticleStockValues', TailFunction: 'ShowArticleStockValues', PreFunction : 'ShowArticleStockValues', 'LineNumber' : '" + i + "', ArtId: '" + ArtId + "'});\">";
                  saldoText = "Denna artikel lagerförs ej hos Atoy. Klicka på i:et för saldoinformation hos leverantören";
              }
              else {
                  saldoText = "Centrallagers lagersaldo - " + Text;
              }

              if (UserRole == 'wholesaler' || UserRole == 'bds_wholesaler') {
                  noSale = "<span title='Skulle ha beställt om artikeln hade funnits i lager.' onclick=\"GetByAjax({file : 'ajax.php', PageName : 'MissedSale', element : 'MissedSale', ArtId: '" + ArtId + "', UserId: '" + UserId + "'});alert('Tack!');_gaq.push(function() { var pageTracker = _gat._getTracker('UA-9150002-1'); pageTracker._trackPageview('/NoSale/" + ArtId + "'); });\"><img src=\"images/nosales.gif\" alt=\"Skulle ha beställt om artikeln hade funnits i lager.\" style='cursor:pointer' ></span>";
              }

          }
          var zz = z + 1;
          if (AvailableCustomer != ''){
                  $('ape_' + BlockId + '_saldo'+z).innerHTML= "<span style='white-space:nowrap'>"
                                            + "<span TITLE='" + saldoText
                                            + "'>" + Saldo + "</span>"
                                            + " &nbsp;"
                                            + "<span TITLE='Butikens lagersaldo'>"
                                            + "<b style='color:green'> "
                                            + AvailableCustomer + "</b></span></span>";
                if ($('no_sale_' + BlockId + '_' + zz)) {  $('no_sale_' + BlockId + '_' + zz).innerHTML = noSale; }
          } else {
            $('ape_' + BlockId + '_saldo'+z).innerHTML = "<span TITLE='" + saldoText
                                            + "'>" + Saldo + "</span>";
            if ($('no_sale_' + BlockId + '_' + zz)) {  $('no_sale_' + BlockId + '_' + zz).innerHTML = noSale; }
          }
      }
      if ((AvailableRMA != '') && (UserRole == 'wholesaler' || UserRole == 'bds_wholesaler')){
        if ($('in_rma_' + BlockId + '_'+(z+1))) {
          $('in_rma_' + BlockId + '_'+(z+1)).innerHTML = '<span TITLE="Artikeln markerad för retur, men har inte anlänt till APE"><img src="images/rmaicon.gif"></span>';
        }
      }

      $('ape_' + BlockId + '_code'+z).innerHTML = ABCCode+ "  "+Discount;


      var j = i + 1;
      if ($('Price_' + BlockId + '_'+j) && $('VatPrice_' + BlockId + '_'+j)){
          $('Price_' + BlockId + '_'+j).value = Price;
          $('VatPrice_' + BlockId + '_'+j).value = TaxPrice;
          $('StockValue_' + BlockId + '_'+j).value = Saldo;
      }

      i++;
      z++;

    }
}

function ShowLoading() {
	var loading;
	loading = $('LoadingText');
	loading.style.color	 = '#fff';
	loading.style.position  = 'absolute';
	loading.style.top   = 0;
	loading.style.right	 = 0;
	loading.style.backgroundColor = '#f00';
	loading.style.border		= '1px solid #f99';
	loading.style.width		 = '80px';
	loading.style.padding	   = '4px';
	loading.style.fontFamily	= 'Arial, Helvetica, sans';
	loading.count = 0;
	loading.style.display = 'block';
}

function HideLoading() {
  $('LoadingText').style.display = 'none';
}





