var addressBook = {};
$(document).ready(function(){
	//attach an event to each address book selector
	$(".selectFromAddressBook").each(function(){
		var unique = Math.floor(Math.random()*99999999999)
		$(this).attr("id", "ab-" + unique);
		//create an event that fires off when someone interacts with the address book selector
		$(this).change(function(){
		
			//create a unique ID for each address book selector
			addressBook.current = $(this).attr("id");
			var nickname = $(this).val();
			var url = "ajaxGetAddressBook.jsp?nickname=" + nickname;
			var addressData = $.ajax({
				type: "GET",
				url: url,
				dataType: "xml",
				success: function(data, status){
				
					var container = $("#" + addressBook.current).parent().parent();
					
					var updateSelectBox = function(obj){
						var rightOption = $(obj.select).children("option[value='" + obj.toValue + "']");
						if(rightOption.length > 0){
							$(rightOption).attr("selected", "true");
						}
					
					}
					
					//function return an empty string if there is no content for that particular node
					var returnContentNode = function(flag, data){
						
						var str;
						if($(data).find(flag).length > 0){
							str = $(data).find(flag).text();
						} else {
							str = "";
						}
						
						return str;
					}
					
					// populate the existing form with the data returned from the service
					// select boxes first
					updateSelectBox({
						select: $(container).find("select.ab-regionSelector"),
						toValue: returnContentNode("location", data)
					})
					updateSelectBox({
						select: $(container).find("select.ab-state"),
						toValue: returnContentNode("state", data)
					})
					updateSelectBox({
						select: $(container).find("select.ab-country"),
						toValue: returnContentNode("country", data)
					})
					/*
					$(container).find("select.regionSelector").attr("value", returnContentNode("location", data));
					$(container).find("select.ab-state").attr("value", returnContentNode("state", data));
					$(container).find("select.ab-country").attr("value", returnContentNode("country", data));
					*/
					//input fields
					$(container).find("input.ab-street1").attr("value", returnContentNode("address1", data));
					$(container).find("input.ab-street2").attr("value", returnContentNode("address2", data));
					$(container).find("input.ab-street3").attr("value", returnContentNode("address3", data));
					$(container).find("input.ab-street4").attr("value", returnContentNode("address4", data));
					$(container).find("input.ab-city").attr("value", returnContentNode("city", data));
					$(container).find("input.ab-otherCountry").attr("value", returnContentNode("otherCountry", data));
					$(container).find("input.ab-province").attr("value", returnContentNode("province", data));
					$(container).find("input.ab-zip").attr("value", returnContentNode("zip", data));
					$(container).find("input.ab-postalCode").attr("value", returnContentNode("postalCode", data));
					$(container).find("input.ab-phoneNumber").attr("value", returnContentNode("phoneNumber", data));
					$(container).find("").attr("value", returnContentNode("", data));

					//if there is a regionSelector fire off the update event
					$(this).updateRegion($(container).find("select.ab-regionSelector"));



					var countryField = $(container).find("select.ab-country").get(0)

					var whichCountry = returnContentNode("country", data)
					var el = $(countryField).parent().parent().children(".otherCountryField").get(0);
					if (whichCountry == "XX"){
						$(el).removeClass("hidden");
					}else{
						$(el).addClass("hidden");
					}

				},
				error: function(status){
					//console.log(status)
				}

			});
		});
	});
});