$(document).ready(function() {
	//###############   Payment - Further Information Delivery Address Toggle   ###############

	//###   Billing and Delivery Addresses - Same address toggle   ###
	//###   Defaults   ###
	if ( $("#user-details-form #delivery-same-address").attr("checked") ) {
		$("#user-details-form #delivery-address").hide();
		$("#user-details-form #delivery-address").children().attr("disabled","disabled");
	}
	//###   Interaction - show or hide group   ###


	$("#user-details-form #delivery-same-address").bind(($.browser.msie ? "click" : "change"), function () {
	//$("#user-details-form #delivery-same-address").change(function () {
		if ( $(this).attr("checked") ) {
			$("#user-details-form #delivery-address").children().attr("disabled","disabled");
			$("#user-details-form #delivery-address").fadeOut("normal");
			SetAreaFields( $("#user-details-form #billing-country") );
		} else {
			$("#user-details-form #delivery-address").children().removeAttr("disabled");
			$("#user-details-form #delivery-address").fadeIn("normal");
			SetAreaFields( $("#user-details-form #delivery-country") );
		}
	});

	//###   Billing and Delivery Addresses & country vs county/state/province   ###
	//###   Defaults   ###
	if ( $("#user-details-form").length == 1 ) {
		SetAreaFields( $("#user-details-form #billing-country") );

		if ( $("#user-details-form #delivery-same-address").attr("checked") != true ) {
			SetAreaFields( $("#user-details-form #delivery-country") );
		}

		//###   Interaction   ###
		//$("#user-details-form #billing-country, #user-details-form #delivery-country").bind(($.browser.msie ? "click" : "change"), function () {
		$("#user-details-form #billing-country, #user-details-form #delivery-country").change(function () {
			SetAreaFields( $(this) );
		});
	}
}); //###   End of DOM Ready   ###

function SetAreaFields($CountryField) {
	$AddressGroup = $CountryField.parent();

	if ($CountryField.find("option:selected").val() != "") {
		$AddressGroup.find("*").removeAttr("disabled").fadeTo("slow", 1);
		$AddressGroup.find(".instructions").hide();

		if ($CountryField.val() == "UK" || $CountryField.val() == "GB") {
			$AddressGroup.find("*[id$='county']").fadeIn("slow").prev().fadeIn("slow");	//###   Additional as FadeTo doesn't work if hidden
			$AddressGroup.find("*[id$='state']").attr("disabled","disabled").hide().prev().hide();
			$AddressGroup.find("*[id$='province']").attr("disabled","disabled").hide().prev().hide();

			$AddressGroup.find("select[id$='state']").children(":first").attr("selected", "selected");
			$AddressGroup.find("input[id$='province']").val("");
			$AddressGroup.find("input[id$='postcode']").prev().html("Postcode");

/*				$("#user-details-form #billing-county").fadeIn("slow").prev().fadeIn("slow");	//###   Additional as FadeTo doesn't work if hidden
				$("#user-details-form #billing-state").attr("disabled","disabled").hide().prev().hide();
			$("#user-details-form #billing-province").attr("disabled","disabled").hide().prev().hide(); */
		} else if ($CountryField.val() == "US") {
			$AddressGroup.find("*[id$='county']").attr("disabled","disabled").hide().prev().hide();
			$AddressGroup.find("*[id$='state']").fadeIn("slow").prev().fadeIn("slow");
			$AddressGroup.find("*[id$='province']").attr("disabled","disabled").hide().prev().hide();

			$AddressGroup.find("select[id$='county']").children(":first").attr("selected", "selected");
			$AddressGroup.find("input[id$='province']").val("");
			$AddressGroup.find("input[id$='postcode']").prev().html("Zip");
		} else {
			$AddressGroup.find("*[id$='county']").attr("disabled","disabled").hide().prev().hide();
			$AddressGroup.find("*[id$='state']").attr("disabled","disabled").hide().prev().hide();
			$AddressGroup.find("*[id$='province']").fadeIn("slow").prev().fadeIn("slow");

			$AddressGroup.find("select[id$='county']").children(":first").attr("selected", "selected");
			$AddressGroup.find("select[id$='state']").children(":first").attr("selected", "selected");
			$AddressGroup.find("input[id$='postcode']").prev().html("Areacode/zip");
		}

	} else {
		//###   Defaults   ###
		$AddressGroup.find("p.instructions").remove();
		$AddressGroup.find("legend").after('<p class="instructions">Start by selecting your country...</p>');
		$AddressGroup.children(":not(legend,.instructions)").attr("disabled","disabled").fadeTo("slow", 0.20);
		$AddressGroup.find("*[id$='country']").removeAttr("disabled").fadeTo("fast", 1).prev().fadeTo("fast", 1);
		$AddressGroup.find("*[id$='county']").show().prev().show();
		$AddressGroup.find("input[id$='postcode']").prev().html("Postcode");
		$AddressGroup.find("*[id$='state']").hide().prev().hide();
		$AddressGroup.find("*[id$='province']").hide().val("").prev().hide();
	}

	//###   Update Cart (total delivery) via AJAX   ###
	$.ajax({
		url: "/AjaxHandler/delivery-cost/" + $CountryField.val(),
		type: "POST",
		dataType: "html",
		success: function (html) {
			//alert(html);
			$(".content #basket-totals p:not(:first)").remove();
			$(".content #basket-totals").append(html);
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			//alert("Error: " + textStatus + " - " + errorThrown);
		}
	});
} //###   End of SetAreaFields Function   ###

