// Things to add on (by order of priority)
// - AJAX validation of input
// - Hide/show certain areas based on form input

var valid = false;

function showHideDiv(showBlock, divBlock) {
	if (showBlock) $(divBlock).fadeIn();
	else           $(divBlock).fadeOut();
}

function toggleDiv(caller, divBlock) {
	if ( $(caller + ":checked").val() != "" ) showHideDiv(true, divBlock);
	else                                      showHideDiv(false, divBlock);
}

function showNewComerPopUp(isNewComer) {
	if (isNewComer)
	{
		$('#newcomer-no').hide();
		$('#newcomer-no-with-referral').hide();
		showHideDiv(true,'#newcomer-yes');
		
		if ( $("#group_47_1:checked").val() == "Yes" ) 
		{
			showHideDiv(true,'#newcomer-yes-with-referral');
		}
		else
		{
			$('#newcomer-yes-with-referral').hide();
			$("#entry_51").val("");
		}
		
		// clear newcomer-no fields
		$("#group_49_1").removeAttr('checked');
		$("#group_49_2").removeAttr('checked');
		$("#entry_53").val("");  
	}
	else
	{
		$('#newcomer-yes').hide();
		$('#newcomer-yes-with-referral').hide();
		showHideDiv(true,'#newcomer-no');
		
		if ( $("#group_49_1:checked").val() == "Yes" ) 
		{
			showHideDiv(true,'#newcomer-no-with-referral');
		}
		else
		{
			$('#newcomer-no-with-referral').hide();
			$("#entry_53").val("");
		}
		
		// clear newcomer-yes fields
		$("#group_47_1").removeAttr('checked');
		$("#group_47_2").removeAttr('checked');
		$("#entry_51").val("");  
	}
}

function validateInput() {

	// Clear out all the error display fields
	$('#registration #errors').html("");
	$('#registration div[id$="_error"]').html("");

	// Call the validator script
	var post = false;
	$.post(
		"scripts/registration/calls/validate.php",
		$("#registration #registrationForm").serialize(),
		function(data) {
			valid = data.valid;
			if (!data.valid) {
				$("#registration #errors").html("<h2>Errors Found</h2><p>Sorry! We found a few errors while processing your registration. Please address these things and try again.</p>");
				for (field in data.error) updateErrors(field, data.error[field]);
			}
			post = true;
		},
		"json"
	);

	return valid;
}

function updateErrors(id, text) {
	id = "#" + id + "_error";
	$(id).html(text);
}

$(document).ready(
	function() {
		if ( $("#group_45_1:checked").val() == "Yes" ) showNewComerPopUp(true);
		else if ( $("#group_45_2:checked").val() == "No" ) showNewComerPopUp(false);
		
		if ( $("#catholic_yes:checked").val() != "Yes" ) $("#catholic_add").hide();
		if ( $("#transport_carpool_group:checked").val() != "Carpool from a location" ) $("#carpool_location_options").hide();
		if ( $("#transport_carpool_friend:checked").val() != "Carpool from a friend" ) $("#carpool_friend_options").hide();
		if ( $("#transport_drive:checked").val() != "Driving on my own" ) $("#carpool_driver_options").hide();
		if ( $("#driver_yes:checked").val() != "Yes" ) $("#driver_options").hide();
		if ( $("#volunteer_reader:checked").val() != "Reader" ) $("#reader_options").hide();
		if ( $("#volunteer_instrument:checked").val() != "Instrument" ) $("#instrument_options").hide();

		$("#registration #catholic_yes").click( function() { showHideDiv(true, "#registration div#catholic_add") } );
		$("#registration #catholic_no").click( function() { showHideDiv(false, "#registration div#catholic_add") } );
		
		$("#registration #transport_carpool_group").click( function() { 
			showHideDiv(false, "#registration div#carpool_driver_options");
			showHideDiv(false, "#registration div#carpool_friend_options");
			showHideDiv(true,  "#registration div#carpool_location_options");
		} );
		$("#registration #transport_carpool_friend").click( function() { 
			showHideDiv(false, "#registration div#carpool_location_options"); 
			showHideDiv(false, "#registration div#carpool_driver_options");
			showHideDiv(true,  "#registration div#carpool_friend_options");
		} );
		$("#registration #transport_drive").click( function() { 
			showHideDiv(false, "#registration div#carpool_location_options"); 
			showHideDiv(false, "#registration div#carpool_friend_options");
			showHideDiv(true,  "#registration div#carpool_driver_options");
		} );

		$("#registration #driver_yes").click( function() { showHideDiv(true, "#registration div#driver_options") } );
		$("#registration #driver_no").click( function() { showHideDiv(false, "#registration div#driver_options") } );

		$("#registration #volunteer_reader").click( function() { showHideDiv($(this).is(":checked"), "#registration div#reader_options") } );
		$("#registration #volunteer_instrument").click( function() { showHideDiv($(this).is(":checked"), "#registration div#instrument_options") } );

		//$("#registration #registrationForm").submit( function(event) { return validateInput(); } );
	}
);


