// Handles localising the address form based on the country

// bind to change event on country combo

// change labels on form

// change region to be dropdown if regions defined for that country code, otherwise just show
// a text box

// closure
(function($){
	var countries = {
	"US": {
		"regions" : {
			"AL" : "Alabama",	"AK" : "Alaska", "AZ" : "Arizona","AR" : "Arkansas","CA" : "California",
			"CO" : "Colorado","CT" : "Connecticut","DE" : "Delaware","DC" : "District of Columbia",
			"FL" : "Florida","GA" : "Georgia","HI" : "Hawaii","ID" : "Idaho",	"IL" : "Illinois",
			"IN" : "Indiana","IA" : "Iowa","KS" : "Kansas","KY" : "Kentucky","LA" : "Louisiana",
			"ME" : "Maine","MD" : "Maryland","MA" : "Massachusetts","MI" : "Michigan","MN" : "Minnesota",
			"MS" : "Mississippi","MO" : "Missouri","MT" : "Montana","NE" : "Nebraska","NV" : "Nevada",
			"NH" : "New Hampshire","NJ" : "New Jersey","NM" : "New Mexico","NY" : "New York",
			"NC" : "North Carolina","ND" : "North Dakota","OH" : "Ohio","OK" : "Oklahoma","OR" : "Oregon",
			"PA" : "Pennsylvania","RI" : "Rhode Island","SC" : "South Carolina","SD" : "South Dakota", 
			"TN" : "Tenessee","TX" : "Texas","UT" : "Utah","VT" : "Vermont","VA" : "Virgina",
			"WA" : "Washington","WI" : "Wisconsin","WV" : "West Virginia","WY" : "Wyoming"
		},
		"townLabel" : "City",
		"regionLabel" : "State",
		"postcodeLabel" : "ZIP"
	},
	"CA" : {
		"regions" : {
			"AB" : "Alberta","BC" : "British Columbia","MB" : "Manitoba","NB" : "New Brunswick", 
			"NF" : "Newfoundland and Labrador","NS" : "Nova Scotia","NT" : "Northwest Territories", 
			"NU" : "Nunavut","ON" : "Ontario","PE" : "Prince Edward Island","QC" : "Québec", 
			"SK" : "Saskatchewan", "YT" : "Yukon"
		},
		"townLabel" : "City",
		"regionLabel" : "Province",
		"postcodeLabel" : "ZIP"
	},
	"MX" : {
		"regions" : {
			"AG" : "Aguascalientes","BC" : "Baja California","BS" : "Baja California Sur ",
			"CH" : "Chihuahua","CL" : "Colima ","CM" : "Campeche ","CO" : "Coahuila",
			"CS" : "Chiapas","DF" : "Distrito Federal","DG" : "Durango","GR" : "Guerrero",
			"GT" : "Guanajuato","HG" : "Hidalgo","JA" : "Jalisco","MI" : "Michoacan",
			"MO" : "Morelos","MX" : "Edo. Mexico ","NA" : "Nayarit ","NL" : "Nuevo Leon",
			"OA" : "Oaxaca","PU" : "Puebla","QR" : "Quintana Roo","QT" : "Queretaro",
			"SI" : "Sinaloa","SL" : "San Luis Potosi","SO" : "Sonora","TB" : "Tabasco",
			"TL" : "Tlaxcala","TM" : "Tamaulipas","VE" : "Veracruz","YU" : "Yucatan",
			"ZA" : "Zacatecas"
		},
		"townLabel" : "City",
		"regionLabel" : "State",
		"postcodeLabel" : "ZIP"
	}
	};
	
	var defaultLookup = {
		"townLabel" : "City/Town",
		"regionLabel" : "Region/County/State",
		"postcodeLabel" : "Postcode/ZIP"
	};
	
	$.fn.addressHandler = function() {
		return this.each(function(){
			// attach our event handler
			$this = $(this);
			$this.change(handleAddressChange);
			// initial setup
			changeCountry($this, false);
		});		
	};
	
	function changeCountry(countrySelect, clearRegion){
		if (countrySelect == null || countrySelect.length == 0) {
			return;
		}
		form = $(countrySelect[0].form);
		if (form.length == 0) {
			return;
		}
		
		var countryCode = countrySelect.val();
		
		var lookup = countries[countryCode];
		
		var fieldName = countrySelect.attr("name");
		var prefix = "";
		if (fieldName.length > 8){
			prefix = fieldName.substring(0, fieldName.length-8);
		}
		var region = form.find(":input[name='" + prefix + ".region']");
		
		if (lookup == null){
			// make sure region is a textbox
			if (region.attr("type") != "text") {
				region.replaceWith("<input type='text' name='" + region.attr("name") + "' class='" + region.attr("class") + "' length='50'/>");
			}
			
			lookup = defaultLookup;
		}
		else {
			var selectContent = "<select name='" + region.attr("name") + "' class='" + region.attr("class") + "'>";
			var existing = region.val();
						
			// change region to a select with the regions specified
			selectContent += "<option value=''>Select " + lookup.regionLabel + "</option>";
			var regionCode;
			for (regionCode in lookup.regions){
				selectContent += "<option value='" +  regionCode + "'";
				if (!clearRegion && existing ==  regionCode){
					selectContent += " selected='selected'";
				}
				selectContent += ">" + lookup.regions[regionCode] + "</option>";
			}
			
			
			selectContent += "</select>";
			region.replaceWith(selectContent);
		}
		
		//labels
		form.find("label[for*='" + prefix + ".region']").each(function(){
			this.innerHTML = lookup.regionLabel;
		});
		form.find("label[for*='" + prefix + ".town']").each(function(){
			this.innerHTML = lookup.townLabel;
		});
		form.find("label[for*='" + prefix + ".postcode']").each(function(){
			this.innerHTML = lookup.postcodeLabel;
		});
	}
	
	function handleAddressChange() {
		$this = $(this);
		changeCountry($(this), true);
	}
	
})(jQuery); // end closure;

$(document).ready(function(){
 	$(":input[name*='.country']").addressHandler();
 });
