google.load("jquery", "1.4.2");

google.setOnLoadCallback(function(){
	
	$(function(){
			   
		$("legend a").show();
		
		// Hide all Information and Subscriptions fieldsets if sent here with type=event in the querystring
		if (qs = location.search) {
			var segments = qs.split("?")[1].split("&");
			for (var item in segments) {
				var pair = segments[item].split("=");
				if (pair[0]=="type"&&pair[1]=="event") {
					
					// Only hide if there are no errors in this section
					if (
						!( "firstName" in oc(errorFields) )
						&& !( "lastName" in oc(errorFields) )
						&& !( "email" in oc(errorFields) )
						&& !( "phone" in oc(errorFields) )
						&& !( "country" in oc(errorFields) )
						&& !( "organization" in oc(errorFields) )
						&& !( "jobFunction" in oc(errorFields) )
						&& !( "position" in oc(errorFields) )
						)
					$("#information div.fieldset", "#subscribeUpdate").hide();
					
					// Always hide
					// $("#subscriptions div.fieldset", "#subscribeUpdate").hide();
				}
			}
		}
		
		// Hide password as long as there are no errors with it
		if( !("passWord" in oc(errorFields)) && !("hint" in oc(errorFields)) ) {
			$("#setPassword>div", "#subscribeUpdate").hide();
			$("#passWord, #passwordConfirm", "#subscribeUpdate").attr("disabled", true);
		}
		
		// Show/hide each fieldset
		$("#subscription legend a", "body")
			.click(function(e){
				e.preventDefault();
				
				var txt = ["hide", "show"],
					t = $(this);
				
				// Fade out the trigger and return it's fieldset for toggling
				$( t.blur().fadeOut(200).parents("legend").siblings("div.fieldset") ).slideToggle(function(){
					
					// For setPassword fieldset only...
					if ( t.parents("#setPassword").length ) {
						
						txt[0] = "don't change password";
						
						if ( $(this).is(":hidden") ) {
							$("#passWord, #passwordConfirm").attr("disabled", true);
						} else {
							$("#passWord, #passwordConfirm").removeAttr("disabled");
						}
					}
					
					// Change the text
					t.text( $(this).is(":hidden") ? txt[1] : txt[0] ).fadeIn(200);
				});
				
			})
			// If hidden on load, change the text to "show"
			.each(function(){
				var f = $(this).parents("legend").siblings("div.fieldset");
				if ( f.is(":hidden") ) {
					$(this).text("show");
				}
			});
		
		// Show/hide EVENT requirement fields
		$("input:radio[name=event]").change(function(){
			if ($('input:radio[name=event]:checked').val() == "yes") {
				// $(this).val()=="Singapore"
				$(".reqEvent, #billingAddress").slideDown();
			} else {
				$(".reqEvent").slideUp();
				if ($("#country").val()!="Singapore") $("#billingAddress").slideUp();
			}
		}).change();
		
		// Show/hide dietary requirements
		$("input:radio[name=dietaryOp]").change(function(){
			if ($('input:radio[name=dietaryOp]:checked').val() == "yes") {
				// $("#dietary").removeAttr("disabled");
				$("#dietaryDetails").slideDown();
			} else {
				$("#dietaryDetails").slideUp();
				// $("#dietary").attr("disabled", true);
			}
		}).change();
		
		// Show/hide assistance
		$("input:radio[name=assistanceOp]").change(function(){
			if ($('input:radio[name=assistanceOp]:checked').val() == "yes") {
				// $("#assistance").removeAttr("disabled");
				$("#assistanceDetails").slideDown();
			} else {
				$("#assistanceDetails").slideUp();
				// $("#assistance").attr("disabled", true);
			}
		}).change();
		
		// Show/hide Singapore validation fields
		$("#country").change(function(){
			if ($(this).val()=="Singapore") {
				if ($("#events div.fieldset:hidden").length) $("#events legend a").click();
				$("#billingAddress").slideDown();
				$("span.valSingapore").fadeIn();
			} else {
				if ($('input:radio[name=event]:checked').val() != "yes") $("#billingAddress").slideUp();
				$("span.valSingapore").fadeOut();
			}
		}).change();

	});
	
});

function oc(a)
{
  var o = {};
  for(var i=0;i<a.length;i++)
  {
    o[a[i]]='';
  }
  return o;
}