$(document).ready(function () {

	if ( $('#use-billing').length > 0 )
	{
		var use_billing = $('#use-billing');
		
		use_billing.click(function()
		{
			if ( $(this).is(':checked') ) {
				var billing_1 = $('#billing_address_1').val();
				var billing_2 = $('#billing_address_2').val();
				var billing_3 = $('#billing_address_3').val();
				var billing_4 = $('#billing_address_4 option:selected').val();
				
				$('#delivery_address_1').val(billing_1);
				$('#delivery_address_2').val(billing_2);
				$('#delivery_address_3').val(billing_3);
				$('#delivery_address_4').val(billing_4);
				
			} else if ( $(this).not(':checked') ) {
				$('#delivery_address_1').val('');
				$('#delivery_address_2').val('');
				$('#delivery_address_3').val('');
				$('#delivery_address_4').val('');
			}
			
		});
	}
	
	if ( $('#animal_id').length > 0 )
	{
		
		$('.animal-panel a').click(function()
		{
			$('.animal-panel').removeClass('animal-active');
			$(this).parent().addClass('animal-active');
			
			var animal_id = $(this).attr('title');
			var animal_name = $(this).find('span').text()
			var option = $('#animal_id').find(':first');
			option.attr('value', animal_id);
			option.text(animal_name);
		});
		
		var selected_id = $('#animal-grid').attr('rel');
		if ( selected_id != '') {
			$('#animal-grid').find('a[title='+selected_id+']').addClass('animal-active');
		}
	}
	
	// MEMBERSHIP ADOPTION FORMS
	function phoneIE (phone_number, element) 
	{
		phone_number = phone_number.replace(/\s+/g, "");
		return this.optional(element) || phone_number.length > 9 && phone_number.match(/[0-9]+\s*/); 
	}
	
	jQuery.validator.addMethod("phoneIE", phoneIE, "Please specify a valid phone number");
	
	$(".payment-form").validate(
	{
		rules: {
			email: {
				required: true,
				email: true
			},
			phone: {
				required: true,
				phoneIE: true
			},
			billing_address_4: {
				required: true
			},
			delivery_address_4: {
				required: true
			}

		},

		/*submitHandler: function(form) {
			form.submit();
		}*/
	});

});