$(document).ready(function()
{
	/**
	 * Live validation on reservation form
	 */
	$('#PersonRegisterForm #PersonEmail').keyup(function()
	{
		if (ValidField('PersonEmail', 'email'))
		{
			$('#PersonRegisterForm #connection .nextStep').fadeIn();
		}
		else
		{
			$('#PersonRegisterForm #connection .nextStep').fadeOut();
			noticeHide();
		}
	});

	/**
	 * Valid email before next step
	 */
	$('#PersonRegisterForm #connection .button').click(function()
	{
		notice('loader', 'Vérification de votre adresse email en cours');

		$.get('/people/checkEmail/' + $('#PersonRegisterForm #PersonEmail').val(), function (data)
		{

			if (data == -1)
			{
				HighlightForm('PersonEmail', false);
				notice('error', 'Votre adresse email n\'est pas valide');
				$('#PersonRegisterForm #connection .nextStep').fadeOut();
			}
			else if (data == 0)
			{
				noticeHide();
				$('#PersonRegisterForm #connection').fadeOut();
				$('#PersonRegisterForm #password').fadeIn();
			}
			else
			{
				$('#PersonRegisterForm #connection').fadeOut();

				if (data.Person.password)
				{
					notice('information', 'Vous possédez déjà un compte');
					$('#PersonRegisterForm #PersonLogin').val(1);
					$('#PersonRegisterForm #login').fadeIn();
				}
				else
				{
					/**
					 * Prefill data
					 */
					noticeHide();
					$('#PersonFirstname').val(data.Person.firstname);
					$('#PersonLastname').val(data.Person.lastname);
					if (data.Person.gender == 1)
					{
						$('#PersonGender1').prop('checked', true);
					}
					else if (data.Person.gender == 2)
					{
						$('#PersonGender2').prop('checked', true);
					};

					var aBirthDate = explode('-', data.Person.date_of_birth);
					if (aBirthDate.length == 3)
					{
						$('#PersonDayOfBirthDay').val(aBirthDate[2]);
						$('#PersonMonthOfBirth').val(aBirthDate[1]);
						$('#PersonYearOfBirthYear').val(aBirthDate[0]);
					}
					$('#PersonCountry').val(data.Contact[0].country);
					$('#PersonAddress').val(data.Contact[0].address);
					$('#PersonPostalCode').val(data.Contact[0].postal_code);
					$('#PersonCity').val(data.Contact[0].city);
					$('#PersonTelephone').val(data.Contact[0].telephone);
					$('#PersonCellphone').val(data.Contact[0].cellphone);

					$('#PersonRegisterForm #password').fadeIn();
				}

			}
		}, 'json');
	});


	/**
	 * Valid password
	 */
	$('#PersonRegisterForm #PersonPassword').keyup(function()
	{
		ValidField('PersonPassword', 'password');
	});
	$('#PersonRegisterForm #PersonPasswordConfirmation').keyup(function()
	{
		if (ValidField('PersonPasswordConfirmation', 'password'))
		{
			if ($('#PersonRegisterForm #PersonPassword').val() == $('#PersonRegisterForm #PersonPasswordConfirmation').val())
			{
				HighlightForm('PersonPasswordConfirmation', true);
				$('#PersonRegisterForm #password .nextStep').fadeIn();
			}
			else
			{
				HighlightForm('PersonPasswordConfirmation', false);
				$('#PersonRegisterForm #password .nextStep').fadeOut();
			}
		}
		else
		{
			$('#PersonRegisterForm #password .nextStep').fadeOut();
		}
	});

	$('#PersonRegisterForm #password .button').click(function()
	{
		$('#PersonRegisterForm #password').fadeOut();
		$('#PersonRegisterForm #informations').fadeIn();
	});

	/**
	 * Login person
	 */
	$('#PersonRegisterForm #PersonPasswordLogin').keyup(function()
	{
		if (ValidField('PersonPasswordLogin', 'password'))
		{
			$('#PersonRegisterForm #login .nextStep').fadeIn();
		}
		else
		{
			$('#PersonRegisterForm #login .nextStep').fadeOut();
		}
	});

	$('#PersonRegisterForm #login .button').click(function()
	{
		$('#PersonRegisterForm').submit();
	});


	/**
	 * Informations
	 */
	$('#PersonRegisterForm #informations :radio').click(function()
	{
		$('.input.radio label').removeClass('error');
	});
	$('#PersonRegisterForm #PersonFirstname').keyup(function()
	{
		ValidField('PersonFirstname', 'empty');
	});
	$('#PersonRegisterForm #PersonLastname').keyup(function()
	{
		ValidField('PersonLastname', 'empty');
	});
	$('#PersonRegisterForm #PersonDayOfBirthDay').change(function()
	{
		if ($('#PersonRegisterForm #PersonDayOfBirthDay').val() != ''
				&& $('#PersonRegisterForm #PersonMonthOfBirth').val() != ''
				&& $('#PersonRegisterForm #PersonYearOfBirthYear').val() != '')
		{
			HighlightForm('PersonDateOfBirth', true);
		}
		else
		{
			HighlightForm('PersonDateOfBirth', false);
		}
	});
	$('#PersonRegisterForm #PersonMonthOfBirth').change(function()
	{
		if ($('#PersonRegisterForm #PersonDayOfBirthDay').val() != ''
				&& $('#PersonRegisterForm #PersonMonthOfBirth').val() != ''
				&& $('#PersonRegisterForm #PersonYearOfBirthYear').val() != '')
		{
			HighlightForm('PersonDateOfBirth', true);
		}
		else
		{
			HighlightForm('PersonDateOfBirth', false);
		}
	});
	$('#PersonRegisterForm #PersonYearOfBirthYear').change(function()
	{
		if ($('#PersonRegisterForm #PersonDayOfBirthDay').val() != ''
				&& $('#PersonRegisterForm #PersonMonthOfBirth').val() != ''
				&& $('#PersonRegisterForm #PersonYearOfBirthYear').val() != '')
		{
			HighlightForm('PersonDateOfBirth', true);
		}
		else
		{
			HighlightForm('PersonDateOfBirth', false);
		}
	});


	$('#PersonRegisterForm #informations .button').click(function()
	{
		/**
		 * Check input text
		 */
		var bResult = true;

		/**
		 * Check date of birth
		 */
		if ($('#PersonDayOfBirthDay').val() == '' || $('#PersonMonthOfBirth').val() == '' || $('#PersonYearOfBirthYear').val() == '')
		{
			HighlightForm('PersonDateOfBirth', false);
			bResult = false;
		}
		else
		{
			HighlightForm('PersonDateOfBirth', true);
		}

		/**
		 * Check gender checkboxes
		 */
		if ($('#PersonRegisterForm #informations :radio:checked').length)
		{
			HighlightForm('PersonGender1', true);
			HighlightForm('PersonGender2', true);
		}
		else
		{
			HighlightForm('PersonGender1', false);
			HighlightForm('PersonGender2', false);
			bResult = false;
		}

		if (!bResult || !ValidField('PersonFirstname', 'empty') || !ValidField('PersonLastname', 'empty'))
		{
			notice('error', 'Merci de remplir/corriger les champs demandés');
		}
		else
		{
			noticeHide();
			$('#PersonRegisterForm #informations').fadeOut();
			$('#PersonRegisterForm #location').fadeIn();
		}
	});

	/**
	 * Valid location
	 */
	$('#PersonRegisterForm #PersonAddress').keyup(function()
	{
		ValidField('PersonAddress', 'empty');
	});
	$('#PersonRegisterForm #PersonPostalCode').keyup(function()
	{
		if ($('#PersonRegisterForm #PersonCountry').val() == 'fr')
		{
			ValidField('PersonPostalCode', 'postal_code');
		}
		else
		{
			ValidField('PersonPostalCode', 'empty');
		}
	});
	$('#PersonRegisterForm #PersonCity').keyup(function()
	{
		ValidField('PersonCity', 'empty');
	});
	$('#PersonRegisterForm #PersonTelephone').keyup(function()
	{
		if ($('#PersonRegisterForm #PersonCountry').val() == 'fr')
		{
			ValidField('PersonTelephone', 'telephone');
		}
		else
		{
			ValidField('PersonTelephone', 'empty');
		}
	});
	$('#PersonRegisterForm #PersonCellphone').keyup(function()
	{
		if ($('#PersonRegisterForm #PersonCountry').val() == 'fr')
		{
			ValidField('PersonCellphone', 'cellphone');
		}
		else
		{
			ValidField('PersonCellphone', 'empty');
		}
	});


	$('#PersonRegisterForm #location .button').click(function()
	{
		/**
		 * Check input text
		 */
		var bResult = true;
		var message = '';

		/**
		 * Check postal code
		 */
		if ($('#PersonRegisterForm #PersonCountry').val() == 'fr')
		{
			bResult = bResult && ValidField('PersonPostalCode', 'postal_code');
		}
		else
		{
			bResult = bResult && ValidField('PersonPostalCode', 'empty');
		}


		/**
		 * Check telephone/cellphone
		 */
		if ($('#PersonTelephone').val() == '' && $('#PersonCellphone').val() == '')
		{
			message = message + 'Veuillir renseigner au moins un numéro de téléphone<br />';
			bResult = false;
		}
		else if ($('#PersonRegisterForm #PersonTelephone').val() != '')
		{
			if ($('#PersonRegisterForm #PersonCountry').val() == 'fr')
			{
				bResult = bResult && ValidField('PersonTelephone', 'telephone');
			}
			else
			{
				bResult = bResult && ValidField('PersonTelephone', 'empty');
			}
		}
		else if ($('#PersonRegisterForm #PersonCellphone').val() != '')
		{
			if ($('#PersonRegisterForm #PersonCountry').val() == 'fr')
			{
				bResult = bResult && ValidField('PersonCellphone', 'cellphone');
			}
			else
			{
				bResult = bResult && ValidField('PersonCellphone', 'empty');
			}
		}




		if (!bResult
				|| !ValidField('PersonAddress', 'empty')
				|| !ValidField('PersonCity', 'empty'))
		{
			notice('error', 'Merci de remplir/corriger les champs demandés');
		}
		else
		{
			noticeHide();
			$('#PersonRegisterForm').submit();
		}
	});


	/**
	 * Account retrieve password
	 */
	$('#PersonForgetPasswordForm #PersonEmail').keyup(function()
	{
		if (!ValidField('PersonEmail', 'email'))
		{
			notice('error', 'Merci de saisir une adresse email valide');
		}
		else
		{
			noticeHide();
		}

	});

	$('#PersonForgetPasswordForm .submit input').click(function()
	{
		if (!ValidField('PersonEmail', 'email'))
		{
			notice('error', 'Merci de saisir une adresse email valide');
			return false;
		}
		else
		{
			noticeHide();
			return true;
		}
	});


	/**
	 * Account change password
	 */
	$('#PersonChangePasswordForm #PersonPassword').keyup(function()
	{
		ValidField('PersonPassword', 'password');
	});


	$('#PersonChangePasswordForm #PersonPasswordConfirmation').keyup(function()
	{
		if (ValidField('PersonPasswordConfirmation', 'password'))
		{
			noticeHide();
		}
		else
		{
			notice('error', 'Merci de saisir le même mot de passe');
		}
	});

	$('#PersonChangePasswordForm .submit input').click(function()
	{
		ValidField('PersonPassword', 'password');
		ValidField('PersonPasswordConfirmation', 'password');

		if ($('#PersonChangePasswordForm #PersonPassword').val() == $('#PersonChangePasswordForm #PersonPasswordConfirmation').val())
		{
			noticeHide();
			return true;
		}
		else
		{
			notice('error', 'Merci de saisir un mot de passe valide');
			return false;
		}
	});



});

