/**
 * @author groening
 */

function submitForm(validate, form){

	if (!form || !document.getElementById(form)) {
		splitarea();
		if (validate == 1) {
			newsearch = document.getElementById('newsearch');
			if (newsearch) {
				newsearch.value = '1';
			}
		}
		
		document.forms[0].validate_data.value = validate;
		document.forms[0].submit();
	}
	else {
		var f = document.getElementById(form) ? document.getElementById(form) : form;
		splitarea();
		
		if (validate == 1) {
			newsearch = document.getElementById('newsearch');
			if (newsearch) {
				newsearch.value = '1';
				
			}
		}
		f.validate_data.value = validate;
		f.submit();
	}
}

function submitFormOnEnter (e){

	e = e || window.event;
	
	var target = (e.target) ? e.target : e.srcElement;
	var target_name = target.tagName;
	
	var key = e.which || e.keyCode;
	
	// Reagiert nur auf Enter
	if (key == 13 && target_name != 'TEXTAREA') {
		// Entscheidung, wie das Formular zu übermitteln ist
		// anhand der aktuellen Location
		var href = location.href;

		// Suche und Ergebnisliste
		if( ( ( t = href.search(/ferienhaeuser.+/) ) != -1 )
		 || ( ( t = href.search(/winter.+/) ) != -1 )
		 || ( ( t = href.search(/lastminute.+/) ) != -1 ) 
		 || ( ( t = href.search(/caravanparks.+/) ) != -1 )
		 || ( ( t = href.search(/ergebnisliste.+/) ) != -1 )) {	
			lockPage('search');
			submitForm(1, 'form_search');
		}
		// Detailanzeige
		else if( ( t = href.search(/detail.+/) ) != -1 ) {
		
			var route_start = document.getElementById( 'route_start' );
			if( route_start )
			{
				showRoutePlanner();
			}
			else
			{
			var booking_date_from = document.getElementById( 'datum_von' );
			var booking_date_to = document.getElementById( 'datum_bis' );
			var ocean_avail = document.getElementById( 'ocean_avail' );
			var show_notvacant_err = document.getElementById( 'show_notvacant_err' );
			var flexdays = document.getElementById( 'flexdays' );
			
				if( ( booking_date_from.value == "von" ) || ( booking_date_to.value == "bis" ) )
			{
				if( ( t = href.search(/buchen.+/) ) == -1 ) {
					location.href = href + "#buchen";
				}			
				else
				{
					location.href = href;
				}
			}
			else
			{
				if( ( show_notvacant_err.value != '1' ) && ( flexdays.value != '1' ) )
				{
					lockPage('search');
						bookObject('form_detail', 'booking_key', document.getElementById('booking_key').value, 
						document.getElementById('datum_von').value, document.getElementById('datum_bis').value, 
						document.getElementById('dauer_select').value);
				}
				else if( ocean_avail.value == '1' )
				{
					lockPage('search');
						submitForm('query', 'form_detail');
				}
				else
				{
					location.href = "/error_no_booking.html";
				}
			}
		}
		}
		// Buchungsdateneingabe
		else if( ( t = href.search(/buchung_daten.+/) ) != -1 ) {
			submitForm('1', 'form_booking_data');
		}
		// Buchungsdatenübersicht
		else if( ( t = href.search(/buchung_uebersicht.+/) ) != -1 ) {
			submitForm('book', 'form_booking_rrv');
		}
		// Hauptseite
		else if( (href == 'http://www.tui-ferienhaus.de/') || (href == 'http://tui.wol.de/') || (href == 'http://tui_dev2.wol.de/'))
		{
			lockPage('search');
			document.expresssearch.validate_data.value='1';
			document.expresssearch.submit();
		}
	}
}

function lockPage(task){
	var idname = 'bw';
	if ((t = task.search(/book.+/)) != -1) {
		idname = 'bwb';
	}
	else 
		if ((t = task.search(/search/)) != -1) {
			idname = 'bws';
		}
		else 
			if ((t = task.search(/detail/)) != -1) {
				idname = 'bwdetail';
			}
			else 
				if ((t = task.search(/query/)) != -1) {
					return false;
				}
	
	if(idname == 'bws' || idname == 'bwdetail' ) {
		if( $('#datum_von').val() && $('#datum_von').val() != 'von' ){
			var d = new Date( $('#datum_von').val().substr(6,4), Number($('#datum_von').val().substr(3,2))-1, $('#datum_von').val().substr(0,2) );
			if( d.getDay() != 6 ) {
				var loaderText = '<strong>Sie haben einen Anreisetag gew&auml;hlt, der nicht unser Standardanreisetag ist. Die Suche kann daher etwas l&auml;nger dauern (bis zu 1 Minute).</strong><br />Bitte haben Sie einen Augenblick Geduld';
				if( $('#bws_loader p').length ){					
					$('#bws_loader p').html( loaderText );
				}
				if( $('#bwdetail_loader p').length ){
					$('#bwdetail_loader p').html( loaderText );
				}
					
			}			
		}
	}
	
	if ($.browser.msie) {
		placeDiv(idname + '_loader');
		$('select').hide();
	}	
	$('#' + idname).css( 'width', $(window).width() + 'px');
	$('#' + idname).css( 'height', $('#footer').position().top + $('#footer').height() + 'px');
	
	$('#' + idname).show();
}

function splitarea()
{
	if( document.getElementById('area_select') )
	{
		var Current = document.getElementById('area_select').selectedIndex;
	
		
		var areastr = document.getElementById('area_select').options[Current].value.split(",");
		document.getElementById('area_select').options[Current].value = areastr[0];
		
		if(document.getElementById('area_select').selectedIndex != 0)		
		{	var r_s = document.getElementById('region_select').selectedIndex;
			document.getElementById('region_select').options[r_s].value = areastr[1];
		}
	}
}

