function updatePrice(new_price)
{
  if(new_price == -1)
    x = '<span class="invalid-combo">Ongeldig!</span>';
  else
    x = new_price;
  $("signup-price").innerHTML = x;
}

function calcAndUpdatePrice()
{
  var prParams = {
                  'competition': $("signup-possible-competition").options[$("signup-possible-competition").selectedIndex].text,
                  'information': $('signup-possible-information').value,
                  'account': $('signup-possible-account').value
                  };
  new Ajax.Request('calc-price.ajax.php', {
    method: 'get',
    parameters: prParams,
    onSuccess: function(x){
      updatePrice(x.responseText);
    }
  });
}

function selectCompetition()
{
  var competition_id = $("signup-possible-competition").value;
  new Ajax.Request('get-possible-selections.ajax.php', {
    method: 'get',
    parameters: {
                  competition_id: competition_id, 
                  information: $('signup-possible-information').value
                },
    onSuccess: function(x){
      selectCompetitionSucces(x.responseText.evalJSON());
      updateAccountList(x.responseText.evalJSON());
    }
  });
}

function selectCompetitionSucces(data)
{
  // update the clublist
  var list = $("signup-possible-club");
  list.innerHTML = "";

  var i = 0;
  data["clubs"].each(function(x){
    list.options[i] = new Option(x.name, x.club_id);
		i++;
  });
}

function updateAccountList(data)
{
  // update the list of possible account-types
  var list = $("signup-possible-account");
  list.innerHTML = '';
  
  var i = 0;
  data["accounts"].each(function(x){
     list.options[i] = new Option(x, x);
 		i++;
  });
}

Event.observe(window, 'load', function(){
  selectCompetition();

  Event.observe($("signup-possible-competition"), 'change', function(e){
    selectCompetition();
  });

  Event.observe($("signup-possible-information"), 'change', function(e){
    if($("signup-possible-account").value)
      calcAndUpdatePrice();
  });

  Event.observe($("signup-possible-account"), 'change', function(e){
    calcAndUpdatePrice();
  });
});