jQuery(document).ready(function() {
	// disable province selector if country has not been selected yet
	if(jQuery("#UserCountry").val() == "") {
		jQuery("#UserProvinceId").attr("disabled", true);
		jQuery("#UserProvinceId").html("<option value=''>Select a country to continue.</option>");
	}
	else {
		var countryId = jQuery("#UserCountry").val();
		var provinceSelected = jQuery("#UserProvinceId").val();
		jQuery("#UserProvinceId").html("<option value=''>Loading...</option>");
		jQuery.get("/provinces/options/"+countryId, function(data) {
			jQuery("#UserProvinceId").html(data);
			jQuery("#UserProvinceId").val(provinceSelected);
			jQuery("#UserProvinceId").attr("disabled", false);
			jQuery("#UserProvinceId").focus();
		});
		
	}
	// when they change their country selector, we need to 
	// reload the option box so they only have to select relavant provinces/states
	jQuery("#UserCountry").change(function() {
		var countryId = jQuery(this).val();
		jQuery("#UserProvinceId").attr("disabled", true);
		jQuery("#UserProvinceId").html("<option value=''>Loading...</option>");
		jQuery.get("/provinces/options/"+countryId, function(data) {
			jQuery("#UserProvinceId").html(data);
			jQuery("#UserProvinceId").attr("disabled", false);
			jQuery("#UserProvinceId").focus();
		});
	});
	
	jQuery("input:radio[name=connection]").click(function() {
    	if(jQuery(this).val() != "") {
			escape(jQuery(this).val()).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
    		jQuery("#disability-info").html('<p>Loading...</p>');
    		jQuery.get("/disability_infos/register/"+jQuery(this).val(), function(data) {
   				jQuery("#disability-info").html(data);
    		});
    	}
    }); 
    if(jQuery("input:radio[name=connection]:checked").val()) {
		jQuery("input:radio[name=connection]:checked").click();
	}
});
