// JavaScript Document
$(document).ready(function() 
{ 
	// Add action to form
	$('#submitForm').click(function()
	{
		// Validate form
		validateForm();
		return false;
	});
	
	initialiseDelegates();
});

function initialiseDelegates()
{
	// Add another delegate into delegate 2 div
	$('#addDelegate1').click(function()
	{
		// Firstly load in another delegate
		$.ajax({
		url: 'delegate.php?delegate=2',
		success: function(data) 
			{
			// Load into delegate2
			$('#delegate2').html(data);
			
			// Re initialise
			initialiseDelegates();
			
			
			}
		});
		
		// Now remove the first another delegate bit
		$('#another1').hide();
		
		return false;
	});

	// Add another delegate into delegate 3 div
	$('#addDelegate2').click(function()
	{
		// Firstly load in another delegate
		$.ajax({
		url: 'delegate.php?delegate=3',
		success: function(data) 
			{
			// Load into delegate 3
			$('#delegate3').html(data);
			
			// Re initialise
			initialiseDelegates();
			}
		});
		
		// Now remove the first another delegate bit
		$('#another2').hide();
		
		return false;
	});
	
	// Add another delegate into delegate 4div
	$('#addDelegate3').click(function()
	{
		// Firstly load in another delegate
		$.ajax({
		url: 'delegate.php?delegate=4',
		success: function(data) 
			{
			$('#delegate4').html(data);
			
			// Re initialise
			initialiseDelegates();
			}
		});
		
		// Now remove the first another delegate bit
		$('#another3').hide();
		
		return false;
	});
	
	// Add another delegate into delegate 5 div
	$('#addDelegate4').click(function()
	{
		// Firstly load in another delegate
		$.ajax({
		url: 'delegate.php?delegate=5',
		success: function(data) 
			{
			$('#delegate5').html(data);
			
			// Re initialise
			initialiseDelegates();
			}
		});
		
		// Now remove the first another delegate bit
		$('#another4').hide();
		
		return false;
	});
	
	// Remove Delegate2
	$('#removeDelegate2').click(function()
	{
		// Remove delegate 2
		$('#delegate2').html(null);
		
		// Show add delegate 2
		$('#another1').show();
		
		return false;
	});
	
	// Remove Delegate3
	$('#removeDelegate3').click(function()
	{
		// Remove delegate 3
		$('#delegate3').html(null);
		
		// Show add delegate 3
		$('#another2').show();
		
		return false;
	});
	
	// Remove Delegate4
	$('#removeDelegate4').click(function()
	{
		// Remove delegate 4
		$('#delegate4').html(null);
		
		// Show add delegate 4
		$('#another3').show();
		
		return false;
	});
	
	// Remove Delegate5
	$('#removeDelegate5').click(function()
	{
		// Remove delegate 5
		$('#delegate5').html(null);
		
		// Show add delegate 5
		$('#another4').show();
		
		return false;
	});
}

function validateForm()
{
	var error;
	var message;
	
	error = false;
	
	// Terms
	if(!$('#agreeTerms:checked').val())
	{
		error = true;
		message = "Please read and accept the terms and conditions";
		node = $('#agreeTerms');
	}
	
	// Where hear about us
	if($('#hearAboutUs').val() == "")
	{
		error = true;
		message = "Please select where you heard about us";
		node = $('#hearAboutUs');
	}
	
	// Fifth Delegate
	if($('#secondChoice5').length > 0)
	{
		// Second Choice
		if($('#secondChoice5').val() == "")
		{
			error = true;
			message = "Please select the second workshop choice for your fifth delegate";
			node = $('#secondChoice5');
		}
		
		// First Choice
		if($('#firstChoice5').val() == "")
		{
			error = true;
			message = "Please select the first workshop choice for your fifth delegate";
			node = $('#firstChoice5');
		}
		
		// Job Title
		if(!$('#jobtitle5').val())
		{
			error = true;
			message = "Please enter the job title for your fifth delegate";
			node = $('#jobtitle5');
		}
		
		// Telephone
		if(!$('#telephone5').val())
		{
			error = true;
			message = "Please enter the telephone number for your fifth delegate";
			node = $('#telephone5');
		}
		
		// Email
		if(!$('#email5').val())
		{
			error = true;
			message = "Please enter the email address for your fifth delegate";
			node = $('#email5');
		}
		
		// Surname
		if(!$('#surname5').val())
		{
			error = true;
			message = "Please enter the surname of your fifth delegate";
			node = $('#surname5');
		}
		
		// Firstname
		if(!$('#firstname5').val())
		{
			error = true;
			message = "Please enter the firstname of your fifth delegate";
			node = $('#firstname5');
		}
	}
	
	// Fourth Delegate
	if($('#secondChoice4').length > 0)
	{
		// Second Choice
		if($('#secondChoice4').val() == "")
		{
			error = true;
			message = "Please select the second workshop choice for your fourth delegate";
			node = $('#secondChoice4');
		}
		
		// First Choice
		if($('#firstChoice4').val() == "")
		{
			error = true;
			message = "Please select the first workshop choice for your fourth delegate";
			node = $('#firstChoice4');
		}
		
		// Job Title
		if(!$('#jobtitle4').val())
		{
			error = true;
			message = "Please enter the job title for your fourth delegate";
			node = $('#jobtitle4');
		}
		
		// Telephone
		if(!$('#telephone4').val())
		{
			error = true;
			message = "Please enter the telephone number for your fourth delegate";
			node = $('#telephone4');
		}
		
		// Email
		if(!$('#email4').val())
		{
			error = true;
			message = "Please enter the email address for your fourth delegate";
			node = $('#email4');
		}
		
		// Surname
		if(!$('#surname4').val())
		{
			error = true;
			message = "Please enter the surname of your fourth delegate";
			node = $('#surname4');
		}
		
		// Firstname
		if(!$('#firstname4').val())
		{
			error = true;
			message = "Please enter the firstname of your fourth delegate";
			node = $('#firstname4');
		}
	}
	
	// Third Delegate
	if($('#secondChoice3').length > 0)
	{
		// Second Choice
		if($('#secondChoice3').val() == "")
		{
			error = true;
			message = "Please select the second workshop choice for your third delegate";
			node = $('#secondChoice3');
		}
		
		// First Choice
		if($('#firstChoice3').val() == "")
		{
			error = true;
			message = "Please select the first workshop choice for your third delegate";
			node = $('#firstChoice3');
		}
		
		// Job Title
		if(!$('#jobtitle3').val())
		{
			error = true;
			message = "Please enter the job title for your third delegate";
			node = $('#jobtitle3');
		}
		
		// Telephone
		if(!$('#telephone3').val())
		{
			error = true;
			message = "Please enter the telephone number for your third delegate";
			node = $('#telephone3');
		}
		
		// Email
		if(!$('#email3').val())
		{
			error = true;
			message = "Please enter the email address for your third delegate";
			node = $('#email3');
		}
		
		// Surname
		if(!$('#surname3').val())
		{
			error = true;
			message = "Please enter the surname of your third delegate";
			node = $('#surname3');
		}
		
		// Firstname
		if(!$('#firstname3').val())
		{
			error = true;
			message = "Please enter the firstname of your third delegate";
			node = $('#firstname3');
		}
	}
	
	// Second Delegate
	if($('#secondChoice2').length > 0)
	{
		// Second Choice
		if($('#secondChoice2').val() == "")
		{
			error = true;
			message = "Please select the second workshop choice for your second delegate";
			node = $('#secondChoice2');
		}
		
		// First Choice
		if($('#firstChoice2').val() == "")
		{
			error = true;
			message = "Please select the first workshop choice for your second delegate";
			node = $('#firstChoice2');
		}
		
		// Job Title
		if(!$('#jobtitle2').val())
		{
			error = true;
			message = "Please enter the job title for your second delegate";
			node = $('#jobtitle2');
		}
		
		// Telephone
		if(!$('#telephone2').val())
		{
			error = true;
			message = "Please enter the telephone number for your second delegate";
			node = $('#telephone2');
		}
		
		// Email
		if(!$('#email2').val())
		{
			error = true;
			message = "Please enter the email address for your second delegate";
			node = $('#email2');
		}
		
		// Surname
		if(!$('#surname2').val())
		{
			error = true;
			message = "Please enter the surname of your second delegate";
			node = $('#surname2');
		}
		
		// Firstname
		if(!$('#firstname2').val())
		{
			error = true;
			message = "Please enter the firstname of your second delegate";
			node = $('#firstname2');
		}
	}
	
	
	
	// First Delegate
	
	// Second Choice
	if($('#secondChoice1').val() == "")
	{
		error = true;
		message = "Please select your second workshop choice";
		node = $('#secondChoice1');
	}
	
	// First Choice
	if($('#firstChoice1').val() == "")
	{
		error = true;
		message = "Please select your first workshop choice";
		node = $('#firstChoice1');
	}
	
	// Job Title
	if(!$('#jobtitle1').val())
	{
		error = true;
		message = "Please enter your job title";
		node = $('#jobtitle1');
	}
	
	// Telephone
	if(!$('#telephone1').val())
	{
		error = true;
		message = "Please enter your telephone number";
		node = $('#telephone1');
	}
	
	// Email
	if(!$('#email1').val())
	{
		error = true;
		message = "Please enter your email address";
		node = $('#email1');
	}
	
	// Surname
	if(!$('#surname1').val())
	{
		error = true;
		message = "Please enter your surname";
		node = $('#surname1');
	}
	
	// Firstname
	if(!$('#firstname1').val())
	{
		error = true;
		message = "Please enter your firstname";
		node = $('#firstname1');
	}
	
	
	
	if($('#postcode').val() == "")
	{
		error = true;
		message = "Please enter your postcode";
		node = $('#postcode');
	}
	
	if($('#county').val() == "")
	{
		error = true;
		message = "Please enter your county";
		node = $('#county');
	}
	
	if($('#city').val() == "")
	{
		error = true;
		message = "Please enter your town / city";
		node = $('#city');
	}
	
	if($('#address1').val() == "")
	{
		error = true;
		message = "Please enter your address";
		node = $('#address1');
	}
	
	if(error === true)
	{
		// Show messages
		alert(message);
		
		// Scroll
		$('html, body').animate({ scrollTop: node.offset().top }, 1000);
	}
	else
	{
		// Submit form
		$('#registration').submit();
	}
}
