$(function() {

	// Zebra Stripe each form paragraph that isn't hidden
	$(".form p:not(.hide):even").addClass("even");

	// When the input is focused, hide any errors and make background of p an active state
	$(".form .input").focus(function() {
		$(this).parent("p").addClass("active").removeClass("error");
		$(".infospan", $(this).parent("p")).show();
		$(".error", $(this).parent("p")).remove();
	});

	// Remove active state when input is blurred
	$(".form .input").blur(function() {
		$(this).parent("p").removeClass("active");
	});

	// If a form is required, add a star to the the label
	$(".form .required").siblings("label").append(" *");

	// If an input is blurred and required, check for empty value, and display error message if true
	$(".form .required").blur(function() {
		$value = $(this).val();
		if ($value == "") {
			$(this).parent("p").addClass("error");
			$(".infospan", $(this).parent("p")).hide();
			if ($(".error", $(this).parent("p")).length > 0) {
			}
			else $(this).after('<span class="error">This field is required</span>');
		}
	});

	// Check the input against
	$(".form .email").blur(function() {
		$value = $(this).val();
		var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
		if (!filter.test($value)) {
			$(".infospan", $(this).parent("p")).hide();
			if ($(".error", $(this).parent("p")).length > 0) {
				$(this).parent("p").addClass("error");
				$(".error", $(this).parent("p")).html("Please enter a valid email address");
			}
			else {
				$(this).parent("p").addClass("error");
				$(this).after('<span class="error">Please enter a valid email address</span>');
			}
		}
	});

	$(".form .nmbr").blur(function() {
		$value = $(this).val().replace(/ /g, "");
		var filter = /^([0-9])+$/;		
		if (!filter.test($value)) {
			$(".infospan", $(this).parent("p")).hide();
			if ($(".error", $(this).parent("p")).length > 0) {
				$(this).parent("p").addClass("error");
				$(".error", $(this).parent("p")).html("Please enter a valid number");
			}
			else {
				$(this).parent("p").addClass("error");
				$(this).after('<span class="error">Please enter a valid number</span>');
			}
		}
	});

	// Set up variables for UK Address slide down
	var $ukaddselector = $(".form input[name='UKAddressRadioGroup']");
	var $ukadddestination = $(".form #ukaddress");

	// When UK Address Provider radio buttons are changed, execute hideUKAdd()
	$ukaddselector.change(function() {
		hideUKAdd(true);
	});

	// On page load, execute hideUKAdd() without animation
	hideUKAdd(false);

	function hideUKAdd($animate) {

		// Get value of currently checked radio button
		$ukaddvalue = $(".form input[name='UKAddressRadioGroup']:checked").val();

		// If the value of the radio button is true, show #ukaddress, either with or without animation
		if ($ukaddvalue == "UKAddressProviderRadio_FriendRelative") {

			$(".unrequired", $ukadddestination).removeClass("unrequired").addClass("required");

			if ($animate == true) $ukadddestination.slideDown();
			else $ukadddestination.show();
		}
		else {

			$(".required", $ukadddestination).removeClass("required").addClass("unrequired");

			if ($animate == true) $ukadddestination.slideUp();
			else $ukadddestination.hide();
		}
	}

	if (jQuery.support.opacity) {
		var currindex = $("#aside ol li.current").index()

		$("#aside ol li").each(function(index) {

			var $opacityindex = 2 * (index - currindex);
			if ($opacityindex < 0) $opacityindex *= -1;
			var $opacity = (10 - $opacityindex) / 10;

			$(this).css({ 'opacity': $opacity }); ;

		});
	}

	$(".form").bind("submit", function() {
		$(".required").trigger("blur");
		$(".unrequired").siblings(".error").remove();
		$(".unrequired").parent("p").removeClass("error");

		if ($(".form .error").length > 0) {
		}
		else {
			//alert("Form submitted");
			return true;
		}

		return false;
	});

});

function gotopage($id) {
	window.location = "orderform-page"+$id+".html";
}
