// Strecken + Streckenmatrix laden, muss durch down compat. im global scope geladen werden
$.ajax({url: document.URL, async: false, cache: false, dataType: "script", data: {_ajaxLoader: true}});
	
$(document).ready(function() {
	
	// 3-Lettercode Eingabefelder einblenden
	$("#outbound-3lc, #left-side #inbound-3lc").show();
	$("#outbound-3lc").focus();
	
	$.fn.extend({
		
		initCalendar: function() {
		
			$(".draw-calendar").html('<img src="/site/css/images/ajax-loader-wheel.gif" />');
			$.include('/site/css/screen/jquery.datepicker.css');
			
			$.include('jquery/jquery.datepicker.js', function() {
	
				var calendarLanguageFile = '/site/javascript/jquery/ui/i18n/ui.datepicker-' + calendarLanguage + '.js';
				
				if (calendarLanguage != 'en') {
					var calPluginLoadStatus = false;
					var calendarLanguagePlugin = $.ajax({url: calendarLanguageFile, async: false, success: function(data, status) {calPluginLoadStatus = status}}).responseText;
					if (calPluginLoadStatus == 'success') eval(calendarLanguagePlugin);
				}
				
				$(this).loadDateHandler();
				
			});
		
		},
		
		loadDateHandler: function() {
			
			var outboundDate = $("#outbound-month").val() + '-' + $("#outbound-day").val();
			var inboundDate = $("#inbound-month").val() + '-' + $("#inbound-day").val();
			
			// Kalender init
			$(".draw-calendar").html("").datepicker({
				firstDay: 1,
				changeFirstDay: false,
				changeMonth: false,
				changeYear: false,
				minDate: new Date(),
				maxDate: '+' + futureMonths + 'm', // futureMonths wird mit der Streckenmatrix asynchron geladen
				dateFormat: 'yy-mm-dd'
			});

			// outbound date aus Selectboxen in den Kalender uebernehmen
			$("#route-outbound").find(".draw-calendar").setDate('outbound', outboundDate, true);
			
			// outbound day change
			$("#outbound-day").bind('change keyup', function() {
				$(this).setDate('outbound', $("#outbound-month").val() + '-' + $("#outbound-day").val());
				$(this).setDate('inbound', $("#outbound-month").val() + '-' + $("#outbound-day").val(), false, true);
			});
			
			// outbound month change
			$("#outbound-month").bind('change keyup', function() {
				$(this).setDate('outbound', $("#outbound-month").val() + '-' + $("#outbound-day").val(), true);
				$(this).setDate('inbound', $("#outbound-month").val() + '-' + $("#outbound-day").val(), true, true);
			});
			
			// outbound calendar change
			$("#route-outbound").find(".draw-calendar").datepicker("change", {
				onSelect: function(date) {
					$(this).setDate('outbound', date);
					$(this).setDate('inbound', date, false, true);
				}
			});

			// inbound date aus Selectboxen in den Kalender uebernehmen
			$("#outbound-day").change();
			$("#route-inbound").find(".draw-calendar").setDate('inbound', inboundDate, true);
			
			// inbound day change
			$("#inbound-day, #inbound-month").bind('change keyup', function() {
				$(this).setDate('inbound', $("#inbound-month").val() + '-' + $("#inbound-day").val());
			});
			
			// inbound month change
			$("#inbound-month").bind('change keyup', function() {
				$(this).setDate('inbound', $("#inbound-month").val() + '-' + $("#inbound-day").val(), true);
			});
			
			// inbound calendar change
			$("#route-inbound").find(".draw-calendar").datepicker("change", {
				onSelect: function(date) {$(this).setDate('inbound', date)}
			});	
			
		},
		
		// Datum setzen
		setDate: function(type, date, resetDayRange, setToMinDate) {

			switch (type) {
				case 'outbound':
					var now = new Date();
					var compareDate = new Date(now.getFullYear(), now.getMonth(), now.getDate());
					var setDate = compareDate.getFullYear() + '-' + compareDate.getMonth() + '-' + compareDate.getDate();
				break;
				case 'inbound':
					var outboundCalendarDate = $("#route-outbound").find(".draw-calendar").datepicker().val();
					var compareDate = new Date(outboundCalendarDate.split('-')[0], outboundCalendarDate.split('-')[1] - 1, outboundCalendarDate.split('-')[2]);
					var setDate = outboundCalendarDate;
				break;
			}
			
			var dayField = $("#" + type + "-day");
			var monthField = $("#" + type + "-month");
			var calendar = $("#route-" + type).find(".draw-calendar");
			
	 		// max. Tage des ausgewaehlten Monats bestimmen
		    var daysInMonth = 32 - new Date(date.split('-')[0], date.split('-')[1] - 1, 32).getDate();
		    // wenn gewaehlter Tag des Monats zu gross, dann letzten Tag nehmen
			if (parseInt(date.split('-')[2]) > daysInMonth) date = date.split('-')[0] + '-' + date.split('-')[1] + '-' + daysInMonth;
			// uebergebenes Datum als Zeitstempel
			var currentDate = new Date(date.split('-')[0], date.split('-')[1] - 1, date.split('-')[2]);
			// wenn Datum kleiner als Vergleichsdatum, dann auf setDate setzen
			if (currentDate < compareDate) date = setDate;
			// Anzahl der Tage in Selectbox neu generieren
			if (resetDayRange == true) dayField.setMonthsDayRange(daysInMonth);
			// Datum explizit setzen
			try {dayField.val(date.split('-')[2])} catch(err) {};
			monthField.val(date.split('-')[0] + '-' + date.split('-')[1]);
			// Datum als Mindestdatum setzen
			if (setToMinDate == true) calendar.datepicker("change", {minDate: new Date(date.split('-')[0], date.split('-')[1] - 1, date.split('-')[2])});
			calendar.datepicker("setDate", new Date(date.split('-')[0], date.split('-')[1] - 1, date.split('-')[2]));

		},
		
		// Tage in den Selectboxen auf korrekte Tageslaenge setzen
		setMonthsDayRange: function(daysInMonth) {

			$(this).find("option").remove();
	    	for (dayCounter = 1; dayCounter <= daysInMonth; dayCounter++) {
	    		var dayValue = dayCounter < 10 ? '0' + dayCounter : dayCounter;
	    		$('<option></option>').val(dayValue).html(dayValue).appendTo($(this));
	    	}
	    	
		}
		
	});
	
	// Kalender laden
	$(this).initCalendar();
	
	// Flash-Funktionen einbinden
	$.include('/site/api/routeselection.js', function() {
		$.include('/site/api/flashfunctions.js', function() {

			var outboundInput = $("input[name=LV3]");
			var outboundSelect = $("select[name=VON]");

			var inboundInput = $("input[name=LN3]");
			var inboundSelect = $("select[name=NACH]");

			// in der Funktion Portupdate werden jene Objektnamen (formXXX) noch so benoetigt
			formObj1 = document.getElementsByName(outboundSelect.attr("name"))[0];
			formObj4 = document.getElementsByName(inboundSelect.attr("name"))[0];
			formObj12 = document.getElementsByName(outboundInput.attr("name"))[0];
			formObj13 = document.getElementsByName(inboundInput.attr("name"))[0];
			
			// PortUpdate ausfuehren, wenn outbound 3-Lettercode vorbelegt ist
			if (outboundInput.val()) PortUpdate(outboundInput.val());
			
			// outbound selectbox change
			outboundSelect.change(function() {PortUpdate($(this).val())});
			
			// eingabe 3 lettercode outbound
			outboundInput.keyup(function() {
				var inputValue = $(this).val().toUpperCase();
				$(this).val(inputValue);
				if (inputValue.length == 3 || inputValue.length == 0) {
					outboundSelect.val(inputValue);
					PortUpdate(inputValue);
				}
			});	

			// inbound selectbox change
			inboundSelect.change(function() {inboundInput.val($(this).val())});
			
			// eingabe 3 lettercode inbound
			inboundInput.keyup(function() {
				var inputValue = $(this).val().toUpperCase();
				$(this).val(inputValue);				
				if (inputValue.length == 3  || inputValue.length == 0) {
					inboundSelect.val(inputValue);
				}
			});	

		});
	});
	
	// oneway kalender ausblenden
	if ($("input[name=ONEWAY]").attr("checked")) $("#route-inbound").find("#inbound-date").hide();
	$("input[name=ONEWAY]").click(function() {$("#route-inbound").find("#inbound-date").toggle()});
	
	// Ajax-Formcheck
	$("#frm-route-selection").submit(function() {
		
		var submitter = $(this).find("input[type=submit]");
		
		var data = $.ajax({
			url: $(this).attr('action'), async: false, type: 'POST', cache: false,
			data: '_ajaxFormCheck=true&'+submitter.attr("name")+'='+submitter.val()+'&'+$(this).serialize()
		}).responseText;
						
		if (data == true) {
			return true;
		} else {
			alert(data);
			return false;
		}
		
	});

	// WebCheckin Form - Title als Value einsetzen
	$("#wbciForm input[type=text]").each(function() {
		$(this).val($(this).attr("title"));
	});
	
	// WebCheckin Form - Value bei Focus entfernen, wenn Title
	$("#wbciForm").find("#wbciBox_nr, #wbciBox_name").focus(function() {
		if ($(this).val() == $(this).attr("title")) $(this).val("");
	});

});