function initCosts ()
{
  calculateTotal ( );
  articleShow ( );
  title2Library ( );
}

function displayElement ( elm, disp )
{
  if ( typeof ( elm ) == 'string' )
    elm = document.getElementById ( elm );

  if ( elm )
  {
    if ( disp )
    {
      if ( navigator.userAgent.indexOf ( 'MSIE' ) == -1 )
      {
        if ( elm.tagName.toLowerCase ( ) == 'tr' )
          elm.style [ 'display' ] = 'table-row';
        else if ( elm.tagName.toLowerCase ( ) == 'tbody' )
          elm.style [ 'display' ] = 'table-row-group';
        else if ( elm.tagName.toLowerCase ( ) == 'table' )
          elm.style [ 'display' ] = 'table';
        else
          elm.style [ 'display' ] = 'block';
      }
      else
        elm.style [ 'display' ] = 'block';
    }
    else
      elm.style [ 'display' ] = 'none';
  }

  return elm;
}

function articleShow ( catID, mode )
{
  catIDs = Array ();

  elms = document.forms [ 'articleForm' ].elements;

  for ( a = 0; a < elms.length; a ++ )
  {
    if ( elms [ a ].name.indexOf ( 'articlePrice[' ) != -1 )
    {
      catIDs [ catIDs.length ] = elms [ a ].name.substring ( elms [ a ].name.indexOf ( '[' ) + 1, elms [ a ].name.indexOf ( ']' ) );

      if ( typeof ( mode ) != 'undefined' )
      {
        if ( mode == 'prev' && catIDs.length > 1 && catID == catIDs [ catIDs.length - 1 ] )
        {
          catID = catIDs [ catIDs.length - 2 ];
          mode = false;
        }
        else if ( mode == 'next' && catIDs.length >= 1 && catID == catIDs [ catIDs.length - 1 ] )
          mode = 'set';
        else if ( mode == 'set' && catIDs.length > 1 )
        {
          catID = catIDs [ catIDs.length - 1 ];
          mode = false;
        }
      }
    }
  }

  if ( typeof ( catID ) == 'undefined' )
    catID = catIDs [ 0 ];

  for ( a = 0; a < catIDs.length ; a ++ )
    displayElement ( document.getElementById ( 'articleMain_' + catIDs [ a ] ), ( catID == catIDs [ a ] ) );

  //displayElement ( document.getElementById ( 'detailButtonID' ), ( typeof ( mode ) != 'undefined' && mode == 'set' ) );
}

function getRadioValue ( elm )
{
  catID = elm.name.substring ( elm.name.indexOf ( '[' ) + 1, elm.name.indexOf ( ']' ) );
  articleID = elm.value;

  if ( document.getElementById ( 'articlePrice_' + articleID ) )
  {
    price = document.getElementById ( 'articlePrice_' + articleID ).innerHTML;
    document.getElementById ( 'articleTextbox_' + catID ).value = price;
  }

  calculateTotal ();

  return;
}

function calculateTotal ()
{
  elms = document.forms [ 'articleForm' ].elements;
  totalPrice = 0;

  for ( a = 0; a < elms.length; a ++ )
  {
    if ( elms [ a ].name.indexOf ( 'articlePrice[' ) != -1 )
    {
      if ( !isNaN ( parseFloat ( elms [ a ].value ) ) )
        totalPrice += parseFloat ( elms [ a ].value );
    }
  }

  document.getElementById ( 'costTotalLabel' ).innerHTML = number_format ( totalPrice, 2, '.', '' );
}

function number_format ( price, i, sep, thousand )
{
  if ( isNaN ( price ) || price < 0 )
    price = 0;

  if ( parseInt ( price ) != price )
  {
    eur = parseInt ( price );
    cent = Math.round ( ( price - eur ) * 100 );

    if ( cent < 10 )
      cent = '0' + cent;
  }
  else
  {
    eur = price;
    cent = '00';
  }

  if ( eur > 0 )
  {
    t = eur + '';

    eur = '';
    while ( t.length > 3 )
    {
      eur = thousand + t.substring ( t.length - 3, t.length ) + eur;
      t = t.substring ( 0, t.length - 3 );
    }
    if ( t > 0 )
      eur = t + eur;
  }
  else
    eur = '0';

  return eur + sep + cent;
}

function printPage()
{
  tmpContent = null;
  if ( elm = document.getElementById ( 'monuCosts' ) )
  {
    b = document.getElementsByTagName ( 'body' ) [ 0 ];
    tmpContent = b.innerHTML;

    b.innerHTML = elm.innerHTML;
    b.id = 'monuCosts';
    b.style [ 'backgroundColor' ] = '#fff';

    st = document.getElementsByTagName ( 'link' );
    l = st.length;
  window.print();
  }
  return true;
}

function endPrintPage ()
{
  b = document.getElementsByTagName ( 'body' ) [ 0 ];

  b.id = '';
  b.style [ 'backgroundColor' ] = '';
  b.innerHTML = tmpContent;
}

function showLibraryInfo(elm, info) {
  if ((lDiv = document.getElementById("libraryDiv")) == null) {
    lDiv = document.createElement("div");
    lDiv.id = "libraryDiv";
    lDiv.className = "infoField";
    lDiv.style [ 'position' ] = 'absolute';
    document.getElementById ( "monuCosts" ).appendChild ( lDiv );
  }

  wt = 250;
  w = 7 * info.length + 6;

  if (w > wt) {
    lDiv.style.width = wt + "px";
  } else {
    lDiv.style.width = w + "px";
  }

  if (info.length > 0) {
    leftOff = 24;
    topOff = 0;
    tElm = elm;
    while (tElm.tagName.toLowerCase() != "body") {
      topOff += tElm.offsetTop;
      leftOff += tElm.offsetLeft;
      tElm = tElm.offsetParent;
    }
    if (leftOff > tElm.clientWidth / 2) {
      leftOff = leftOff - lDiv.clientWidth;
    } else {
      leftOff = leftOff + elm.clientWidth;
    }

    lDiv.innerHTML = '<p style="margin: 0px;">' + info + '</p>';
    lDiv.style.left = leftOff + "px";
    lDiv.style.top = ( topOff + elm.clientHeight ) + "px";
    lDiv.style.visibility = "visible";
    lDiv.style.display = "block";
  } else {
    lDiv.style.visibility = "hidden";
    lDiv.style.display = "none";
  }
}

function title2Library ()
{
  tds = document.getElementsByTagName ( 'span' );
  for ( a = 0; a < tds.length; a ++ )
  {
    if ( tds [ a ].className == 'info' && tds [ a ].title.length > 0 )
    {
      while ( ( b = tds [ a ].title.indexOf ( '  ' ) ) != -1 )
        tds [ a ].title = tds [ a ].title.substring ( 0, b ) + '<br />' + tds [ a ].title.substring ( b + 2, tds [ a ].title.length );

      eval ( 'tds [ a ].onmouseover = function () {showLibraryInfo ( this, \'' + tds [ a ].title + '\' );}' );
      tds [ a ].onmouseout = function () {
        showLibraryInfo ( this, '' );
      }
      tds [ a ].title = '';
    }
  }
}
