/**
 * N.B. This file is also used on checkout_verify_address.html and
 * register.html. So keep that in mind if you make changes.
 */

$( function() {

	$("#bill_province_busy").css("visibility","hidden");
	$("#ship_province_busy").css("visibility","hidden");

	// enable/disable the shipping address fields, depending on whether or
	// not ship_same is checked
	toggleShip();
	$("#ship_same").click( toggleShip );
	
	// enable/disable bill_business_name depending on the value of bill_address_type
	toggleBillBusiness();
	$("#bill_address_type_residential,#bill_address_type_business").click( toggleBillBusiness );
	
	toggleBillProvince( true );
	$("#bill_country_id").change( toggleBillProvince );
	
	// enable/disable ship_business_name depending on the value of ship_address_type
	toggleShipBusiness();
	$("#ship_address_type_residential,#ship_address_type_business").click( toggleShipBusiness );

	toggleShipProvince( true );
	$("#ship_country_id").change( toggleShipProvince );
	
	function toggleBillBusiness() {
		if ( $("#bill_address_type_business").is(":checked") ) {
			$("#bill_business_name").removeAttr("disabled");
		} else {
			$("#bill_business_name").attr("disabled","disabled");
		}
	}
	
	function toggleBillProvince( initial ) {
		
		if ( typeof initial === "undefined" ) {
			initial = false;
		}
		
		$("#bill_state_id").attr("disabled","disabled").hide();
		$("#bill_province_select").attr("disabled","disabled").hide();
		$("#bill_province_text").attr("disabled","disabled").hide();
		
		if ( $("#bill_country_id").val() ) {
			
			if ( $("#bill_country_id").val() === "1" ) { // united states
				
				$("#bill_state_id").removeAttr("disabled").show();
				
			} else { // a furrin' country
		
				$("#bill_province_busy").css("visibility","visible");
				
				$.ajax( {
					url: "/api/",
					type: "POST",
					data: {
						"method": "getProvinces",
						"country_id": $("#bill_country_id").val()
					},
					dataType: "json",
					success: function( response ) {
						// if we got some results, then load them into the
						// dropdown. otherwise, hide the dropdown and show the
						// free-text box
						if ( response.length ) {
							$("#bill_province_select").empty().append( $("<option></option>").val("") );
							$.each( response, function( i ) {
								$("#bill_province_select").append(
									$("<option></option>")
										.val( this.iso_code )
										.append( this.name )
								);							
							} );
							if ( initial ) {
								$("#bill_province_select").val( $("#bill_province_text").val() );
							}
							$("#bill_province_select").removeAttr("disabled").show();
						} else {
							$("#bill_province_text").removeAttr("disabled").show();
						}
					},
					complete: function() {
						$("#bill_province_busy").css("visibility","hidden");
					}
				} );
				
			}
			
		}
		
	}
	
	function toggleShipBusiness() {
		if ( $("#ship_address_type_business").is(":checked") ) {
			$("#ship_business_name").removeAttr("disabled");
		} else {
			$("#ship_business_name").attr("disabled","disabled");
		}
	}	

	function toggleShipProvince( initial ) {
		
		if ( typeof initial === "undefined" ) {
			initial = false;
		}

		$("#ship_state_id").attr("disabled","disabled").hide();
		$("#ship_province_select").attr("disabled","disabled").hide();
		$("#ship_province_text").attr("disabled","disabled").hide();
		
		// if a billing country is selected, then retrieve the corresponding
		// provinces, and set up the province controls accordingly
		if ( $("#ship_country_id").val() ) {
		
			if ( $("#ship_country_id").val() === "1" ) { // united states
				
				if ( !$("#ship_same").is(":checked") ) { 
					$("#ship_state_id").removeAttr("disabled");
				}
				$("#ship_state_id").show();
				
			} else { // a furrin' country

				$("#ship_province_busy").css("visibility","visible");
			
				$.ajax( {
					url: "/api/",
					type: "POST",
					data: {
						"method": "getProvinces",
						"country_id": $("#ship_country_id").val()
					},
					dataType: "json",
					success: function( response ) {
						// if we got some results, then load them into the
						// dropdown. otherwise, hide the dropdown and show the
						// free-text box
						if ( response.length ) {
							$("#ship_province_select").empty().append( $("<option></option>").val("") );
							$.each( response, function( i ) {
								$("#ship_province_select").append(
									$("<option></option>")
										.val( this.iso_code )
										.append( this.name )
								);							
							} );
							if ( initial ) {
								$("#ship_province_select").val( $("#ship_province_text").val() );
							}
							if ( !$("#ship_same").is(":checked") ) {
								$("#ship_province_select").removeAttr("disabled");
							}
							$("#ship_province_select").show();
						} else {
							if ( !$("#ship_same").is(":checked") ) {
								$("#ship_province_text").removeAttr("disabled");
							}
							$("#ship_province_text").show();
						}
					},
					complete: function() {
						$("#ship_province_busy").css("visibility","hidden");
					}
				} );

			}
			
		}
		
	}
	
	function toggleShip() {
		var $shipFields = $("input,select", $("#ship") );
		if ( $("#ship_same").is(":checked") ) {
			$shipFields.attr("disabled","disabled");
		} else {
			$shipFields.removeAttr("disabled");
			toggleShipBusiness();
			// this isn't completely elegant, but it gets the job done
			toggleShipProvince();
		}
	}
	
} );
