function toggle_country() {
  new_value = $("country_select").value;
  var country;
  if (new_value == 'CA') {
    country = 'canada'
  } else if (new_value == 'US') {
    country = 'us'
  } else if (new_value == 'GB') {
    country = 'uk'
  } else if (new_value == 'AU') {
    country = 'australia'
  } else {
    country = 'other'
  }
  var switch_performed;
  ['canada', 'us', 'uk', 'australia', 'other'].each(function(name, index) {
    the_div = $("states_for_" + name);
    the_select = $("state_select_for_" + name);
    if (country == name) {
      switch_performed = !the_div.visible();
      the_div.show();
      the_select.name = "user[state_full]";
    } else {
      switch_performed = the_div.visible();
      the_div.hide();
      the_select.name = "state_" + name;
    }
    if (switch_performed) {
      the_select.value = "";
    }
  });
}


// BEGIN JQUERY CODE

var $j = jQuery.noConflict();

$j(function() {
	
	$j(".booking-screen #user_country").toggleStateField();
	
	$j(".booking-screen #card_type").hide();
	$j(".booking-screen #order_card_number").determineCardType();
	
	$j("#booking-payment button").click(function() {
		$(this).hide();
		$j("#booking-payment #loader").show();
		$j("#booking-payment #loader").html('<img src="/images/fresh-booking/loading.gif" width="16" height="11">');
	});

});

jQuery.fn.extend({
	
	toggleStateField:function() {
		$j(this).change(function() {
			// Determine selected country
			selected_country = $j(this).val();
			// Hide and disable all state fields
			$j('.state_option').hide().children().attr("disabled","disabled");
			// Translate the country name
			switch(selected_country) {
				case 'US':
					state_field = 'us'
					break;
				case 'AU':
					state_field = 'australia'
					break;
				case 'CA':
					state_field = 'canada'
					break;
				case 'GB':
					state_field = 'uk'
					break;
				default:
					state_field = 'other'
					break;
			}
			// Enable and show the appropiate state field 
			$j('#state_' + state_field).show().children().removeAttr("disabled");
		}).change(); // Call change() to trigger the preceding function on page load
	},
	
	determineCardType:function() {
		$j(this).keyup(function() {
			fieldValue = $j(this).val();
			
			cardTypeField = $j(".booking-screen #order_credit_card");
			acceptanceLogos = $j("span#cc_acceptance_logo");
			
			if (fieldValue.match(/^3/)) {
				cardTypeField.val("American Express");
				acceptanceLogos.css("background-position","0 -72px");
			} else if (fieldValue.match(/^4/)) {
				cardTypeField.val("Visa");
				acceptanceLogos.css("background-position","0 -24px");
			} else if (fieldValue.match(/^5/)) {
				cardTypeField.val("MasterCard");
				acceptanceLogos.css("background-position","0 -48px");
			} else if (fieldValue.match(/^6/)) {
				cardTypeField.val("Discover");
				acceptanceLogos.css("background-position","0 -96px");
			} else {
				cardTypeField.val("--");
				acceptanceLogos.css("background-position","0 0");
			}
		}).keyup();
	}
});