/*********************************************************/
/*  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 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
		{
			if (p2p_st == 1 && p2p == 1)    //  P2P schon zu Beginn ?
			{
				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);

	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;
}
function init_brokerhits ()
{
	anzeigen ("Kalender_R1", 1);
	anzeigen ("Kalender_R2", 0);
	anzeigen ("pause", 0);
}

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 ("Bitte geben Sie ein gültiges Datum der Form TT.MM.JJJJ ein");
			break;
		}

		if (typ == 1)
		{
			if (dat < parseInt(document.forms.startform.mindatum.value))
			{
				alert ("Sie können frühestens fliegen ab dem"+" "+document.forms.startform.mindatumt.value);
				break;
			}
			if (dat > parseInt(document.forms.startform.maxdatum.value))
			{
				alert ("Sie können spätestens fliegen ab dem"+" "+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;
	}

	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 ("Bitte geben Sie einen Abflugort an");
			theForm.fromt[2].focus();
			return (false);
		}
		theForm.destt[0].style.backgroundColor = "#ffffff";
		if (theForm.destt[0].value == "")
		{
			theForm.destt[0].style.backgroundColor = "#00ffff";
			alert ("Bitte geben Sie ein Reiseziel an");
			theForm.destt[0].focus();
			return (false);
		}
		theForm.rfromt.style.backgroundColor = "#ffffff";
		if (theForm.rfromt.value == "")
		{
			theForm.rfromt.style.backgroundColor = "#00ffff";
			alert ("Bitte geben Sie einen Abflugort an");
			theForm.rfromt.focus();
			return (false);
		}
		theForm.rdestt.style.backgroundColor = "#ffffff";
		if (theForm.rdestt.value == "")
		{
			theForm.rdestt.style.backgroundColor = "#00ffff";
			alert ("Bitte geben Sie ein Reiseziel an");
			theForm.rdestt.focus();
			return (false);
		}

		theForm.fromt[0].length = 0;
		
		theForm.fromt[0].value = "";
		theForm.fromt[1].value = "";
		theForm.destt[1].value = "";
		
	}
	else
	{
		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 ("Bitte geben Sie einen Abflugort an");
				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 ("Bitte geben Sie ein Reiseziel an");
			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 ("Dieses Datum gibt es nicht");
		theForm.hdate1.focus();
		return (false);
	}

	if (hdat < theForm.mindatum.value)
	{
		alert ("Sie können frühestens fliegen ab dem"+" "+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 ("Sie können nicht später zurückreisen als der"+" "+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 ("Dieses Datum gibt es nicht");
			if (parseInt(theForm.freedate.value) == 1)
				theForm.rdate.focus();
			else
				theForm.rdate1.focus();
			return (false);
		}

		if (rdat < hdat)
		{
			alert ("Die Rückreise kann nur nach der Abreise erfolgen");
			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 ("Sie müssen mindestens einen Erwachsenen oder einen Jugendlichen als Reiseteilnehmer wählen");
		theForm.adult.focus();
		return (false);
	}
	if (adult_zahl != 0 && youth_zahl != 0) 
	{
		alert ("Sie können nicht gleichzeitig Erwachsene und Jugendliche buchen, nehmen Sie die Buchung bitte in zwei separaten Buchungsvorgängen vor");
		theForm.adult.focus();
		return (false);
	}

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

	anzeigen ("pause", 1);

	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 ("Dieses Datum gibt es nicht");
		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 ("Sie können frühestens fliegen ab dem"+" "+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 ("Sie können nicht später zurückreisen als der"+" "+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 ("Dieses Datum gibt es nicht");
		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 ("Sie müssen mindestens einen Erwachsenen oder einen Jugendlichen als Reiseteilnehmer wählen");
		theForm.adult.focus();
		return (false);
	}
	if (adult_zahl != 0 && youth_zahl != 0) 
	{
		alert ("Sie können nicht gleichzeitig Erwachsene und Jugendliche buchen, nehmen Sie die Buchung bitte in zwei separaten Buchungsvorgängen vor");
		theForm.adult.focus();
		return (false);
	}

	anzeigen ("pause", 1);

	return (true);
}


function extract_datum (datstr)
{
	var pos1, pos2, tag, monat, jahr;
	
	pos1 = datstr.indexOf (".");	
	if (pos1 < 0) pos1 = datstr.indexOf ("-");
	if (pos1 < 0) pos1 = datstr.indexOf ("/");
	
	if (pos1 < 0) return (0);
	
	pos2 = datstr.indexOf (".",pos1+1);	
	if (pos2 < 0) pos1 = datstr.indexOf ("-",pos1+1);
	if (pos2 < 0) pos1 = datstr.indexOf ("/", pos1+1);
	
	if (pos2 < 0) return (0);

	tag = parseInt (parseFloat(datstr));
	monat = parseInt (parseFloat(datstr.substr(pos1+1,2)));
	jahr = parseInt (parseFloat(datstr.substr(pos2+1,4)));

	if (jahr <= 99) jahr += 2000;

	if (! teste_datum (tag,monat,jahr)) return (0);

	dat = jahr*10000 + monat*100 + tag;	
		
	return (dat);
}
		
function change_fromt ()
{
if (document.forms.startform.fromt[0].selectedIndex == document.forms.startform.fromt[0].length-1)
{	
	anzeigen ("input_dropdown", 0);
	anzeigen ("input_texthin", 1);
	dropdown = 0;
	
	document.startform.fromt[0].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);
	}
	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);
	}
	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 teste_datum (tag,monat,jahr)
{
mleng = new Array(31,28,31,30,31,30,31,31,30,31,30,31)
	
	if (tag > 0 && tag <= mleng[monat-1]) return (true);
		
	if (monat == 2 && tag == 29)   //  beim Februar könnte es sich um ein Schaltjahr handeln
	{
      if (! (jahr%100) && (jahr/100)%4 ) return (false);
		if (jahr % 4) return (false);
		return (true);
	}
				
	return (false);
}
	
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");
} 


