  var verdeling;
  var _total;
	var _rowAppend = 0;
	
function initBedden( aantalPersonen )
{
  if ( aantalPersonen)
    document.getElementById('kamerindeling').style.display = 'block';
  else
    document.getElementById('kamerindeling').style.display = 'none';
    
  var p1 = 0;
  var p2 = 0;
  var p3 = 0;
  var r = 0;

  document.getElementById('bedden').innerHTML = '';

  if ( aantalPersonen == 1 )
  {
    p1 = 1;
  }
  else
  {
    p2 = Math.floor ( aantalPersonen / 2 );
    r = aantalPersonen - Math.floor ( aantalPersonen / 2 ) * 2;
    
    if ( r )
    {
      p2 -= 1;
      p3 += 1;
    }
  }

	verdeling = new Array(p3, p2, p1);

	_total = aantalPersonen;

  for ( x=0; x<verdeling[2]; x++)	
  	createRow ( 1 );

  for ( x=0; x<verdeling[1]; x++)	
  	createRow ( 2 );

  for ( x=0; x<verdeling[0]; x++)	
  	createRow ( 3 );

  countPersons();
}

function initBedden2 ( array )
{
  var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
  
  if ( is_chrome )
  {
    window.setTimeout( function(){
      $('aantalVolwassenen').selectedIndex = 0;
    }, 1000 );
  }  

  if ( array )
  {
    document.getElementById('kamerindeling').style.display = 'block';
  }
  else
  {
    document.getElementById('kamerindeling').style.display = 'none';  
  }
  document.getElementById('bedden').innerHTML = '';  
  
  //_total = 4; 
  
  for ( x=0; x<array.length; x++ )
  {
    switch ( array[x] )
    {
      case 'single':
        createRow( 1 );
      break;

      case 'twin':
      case 'double':
        createRow( 2 );
      break;

      case 'singledouble':
      case 'triplesingle':
        createRow( 3 );
      break;
    }
  }
  countPersons();

  // set selections
  for ( x=0; x<array.length; x++ )
  {
    switch ( array[x] )
    {
      case 'double':
        document.getElementById('bedType' + x ).selectedIndex = 1;
      break;

      case 'triplesingle':
        document.getElementById('bedType' + x ).selectedIndex = 1;
      break;
    }
  }

}


	function countPersons( r )
	{
	  document.getElementById( 'personsValid' ).innerHTML = '';
	  count = 0;
    len = document.getElementById('bedden').childNodes.length;
    for ( x=0; x<len; x++ )
    {
      select = document.getElementById('bedden').childNodes[x].childNodes[1].id;
      select = document.getElementById ( select ).selectedIndex ;
      count += select;
      
      invalid = count - _total;
      //window.status = invalid;      

      if ( select )
      {      
        drawValid( select, invalid, x );
      }
    }
    return count;
	}
	
	function drawValid( count, invalid )
	{
	  id = document.getElementById( 'personsValid' );
    id.style.display = 'none';
    var select;

    if ( count )    
    {
      if ( invalid>0 ) count -= invalid;
   
      span = document.createElement('div');
      span.className = 'kamer';
      
      for ( i=0; i<count; i++)
      {
        img = document.createElement('IMG');
        img.src = '../pics/wizard/person_valid.png';
        span.appendChild(img);
      }
      
      if ( invalid>0 )
      {
        for ( i=0; i<invalid; i++)
        {
          img = document.createElement('IMG');
          img.src = '../pics/wizard/person_invalid.png';
          span.appendChild(img);
        }
      }
      else
      {
        if ( count )
        {
      		select = document.createElement("SELECT");
          select.className = 'bedType';
      		select.name = 'bedType[]';
      		select.id = 'bedType' + x;
        }

        if ( count == 1 )
        {
          span.innerHTML += '<select id="bedType' + x + '" class="bedType" name="bedType[]"><option value="single">eenpersoonsbed</option>';
        }
        else
        if ( count == 2 )
        {
          span.innerHTML += '<select id="bedType' + x + '" class="bedType" name="bedType[]"><option value="twin">twee eenpersoonsbedden (twin)</option><option value="double">tweepersoonsbed (double)</option>';
        }
        else
        if ( count == 3 )
        {
          span.innerHTML += '<select id="bedType' + x + '" class="bedType" name="bedType[]"><option value="triplesingle">eenpersoonsbed, 2 eenpersoonsbedden</option><option value="singledouble">eenpersoonsbed, 1 tweepersoonsbed</option>';
        }
      }
  
      id.appendChild(span);
      id.innerHTML += ' ';
      id.style.display = 'block'; 
    }
	}
	
	function drawOver()
	{
	  id2 = document.getElementById( 'personsOver' );
	  id2.innerHTML = 'Nog niet ingedeeld: ';
    id2.style.display = 'none';
    
    count = countPersons();
    
    over = _total - count;
    if ( over > 0 )
    {
      for ( i=0; i<over; i++)
      {
        id2.innerHTML += '<img src="../pics/wizard/person_over.png"></img>';
      }
      id2.style.display = 'block';
    } 
	}

	function createRow( sIndex )
	{		
    newIx = document.getElementById('bedden').childNodes.length;

		div = document.getElementById('bedden');

    var container = document.createElement("DIV");
    var nummer = document.createElement("SPAN");
    nummer.className = 'kamerNo';
    nummer.innerHTML = newIx + 1;
    container.appendChild ( nummer );
    
    container.id = 'container_' + (newIx);

		select = document.createElement("SELECT");
		select.id = 'select_' + (newIx);
		select.name = 'bedden[]';
		select.index = newIx;
		select.className = 'bedSelect';

    select.onchange = function()
    {
      
      len = document.getElementById('bedden').childNodes.length;
      for ( x=this.index+1; x<len; x++ )
      {
        container = document.getElementById('container_' + x);
        container.innerHTML = '';
        div.removeChild(container);
      }

      drawOver() ;
      count = countPersons();

      if ( count<_total && this.selectedIndex )
      {
        createRow( 0 );
      }
    }

    option = document.createElement("OPTION");
    option.innerHTML = '';
    option.value = '';
    select.appendChild( option );		

    option = document.createElement("OPTION");
    option.innerHTML = 'eenpersoonskamer';
    option.value = 1;
    select.appendChild( option );		

    option = document.createElement("OPTION");
    option.innerHTML = 'tweepersoonskamer';
    option.value = 2;
    select.appendChild( option );	
    
    option = document.createElement("OPTION");
    option.innerHTML = 'driepersoonskamer';
    option.value = 3;
    select.appendChild( option );	    
		select.selectedIndex = sIndex;

    container.appendChild ( select );

		div.appendChild( container );

    return ( container );
	}
	
	function getVerdeling()
	{
		v = new Array(0, 0, 0);
	  count = 0;
    len = document.getElementById('bedden').childNodes.length;
    for ( x=0; x<len; x++ )
    {
      select = document.getElementById('bedden').childNodes[x].childNodes[1].id;
      select = document.getElementById ( select ).selectedIndex ;
      v[select-1]++;
    }
    return v;	
	}
