function getValue(radioGroupName)
{
  radios = document.getElementsByName(radioGroupName);
  for (i = 0; i < radios.length; i++)
  {
    if (radios[i].checked) return radios[i].value;
  }
}

function NulstilDato(Nr)
{
  var selObj = document.getElementById('dag'+Nr);
  selObj.selectedIndex = 0;
  var selObj = document.getElementById('maaned'+Nr);
  selObj.selectedIndex = 0;
  var selObj = document.getElementById('aar'+Nr);
  selObj.selectedIndex = 0;
  document.getElementById("dato").value = '';
  VisDesign();
}

function VisSymbol(Nr)
{
  document.getElementById("symbol").value = Nr;
  VisDesign(2);
}

function formatNumber(obj)
{
  anynum = eval(obj);
  divider = 100;
  decimal = 2;

  workNum=Math.abs((Math.round(anynum*divider)/divider));

  workStr = "" + workNum;

  if (workStr.indexOf(".") == -1){ workStr+="." }

  dStr = workStr.substr(0,workStr.indexOf("."));
  dNum = dStr-0;
  pStr = workStr.substr(workStr.indexOf("."));

  while (pStr.length-1< decimal) { pStr+="0" }

  if (pStr == '.') pStr ='';

  //--- Adds a comma in the thousands place.
  if (dNum>=1000)
  {
    dLen = dStr.length
    dStr = parseInt(""+(dNum/1000)) + "," + dStr.substring(dLen-3,dLen)
  }

  retval = dStr + pStr
  retval = retval + " DKK";
  retval = retval.replace(".", ",");

  return retval;
}

function VisDesign(Nr)
{
  var tekst  = false;
  var vareid = document.getElementById("VareID").value;

  if (document.getElementById("nettopris") != null)
  {
    var pris = parseFloat(document.getElementById("nettopris").value);
  }

  if (document.getElementById("symbol") != null)
  {
    var symbol    = document.getElementById("symbol").value;
    if (symbol > 0 && (vareid != 53 && vareid != 54)) pris = pris + 55;
  }
  else var symbol = "";

  if (Nr == 0 || Nr == null) Nr = getValue("Placering");

  if (document.getElementById("tekst1") != null)
  {
    var tekst1    = document.getElementById("tekst1").value;
    if (tekst1.length > 0) tekst = true;
  }
  else var tekst1 = "";

  if (document.getElementById("tekst2") != null)
  {
    var tekst2    = document.getElementById("tekst2").value;
    if (tekst2.length > 0) tekst = true;
  }
  else var tekst2 = "";

  if (document.getElementById("placeringaftekst") != null)
  {
    placering = getValue("Placering");
  }
  else var placering = 0;

  if (document.getElementById("dag1") != null)
  {
    if (document.getElementById("dag1").value != "-")
    {
      var dato   = document.getElementById("dag1").value + ". " + document.getElementById("maaned1").value + " " + document.getElementById("aar1").value;
      document.getElementById("dato").value = dato;
      tekst = true;
    }
    else if (document.getElementById("dag2").value != "-")
    {
      var dato   = document.getElementById("dag2").value + "-" + document.getElementById("maaned2").value + "-" + document.getElementById("aar2").value;
      document.getElementById("dato").value = dato;
      tekst = true;
    }
    else dato = "";
  }
  else dato = "";

  if (tekst)
  {
    pris = pris + 85;
  }

  if (document.getElementById("nettopris") != null)
  {
    document.getElementById("VarePris").innerHTML = formatNumber(pris);
    document.getElementById("pris").value = pris;
  }

  document.getElementById('thumb1').src='/billed.php?VareID='+ vareid +'&billed='+ Nr +'&placeringaftekst='+ placering +'&tekst1=' + tekst1 +'&tekst2=' + tekst2 + '&dato=' + dato + '&symbol=' + symbol;
  if (document.getElementById('stortbilled') != null) document.getElementById('stortbilled').href='/billed.php?VareID='+ vareid +'&stort=1'+'&billed='+ Nr +'&placeringaftekst='+ placering +'&tekst1=' + tekst1 +'&tekst2=' + tekst2 + '&dato=' + dato + '&symbol=' + symbol;
  if (document.getElementById('stortbilledforstor') != null) document.getElementById('stortbilledforstor').href='/billed.php?VareID='+ vareid +'&stort=1'+'&billed='+ Nr +'&placeringaftekst='+ placering +'&tekst1=' + tekst1 +'&tekst2=' + tekst2 + '&dato=' + dato + '&symbol=' + symbol;
}

$(function()
{
  var hideDelay = 0;
  var currentID;
  var hideTimer = null;

  // One instance that's reused to show info for the current person
  var container = $('<div id="personPopupContainer">'
      + '<table width="" border="0" cellspacing="0" cellpadding="0" align="center" class="personPopupPopup">'
      + '<tr>'
      + '   <td class="corner topLeft"></td>'
      + '   <td class="top"></td>'
      + '   <td class="corner topRight"></td>'
      + '</tr>'
      + '<tr>'
      + '   <td class="left">&nbsp;</td>'
      + '   <td><div id="personPopupContent"></div></td>'
      + '   <td class="right">&nbsp;</td>'
      + '</tr>'
      + '<tr>'
      + '   <td class="corner bottomLeft">&nbsp;</td>'
      + '   <td class="bottom">&nbsp;</td>'
      + '   <td class="corner bottomRight"></td>'
      + '</tr>'
      + '</table>'
      + '</div>');

  $('body').append(container);

  $('.personPopupTrigger').live('mouseover', function()
  {
      // format of 'rel' tag: pageid,personguid
      var settings = $(this).attr('rel').split(',');
      var pageID = settings[0];
      currentID = settings[1];

      if (hideTimer)
          clearTimeout(hideTimer);

      var pos = $(this).offset();
      var width = $(this).width();

      if ((pos.left+550) > $(window).width()) container.css({ left: (pos.left + width - 520) + 'px', top: pos.top - 90 + 'px'});
      else container.css({ left: (pos.left + width - 0) + 'px', top: pos.top - 90 + 'px'});

      $('#personPopupContent').html('&nbsp;');

      $.ajax({
          type: 'GET',
          url: 'http://www.dendanskesalmebog.dk/typosite/webshop/45/udgivelseinfo.php',
          data: 'page=' + pageID + '&guid=' + currentID,
          success: function(data)
          {
            $('#personPopupContent').html(data);
          }
      });

      container.css('display', 'block');
  });

  $('.personPopupTrigger').live('mouseout', function()
  {
      if (hideTimer)
          clearTimeout(hideTimer);
      hideTimer = setTimeout(function()
      {
          container.css('display', 'none');
      }, hideDelay);
  });

  // Allow mouse over of details without hiding details
  //$('#personPopupContainer').mouseover(function()
  //{
  //    if (hideTimer)
  //        clearTimeout(hideTimer);
  //});

  // Hide after mouseout
  $('#personPopupContainer').mouseout(function()
  {
      if (hideTimer)
          clearTimeout(hideTimer);
      hideTimer = setTimeout(function()
      {
          container.css('display', 'none');
      }, hideDelay);
  });
});

