function readNumeric(text)
{
  var i, output = "";
  for (i = 0; i < text.length; i++)
  {
    var c = text.charAt(i);
    if (c == '.' || c == ',')
      break;
    if (c >= '0' && c <= '9')
      output += c;
    if (c == '/')
      break;
    if (c >= 'a' && c[i] <= 'z')
      break;
    if (c == '%' && i > 0)
    {
      var c2 = text.charAt(i-1);
      if (c2 >= '0' && c2 <= '9')
        return "";
    }
  }
  return output;
}

function readNumericEUR(text)
{
  var i, output = "";
  var cents = 0;

  var t = text;
  if (text.indexOf(".")<=0 && text.indexOf(",")<=0)
    t = text + ",00";

  for (i = 0; i < t.length; i++)
  {
    var c = t.charAt(i);
    if (c == '.' || c == ',')
    {
      cents=1;
    }
    if (c >= '0' && c <= '9')
      output += c;
    if (c == '/')
      break;
    if (c >= 'a' && c[i] <= 'z')
      break;
    if (c == '%' && i > 0)
    {
      var c2 = t.charAt(i-1);
      if (c2 >= '0' && c2 <= '9')
        return "";
    }
  }
  if (cents==0)
    return output * 100.0;
  return output;
}

function toEUR(inSKK)
{
  var inEUR = inSKK / 30.126;
  var eur = "€ " + inEUR;
  var token = eur.split(".");
  
  if (token.length > 1)
    return inSKK + ' Sk = ' + token[0] + '.' + token[1].substr(0,2);
  return inSKK + ' Sk = ' + token[0];
}

function toSKK(inEUR)
{
//  var inSKK = inEUR * 30.126;
  var inSKK = inEUR * 0.30126;
  var skk =  inSKK + " Sk";
  var token = skk.split(".");

  var t = inEUR/100.0 + "";
  if (t.indexOf(".")<=0)
    t = t + ".";
  t = t + "00";
  
  var t2 = t.split(".");

  if (token.length > 1)
    return "€ " + t2[0] + ',' + t2[1].substr(0,2) + ' = ' + token[0] + '.' + token[1].substr(0,2) + " Sk";
  return "€ " + t2[0] + ',' + t2[1].substr(0,2) + ' = ' + token[0] + " Sk";
}

function hideEUR()
{
  var e = document.getElementById('euroBox');
  e.style.display = 'none';
  e.innerHTML = "";
}

function showEUR(text,x,y,w,h)
{
  var e = document.getElementById('euroBox');
  e.style.display = 'block';
  e.innerHTML = text;

  var ox = 0, oy = 0;
  if (window.pageXOffset != null)
  { ox = x - window.pageXOffset; oy = y - window.pageYOffset; }
  else
  { ox = x - document.body.scrollLeft; oy = y - document.body.scrollTop; }
  
  oy -= 20;
  ox += (w-e.offsetWidth)/2;

  e.style.top = oy + 'px';
  e.style.left = ox + 'px';
}

function processPricelistTableCell(tcell)
{
  var inSKK = readNumericEUR(tcell.innerHTML);
  if (inSKK.length > 0)
  {
//    var inEUR = toEUR(inSKK);
    // EUR -> SKK
    var inEUR = toSKK(inSKK);
    var old = tcell.innerHTML;

    var curleft = curtop = 0;
    var obj = tcell;

    if (obj.offsetParent)
    {
      do
      { 
        curleft += obj.offsetLeft;
        curtop += obj.offsetTop;
      } while (obj = obj.offsetParent);
    }
    
    tcell.innerHTML = '<span onmouseover="showEUR(\'' + inEUR + '\',' + curleft + ',' + curtop + ',' + tcell.offsetWidth + ',' + tcell.offsetHeight + ')" onmouseout="hideEUR();">' + old + '</span>';
  }
}

function processPricelistTableRow(trow)
{
  var i;
  for(i = 0; i < trow.childNodes.length; i++)
  {
    var s = trow.childNodes[i].tagName;
    if (s == "TD" || s == "td")
      processPricelistTableCell(trow.childNodes[i]);
  }
}

function processPricelistTableBody(tbody)
{
  var i;
  if (tbody.className != 'eur')
    return;
 
  for(i = 0; i < tbody.childNodes.length; i++)
  {
    var s = tbody.childNodes[i].tagName;
    if (s == "TR" || s == "tr")
      processPricelistTableRow(tbody.childNodes[i]);
  }
}

function processPricelistTable(table)
{
  var i;
  for(i = 0; i < table.childNodes.length; i++)
  {
    var s = table.childNodes[i].tagName;
    if (s == "TBODY" || s == "tbody")
      processPricelistTableBody(table.childNodes[i]);
  }
}

function processPricelistDiv(pricelist)
{
  var i;
  for(i = 0; i < pricelist.childNodes.length; i++)
  {
    var s = pricelist.childNodes[i].tagName;
    if (s == "TABLE" || s == "table")
      processPricelistTable(pricelist.childNodes[i]);
  }
}

function parsePricelistDivs()
{
  var i, array = document.getElementsByTagName("div");
  for(i = 0; i < array.length; i++)
    if (array[i].className == "pricelist")
      processPricelistDiv(array[i]);
}

window.onload = parsePricelistDivs;
