(function(){
	var isoCountries = {
		"BEL": "BE",
		"DNK": "DK",
		"DEU": "DE",
		"FRA": "FR",
		"GBR": "GB",
		"ITA": "IT",
		"LIE": "LI",
		"LUX": "LU",
		"NLD": "NL",
		"NOR": "NO",
		"AUT": "AU",
		"PRT": "PT",
		"CHE": "CH",
		"ESP": "ES",
		"SWE": "SW",
		"EST": "EE",
		"LVA": "LV",
		"LTU": "LT",
		"POL": "PL",
		"CZE": "CZ",
		"SVK": "SK",
		"SVN": "SI",
		"HUN": "HU",
		"MLT": "MT",
		"CYP": "CY",
		"TUR": "TR",
		"FIN": "FI",
		"GRC": "GR",
		"IRL": "IE",
		"ROM": "RO",
		"BGR": "BG"
	};
	var michelinCountries = {
		"BEL": "311",
		"DNK": "1473",
		"DEU": "240",
		"FRA": "1424",
		"GBR": "1138",
		"ITA": "612",
		"LIE": "108",
		"LUX": "247",
		"NLD": "285",
		"NOR": "1574",
		"AUT": "106",
		"PRT": "669",
		"CHE": "185",
		"ESP": "844",
		"SWE": "1507",
		"EST": "1860861",
		"LVA": "1851089",
		"LTU": "1851066",
		"POL": "1743",
		"CZE": "1694",
		"SVK": "1697",
		"SVN": "1746",
		"HUN": "1741",
		"TUR": "2059154",
		"FIN": "1792",
		"GRC": "1945835",
		"IRL": "919",
		"ROM": "1025349",
		"BGR": "1025340"
	};
	var teleinfoCountries = {
		"BEL": "B",
		"DNK": "DK",
		"DEU": "D",
		"FRA": "F",
		"GBR": "GB",
		"ITA": "I",
		"LIE": "CH",
		"LUX": "L",
		"NLD": "NL",
		"NOR": "N",
		"AUT": "A",
		"PRT": "P",
		"CHE": "CH",
		"ESP": "E",
		"SWE": "S",
		"EST": "EST",
		"LVA": "LV",
		"LTU": "LT",
		"POL": "PL",
		"CZE": "CZ",
		"SVK": "SK",
		"SVN": "SLO",
		"HUN": "H",
		"TUR": "TR",
		"FIN": "FIN",
		"GRC": "GR",
		"IRL": "IRL",
		"ROM": "RO",
		"BGR": "BG"
	};
	var reiseplanungCountries = {
		"ALB": "AL",
		"AND": "AND",
		"BEL": "B",
		"BIH": "BIH",
		"BGR": "BG",
		"DNK": "DK",
		"DEU": "D",
		"EST": "EST",
		"FIN": "FIN",
		"FRA": "F",
		"GEO": "GE",
		"GRC": "GR",
		"GBR": "GB",
		"IRL": "IRL",
		"ISL": "IS",
		"ITA": "I",
		"HRV": "HR",
		"LVA": "LV",
		"LIE": "FL",
		"LTU": "LT",
		"LUX": "L",
		"MLT": "M",
		"MKD": "MK",
		"MDA": "MD",
		"MCO": "MC",
		"NLD": "NL",
		"NOR": "N",
		"AUT": "A",
		"POL": "PL",
		"PRT": "P",
		"ROM": "RO",
		"RUS": "RUS",
		"SWE": "S",
		"CHE": "CH",
		"SCG": "SRB",
		"SVK": "SK",
		"SVN": "SLO",
		"ESP": "E",
		"CZE": "CZ",
		"TUR": "TR",
		"UKR": "UA",
		"HUN": "H",
		"BLR": "BY",
		"CYP": "CY"
	};

	// temporary
	var client = "webde";

	var popup = function(url, name, checksum) {
		window.open('http://dereferer.web.de/dereferer.do?dc=3435&dl=' + name + '&dest=' + encodeURIComponent(url) + '&s=' + checksum, name);
		//window.open(url, name);

		var fromStreet = $ui('#start-adress').val() == "Adresse" ? "" : encodeURIComponent($ui('#start-adress').val());
		var fromZip = $ui('#start-zip').val() == "PLZ" ? "" : encodeURIComponent($ui('#start-zip').val());
		var fromCity = $ui('#start-city').val() == "Ort" ? "" : encodeURIComponent($ui('#start-city').val());
		var fromCountry = $ui('#start-country').val() == "Ort" ? "" : encodeURIComponent($ui('#start-country').val());
		var toStreet = $ui('#dest-adress').val() == "Adresse" ? "" : encodeURIComponent($ui('#dest-adress').val());
		var toZip = $ui('#dest-zip').val() == "PLZ" ? "" : encodeURIComponent($ui('#dest-zip').val());
		var toCity = $ui('#dest-city').val() == "Ort" ? "" : encodeURIComponent($ui('#dest-city').val());
		var toCountry = $ui('#dest-country').val() == "Ort" ? "" : encodeURIComponent($ui('#dest-country').val());
		var qurl = "";
		qurl = '/?template=reloaded&fromstreet=' + fromStreet + '&fromplz=' + fromZip + '&fromcity=' + fromCity + '&fromcountry=' + fromCountry + '&tostreet=' + toStreet + '&toplz=' + toZip + '&tocity=' + toCity + '&tocountry=' + toCountry;
		if (name != 'hotelsuche') {
			window.setTimeout(function() {
				document.location.href = qurl;
			}, 500);
		}
	};

	var checkInput = function (evt) {
		evt.preventDefault();
		if ($ui("#routeform").valid()) {
			/* via michelin does not work with encodeURIComponent */
			var from_city = ($ui('#start-city').val() != "Ort") ? escape($ui('#start-city').val()) : "";
			var from_plz = ($ui('#start-zip').val() != "PLZ") ? escape($ui('#start-zip').val()) : "";
			var from_street = ($ui('#start-adress').val() != "Adresse") ? escape($ui('#start-adress').val()) : "";
			var from_country = ($ui('#start-country').val() != "---") ? $ui('#start-country').val() : "---";
			//var from_country_text = ($ui('#start-country :selected').text() != "Bitte w??hlen...") ? $ui('#start-country :selected').text() : "";
			var to_city = ($ui('#dest-city').val() != "Ort") ? escape($ui('#dest-city').val()) : "";
			var to_plz = ($ui('#dest-zip').val() != "PLZ") ? escape($ui('#dest-zip').val()) : "";
			var to_street = ($ui('#dest-adress').val() != "Adresse") ? escape($ui('#dest-adress').val()) : "";
			var to_country = ($ui('#dest-country').val() != "---") ? $ui('#dest-country').val() : "---";
			//var to_country_text = ($ui('#dest-country :selected').text() != "Bitte w??hlen...") ? $ui('#dest-country :selected').text() : "";
			// Redirecten zum entsprechenden Routenplaner ...
			var url = "http://";
			switch (evt.currentTarget.id.replace(/btn\-/, "")) {
				case "map24":
					from_country = isoCountries[from_country] ? isoCountries[from_country] : 'DE';
					to_country = isoCountries[to_country] ? isoCountries[to_country] : 'DE';
					url = url + "link2.map24.com/?newroute=1";
					url = url + "&sstreet=" + from_street + "&szip=" + from_plz + "&scity=" + from_city + "&scountry=" + from_country + "&dstreet=" + to_street + "&dzip=" + to_plz + "&dcity=" + to_city + "&dcountry=" + to_country;
					url = url + "&mid=LINK2&lid=a36ca77b";
					popup(url, "Map24", "Eb6e9");
				break;
				case "viaMichelin":
					from_country = michelinCountries[from_country] ? michelinCountries[from_country] : '240';
					to_country = michelinCountries[to_country] ? michelinCountries[to_country] : '240';
					var vehicle = $ui("#vm-vehicle").val();
					var routeType = $ui("#vm-routetype").val();
					var mandatory = $ui('#vmh-mandatory').val();
					if(vehicle != 0 ) { //Not the car, set Routy Type to 1
						routeType = 1;
					}
					/*switch(client) {
						case "gmx":
							url = url + "ad.doubleclick.net/clk;212143260;12088395;w?";
						break;
						case "webde":
							url = url + "ad.doubleclick.net/clk;212143262;12088395;y?";
						break;
						case "1und1":
							url = url + "ad.doubleclick.net/clk;212143258;12088395;d?";
						break;
					}*/
					url = url + "www.viamichelin.de/web/Routenplaner?";
					url = url + "strStartAddress=" + from_street + "&strStartCP=" + from_plz + "&strStartCity=" + from_city + "&strStartCityCountry=" + from_country + "&strDestAddress=" + to_street + "&strDestCP=" + to_plz + "&strDestCity=" + to_city + "&strDestCityCountry=" + to_country;
					url = url + "&strVehicle=" + vehicle + "&intItineraryType=" + routeType + "&from=" + mandatory;
					popup(url, "ViaMichelin", "TC7fR");
				break;
				case "Shell":
					from_country = isoCountries[from_country] ? isoCountries[from_country] : 'DE';
					to_country = isoCountries[to_country] ? isoCountries[to_country] : 'DE';
					url = url + "www.shellgeostar.de/share/iti.asp?";
					url = url + "ITI_MODE=vehicule&ITI_MODE_OPTION_vehicule=Time";
					url = url + "&ITI_START_ADDRESS=&ITI_END_ADDRESS=&ITI_START_ZIPCODE=" + from_plz + "&ITI_END_ZIPCODE=" + to_plz + "&ITI_START_COUNTRYCODE=" + from_country + "&ITI_END_COUNTRYCODE=" + to_country + "&SEARCH_ITI.x=0&VN=Export&VV=&ITIERRORCITYSTART=&ITIERRORCITYEND=&ITI_START_CITYNAME=" + from_city + "&ITI_END_CITYNAME=" + to_city;
					popup(url, "Shell", "KWTSr");
				break;
				case "dea":
					if (from_country != "DEU" || to_country != "DEU") {
						document.getElementById('start-country').focus();
						alert("DEA unterst??tzt nur Deutschland!");
						return false;
					}
					url = url + "dea.mapandguide.com/relaunch/geocode_routing.asp?";
					url = url + "BCASE=Ts&PLZ_START=" + from_plz + "&PLZ_ZIEL=" + to_plz + "&HAUPTORT_START=" + from_city + "&HAUPTORT_ZIEL=" + to_city + "&TEILORT_START=&TEILORT_ZIEL=&STRASSE_START=" + from_street + "&STRASSE_ZIEL=" + to_street + "&KORRI=1000&OPTIM=70&FAHRZ=1";
					window.open(url, "DEA");
				break;
				case "teleInfo":
					from_country = teleinfoCountries[from_country] ? teleinfoCountries[from_country] : 'D';
					to_country = teleinfoCountries[to_country] ? teleinfoCountries[to_country] : 'D';
					url = url + "www.telemap.de/free/router.asp?id=za&";
					url = url + "Startplz=" + from_plz + "&StartOrt=" + from_city + "&Startstr=" + from_street + "&Startland=" + from_country + "&Zielplz=" + to_plz + "&ZielOrt=" + to_city + "&Zielstr=" + to_street + "&Zielland=" + to_country;
					popup(url, "Teleinfo", "6gIvz");
				break;
				case 'reiseplanungde':
					from_country = reiseplanungCountries[from_country] ? reiseplanungCountries[from_country] : 'D';
					to_country = reiseplanungCountries[to_country] ? reiseplanungCountries[to_country] : 'D';
					url = url + "www.reiseplanung.de/default.aspx?task=route_address";
					url = url + "&zipcode=" + to_plz + "," + from_plz;
					url = url + "&city=" + to_city + "," + from_city;
					url = url + "&street=" + to_street + "," + from_street;
					url = url + "&country=" + to_country + "," + from_country;
					popup(url, "reiseplanung", "NOtzD");
				break;
				case 'klickTel':
					if (from_country != "DEU" || to_country != "DEU") {
						document.getElementById('start-country').focus();
						alert("DEA unterst??tzt nur Deutschland!");
						return false;
					}
					url = url + "routenplaner.klicktel.de/?";
					var from = [];
					if (from_street && from_street !== '') {
						from.push(from_street.replace(' ', '%20'));
					}
					if (from_plz && from_plz !== '') {
						from.push(from_plz.replace(' ', '%20'));
					}
					if (from_city && from_city !== '') {
						from.push(from_city.replace(' ', '%20'));
					}
					var to = [];
					if (to_street && to_street !== '') {
						to.push(to_street.replace(' ', '%20'));
					}
					if (to_plz && to_plz !== '') {
						to.push(to_plz.replace(' ', '%20'));
					}
					if (to_city && to_city !== '') {
						to.push(to_city.replace(' ', '%20'));
					}
					url = url + "s0=" + from.join(encodeURIComponent(' ')) + "&s4=" + to.join(encodeURIComponent(' '));
					popup(url, "Klicktel", "kR89s");
				break;
			}
		}
		else {
			// show tooltip at first error
			$ui('#routeform :input.error:first').focus();
		}
	};

	//Via Michelin Hotelsearch
	var checkViaMichelinHotelSearch = function() {
		if ($ui("#hotelform").valid()) {
			if($ui('#vmh-arrival').val() == 'Ankunft (tt/mm/yyyy)') {
				$ui('#vmh-arrival').val('')
			} else {
				var arrival = ($ui('#vmh-arrival').val());
				if(arrival.lastIndexOf(".") != "-1") {
					arrival = arrival.split(".");
					arrival = arrival[0] + "/" + arrival[1] + "/" + arrival[2];
					$ui('#vmh-arrival').val(arrival);
				}
			}
			if($ui('#vmh-departure').val() == 'Abfahrt (tt/mm/yyyy)') {
				$ui('#vmh-departure').val('')
			} else {
				var departure = ($ui('#vmh-departure').val());
				if(departure.lastIndexOf(".") != "-1") {
					departure = departure.split(".");
					departure = departure[0] + "/" + departure[1] + "/" + departure[2];
					$ui('#vmh-departure').val(departure);
				}
			}

			if($ui('#vmh-adress').val() == 'Adresse') {
				$ui('#vmh-adress').val('');
			}

			var url = "http://";
			var adUrl = "";

			switch(client) {
				case "gmx":
					adUrl = adUrl + "ad.doubleclick.net/clk;212143261;12088395;x?";
					break;
				case "webde":
					adUrl = adUrl + "ad.doubleclick.net/clk;212143240;12088395;u?";
					break;
				case "1und1":
					adUrl = adUrl + "ad.doubleclick.net/clk;212143259;12088395;e?";
					break;
			}

			var destUrl = "http://";
			destUrl = destUrl + "www.viamichelin.de/web/Hotels?";
			destUrl = destUrl + "partner=" + $ui("#vmh-pt_partner").val();
			destUrl = destUrl + "&treatment=" + $ui("#vmh-pt_treatment").val();
			destUrl = destUrl + "&listCode=" + $ui("#vmh-pt_listCode").val();
			destUrl = destUrl + "&spatialType=" + $ui("#vmh-pt_spatialType").val();
			destUrl = destUrl + "&ie=" + $ui("#vmh-pt_ie").val();

			destUrl = destUrl + "&strCountry=" + escape($ui("#vmh-country").val());
			destUrl = destUrl + "&strMerged=" + escape($ui("#vmh-city").val());
			destUrl = destUrl + "&strAddress=" + escape($ui("#vmh-adress").val());
			destUrl = destUrl + "&strArrivalDate=" + $ui("#vmh-arrival").val();
			destUrl = destUrl + "&strDepartureDate=" + $ui("#vmh-departure").val();
			destUrl = destUrl + "&strPersons=" + $ui("#vmh-persons").val();
			//destUrl = escape(destUrl);
			url = url + adUrl + destUrl;
			popup(url, "hotelsuche", "mqxEX");
			//reset fields if manipulated by javascript
			if($ui('#vmh-adress').val() == '') {
				$ui('#vmh-adress').val('Adresse');
			}
		}
		return false;
	};

	$ui(function () {
		$ui("button[type='submit'].planer").bind("click", null, checkInput);
		$ui("#btn-viemichelin-hotel").bind("click", null, checkViaMichelinHotelSearch);
	});
	
})();
