$(document)
		.ready(
				function() {
					states = $("select[name*='State']");
					countries = $("select[name*='Country']");
					if(states.val() == "" && countries.length != 0) {
						disableStatesFields();
					}

					lotSize = $("input[name='LotSize']");
					price = $("input[name='Price']");

					lotSize.change(function() {
						setSquareMetrePrice(lotSize.val(), price.val());
					});

					price.change(function() {
						setSquareMetrePrice(lotSize.val(), price.val());
					});

					countries
							.change(
									function(event) {
										var country = $(this);
										if (country.attr('value') == "") {
											disableStatesFields();
										} else {
											states
													.each(function() {
														var first_option = $("#"
																+ $(this).attr(
																		"id")
																+ " option:first");
														first_option.nextAll()
																.remove();
														switch (country
																.attr('value')) {
														case 'Germany':
															addOptions(
																	states_de,
																	first_option);
															enableStatesFields();
															break;
														case 'Austria':
															addOptions(
																	states_at,
																	first_option);
															enableStatesFields();
															break;
														case 'USA':
															addOptions(
																	states_us,
																	first_option);
															enableStatesFields();
															break;
														default:
															disableStatesFields();
															break;
														}

													});
										}
									});

					function addOptions(statesArray, firstElement) {
						$.each(statesArray, function(name, value) {
							$(
									'<option value="' + name + '">' + value
											+ '</option>').insertAfter(
									firstElement);
						});
					}

					function setSquareMetrePrice(lotSize, price) {
						squareMetrePrice = $("input[name='PriceAtSquareMetre']");
						lotSize = parseFloat(lotSize);
						price = parseFloat(price);
						squareMetrePrice.val(formatZahl((price / lotSize).toString(), 2, true));
					}

					function disableStatesFields() {
						states.each(function() {
							$(this).attr('disabled', true);
						});
					}
					function enableStatesFields() {
						states.each(function() {
							$(this).attr('disabled', false);
						});
					}

					function formatZahl(zahl, k, fix) {

						if (!k)
							k = 0;

						var neu = '';

						var dec_point = ',';

						var thousands_sep = '.';

						// Runden

						var f = Math.pow(10, k);

						zahl = ''
								+ parseInt(zahl * f
										+ (.5 * (zahl > 0 ? 1 : -1))) / f;

						// Komma ermittlen

						var idx = zahl.indexOf('.');

						// fehlende Nullen einfügen

						if (fix) {

							zahl += (idx == -1 ? '.' : '')

							+ f.toString().substring(1);

						}

						var sign = zahl < 0;

						if (sign)
							zahl = zahl.substring(1);

						idx = zahl.indexOf('.');

						// Nachkommastellen ermittlen

						if (idx == -1)
							idx = zahl.length;

						else
							neu = dec_point + zahl.substr(idx + 1, k);

						while (idx > 0) {

							if (idx - 3 > 0)

								neu = thousands_sep
										+ zahl.substring(idx - 3, idx) + neu;

							else

								neu = zahl.substring(0, idx) + neu;

							idx -= 3;

						}

						return (sign ? '-' : '') + neu;

					}

				});