$(function() {
	
	$('#RegistrationTitle').change(changeRegistrationTitle);
	$('#RegistrationTitle').change();
	
	$('#RegistrationCountry').change(changeRegistrationCountry);
	$('#RegistrationCountry').change();
	
	$('#RegistrationPurchaseGift').change(changeRegistrationPurchaseGift);
	$('#RegistrationCountry').change();
	
	$('#FeedbackPreviouslyOwnedABritaSystem').change(changeFeedbackPreviouslyOwnedABritaSystem);
	$('#RegistrationCountry').change();
	
	$('#GiftInNameOfInHonourOf').parent('div').find('input')
		.change(function(){
			changeGiftInNameOfInHonourOf($('#GiftInNameOfInHonourOf')[0]);
		});
	$('#GiftInNameOfInHonourOf').change();
	
	$("#FeedbackRecommendItBecause").keyup(keyupFeedbackReccommendItBecause);
});


function keyupFeedbackReccommendItBecause() {
	var max = 150;
	
	if ($(this).val().length > max)
	{
    	$(this).val($(this).val().substr(0, max)); 
		$(this).parent().find('small').css('color', 'red');
    } 	
}


function changeGiftInNameOfInHonourOf(e) {
	if (e.checked) {
		$('#GiftHonoreeFirstName').removeAttr('disabled');
		$('#GiftHonoreeSurname').removeAttr('disabled');
	}
	else {
		$('#GiftHonoreeFirstName').attr('disabled', 'disabled');
		$('#GiftHonoreeSurname').attr('disabled', 'disabled');
	}
}


function changeFeedbackPreviouslyOwnedABritaSystem()
{
	var t = $(this);
	if (1 == t.val()) {
		$('#FeedbackOwnedABritaSystemFor').parent('div').addClass('required');		
	}
	else {
		$('#FeedbackOwnedABritaSystemFor').parent('div').removeClass('required');	
	}
}

function changeRegistrationPurchaseGift()
{
	var t = $(this);
	if (1 == t.val()) {
		t.after('<p style="padding-left: 13px; padding-top: 5px; font-weight:bold; color: #008000;">Make sure you see the Greening Australia page at the end of this questionnaire.</p>');
	}
}

function changeRegistrationCountry()
{
	var country = $(this).val();
	
	if (!country || country == '') {
		$('#RegistrationNewZealandTownCity')
			.attr('disabled', 'disabled')
			.parents('.input').hide();
		
		$('#RegistrationAustraliaState')
			.attr('disabled', 'disabled')
			.parents('.input').show();
	}
	else if (country == 'au') {
		$('#RegistrationNewZealandTownCity')
			.val('')
			.attr('disabled', 'disabled')
			.parents('.input').hide();
					
		$('#RegistrationAustraliaState')
			.removeAttr('disabled')
			.parents('.input').show();
			
		$('#RegistrationSuburb').parents('.input').find('label').html('Suburb');
	}
	else {
		$('#RegistrationAustraliaState')
			.val('')
			.attr('disabled', 'disabled')
			.parents('.input').hide();
				
		$('#RegistrationNewZealandTownCity')
			.removeAttr('disabled')
			.parents('.input').show();
			
		$('#RegistrationSuburb').parents('.input').find('label').html('Suburb / Locality');
	}
}


function changeRegistrationTitle()
{
	var title = $(this).val();
	
	if (title == 'dr')	{
		$('#RegistrationGender').removeAttr('disabled');
	}
	else {
		$('#RegistrationGender').attr('disabled', 'disabled');
	}
	
	if (!title || title == '') {
		$('#RegistrationGender').val('');
	}
	else if (title == 'dr') {
		// do nothing
	}
	else if (title == 'mr') {
		$('#RegistrationGender').val('m');
	}
	else {
		$('#RegistrationGender').val('f');
	}
}
