function add_attendee(count)
{
  if(count <= 0)
    return;

	var new_index = parseInt($('#index').val())+ 1;
  var cloned_attendee;

  for (index = new_index; index < new_index + count; index++)
 	{
  	cloned_attendee = $('#attendee_container table:first').clone();

  	cloned_attendee.find('input, select').each(function()
  	{
  		var old_name = $(this).attr('name');
  		var new_name = old_name.substring(0, old_name.indexOf('[')+1) + index + old_name.substring(old_name.indexOf(']'));

  		$(this).attr('name', new_name);

  		if($(this).attr("type") == "text")
  			$(this).attr("value", '');                                    // clear any input value that are not radio
  		else if($(this).attr("type") == "checkbox")
  			$(this).attr("checked", 0);                                   // uncheck everything
  		else $(this).find("option:first").attr('selected', 'selected');
  	});

	  cloned_attendee.append('<tr><th class="reg" colspan="2"><a href="javascript:;" onclick="$(this).parents(\'table\').remove()">Remove this attendee</a></td></tr>');
	  $('#attendee_container').append(cloned_attendee);
  }

	$('#index').attr('value', new_index + count);
}

function booking_state(state)
{
  $('#booking_invalid').attr('value', state);
}

function booking_load(name, values)
{
  $(name).populate(values);
}

$(document).ready(function()
{
  $('#registration_container').show();
	$('#confirmation_container').hide();
	$('#payment_container').hide();
	$('#completion_container').hide();

  document.forms[0].txtBookingCode.focus();

	$('form#registration').ajaxForm({
		beforeSubmit: function() { $('#status').html('Please wait, validating registrations...'); },
		success: proceed_to_confirmation
	});

	$('form#confirmation').ajaxForm({
		beforeSubmit: function() { $('#status').html('Please wait, confirmation...'); },
		success: proceed_to_payment
	});

	$('form#payment').ajaxForm({
		beforeSubmit: function() { $('#status').html('Please wait, validating payment...'); },
		success: proceed_to_completion
	});
});

function proceed_to_confirmation(responseText, statusText)
{
	if(responseText == 0)         //  no error
	{
    $('#status').html('');
    $('#registration_container').hide();
    $('#payment_container').hide();
  	$('#completion_container').hide();

		$.post("../uploads/registration/confirmation.php", { 'type' : 'ajax' }, function(response)
		{
			$('#confirmation_container').show();
			$("#confirmation_details").html(response);
		});
	}
	else
	{
		$('#status').html(responseText);
	}
}

function proceed_to_payment(responseText, statusText)
{
	if(responseText == 0)         //  no error
	{
    $('#status').html('');
    $('#registration_container').hide();
    $('#confirmation_container').hide();
  	$('#completion_container').hide();

		$.post("../uploads/registration/payment.php", { 'type' : 'ajax' }, function(response)
		{
    	$('#payment_container').show();
			$("#payment_details").html(response);
		});
	}
	else
	{
		$('#status').html(responseText);
	}
}

function proceed_to_completion(responseText, statusText)
{
	if(responseText == 0)         //  no error
	{
    $('#status').html('');
    $('#registration_container').hide();
    $('#confirmation_container').hide();
    $('#payment_container').hide();

		$.post("../uploads/registration/completion.php", { 'type' : 'ajax' }, function(response)
		{
    	$('#completion_container').show();
			$("#completion_details").html(response);
		});
	}
	else
	{
		$('#status').html(responseText);
	}
}

function back_to_registration()
{
  $('#status').html('');
	$('#registration_container').show();
	$('#confirmation_container').hide();
	$('#payment_container').hide();
}

function refresh()
{
  window.location.reload(false);
}

