/*********************************************************/
/*  Validierungsfunktion für die Startmaske (Standard)   */
/*********************************************************/

/*

fromt[0]  =  Abflug (aus dropdown, normal)
fromt[1]  =  Abflug (aus Textinputfeld, normal)
fromt[2]  =  Abflug (aus Textinputfeld bei Gabelflügen)

destt[0]  =  Ziel (aus Textinputfeld bei Gabelflügen)
destt[1]  =  Ziel (aus Textinputfeld, normal)

*/

var xxx = 0;
var dropdown=0;

function init_page ()
{
	var p2p, aktindex;
	var ftype = document.forms.startform.flugart.options[document.forms.startform.flugart.selectedIndex].value;

	p2p = 0;
	if (document.forms.startform.p2p)    //  P2P erlaubt ?
	{
		if (parseInt(document.forms.startform.p2p.value) == 1) p2p = 1;
	}
	p2p_st = 0;
	if (document.forms.startform.p2p_start)    //  P2P schon zu Beginn ?
	{
		if (parseInt(document.forms.startform.p2p_start.value) == 1) p2p_st = 1;
	}

//  A) es wurde ein Gabelflug gewählt

	
	if (ftype == "G")
	{
		anzeigen ("input_dropdown", 0);
		anzeigen ("input_texthin", 0);
		anzeigen ("input_textrueck", 0);
		anzeigen ("input_openjawhin", 1);
		anzeigen ("input_openjawrueck", 1);
//document.forms.startform.fromt[0].length = 0;
	}

// B) Oneway- oder Return-Flug			
	else
	{
		anzeigen ("input_openjawhin", 0);
		anzeigen ("input_openjawrueck", 0);

//  wir prüfen, ob wir einen fromt-Vorgabewert haben	

		if (document.forms.startform.startfromt.value == "")  //  keine Vorgaben
		{
			// alert(document.forms.startform.fromt[0].length);
			// if (p2p_st == 1 && p2p == 1 // changed sma 14.05.08
			if (p2p_st == 1 && p2p == 1 && document.forms.startform.fromt[0].length == 1)    //  P2P schon zu Beginn (aber nur, wenn nicht bereits eine Anfrage gemacht wurde, die mehrere mögliche Abflugsorte zurückgibt) ?
			{
				anzeigen ("input_dropdown", 0);
				anzeigen ("input_texthin", 1);
				anzeigen ("input_textrueck", 1);
//document.forms.startform.fromt[0].length = 0;
			}
			else
			{
				anzeigen ("input_dropdown", 1);
				anzeigen ("input_texthin", 0);
				anzeigen ("input_textrueck", 1);
				dropdown = 1;
			}
		}
		else    //  es gibt eine Vorgabe
		{
			aktindex = 0;
			for (i=0 ; i<document.forms.startform.fromt[0].length; i++)	
			{
				if (document.forms.startform.fromt[0].options[i].value == document.forms.startform.startfromt.value)
				{	
					aktindex = i;
					break;
				}
			}
			if (i < document.forms.startform.fromt[0].length)
			{
				document.forms.startform.fromt[0].selectedIndex = aktindex;		
				anzeigen ("input_dropdown", 1);
				anzeigen ("input_texthin", 0);
				anzeigen ("input_textrueck", 1);
				dropdown = 1;
			}
			else
			{
				document.forms.startform.fromt[0].selectedIndex = aktindex;		
				anzeigen ("input_dropdown", 0);
				anzeigen ("input_texthin", 1);
				anzeigen ("input_textrueck", 1);
//document.forms.startform.fromt[0].length = 0;
			}
		}					
	}

	if (ftype == "O")  //  Oneway
	{
		if (document.startform.rdate1 && document.startform.rdate2)
		{
			document.startform.rdate1.disabled = "true";
			document.startform.rdate2.disabled = "true";
		}
		if (document.startform.rdate)
		{
			document.startform.rdate.disabled = "true";
		}
		document.startform.rtime.disabled = "true";
		anzeigen ("Kalender_R1", 0);
		anzeigen ("Kalender_R2", 1);
	}
	else  //  Return- oder Gabelflug
	{
		if (document.startform.rdate1 && document.startform.rdate2)
		{
			document.startform.rdate1.disabled = 0;
			document.startform.rdate2.disabled = 0;
		}
		if (document.startform.rdate)
		{
			document.startform.rdate.disabled = 0;
		}
		document.startform.rtime.disabled = 0;
		anzeigen ("Kalender_R1", 1);
		anzeigen ("Kalender_R2", 0);
	}	
	
	anzeigen ("pause", 0);
	anzeigen ("error", 1);

	aktindex = 0;
	for (i=0 ; i<document.forms.startform.carrt.length; i++)	
	{
		if (document.forms.startform.carrt.options[i].value == document.forms.startform.startcarrt.value)
		{
			aktindex = i;
			break;
		}
	}
	document.forms.startform.carrt.selectedIndex = aktindex;

	if (parseInt (document.forms.startform.freedate.value) == 0)
	{
		//change_backdate (7);
	}	
	else
	{
		//check_datum (1);
	}
}


function init_brokerhits ()
{
	anzeigen ("Kalender_R1", 1);
	anzeigen ("Kalender_R2", 0);
	anzeigen ("pause", 0);
	anzeigen ("error", 1);
}

function check_datum (typ)
{
	var status, dat, jahr, tag, monat;

	if (typ == 1)
		str = new String(document.forms.startform.hdate.value);
	else
		str = new String(document.forms.startform.rdate.value);

	dat = extract_datum (str);

	jahr = parseInt (dat / 10000);
	tag = dat % 100;
	monat = parseInt ((dat-jahr*10000)/100.);

	status = 0;

	while (true)
	{

		if (dat <= 0)
		{
			alert (msg[0]);
			break;
		}

		if (typ == 1)
		{
			if (dat < parseInt(document.forms.startform.mindatum.value))
			{
				alert (msg[1]+" "+document.forms.startform.mindatumt.value);
				break;
			}
			if (dat > parseInt(document.forms.startform.maxdatum.value))
			{
				alert (msg[2]+" "+document.forms.startform.maxdatumt.value);
				break;
			}
		}
		status = 1;
		break;
	}

	if (status == 0) 
	{
		if (typ == 1) document.forms.startform.hdate.value = document.forms.startform.hdate_start.value;
		if (typ == 2) document.forms.startform.rdate.value = document.forms.startform.rdate_start.value;
	}	
	else
	{
		if (tag < 10) 
			datstr = "0"+tag;
		else
			datstr = tag;

		datstr += ".";

		if (monat < 10) 
			datstr += "0"+monat;
		else
			datstr += monat;
		datstr += ".";

		datstr += jahr;

		if (typ == 1) document.forms.startform.hdate.value = datstr;
		if (typ == 2) document.forms.startform.rdate.value = datstr;
	}

	if (typ == 1)
	{
		rdate = calc_datum_diff (jahr*10000+monat*100+tag, 7);
		jahr = parseInt (rdate / 10000);
		tag = parseInt(rdate % 100);
		monat = parseInt ((rdate-jahr*10000)/100.);

		if (tag < 10) 
			datstr = "0"+tag;
		else
			datstr = tag;

		datstr += ".";

		if (monat < 10) 
			datstr += "0"+monat;
		else
			datstr += monat;
		datstr += ".";

		datstr += jahr;

		document.forms.startform.rdate.value = datstr;
	}
	
	return (true);
}

function startform_valid (theForm)
{
var ftype = theForm.flugart.options[theForm.flugart.selectedIndex].value;



	if (ftype == "G")  //  Gabelflug
	{	
		theForm.fromt[2].style.backgroundColor = "#ffffff";
		if (theForm.fromt[2].value == "")
		{
			theForm.fromt[2].style.backgroundColor = "#00ffff";
			alert (msg[3]);
			theForm.fromt[2].focus();
			return (false);
		}
		theForm.destt[0].style.backgroundColor = "#ffffff";
		if (theForm.destt[0].value == "")
		{
			theForm.destt[0].style.backgroundColor = "#00ffff";
			alert (msg[4]);
			theForm.destt[0].focus();
			return (false);
		}
		theForm.rfromt.style.backgroundColor = "#ffffff";
		if (theForm.rfromt.value == "")
		{
			theForm.rfromt.style.backgroundColor = "#00ffff";
			alert (msg[3]);
			theForm.rfromt.focus();
			return (false);
		}
		theForm.rdestt.style.backgroundColor = "#ffffff";
		if (theForm.rdestt.value == "")
		{
			theForm.rdestt.style.backgroundColor = "#00ffff";
			alert (msg[4]);
			theForm.rdestt.focus();
			return (false);
		}

	/*****************************************************************************
		 sma: todo was wenn einer kein P2P hat? ->  alles für beide Modi 
		 funktionsfähig machen + testen.
		 Problem taucht nur im firefox auf, da dieser auch elemente, die nicht 
		 sichtbar sind (visible:none) an den server weitergibt und das Frontend 
		 dann irrtümlich einen Openjaw annimmt
	*****************************************************************************/

		theForm.fromt[0].length = 0;
		
		theForm.fromt[0].value = "";
		theForm.fromt[1].value = "";
		theForm.destt[1].value = "";
		
	}
	else // oneway / return
	{
		if (dropdown)
		{
			theForm.fromt[1].value = "";
		}
		else
		{
			theForm.fromt[1].style.backgroundColor = "#ffffff";
			if (theForm.fromt[1].value == "")
			{
				theForm.fromt[1].style.backgroundColor = "#00ffff";
				alert (msg[3]);
				theForm.fromt[1].focus();
				return (false);
			}
			theForm.fromt[0].length = 0;
			theForm.fromt[0].value = "";
//alert ("F0:"+theForm.fromt[0].value+"  F1:"+theForm.fromt[1].value);
		}
		
		theForm.destt[1].style.backgroundColor = "#ffffff";
		if (theForm.destt[1].value == "")
		{
			theForm.destt[1].style.backgroundColor = "#00ffff";
			alert (msg[4]);
			theForm.destt[1].focus();
			return (false);
		}
		theForm.fromt[2].value = "";
		theForm.destt[0].value = "";
	}
	
//  und nun die Datumsbetrachtung

	if (parseInt(theForm.freedate.value) == 1)
	{
		hdat = extract_datum (theForm.hdate.value);

		hd3 = parseInt (hdat / 10000);
		hd1 = hdat % 100;
		hd2 = parseInt ((hdat-hd3*10000)/100.);

		theForm.hdate1.value	= hd1;
		theForm.hdate2.value = hd3*100 + hd2;
	}
	else
	{
		hd1 = parseInt (parseFloat(theForm.hdate1.value));
		hd3 = parseInt (theForm.hdate2.value/100.);
		hd2 = parseInt (theForm.hdate2.value % 100.);

		hdat = hd3*10000 + hd2*100 + hd1;
	}

	if (! teste_datum (hd1,hd2,hd3))
	{
		alert (msg[5]);
		theForm.hdate1.focus();
		return (false);
	}

	if (hdat < theForm.mindatum.value)
	{
		alert (msg[1]+" "+theForm.mindatumt.value);
		theForm.hdate1.focus();
		return (false);
	}

	if (ftype != "O")  //  bei onewayFlügen ist Rückflugsdatum irrelevant
	{
		if (parseInt(theForm.freedate.value) == 1)
		{
			rdat = extract_datum (theForm.rdate.value);

			rd3 = parseInt (rdat / 10000);
			rd1 = rdat % 100;
			rd2 = parseInt ((rdat-rd3*10000)/100.);

			theForm.rdate1.value	= rd1;
			theForm.rdate2.value = rd3*100 + rd2;
		}
		else
		{
			rd1 = parseInt (parseFloat(theForm.rdate1.value));
			rd3 = parseInt (theForm.rdate2.value/100.);
			rd2 = parseInt (theForm.rdate2.value % 100.);

			rdat = rd3*10000 + rd2*100 + rd1;
		}

		if (rdat > theForm.maxdatum.value)
		{
			alert (msg[6]+" "+theForm.maxdatumt.value);
			if (parseInt(theForm.freedate.value) == 1)
				theForm.rdate.focus();
			else
				theForm.rdate1.focus();
			return (false);
		}
		if (! teste_datum (rd1,rd2,rd3))
		{
			alert (msg[5]);
			if (parseInt(theForm.freedate.value) == 1)
				theForm.rdate.focus();
			else
				theForm.rdate1.focus();
			return (false);
		}

		if (rdat < hdat)
		{
			alert (msg[7]);
			if (parseInt(theForm.freedate.value) == 1)
				theForm.hdate.focus();
			else
				theForm.hdate1.focus();
			return (false);
		}
	}

	adult_zahl = parseInt (theForm.adult.options[theForm.adult.selectedIndex].value);
	youth_zahl = parseInt (theForm.youth.options[theForm.youth.selectedIndex].value);

	if (adult_zahl == 0 && youth_zahl == 0) 
	{
		alert (msg[8]);
		theForm.adult.focus();
		return (false);
	}
	if (adult_zahl != 0 && youth_zahl != 0) 
	{
		alert (msg[9]);
		theForm.adult.focus();
		return (false);
	}

//alert ("F0:"+theForm.fromt[0].value+"  F1:"+theForm.fromt[1].value);
//return (false);

	anzeigen ("pause", 1);
	anzeigen ("error", 0);

	return (true);
}

function startform_validbrokerhits (theForm)
{
//  die Datumsbetrachtung

	if (parseInt(theForm.freedate.value) == 1)
	{
		hdat = extract_datum (theForm.hdate.value);

		hd3 = parseInt (hdat / 10000);
		hd1 = hdat % 100;
		hd2 = parseInt ((hdat-hd3*10000)/100.);

		theForm.hdate1.value	= hd1;
		theForm.hdate2.value = hd3*100 + hd2;
	}
	else
	{
		hd1 = parseInt (parseFloat(theForm.hdate1.value));
		hd3 = parseInt (theForm.hdate2.value/100.);
		hd2 = parseInt (theForm.hdate2.value % 100.);

		hdat = hd3*10000 + hd2*100 + hd1;
	}

	if (! teste_datum (hd1,hd2,hd3))
	{
//		alert ("Dieses Datum gibt es nicht");
		alert (msg[5]);
		theForm.hdate1.focus();
		return (false);
	}

	if (hdat < theForm.mindatum.value)
	{
//		alert ("<xsl:value-of select="e:i18n('Sie können frühestens fliegen ab dem ')"/>"+" "+theForm.mindatumt.value);
		alert (msg[1]+" "+theForm.mindatumt.value);
		theForm.hdate1.focus();
		return (false);
	}

	if (parseInt(theForm.freedate.value) == 1)
	{
		rdat = extract_datum (theForm.rdate.value);

		rd3 = parseInt (rdat / 10000);
		rd1 = rdat % 100;
		rd2 = parseInt ((rdat-rd3*10000)/100.);

		theForm.rdate1.value	= rd1;
		theForm.rdate2.value = rd3*100 + rd2;
	}
	else
	{
		rd1 = parseInt (parseFloat(theForm.rdate1.value));
		rd3 = parseInt (theForm.rdate2.value/100.);
		rd2 = parseInt (theForm.rdate2.value % 100.);

		rdat = rd3*10000 + rd2*100 + rd1;
	}

	if (rdat > theForm.maxdatum.value)
	{
//		alert ("<xsl:value-of select="e:i18n('Sie können nicht später zurückreisen als der ')"/>"+" "+theForm.maxdatumt.value);
		alert (msg[6]+" "+document.forms.startform.maxdatumt.value);
		if (parseInt(theForm.freedate.value) == 1)
			theForm.rdate.focus();
		else
			theForm.rdate1.focus();
		return (false);
	}
	if (! teste_datum (rd1,rd2,rd3))
	{
//		alert ("<xsl:value-of select="e:i18n('Dieses Datum gibt es nicht')"/>!");
		alert (mgs[5]);
		if (parseInt(theForm.freedate.value) == 1)
			theForm.rdate.focus();
		else
			theForm.rdate1.focus();
		return (false);
	}

	if (rdat < hdat)
	{
//		alert ("<xsl:value-of select="e:i18n('Die Rückreise kann nur nach der Abreise erfolgen')"/>!");
		alert (mgs[7]);
		if (parseInt(theForm.freedate.value) == 1)
			theForm.hdate.focus();
		else
			theForm.hdate1.focus();
		return (false);
	}

	adult_zahl = parseInt (theForm.adult.options[theForm.adult.selectedIndex].value);
	youth_zahl = parseInt (theForm.youth.options[theForm.youth.selectedIndex].value);

	if (adult_zahl == 0 && youth_zahl == 0) 
	{
		alert (msg[8]);
		theForm.adult.focus();
		return (false);
	}
	if (adult_zahl != 0 && youth_zahl != 0) 
	{
		alert (msg[9]);
		theForm.adult.focus();
		return (false);
	}

	anzeigen ("pause", 1);
	anzeigen ("error", 0);

	return (true);
}

function change_backdate (offset)
{
	htag = parseInt (parseFloat(document.startform.hdate1.value));
	hjahr = parseInt (document.startform.hdate2.value / 100.);
	hmonat = parseInt (document.startform.hdate2.value % 100.);
	
	rdate = calc_datum_diff (hjahr*10000+hmonat*100+htag,offset);

	rtag = parseInt (rdate % 100);
	rmon = parseInt (rdate / 100.);

	document.startform.rdate1.selectedIndex =  rtag-1; 
	for (i=0 ; i<document.startform.rdate2.length ; i++)
	{
		if (parseInt(parseFloat(document.startform.rdate2.options[i].value)) == rmon)
		{
			document.startform.rdate2.selectedIndex = i;
			break;
		}
	}		

	return (true);	
}

function change_fromt ()
{
	/* das klappt nicht, wenn man P2P nicht aktiviert hat -> changed sma 8.5.08 */
	//if (document.forms.startform.fromt[0].selectedIndex == document.forms.startform.fromt[0].length-1)
	var sel = document.forms.startform.fromt[0].selectedIndex;
	if (document.forms.startform.fromt[0].options[sel].value == 'other')
	{	
		anzeigen ("input_dropdown", 0);
		anzeigen ("input_texthin", 1);
		dropdown = 0;
		
		document.startform.fromt[0].value = "";
		document.startform.fromt[1].value = "";
		document.startform.fromt[1].focus();
	}
}

function change_fromt_ojaw ()
{
	if (document.startform.rdestt.value == "") document.startform.rdestt.value = document.startform.fromt[2].value;
}
function change_rfromt_ojaw ()
{
	if (document.startform.rfromt.value == "") document.startform.rfromt.value = document.startform.destt[0].value;
}
	
function change_flugart ()
{
	var ftype = document.forms.startform.flugart.options[document.forms.startform.flugart.selectedIndex].value;
	
	if (ftype == "R")  //  Hin- und Rückflug
	{
		if (dropdown)
			anzeigen ("input_dropdown", 1);
		else
			anzeigen ("input_texthin", 1);
		anzeigen ("input_textrueck", 1);
		anzeigen ("input_openjawhin", 0);
		anzeigen ("input_openjawrueck", 0);

		document.forms.startform.rfromt.value == "";
		document.forms.startform.rdestt.value == "";
	}
	else if (ftype == "O")  //  Oneway
	{
		if (dropdown)
			anzeigen ("input_dropdown", 1);
		else
			anzeigen ("input_texthin", 1);
		anzeigen ("input_textrueck", 1);
		anzeigen ("input_openjawhin", 0);
		anzeigen ("input_openjawrueck", 0);

		document.forms.startform.rfromt.value == "";
		document.forms.startform.rdestt.value == "";
	}
	else  //  Gabelflug
	{
		anzeigen ("input_dropdown", 0);
		anzeigen ("input_texthin", 0);
		anzeigen ("input_textrueck", 0);
		anzeigen ("input_openjawhin", 1);
		anzeigen ("input_openjawrueck", 1);
		
		document.forms.startform.fromt[2].value = "";
		document.forms.startform.destt[0].value = "";
	}

	if (ftype == "O")  //  Oneway
	{
		if (document.startform.rdate1 && document.startform.rdate2)
		{
			document.startform.rdate1.disabled = "true";
			document.startform.rdate2.disabled = "true";
		}
		if (document.startform.rdate)
		{
			document.startform.rdate.disabled = "true";
		}
		document.startform.rtime.disabled = "true";
		anzeigen ("Kalender_R1", 0);
		anzeigen ("Kalender_R2", 1);
	}
	else  //  Return- oder Gabelflug
	{
		if (document.startform.rdate1 && document.startform.rdate2)
		{
			document.startform.rdate1.disabled = 0;
			document.startform.rdate2.disabled = 0;
		}
		if (document.startform.rdate)
		{
			document.startform.rdate.disabled = 0;
		}
		document.startform.rtime.disabled = 0;
		anzeigen ("Kalender_R1", 1);
		anzeigen ("Kalender_R2", 0);
	}
}

function Kalender_hin()
{
	posleft = 250;
	postop = 150;

	href = document.startform.cal_url.value + "&mode=14&direction=1" + "&unique_id=" + document.startform.unique_id.value + "&hdate=" + document.startform.hdate.value + "&hdate1=" + document.startform.hdate1.value + "&hdate2=" + document.startform.hdate2.value + "&mindatum=" + document.startform.mindatum.value + "&maxdatum=" + document.startform.maxdatum.value;
	neu = open(href,"Kalender","innerWidth=160,width=175,height=170,menubar=no,resizable=no,scrollbars=no,left=" + posleft + ",top=" + postop);
}

function Kalender_rueck()
{
	posleft = 250;
	postop = 150;

	href = document.startform.cal_url.value + "&mode=14&direction=2" + "&unique_id=" + document.startform.unique_id.value + "&rdate=" + document.startform.rdate.value + "&rdate1=" + document.startform.rdate1.value + "&rdate2=" + document.startform.rdate2.value + "&mindatum=" + document.startform.mindatum.value + "&maxdatum=" + document.startform.maxdatum.value;
	neu = open(href,"Kalender","innerWidth=160,width=175,height=170,menubar=no,resizable=no,scrollbars=no,left=" + posleft + ",top=" + postop);
}	

function RegionOeffnen(typ) 
{
// wenn ein Dropdown beim Abflugsort besteht, dann muss dieses gegen ein Inputfeld getauscht werden	
	if (typ == 5 && dropdown == 1)
	{
		anzeigen ("input_dropdown", 0);
		anzeigen ("input_texthin", 1);
		dropdown = 0;
	
		document.startform.fromt[0].length = 0;
		document.startform.fromt[0].value = "";
		document.startform.fromt[1].focus();
	}
	href = document.startform.cal_url.value + "&mode=10" + "&unique_id=" + document.startform.unique_id.value + "&direction=" + String(typ);
	neu = open (href,"RegionFenster","width=600,height=350,resizable=1,toolbar=0,scrolling=yes,scrollbars=1,location=0");
} 


