$(document).ready(function(){ 
	
	// AJAX update the locations select box with towns from a country
	$("#visitedCountry").change(function() { 
		
		$("#visitedTown").html('<option value="error">Please wait</option>');
		$.post(	'/locations/getcities',
				{ countryId: $("#visitedCountry").val() },
				function(data)
				{
				});
	});
	
	
	
	// AJAX check if user is old enough
	$("#UserDateOfBirthYear").change(function() { 
		
		var day = $("#UserDateOfBirthDay").val();
		var month = $("#UserDateOfBirthMonth").val();
		var year = $("#UserDateOfBirthYear").val();
		
		$.post(	'/users/checkifuserisoldenough',
				{ day: day, month : month, year: year },
				
				function(data)
				{
					if(data > 17)
					{
						$("#notOldEnough").slideUp();
						
					}
					else
					{
						$("#notOldEnough").slideDown();
					}
				});
	});
	
	
	// hide the image error message if clicked
	$("#UserImage").click(function() {
		$("#imageEmptyCauseOfError").slideUp();
	});
	


	/**
	 * adds a hidden form field and displays the selected location to the user
	 *
	 */	
	$("#addPlaceVisited").click(function() {

		var visitedCountry = $("#visitedCountry option:selected").text().length;
				
		if(visitedCountry > 0)
		{
			$("#placesVisited").html($("#placesVisited").html() + '<input type="hidden" name="data[Placesvisited][' +  visitedCountry  + ']" value="' + $("#visitedCountry option:selected").text() + '">' + $("#visitedCountry option:selected").text() + ', ');
		}

	});
	


	//  this isn't working
	$("#addPlaceVisitedMyProfile").click(function() {

	});
	
	
	/**
	 * check if the first name has been entered
	 *
	 */	
	$("#UserFirstName").blur(function() {
		
		if($("#UserFirstName").val().length < 1)
		{
			$("#emptyFirstName").slideDown("fast");
			$("#emptyFirstName").html('Please enter your first name.');
		}
		else
		{
			$("#emptyFirstName").slideUp("fast");
		}
	});



	/**
	 * check if the first name has been entered
	 *
	 */	
	$("#UserEmailAddress").blur(function() {
		
		if($("#UserEmailAddress").val().length < 1)
		{
			$("#emptyEmail").slideDown("fast");
		}
		else
		{
			$("#emptyEmail").slideUp("fast");
		}
	});

	
	
	/**
	 * check if the last name has been entered
	 *
	 */	
	$("#UserLastName").blur(function() {
		
		if($("#UserLastName").val().length < 1)
		{
			$("#emptyLastName").slideDown("fast");
			$("#emptyLastName").html('Please enter your surname.');
		}
		else
		{
			$("#emptyLastName").slideUp("fast");
		}
	});
	
	
	
	/**
	 * check to see if the email addresses match
	 *
	 */	
	$("#UserConfirmEmailAddress").blur(function() {
		
		if($("#UserConfirmEmailAddress").val() != $("#UserEmailAddress").val())
		{
			$("#emailsDontMatch").slideDown("fast");
		}
		else
		{
			$("#emailsDontMatch").slideUp("fast");
		}
	});
	
	
	
	/**
	 * check to see if the passwords match
	 *
	 */	
	$("#confirmUserPword").blur(function() {
		
		if($("#confirmUserPword").val() != $("#UserPword").val())
		{
			$("#passwordsDontMatch").slideDown("fast");
			$("#passwordsDontMatch").html('The passwords you entered do not match');
		}
		else
		{
			$("#passwordsDontMatch").slideUp("fast");
		}
	});	


	
	/**
	 * use AJAX to check if the email is already in use
	 *
	 */	
	$("#UserEmailAddress").blur(function(){
		$.post('/users/checkifemailtaken', { email: $("#UserEmailAddress").val() },
		function(data){
			
			if(data==1)
			{
				$("#emailInUse").slideDown("fast");
			}
			else
			{
				$("#emailInUse").slideUp("fast");
			}
		});
	});
	
	
	
	/**
	 * check to see if username is empty and use AJAX to check if the username is already in use
	 *
	 */	
	$("#UserUsername").blur(function(){
		
		
		if($("#UserUsername").val().length < 1)
		{
			$("#emptyUsername").slideDown("fast");
		}
		else
		{
			$("#emptyUsername").slideUp("fast");
		}
		
		$.post('/users/checkifusernametaken', { username: $("#UserUsername").val() },
		function(data){
			
			if(data==1)
			{
				$("#usernameInUse").slideDown("fast");
			}
			else
			{
				$("#usernameInUse").slideUp("fast");
			}
		});
	});
	
	
	$("#confirmNewEmailAddress").blur(function() {
		
		if($("#confirmNewEmailAddress").val() != $("#newEmailAddress").val())
		{
			$("#emailsDontMatch").slideDown("fast");
			$("#emailsDontMatch").html('The email addresses you entered do not match');
		}
		else
		{
			$("#emailsDontMatch").slideUp("fast");
		}
	});
	
	
	
	/**
	 * check to see if the passwords match
	 *
	 */	
	$("#confirmNewPassword").blur(function() {
		
		if($("#confirmNewPassword").val() != $("#newPassword").val())
		{
			$("#passwordsDontMatch").slideDown("fast");
			$("#passwordsDontMatch").html('The passwords you entered do not match');
		}
		else
		{
			$("#passwordsDontMatch").slideUp("fast");
		}
	});
	
});


/**
 * check if the password has been entered and that it is longer than 6 characters
 *
 */	
function checkPasswordIsLongEnough(divId)
{
	if ($("#" + divId).val().length < 6)
	{
		$("#passwordTooShort").slideDown("fast");
	}
	else
	{
		$("#passwordTooShort").slideUp("fast");
	}		
}



function showhidecities($what, toHide)
{
	var hideThis = document.getElementById(toHide);
	if($what.options[$what.selectedIndex].value=='UK')
	{
		hideThis.className = 'show';
	}
	else
	{
		hideThis.className = 'hide';
	}
}



/**
 * clear the text area if the text is the example copy
 */
function clearTextArea(textareaId, exampleCopy)
{
	if($('#' + textareaId).val()==exampleCopy)
	{
		$('#' + textareaId).html('');
	}
}


function showHidePrivacyPolicy()
{
	$("#privacyPolicy").slideToggle('medium');
}