function setRegionPulldown(objRegion, objState, objCountry) {
	strCountry = objCountry.options[objCountry.selectedIndex].value;
	if (arrRegion[strCountry] != null) {
		for (var i = 0; i < arrRegion[strCountry].length; ++i) {
			strTempCountry = arrRegion[strCountry][i][1];
			//alert(strTempCountry);
			if (objRegion.value.toLowerCase() == strTempCountry.toLowerCase()) {
				objState.options[i].selected = true;
				//alert(strTempCountry);
			}
		}
	}
}

function fillSelectFromArray(selectCtrl, itemArray, resultCtrl, stateId) {
	var i, j;
	if (stateId == null) stateId = "state";

	// empty existing items
	resultCtrl.value = '';
	for (i = selectCtrl.options.length; i >= 0; i--) {
		selectCtrl.options[i] = null; 
	}
	
	j = 0;
	if (itemArray != null) {
		// show State
		$(stateId).show();
		
		// add new items
		for (i = 0; i < itemArray.length; i++) {
			selectCtrl.options[j] = new Option(itemArray[i][0]);
			if (itemArray[i][1] != null) {
				selectCtrl.options[j].value = itemArray[i][1]; 
			}
			j++;
		}
		// select first item (prompt) for sub list
		selectCtrl.options[0].selected = true;
		resultCtrl.value = selectCtrl.options[0].value;
	} else {
		// hide State
		$(stateId).hide();
	}
}

function printAddressForTesting(arrLocations) {
	document.write("Anzahl: "+arrLocations.length+"<br>");
	for (var i=0; i<arrLocations.length; i++) {
		document.write(i+": "+arrLocations[i]+"<br>");
	}
}

function writeAddress(arrLocations) {
	if (nbhd_lang == "de") {
		lang_chooseAddress = "Bitte die korrekte Adresse w&auml;hlen";
		lang_checkAddress = "Adresse &uuml;bernehmen";
		lang_cancel = "Abbruch";
		lang_AddressNotPresent = "Korrekte Adresse nicht dabei";
	} else {
		lang_chooseAddress = "Please choose the correct address";
		lang_checkAddress = "Choose Address";
		lang_cancel = "Cancel";
		lang_AddressNotPresent = "Correct adress not present";
	}
	
	foundAddress = false;
	
	//alert(Object.toJSON(arrLocations));
	strDiv = "";
	strDiv += lang_chooseAddress + "<br />";
	//strDiv += "<form name=\"formchooser\" id=\"formchooser\">";
	strDiv += "<select name=\"selectedAddress\" id=\"selectedAddress\" size=\"5\">";
	for (var i=0; i<arrLocations.length; i++) {
		selected = "";
		if (i==0) selected=" selected";
		loc = arrLocations[i].evalJSON();
		if (loc.country != undefined) {
			foundAddress = true;
			strDiv += "<option value='"+loc.street+"|"+loc.zip+"|"+loc.city+"|"+loc.state+"|"+loc.country+"'" + selected + ">"+loc.street+", "+loc.zip+", "+loc.city+", "+loc.state+", "+loc.country+"";
		}
	}
	if (!foundAddress || arrLocations.length < 1) {
		strDiv += "<option value='||||'>"+lang_AddressNotPresent;
	}
	strDiv += "</select><br />";
	strDiv += "<button type=\"button\" onclick=\"updateAddress($F('selectedAddress'));\">" + lang_checkAddress + "</button>";
	strDiv += "<button type=\"button\" onclick=\"updateAddress('||||');\">" + lang_cancel + "</button>";
	//strDiv += "</form>";
	$('divCheckAddress').setStyle({display: 'block'});
	$('divCheckAddress').innerHTML = strDiv;
}

function parseJson(geoResult) {
	var arrLocations = [];
	
	//alert(Object.toJSON(geoResult));
	if (Object.keys(geoResult).indexOf("Placemark") != -1) {
		for (var i=0; i<geoResult.Placemark.length; i++) {
			var place = geoResult.Placemark[i].AddressDetails.Country.AdministrativeArea;
			
			//alert(Object.toJSON(geoResult.Placemark[i]));
			
			var street = "";
			var city = "";
			var state = "";
			var zip = "";
			
			if (Object.keys(place).indexOf("SubAdministrativeArea") != -1) {
				if (Object.keys(place.SubAdministrativeArea).indexOf("Locality") != -1) {
					
					// street
					if (Object.keys(place.SubAdministrativeArea.Locality).indexOf("DependentLocality") != -1) {
						if (Object.keys(place.SubAdministrativeArea.Locality.DependentLocality).indexOf("Thoroughfare") != -1) {
							street = place.SubAdministrativeArea.Locality.DependentLocality.Thoroughfare.ThoroughfareName;
						}
					} else if (Object.keys(place.SubAdministrativeArea.Locality).indexOf("Thoroughfare") != -1) {
						street = place.SubAdministrativeArea.Locality.Thoroughfare.ThoroughfareName;
					}
					
					// zip
					if (Object.keys(place.SubAdministrativeArea.Locality).indexOf("DependentLocality") != -1) {
						if (Object.keys(place.SubAdministrativeArea.Locality.DependentLocality).indexOf("PostalCode") != -1) {
							zip = place.SubAdministrativeArea.Locality.DependentLocality.PostalCode.PostalCodeNumber;
						}
					} else if (Object.keys(place.SubAdministrativeArea.Locality).indexOf("PostalCode") != -1) {
						zip = place.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber;
					}
					
					// city
					var city = place.SubAdministrativeArea.Locality.LocalityName;
					//if (Object.keys(place.SubAdministrativeArea.Locality).indexOf("DependentLocality") != -1) {
						//if (Object.keys(place.SubAdministrativeArea.Locality.DependentLocality).indexOf("DependentLocalityName") != -1) {
							//city = place.SubAdministrativeArea.Locality.DependentLocality.DependentLocalityName;
						//}
					//}
					
					// state
					var state = place.AdministrativeAreaName;
					
					// country
					var country = geoResult.Placemark[i].AddressDetails.Country.CountryNameCode;
				}	// if Locality
			} // if SubAdministrativeArea
			
			var data = {street: street, city: city, state: state, zip: zip, country: country};
			arrLocations[i] = Object.toJSON(data);
		} // for
	} // if
	return arrLocations;
}

function checkAddress(country, region, city, zip, street) {
	boolConfirmation = false;
  if (!boolConfirmation || confirm("Wirklich Adresse checken?")) {
		var url = '/res/ajax/mapVerifyAddress.php';
		var params = '?country=' + country + '&region=' + region + '&city=' + city + '&zip=' + zip + '&street=' + street;
		var ajax = new Ajax.Request(
			url,
			{
				method: 'get', 
				parameters: params, 
				onFailure: function(transport) {reportError(transport);}, 
			  onSuccess: function(transport) {
					//alert(transport.responseText);
					//alert ('x'+(transport.responseText.evalJSON()));
					writeAddress(parseJson((transport.responseText)));
			  }
			}
		);
  }
}

arrRegion = [];

arrRegion["CA"] = [];
arrRegion["CA"].push(new Array("Alberta", "AB"));
arrRegion["CA"].push(new Array("British Columbia", "BC"));
arrRegion["CA"].push(new Array("Manitoba", "MB"));
arrRegion["CA"].push(new Array("New Brunswick", "NB"));
arrRegion["CA"].push(new Array("Newfoundland and Labrador", "NL"));
//arrRegion["CA"].push(new Array("Northwest Territories", ""));
arrRegion["CA"].push(new Array("Nova Scotia", "NS"));
arrRegion["CA"].push(new Array("Nunavut", "NU"));
arrRegion["CA"].push(new Array("Ontario", "ON"));
arrRegion["CA"].push(new Array("Prince Edward Island", "PE"));
arrRegion["CA"].push(new Array("Quebec", "QC"));
arrRegion["CA"].push(new Array("Saskatchewan", "SK"));
arrRegion["CA"].push(new Array("Yukon Territory", "YT"));

arrRegion["US"] = [];
arrRegion["US"].push(new Array("Alabama", "AL"));
arrRegion["US"].push(new Array("Alaska", "AK"));
arrRegion["US"].push(new Array("Arizona", "AZ"));
arrRegion["US"].push(new Array("Arkansas", "AR"));
arrRegion["US"].push(new Array("California", "CA"));
arrRegion["US"].push(new Array("Colorado", "CO"));
arrRegion["US"].push(new Array("Connecticut", "CT"));
arrRegion["US"].push(new Array("Delaware", "DE"));
//arrRegion["US"].push(new Array("District Of Columbia", ""));
arrRegion["US"].push(new Array("Florida", "FL"));
arrRegion["US"].push(new Array("Georgia", "GA"));
//arrRegion["US"].push(new Array("Guam", ""));
arrRegion["US"].push(new Array("Hawaii", "HI"));
arrRegion["US"].push(new Array("Idaho", "ID"));
arrRegion["US"].push(new Array("Illinois", "IL"));
arrRegion["US"].push(new Array("Indiana", "IN"));
arrRegion["US"].push(new Array("Iowa", "IA"));
arrRegion["US"].push(new Array("Kansas", "KS"));
arrRegion["US"].push(new Array("Kentucky", "KY"));
arrRegion["US"].push(new Array("Louisiana", "LA"));
arrRegion["US"].push(new Array("Maine", "ME"));
arrRegion["US"].push(new Array("Maryland", "MD"));
arrRegion["US"].push(new Array("Massachusetts", "MA"));
arrRegion["US"].push(new Array("Michigan", "MI"));
arrRegion["US"].push(new Array("Minnesota", "MN"));
arrRegion["US"].push(new Array("Mississippi", "MS"));
arrRegion["US"].push(new Array("Missouri", "MO"));
arrRegion["US"].push(new Array("Montana", "MT"));
arrRegion["US"].push(new Array("Nebraska", "NE"));
arrRegion["US"].push(new Array("Nevada", "NV"));
arrRegion["US"].push(new Array("New Hampshire", "NH"));
arrRegion["US"].push(new Array("New Jersey", "NJ"));
arrRegion["US"].push(new Array("New Mexico", "NM"));
arrRegion["US"].push(new Array("New York", "NY"));
arrRegion["US"].push(new Array("North Carolina", "NC"));
arrRegion["US"].push(new Array("North Dakota", "ND"));
arrRegion["US"].push(new Array("Ohio", "OH"));
arrRegion["US"].push(new Array("Oklahoma", "OK"));
arrRegion["US"].push(new Array("Oregon", "OR"));
arrRegion["US"].push(new Array("Pennsylvania", "PA"));
//arrRegion["US"].push(new Array("Puerto Rico", ""));
arrRegion["US"].push(new Array("Rhode Island", "RI"));
arrRegion["US"].push(new Array("South Carolina", "SC"));
arrRegion["US"].push(new Array("South Dakota", "SD"));
arrRegion["US"].push(new Array("Tennessee", "TN"));
arrRegion["US"].push(new Array("Texas", "TX"));
arrRegion["US"].push(new Array("Utah", "UT"));
arrRegion["US"].push(new Array("Vermont", "VT"));
arrRegion["US"].push(new Array("Virginia", "VA"));
arrRegion["US"].push(new Array("Washington", "WA"));
arrRegion["US"].push(new Array("West Virginia", "WV"));
arrRegion["US"].push(new Array("Wisconsin", "WI"));
arrRegion["US"].push(new Array("Wyoming", "WY"));
